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 <tools/string.hxx>
27 : #include <osl/file.hxx>
28 :
29 : #define BR 0x8000
30 40 : sal_Bool FileMove_Impl( const String & rFile1, const String & rFile2, sal_Bool bImmerVerschieben )
31 : {
32 : //printf( "Move from %s to %s\n", rFile2.GetStr(), rFile1.GetStr() );
33 40 : sal_uLong nC1 = 0;
34 40 : sal_uLong nC2 = 1;
35 40 : if( !bImmerVerschieben )
36 : {
37 40 : SvFileStream aOutStm1( rFile1, STREAM_STD_READ );
38 80 : SvFileStream aOutStm2( rFile2, STREAM_STD_READ );
39 40 : 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 40 : }
63 : }
64 40 : OUString fileURL2;
65 40 : osl::FileBase::getFileURLFromSystemPath( rFile2, fileURL2 );
66 40 : if( nC1 != nC2 )
67 : {// something has changed
68 40 : OUString fileURL1;
69 40 : osl::FileBase::getFileURLFromSystemPath( rFile1, fileURL1 );
70 : // move file
71 40 : 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 40 : 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 40 : inline OUString tempFileHelper(OUString const & fname)
86 : {
87 40 : OUString aTmpFile;
88 :
89 40 : sal_Int32 delimIndex = fname.lastIndexOf( '/' );
90 40 : if( delimIndex > 0 )
91 : {
92 40 : OUString aTmpDir( fname.copy( 0, delimIndex ) );
93 40 : osl::FileBase::getFileURLFromSystemPath( aTmpDir, aTmpDir );
94 40 : osl::FileBase::createTempFile( &aTmpDir, 0, &aTmpFile );
95 40 : 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 40 : return aTmpFile;
104 : }
105 :
106 : #if defined( UNX ) || defined (__MINGW32__)
107 8 : int main ( int argc, char ** argv)
108 : {
109 : #else
110 : int cdecl main ( int argc, char ** argv)
111 : {
112 : #endif
113 :
114 8 : OUString aTmpListFile;
115 16 : OUString aTmpSlotMapFile;
116 16 : OUString aTmpSfxItemFile;
117 16 : OUString aTmpDataBaseFile;
118 16 : OUString aTmpHelpIdFile;
119 16 : OUString aTmpCSVFile;
120 16 : OUString aTmpDocuFile;
121 16 : OUString aTmpDepFile;
122 :
123 16 : SvCommand aCommand( argc, argv );
124 :
125 8 : if( aCommand.nVerbosity != 0 )
126 0 : printf( "StarView Interface Definition Language (IDL) Compiler 3.0\n" );
127 :
128 8 : Init();
129 8 : SvIdlWorkingBase * pDataBase = new SvIdlWorkingBase(aCommand);
130 :
131 8 : int nExit = 0;
132 8 : if( !aCommand.aExportFile.isEmpty() )
133 : {
134 8 : osl::DirectoryItem aDI;
135 16 : osl::FileStatus fileStatus( osl_FileStatus_Mask_FileName );
136 8 : osl::DirectoryItem::get( aCommand.aExportFile, aDI );
137 8 : aDI.getFileStatus(fileStatus);
138 16 : pDataBase->SetExportFile( fileStatus.getFileName() );
139 : }
140 :
141 8 : if( ReadIdl( pDataBase, aCommand ) )
142 : {
143 8 : if( nExit == 0 && !aCommand.aDocuFile.isEmpty() )
144 : {
145 0 : aTmpDocuFile = tempFileHelper(aCommand.aDocuFile);
146 0 : SvFileStream aOutStm( aTmpDocuFile, STREAM_READWRITE | STREAM_TRUNC );
147 0 : if( !pDataBase->WriteDocumentation( aOutStm ) )
148 : {
149 0 : nExit = -1;
150 0 : OStringBuffer aStr("cannot write documentation file: ");
151 0 : aStr.append(OUStringToOString(aCommand.aDocuFile, RTL_TEXTENCODING_UTF8));
152 0 : fprintf(stderr, "%s\n", aStr.getStr());
153 0 : }
154 : }
155 8 : if( nExit == 0 && !aCommand.aListFile.isEmpty() )
156 : {
157 8 : aTmpListFile = tempFileHelper(aCommand.aListFile);
158 8 : SvFileStream aOutStm( aTmpListFile, STREAM_READWRITE | STREAM_TRUNC );
159 8 : if( !pDataBase->WriteSvIdl( aOutStm ) )
160 : {
161 0 : nExit = -1;
162 0 : OStringBuffer aStr("cannot write list file: ");
163 0 : aStr.append(OUStringToOString(aCommand.aListFile, RTL_TEXTENCODING_UTF8));
164 0 : fprintf(stderr, "%s\n", aStr.getStr());
165 8 : }
166 : }
167 8 : if( nExit == 0 && !aCommand.aSlotMapFile.isEmpty() )
168 : {
169 8 : aTmpSlotMapFile = tempFileHelper(aCommand.aSlotMapFile);
170 8 : SvFileStream aOutStm( aTmpSlotMapFile, STREAM_READWRITE | STREAM_TRUNC );
171 8 : if( !pDataBase->WriteSfx( aOutStm ) )
172 : {
173 0 : nExit = -1;
174 0 : OStringBuffer aStr("cannot write slotmap file: ");
175 0 : aStr.append(OUStringToOString(aCommand.aSlotMapFile, RTL_TEXTENCODING_UTF8));
176 0 : fprintf(stderr, "%s\n", aStr.getStr());
177 8 : }
178 : }
179 8 : if( nExit == 0 && !aCommand.aHelpIdFile.isEmpty() )
180 : {
181 8 : aTmpHelpIdFile = tempFileHelper(aCommand.aHelpIdFile);
182 8 : SvFileStream aStm( aTmpHelpIdFile, STREAM_READWRITE | STREAM_TRUNC );
183 8 : if (!pDataBase->WriteHelpIds( aStm ) )
184 : {
185 0 : nExit = -1;
186 0 : OStringBuffer aStr("cannot write help ID file: ");
187 0 : aStr.append(OUStringToOString(aCommand.aHelpIdFile, RTL_TEXTENCODING_UTF8));
188 0 : fprintf(stderr, "%s\n", aStr.getStr());
189 8 : }
190 : }
191 8 : if( nExit == 0 && !aCommand.aCSVFile.isEmpty() )
192 : {
193 0 : aTmpCSVFile = tempFileHelper(aCommand.aCSVFile);
194 0 : SvFileStream aStm( aTmpCSVFile, STREAM_READWRITE | STREAM_TRUNC );
195 0 : if (!pDataBase->WriteCSV( aStm ) )
196 : {
197 0 : nExit = -1;
198 0 : OStringBuffer aStr("cannot write CSV file: ");
199 0 : aStr.append(OUStringToOString(aCommand.aCSVFile, RTL_TEXTENCODING_UTF8));
200 0 : fprintf(stderr, "%s\n", aStr.getStr());
201 0 : }
202 : }
203 8 : if( nExit == 0 && !aCommand.aSfxItemFile.isEmpty() )
204 : {
205 0 : aTmpSfxItemFile = tempFileHelper(aCommand.aSfxItemFile);
206 0 : SvFileStream aOutStm( aTmpSfxItemFile, STREAM_READWRITE | STREAM_TRUNC );
207 0 : if( !pDataBase->WriteSfxItem( aOutStm ) )
208 : {
209 0 : nExit = -1;
210 0 : OStringBuffer aStr("cannot write item file: ");
211 0 : aStr.append(OUStringToOString(aCommand.aSfxItemFile, RTL_TEXTENCODING_UTF8));
212 0 : fprintf(stderr, "%s\n", aStr.getStr());
213 0 : }
214 : }
215 8 : if( nExit == 0 && !aCommand.aDataBaseFile.isEmpty() )
216 : {
217 8 : aTmpDataBaseFile = tempFileHelper(aCommand.aDataBaseFile);
218 8 : SvFileStream aOutStm( aTmpDataBaseFile, STREAM_READWRITE | STREAM_TRUNC );
219 8 : pDataBase->Save( aOutStm, aCommand.nFlags );
220 8 : if( aOutStm.GetError() != SVSTREAM_OK )
221 : {
222 0 : nExit = -1;
223 0 : OStringBuffer aStr("cannot write database file: ");
224 0 : aStr.append(OUStringToOString(aCommand.aDataBaseFile, RTL_TEXTENCODING_UTF8));
225 0 : fprintf(stderr, "%s\n", aStr.getStr());
226 8 : }
227 : }
228 8 : if (nExit == 0 && !aCommand.m_DepFile.isEmpty())
229 : {
230 8 : aTmpDepFile = tempFileHelper(aCommand.m_DepFile);
231 8 : SvFileStream aOutStm( aTmpDepFile, STREAM_READWRITE | STREAM_TRUNC );
232 8 : pDataBase->WriteDepFile(aOutStm, aCommand.aTargetFile);
233 8 : if( aOutStm.GetError() != SVSTREAM_OK )
234 : {
235 0 : nExit = -1;
236 : fprintf( stderr, "cannot write dependency file: %s\n",
237 : OUStringToOString( aCommand.m_DepFile,
238 0 : RTL_TEXTENCODING_UTF8 ).getStr() );
239 8 : }
240 : }
241 : }
242 : else
243 0 : nExit = -1;
244 :
245 8 : if( nExit == 0 )
246 : {
247 8 : sal_Bool bErr = sal_False;
248 8 : sal_Bool bDoMove = aCommand.aTargetFile.Len() == 0;
249 16 : String aErrFile, aErrFile2;
250 8 : if( !bErr && !aCommand.aListFile.isEmpty() )
251 : {
252 8 : bErr |= !FileMove_Impl( aCommand.aListFile, aTmpListFile, bDoMove );
253 8 : if( bErr ) {
254 0 : aErrFile = aCommand.aListFile;
255 0 : aErrFile2 = aTmpListFile;
256 : }
257 : }
258 8 : if( !bErr && !aCommand.aSlotMapFile.isEmpty() )
259 : {
260 8 : bErr |= !FileMove_Impl( aCommand.aSlotMapFile, aTmpSlotMapFile, bDoMove );
261 8 : if( bErr ) {
262 0 : aErrFile = aCommand.aSlotMapFile;
263 0 : aErrFile2 = aTmpSlotMapFile;
264 : }
265 : }
266 8 : if( !bErr && !aCommand.aSfxItemFile.isEmpty() )
267 : {
268 0 : bErr |= !FileMove_Impl( aCommand.aSfxItemFile, aTmpSfxItemFile, bDoMove );
269 0 : if( bErr ) {
270 0 : aErrFile = aCommand.aSfxItemFile;
271 0 : aErrFile2 = aTmpSfxItemFile;
272 : }
273 : }
274 8 : if( !bErr && !aCommand.aDataBaseFile.isEmpty() )
275 : {
276 8 : bErr |= !FileMove_Impl( aCommand.aDataBaseFile, aTmpDataBaseFile, bDoMove );
277 8 : if( bErr ) {
278 0 : aErrFile = aCommand.aDataBaseFile;
279 0 : aErrFile2 = aTmpDataBaseFile;
280 : }
281 : }
282 8 : if( !bErr && !aCommand.aHelpIdFile.isEmpty() )
283 : {
284 8 : bErr |= !FileMove_Impl( aCommand.aHelpIdFile, aTmpHelpIdFile, bDoMove );
285 8 : if( bErr ) {
286 0 : aErrFile = aCommand.aHelpIdFile;
287 0 : aErrFile2 = aTmpHelpIdFile;
288 : }
289 : }
290 8 : if( !bErr && !aCommand.aCSVFile.isEmpty() )
291 : {
292 0 : bErr |= !FileMove_Impl( aCommand.aCSVFile, aTmpCSVFile, bDoMove );
293 0 : if( bErr ) {
294 0 : aErrFile = aCommand.aCSVFile;
295 0 : aErrFile2 = aTmpCSVFile;
296 : }
297 : }
298 8 : if( !bErr && !aCommand.aDocuFile.isEmpty() )
299 : {
300 0 : bErr |= !FileMove_Impl( aCommand.aDocuFile, aTmpDocuFile, bDoMove );
301 0 : if( bErr ) {
302 0 : aErrFile = aCommand.aDocuFile;
303 0 : aErrFile2 = aTmpDocuFile;
304 : }
305 : }
306 8 : if (!bErr && !aCommand.m_DepFile.isEmpty())
307 : {
308 8 : bErr |= !FileMove_Impl( aCommand.m_DepFile, aTmpDepFile, bDoMove );
309 8 : if (bErr) {
310 0 : aErrFile = aCommand.m_DepFile;
311 0 : aErrFile2 = aTmpDepFile;
312 : }
313 : }
314 :
315 8 : if( bErr )
316 : {
317 0 : nExit = -1;
318 0 : OStringBuffer aStr("cannot move file from: ");
319 : aStr.append(OUStringToOString(aErrFile2,
320 0 : RTL_TEXTENCODING_UTF8));
321 0 : aStr.append("\n to file: ");
322 : aStr.append(OUStringToOString(aErrFile,
323 0 : RTL_TEXTENCODING_UTF8));
324 0 : fprintf( stderr, "%s\n", aStr.getStr() );
325 : }
326 : else
327 : {
328 8 : if( aCommand.aTargetFile.Len() )
329 : {
330 : #ifdef ICC
331 : DirEntry aT(aCommand.aTargetFile);
332 : aT.Kill();
333 : #endif
334 : // stamp file, because idl passed through correctly
335 : SvFileStream aOutStm( aCommand.aTargetFile,
336 8 : STREAM_READWRITE | STREAM_TRUNC );
337 : }
338 8 : }
339 : }
340 :
341 8 : if( nExit != 0 )
342 : {
343 0 : if( !aCommand.aListFile.isEmpty() )
344 : {
345 0 : osl::FileBase::getSystemPathFromFileURL( aTmpListFile, aTmpListFile );
346 0 : osl::File::remove( aTmpListFile );
347 : }
348 0 : if( !aCommand.aSlotMapFile.isEmpty() )
349 : {
350 0 : osl::FileBase::getSystemPathFromFileURL( aTmpSlotMapFile, aTmpSlotMapFile );
351 0 : osl::File::remove( aTmpSlotMapFile );
352 : }
353 0 : if( !aCommand.aSfxItemFile.isEmpty() )
354 : {
355 0 : osl::FileBase::getSystemPathFromFileURL( aTmpSfxItemFile, aTmpSfxItemFile );
356 0 : osl::File::remove( aTmpSfxItemFile );
357 : }
358 0 : if( !aCommand.aDataBaseFile.isEmpty() )
359 : {
360 0 : osl::FileBase::getSystemPathFromFileURL( aTmpDataBaseFile, aTmpDataBaseFile );
361 0 : osl::File::remove( aTmpDataBaseFile );
362 : }
363 : }
364 :
365 8 : delete pDataBase;
366 8 : DeInit();
367 8 : if( nExit != 0 )
368 0 : fprintf( stderr, "svidl terminated with errors\n" );
369 16 : return nExit;
370 : }
371 :
372 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|