Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <stdlib.h>
22 : #include <stdio.h>
23 : #include <database.hxx>
24 : #include <globals.hxx>
25 : #include <command.hxx>
26 : #include <rtl/ustring.hxx>
27 : #include <osl/file.hxx>
28 :
29 : #define BR 0x8000
30 0 : sal_Bool FileMove_Impl( const OUString & rFile1, const OUString & rFile2, sal_Bool bImmerVerschieben )
31 : {
32 : //printf( "Move from %s to %s\n", rFile2.GetStr(), rFile1.GetStr() );
33 0 : sal_uLong nC1 = 0;
34 0 : sal_uLong nC2 = 1;
35 0 : if( !bImmerVerschieben )
36 : {
37 0 : SvFileStream aOutStm1( rFile1, STREAM_STD_READ );
38 0 : SvFileStream aOutStm2( rFile2, STREAM_STD_READ );
39 0 : if( aOutStm1.GetError() == SVSTREAM_OK )
40 : {
41 0 : sal_uInt8 * pBuf1 = new sal_uInt8[ BR ];
42 0 : sal_uInt8 * pBuf2 = new sal_uInt8[ BR ];
43 0 : nC1 = aOutStm1.Read( pBuf1, BR );
44 0 : nC2 = aOutStm2.Read( pBuf2, BR );
45 0 : while( nC1 == nC2 )
46 : {
47 0 : if( memcmp( pBuf1, pBuf2, nC1 ) )
48 : {
49 0 : nC1++;
50 0 : break;
51 : }
52 : else
53 : {
54 0 : if( 0x8000 != nC1 )
55 0 : break;
56 0 : nC1 = aOutStm1.Read( pBuf1, BR );
57 0 : nC2 = aOutStm2.Read( pBuf2, BR );
58 : }
59 : }
60 0 : delete[] pBuf1;
61 0 : delete[] pBuf2;
62 0 : }
63 : }
64 0 : OUString fileURL2;
65 0 : osl::FileBase::getFileURLFromSystemPath( rFile2, fileURL2 );
66 0 : if( nC1 != nC2 )
67 : {// something has changed
68 0 : OUString fileURL1;
69 0 : osl::FileBase::getFileURLFromSystemPath( rFile1, fileURL1 );
70 : // move file
71 0 : if( osl::FileBase::E_None != osl::File::move( fileURL2, fileURL1 ) )
72 : {
73 : // delete both files
74 0 : osl::File::remove( fileURL1 );
75 0 : osl::File::remove( fileURL2 );
76 0 : return sal_False;
77 : }
78 0 : return sal_True;
79 : }
80 0 : return osl::FileBase::E_None == osl::File::remove( fileURL2 );
81 : }
82 :
83 : //This function gets a system path to a file [fname], creates a temp file in
84 : //the same folder as [fname] and returns the system path of the temp file.
85 0 : inline OUString tempFileHelper(OUString const & fname)
86 : {
87 0 : OUString aTmpFile;
88 :
89 0 : sal_Int32 delimIndex = fname.lastIndexOf( '/' );
90 0 : if( delimIndex > 0 )
91 : {
92 0 : OUString aTmpDir( fname.copy( 0, delimIndex ) );
93 0 : osl::FileBase::getFileURLFromSystemPath( aTmpDir, aTmpDir );
94 0 : osl::FileBase::createTempFile( &aTmpDir, 0, &aTmpFile );
95 0 : osl::FileBase::getSystemPathFromFileURL( aTmpFile, aTmpFile );
96 : }
97 : else
98 : {
99 0 : OStringBuffer aStr("invalid filename: ");
100 0 : aStr.append(OUStringToOString(fname, RTL_TEXTENCODING_UTF8));
101 0 : fprintf(stderr, "%s\n", aStr.getStr());
102 : }
103 0 : return aTmpFile;
104 : }
105 :
106 0 : int main ( int argc, char ** argv)
107 : {
108 0 : OUString aTmpListFile;
109 0 : OUString aTmpSlotMapFile;
110 0 : OUString aTmpSfxItemFile;
111 0 : OUString aTmpDataBaseFile;
112 0 : OUString aTmpHelpIdFile;
113 0 : OUString aTmpCSVFile;
114 0 : OUString aTmpDocuFile;
115 0 : OUString aTmpDepFile;
116 :
117 0 : SvCommand aCommand( argc, argv );
118 :
119 0 : if( aCommand.nVerbosity != 0 )
120 0 : printf( "StarView Interface Definition Language (IDL) Compiler 3.0\n" );
121 :
122 0 : Init();
123 0 : SvIdlWorkingBase * pDataBase = new SvIdlWorkingBase(aCommand);
124 :
125 0 : int nExit = 0;
126 0 : if( !aCommand.aExportFile.isEmpty() )
127 : {
128 0 : osl::DirectoryItem aDI;
129 0 : osl::FileStatus fileStatus( osl_FileStatus_Mask_FileName );
130 0 : osl::DirectoryItem::get( aCommand.aExportFile, aDI );
131 0 : aDI.getFileStatus(fileStatus);
132 0 : pDataBase->SetExportFile( fileStatus.getFileName() );
133 : }
134 :
135 0 : if( ReadIdl( pDataBase, aCommand ) )
136 : {
137 0 : if( nExit == 0 && !aCommand.aDocuFile.isEmpty() )
138 : {
139 0 : aTmpDocuFile = tempFileHelper(aCommand.aDocuFile);
140 0 : SvFileStream aOutStm( aTmpDocuFile, STREAM_READWRITE | STREAM_TRUNC );
141 0 : if( !pDataBase->WriteDocumentation( aOutStm ) )
142 : {
143 0 : nExit = -1;
144 0 : OStringBuffer aStr("cannot write documentation file: ");
145 0 : aStr.append(OUStringToOString(aCommand.aDocuFile, RTL_TEXTENCODING_UTF8));
146 0 : fprintf(stderr, "%s\n", aStr.getStr());
147 0 : }
148 : }
149 0 : if( nExit == 0 && !aCommand.aListFile.isEmpty() )
150 : {
151 0 : aTmpListFile = tempFileHelper(aCommand.aListFile);
152 0 : SvFileStream aOutStm( aTmpListFile, STREAM_READWRITE | STREAM_TRUNC );
153 0 : if( !pDataBase->WriteSvIdl( aOutStm ) )
154 : {
155 0 : nExit = -1;
156 0 : OStringBuffer aStr("cannot write list file: ");
157 0 : aStr.append(OUStringToOString(aCommand.aListFile, RTL_TEXTENCODING_UTF8));
158 0 : fprintf(stderr, "%s\n", aStr.getStr());
159 0 : }
160 : }
161 0 : if( nExit == 0 && !aCommand.aSlotMapFile.isEmpty() )
162 : {
163 0 : aTmpSlotMapFile = tempFileHelper(aCommand.aSlotMapFile);
164 0 : SvFileStream aOutStm( aTmpSlotMapFile, STREAM_READWRITE | STREAM_TRUNC );
165 0 : if( !pDataBase->WriteSfx( aOutStm ) )
166 : {
167 0 : nExit = -1;
168 0 : OStringBuffer aStr("cannot write slotmap file: ");
169 0 : aStr.append(OUStringToOString(aCommand.aSlotMapFile, RTL_TEXTENCODING_UTF8));
170 0 : fprintf(stderr, "%s\n", aStr.getStr());
171 0 : }
172 : }
173 0 : if( nExit == 0 && !aCommand.aHelpIdFile.isEmpty() )
174 : {
175 0 : aTmpHelpIdFile = tempFileHelper(aCommand.aHelpIdFile);
176 0 : SvFileStream aStm( aTmpHelpIdFile, STREAM_READWRITE | STREAM_TRUNC );
177 0 : if (!pDataBase->WriteHelpIds( aStm ) )
178 : {
179 0 : nExit = -1;
180 0 : OStringBuffer aStr("cannot write help ID file: ");
181 0 : aStr.append(OUStringToOString(aCommand.aHelpIdFile, RTL_TEXTENCODING_UTF8));
182 0 : fprintf(stderr, "%s\n", aStr.getStr());
183 0 : }
184 : }
185 0 : if( nExit == 0 && !aCommand.aCSVFile.isEmpty() )
186 : {
187 0 : aTmpCSVFile = tempFileHelper(aCommand.aCSVFile);
188 0 : SvFileStream aStm( aTmpCSVFile, STREAM_READWRITE | STREAM_TRUNC );
189 0 : if (!pDataBase->WriteCSV( aStm ) )
190 : {
191 0 : nExit = -1;
192 0 : OStringBuffer aStr("cannot write CSV file: ");
193 0 : aStr.append(OUStringToOString(aCommand.aCSVFile, RTL_TEXTENCODING_UTF8));
194 0 : fprintf(stderr, "%s\n", aStr.getStr());
195 0 : }
196 : }
197 0 : if( nExit == 0 && !aCommand.aSfxItemFile.isEmpty() )
198 : {
199 0 : aTmpSfxItemFile = tempFileHelper(aCommand.aSfxItemFile);
200 0 : SvFileStream aOutStm( aTmpSfxItemFile, STREAM_READWRITE | STREAM_TRUNC );
201 0 : if( !pDataBase->WriteSfxItem( aOutStm ) )
202 : {
203 0 : nExit = -1;
204 0 : OStringBuffer aStr("cannot write item file: ");
205 0 : aStr.append(OUStringToOString(aCommand.aSfxItemFile, RTL_TEXTENCODING_UTF8));
206 0 : fprintf(stderr, "%s\n", aStr.getStr());
207 0 : }
208 : }
209 0 : if( nExit == 0 && !aCommand.aDataBaseFile.isEmpty() )
210 : {
211 0 : aTmpDataBaseFile = tempFileHelper(aCommand.aDataBaseFile);
212 0 : SvFileStream aOutStm( aTmpDataBaseFile, STREAM_READWRITE | STREAM_TRUNC );
213 0 : pDataBase->Save( aOutStm, aCommand.nFlags );
214 0 : if( aOutStm.GetError() != SVSTREAM_OK )
215 : {
216 0 : nExit = -1;
217 0 : OStringBuffer aStr("cannot write database file: ");
218 0 : aStr.append(OUStringToOString(aCommand.aDataBaseFile, RTL_TEXTENCODING_UTF8));
219 0 : fprintf(stderr, "%s\n", aStr.getStr());
220 0 : }
221 : }
222 0 : if (nExit == 0 && !aCommand.m_DepFile.isEmpty())
223 : {
224 0 : aTmpDepFile = tempFileHelper(aCommand.m_DepFile);
225 0 : SvFileStream aOutStm( aTmpDepFile, STREAM_READWRITE | STREAM_TRUNC );
226 0 : pDataBase->WriteDepFile(aOutStm, aCommand.aTargetFile);
227 0 : if( aOutStm.GetError() != SVSTREAM_OK )
228 : {
229 0 : nExit = -1;
230 : fprintf( stderr, "cannot write dependency file: %s\n",
231 : OUStringToOString( aCommand.m_DepFile,
232 0 : RTL_TEXTENCODING_UTF8 ).getStr() );
233 0 : }
234 : }
235 : }
236 : else
237 0 : nExit = -1;
238 :
239 0 : if( nExit == 0 )
240 : {
241 0 : bool bErr = false;
242 0 : sal_Bool bDoMove = aCommand.aTargetFile.isEmpty();
243 0 : OUString aErrFile, aErrFile2;
244 0 : if( !bErr && !aCommand.aListFile.isEmpty() )
245 : {
246 0 : bErr |= !FileMove_Impl( aCommand.aListFile, aTmpListFile, bDoMove );
247 0 : if( bErr ) {
248 0 : aErrFile = aCommand.aListFile;
249 0 : aErrFile2 = aTmpListFile;
250 : }
251 : }
252 0 : if( !bErr && !aCommand.aSlotMapFile.isEmpty() )
253 : {
254 0 : bErr |= !FileMove_Impl( aCommand.aSlotMapFile, aTmpSlotMapFile, bDoMove );
255 0 : if( bErr ) {
256 0 : aErrFile = aCommand.aSlotMapFile;
257 0 : aErrFile2 = aTmpSlotMapFile;
258 : }
259 : }
260 0 : if( !bErr && !aCommand.aSfxItemFile.isEmpty() )
261 : {
262 0 : bErr |= !FileMove_Impl( aCommand.aSfxItemFile, aTmpSfxItemFile, bDoMove );
263 0 : if( bErr ) {
264 0 : aErrFile = aCommand.aSfxItemFile;
265 0 : aErrFile2 = aTmpSfxItemFile;
266 : }
267 : }
268 0 : if( !bErr && !aCommand.aDataBaseFile.isEmpty() )
269 : {
270 0 : bErr |= !FileMove_Impl( aCommand.aDataBaseFile, aTmpDataBaseFile, bDoMove );
271 0 : if( bErr ) {
272 0 : aErrFile = aCommand.aDataBaseFile;
273 0 : aErrFile2 = aTmpDataBaseFile;
274 : }
275 : }
276 0 : if( !bErr && !aCommand.aHelpIdFile.isEmpty() )
277 : {
278 0 : bErr |= !FileMove_Impl( aCommand.aHelpIdFile, aTmpHelpIdFile, bDoMove );
279 0 : if( bErr ) {
280 0 : aErrFile = aCommand.aHelpIdFile;
281 0 : aErrFile2 = aTmpHelpIdFile;
282 : }
283 : }
284 0 : if( !bErr && !aCommand.aCSVFile.isEmpty() )
285 : {
286 0 : bErr |= !FileMove_Impl( aCommand.aCSVFile, aTmpCSVFile, bDoMove );
287 0 : if( bErr ) {
288 0 : aErrFile = aCommand.aCSVFile;
289 0 : aErrFile2 = aTmpCSVFile;
290 : }
291 : }
292 0 : if( !bErr && !aCommand.aDocuFile.isEmpty() )
293 : {
294 0 : bErr |= !FileMove_Impl( aCommand.aDocuFile, aTmpDocuFile, bDoMove );
295 0 : if( bErr ) {
296 0 : aErrFile = aCommand.aDocuFile;
297 0 : aErrFile2 = aTmpDocuFile;
298 : }
299 : }
300 0 : if (!bErr && !aCommand.m_DepFile.isEmpty())
301 : {
302 0 : bErr |= !FileMove_Impl( aCommand.m_DepFile, aTmpDepFile, bDoMove );
303 0 : if (bErr) {
304 0 : aErrFile = aCommand.m_DepFile;
305 0 : aErrFile2 = aTmpDepFile;
306 : }
307 : }
308 :
309 0 : if( bErr )
310 : {
311 0 : nExit = -1;
312 0 : OStringBuffer aStr("cannot move file from: ");
313 : aStr.append(OUStringToOString(aErrFile2,
314 0 : RTL_TEXTENCODING_UTF8));
315 0 : aStr.append("\n to file: ");
316 : aStr.append(OUStringToOString(aErrFile,
317 0 : RTL_TEXTENCODING_UTF8));
318 0 : fprintf( stderr, "%s\n", aStr.getStr() );
319 : }
320 : else
321 : {
322 0 : if( !aCommand.aTargetFile.isEmpty() )
323 : {
324 : // stamp file, because idl passed through correctly
325 : SvFileStream aOutStm( aCommand.aTargetFile,
326 0 : STREAM_READWRITE | STREAM_TRUNC );
327 : }
328 0 : }
329 : }
330 :
331 0 : if( nExit != 0 )
332 : {
333 0 : if( !aCommand.aListFile.isEmpty() )
334 : {
335 0 : osl::FileBase::getSystemPathFromFileURL( aTmpListFile, aTmpListFile );
336 0 : osl::File::remove( aTmpListFile );
337 : }
338 0 : if( !aCommand.aSlotMapFile.isEmpty() )
339 : {
340 0 : osl::FileBase::getSystemPathFromFileURL( aTmpSlotMapFile, aTmpSlotMapFile );
341 0 : osl::File::remove( aTmpSlotMapFile );
342 : }
343 0 : if( !aCommand.aSfxItemFile.isEmpty() )
344 : {
345 0 : osl::FileBase::getSystemPathFromFileURL( aTmpSfxItemFile, aTmpSfxItemFile );
346 0 : osl::File::remove( aTmpSfxItemFile );
347 : }
348 0 : if( !aCommand.aDataBaseFile.isEmpty() )
349 : {
350 0 : osl::FileBase::getSystemPathFromFileURL( aTmpDataBaseFile, aTmpDataBaseFile );
351 0 : osl::File::remove( aTmpDataBaseFile );
352 : }
353 : }
354 :
355 0 : delete pDataBase;
356 0 : DeInit();
357 0 : if( nExit != 0 )
358 0 : fprintf( stderr, "svidl terminated with errors\n" );
359 0 : return nExit;
360 : }
361 :
362 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|