xref: /trunk/main/idl/source/prj/svidl.cxx (revision 724893d4)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_idl.hxx"
26 
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <database.hxx>
30 #include <globals.hxx>
31 #include <command.hxx>
32 #include <tools/fsys.hxx>
33 #include <tools/string.hxx>
34 
35 #define BR 0x8000
36 sal_Bool FileMove_Impl( const String & rFile1, const String & rFile2, sal_Bool bImmerVerschieben )
37 {
38 	//printf( "Move from %s to %s\n", rFile2.GetStr(), rFile1.GetStr() );
39 	sal_uLong nC1 = 0;
40 	sal_uLong nC2 = 1;
41 	if( !bImmerVerschieben )
42 	{
43 		SvFileStream aOutStm1( rFile1, STREAM_STD_READ );
44 		SvFileStream aOutStm2( rFile2, STREAM_STD_READ );
45 		if( aOutStm1.GetError() == SVSTREAM_OK )
46 		{
47 			sal_uInt8 * pBuf1 = new sal_uInt8[ BR ];
48 			sal_uInt8 * pBuf2 = new sal_uInt8[ BR ];
49 			nC1 = aOutStm1.Read( pBuf1, BR );
50 			nC2 = aOutStm2.Read( pBuf2, BR );
51 			while( nC1 == nC2 )
52 			{
53 				if( memcmp( pBuf1, pBuf2, nC1 ) )
54 				{
55 					nC1++;
56 					break;
57 				}
58 				else
59 				{
60 					if( 0x8000 != nC1 )
61 						break;
62 					nC1 = aOutStm1.Read( pBuf1, BR );
63 					nC2 = aOutStm2.Read( pBuf2, BR );
64 				}
65 			}
66 			delete[] pBuf1;
67 			delete[] pBuf2;
68 		}
69 	}
70 	DirEntry aF2( rFile2 );
71 	if( nC1 != nC2 )
72 	{// es hat sich etwas geaendert
73 		DirEntry aF1( rFile1 );
74 		aF1.Kill();
75 		// Datei verschieben
76 		if( aF2.MoveTo( aF1 ) )
77 		{
78 			// Beide Dateien loeschen
79 			aF1.Kill();
80 			aF2.Kill();
81 			return sal_False;
82 		}
83 /*
84 		else
85 		{
86 			printf( "%s to %s moved\n",
87 					 rFile2.GetStr(), rFile1.GetStr() );
88 		}
89 */
90 		return sal_True;
91 	}
92 	return 0 == aF2.Kill();
93 }
94 
95 /*************************************************************************
96 |*	  main()
97 |*
98 |*	  Beschreibung
99 *************************************************************************/
100 #if defined( UNX ) || (defined( PM2 ) && defined( CSET )) || defined (WTC) || defined (MTW) || defined (__MINGW32__) || defined( OS2 )
101 int main ( int argc, char ** argv)
102 {
103 #else
104 int cdecl main ( int argc, char ** argv)
105 {
106 #endif
107 
108 /*
109     pStr = ::ResponseFile( &aCmdLine, argv, argc );
110 	if( pStr )
111 	{
112 		printf( "Cannot open response file <%s>\n", pStr );
113 		return( 1 );
114 	};
115 */
116 
117     String aTmpListFile;
118     String aTmpSlotMapFile;
119     String aTmpSfxItemFile;
120     String aTmpDataBaseFile;
121     String aTmpCallingFile;
122     String aTmpSrcFile;
123     String aTmpCxxFile;
124     String aTmpHxxFile;
125     String aTmpHelpIdFile;
126     String aTmpCSVFile;
127     String aTmpDocuFile;
128 
129 	SvCommand aCommand( argc, argv );
130 
131     if( aCommand.nVerbosity != 0 )
132         printf( "StarView Interface Definition Language (IDL) Compiler 3.0\n" );
133 
134 	Init();
135 	SvIdlWorkingBase * pDataBase = new SvIdlWorkingBase(aCommand);
136 
137 	int nExit = 0;
138 	if( aCommand.aExportFile.Len() )
139 	{
140 		DirEntry aDE( aCommand.aExportFile );
141 		pDataBase->SetExportFile( aDE.GetName() );
142 	}
143 
144 	if( ReadIdl( pDataBase, aCommand ) )
145 	{
146 		if( nExit == 0 && aCommand.aDocuFile.Len() )
147 		{
148 			DirEntry aDE( aCommand.aDocuFile );
149 			aDE.ToAbs();
150 			aTmpDocuFile = aDE.GetPath().TempName().GetFull();
151 			SvFileStream aOutStm( aTmpDocuFile, STREAM_READWRITE | STREAM_TRUNC );
152 			if( !pDataBase->WriteDocumentation( aOutStm ) )
153 			{
154 				nExit = -1;
155 				ByteString aStr = "cannot write documentation file: ";
156                 aStr += ByteString( aCommand.aDocuFile, RTL_TEXTENCODING_UTF8 );
157                 fprintf( stderr, "%s\n", aStr.GetBuffer() );
158 			}
159 		}
160 		if( nExit == 0 && aCommand.aListFile.Len() )
161 		{
162 			DirEntry aDE( aCommand.aListFile );
163 			aDE.ToAbs();
164 			aTmpListFile = aDE.GetPath().TempName().GetFull();
165 			SvFileStream aOutStm( aTmpListFile, STREAM_READWRITE | STREAM_TRUNC );
166 			if( !pDataBase->WriteSvIdl( aOutStm ) )
167 			{
168 				nExit = -1;
169 				ByteString aStr = "cannot write list file: ";
170                 aStr += ByteString( aCommand.aListFile, RTL_TEXTENCODING_UTF8 );
171                 fprintf( stderr, "%s\n", aStr.GetBuffer() );
172 			}
173 		}
174 		if( nExit == 0 && aCommand.aSlotMapFile.Len() )
175 		{
176 			DirEntry aDE( aCommand.aSlotMapFile );
177 			aDE.ToAbs();
178 			aTmpSlotMapFile = aDE.GetPath().TempName().GetFull();
179 			SvFileStream aOutStm( aTmpSlotMapFile, STREAM_READWRITE | STREAM_TRUNC );
180 			if( !pDataBase->WriteSfx( aOutStm ) )
181 			{
182 				nExit = -1;
183 				ByteString aStr = "cannot write slotmap file: ";
184                 aStr += ByteString( aCommand.aSlotMapFile, RTL_TEXTENCODING_UTF8 );
185                 fprintf( stderr, "%s\n", aStr.GetBuffer() );
186 			}
187 		}
188 		if( nExit == 0 && aCommand.aHelpIdFile.Len() )
189 		{
190 			DirEntry aDE( aCommand.aHelpIdFile );
191 			aDE.ToAbs();
192 			aTmpHelpIdFile = aDE.GetPath().TempName().GetFull();
193 			SvFileStream aStm( aTmpHelpIdFile, STREAM_READWRITE | STREAM_TRUNC );
194 			if (!pDataBase->WriteHelpIds( aStm ) )
195 			{
196 				nExit = -1;
197 				ByteString aStr = "cannot write help ID file: ";
198                 aStr += ByteString( aCommand.aHelpIdFile, RTL_TEXTENCODING_UTF8 );
199                 fprintf( stderr, "%s\n", aStr.GetBuffer() );
200 			}
201 		}
202 		if( nExit == 0 && aCommand.aCSVFile.Len() )
203 		{
204 			DirEntry aDE( aCommand.aCSVFile );
205 			aDE.ToAbs();
206 			aTmpCSVFile = aDE.GetPath().TempName().GetFull();
207 			SvFileStream aStm( aTmpCSVFile, STREAM_READWRITE | STREAM_TRUNC );
208 			if (!pDataBase->WriteCSV( aStm ) )
209 			{
210 				nExit = -1;
211 				ByteString aStr = "cannot write CSV file: ";
212                 aStr += ByteString( aCommand.aCSVFile, RTL_TEXTENCODING_UTF8 );
213                 fprintf( stderr, "%s\n", aStr.GetBuffer() );
214 			}
215 		}
216 		if( nExit == 0 && aCommand.aSfxItemFile.Len() )
217 		{
218 			DirEntry aDE( aCommand.aSfxItemFile );
219 			aDE.ToAbs();
220 			aTmpSfxItemFile = aDE.GetPath().TempName().GetFull();
221 			SvFileStream aOutStm( aTmpSfxItemFile, STREAM_READWRITE | STREAM_TRUNC );
222 			if( !pDataBase->WriteSfxItem( aOutStm ) )
223 			{
224 				nExit = -1;
225 				ByteString aStr = "cannot write item file: ";
226                 aStr += ByteString( aCommand.aSfxItemFile, RTL_TEXTENCODING_UTF8 );
227                 fprintf( stderr, "%s\n", aStr.GetBuffer() );
228 			}
229 		}
230 		if( nExit == 0 && aCommand.aDataBaseFile.Len() )
231 		{
232 			DirEntry aDE( aCommand.aDataBaseFile );
233 			aDE.ToAbs();
234 			aTmpDataBaseFile = aDE.GetPath().TempName().GetFull();
235 			SvFileStream aOutStm( aTmpDataBaseFile, STREAM_READWRITE | STREAM_TRUNC );
236 			pDataBase->Save( aOutStm, aCommand.nFlags );
237 			if( aOutStm.GetError() != SVSTREAM_OK )
238 			{
239 				nExit = -1;
240 				ByteString aStr = "cannot write database file: ";
241                 aStr += ByteString( aCommand.aDataBaseFile, RTL_TEXTENCODING_UTF8 );
242                 fprintf( stderr, "%s\n", aStr.GetBuffer() );
243 			}
244 		}
245 /*
246 		if( nExit == 0 && aCommand.aCallingFile.Len() )
247 		{
248 			DirEntry aDE( aCommand.aCallingFile );
249 			aDE.ToAbs();
250 			aTmpCallingFile = aDE.GetPath().TempName().GetFull();
251 			SvFileStream aOutStm( aTmpCallingFile, STREAM_READWRITE | STREAM_TRUNC );
252 			pDataBase->WriteSbx( aOutStm );
253 			//pDataBase->Save( aOutStm, aCommand.nFlags | IDL_WRITE_CALLING );
254 			if( aOutStm.GetError() != SVSTREAM_OK )
255 			{
256 				nExit = -1;
257 				ByteString aStr = "cannot write calling file: ";
258 				aStr += aCommand.aCallingFile;
259 				fprintf( stderr, "%s\n", aStr.GetStr() );
260 			}
261 		}
262 		if( nExit == 0 && aCommand.aCxxFile.Len() )
263 		{
264 			DirEntry aDE( aCommand.aCxxFile );
265 			aDE.ToAbs();
266 			aTmpCxxFile = aDE.GetPath().TempName().GetFull();
267 			SvFileStream aOutStm( aTmpCxxFile, STREAM_READWRITE | STREAM_TRUNC );
268 
269 			aOutStm << "#pragma hdrstop" << endl;
270 			aOutStm << "#include <";
271 			if( aCommand.aHxxFile.Len() )
272                 aOutStm << DirEntry(aCommand.aHxxFile).GetName().GetBuffer();
273 			else
274 			{
275 				DirEntry aDE( aCommand.aCxxFile );
276 				aDE.SetExtension( "hxx" );
277                 aOutStm << aDE.GetName().GetBuffer);
278 			}
279 			aOutStm << '>' << endl;
280 			if( !pDataBase->WriteCxx( aOutStm ) )
281 			{
282 				nExit = -1;
283 				ByteString aStr = "cannot write cxx file: ";
284                 aStr += ByteString( aCommand.aCxxFile, RTL_TEXTENCODING_UTF8 );
285                 fprintf( stderr, "%s\n", aStr.GetBuffer() );
286 			}
287 		}
288 		if( nExit == 0 && aCommand.aHxxFile.Len() )
289 		{
290 			DirEntry aDE( aCommand.aHxxFile );
291 			aDE.ToAbs();
292 			aTmpHxxFile = aDE.GetPath().TempName().GetFull();
293 			SvFileStream aOutStm( aTmpHxxFile, STREAM_READWRITE | STREAM_TRUNC );
294 
295 			aOutStm << "#include <somisc.hxx>" << endl;
296 			if( !pDataBase->WriteHxx( aOutStm ) )
297 			{
298 				nExit = -1;
299 				ByteString aStr = "cannot write cxx file: ";
300                 aStr += ByteString( aCommand.aHxxFile, RTL_TEXTENCODING_UTF8 );
301                 fprintf( stderr, "%s\n", aStr.GetBuffer() );
302 			}
303 		}
304  */
305 	}
306 	else
307 		nExit = -1;
308 
309 	if( nExit == 0 )
310 	{
311 		sal_Bool bErr = sal_False;
312 		sal_Bool bDoMove = aCommand.aTargetFile.Len() == 0;
313 		String aErrFile, aErrFile2;
314 		if( !bErr && aCommand.aListFile.Len() )
315 		{
316 			bErr |= !FileMove_Impl( aCommand.aListFile, aTmpListFile, bDoMove );
317 			if( bErr ) {
318 				aErrFile = aCommand.aListFile;
319 				aErrFile2 = aTmpListFile;
320 			}
321 		}
322 		if( !bErr && aCommand.aSlotMapFile.Len() )
323 		{
324 			bErr |= !FileMove_Impl( aCommand.aSlotMapFile, aTmpSlotMapFile, bDoMove );
325 			if( bErr ) {
326 				aErrFile = aCommand.aSlotMapFile;
327 				aErrFile2 = aTmpSlotMapFile;
328 			}
329 		}
330 		if( !bErr && aCommand.aSfxItemFile.Len() )
331 		{
332 			bErr |= !FileMove_Impl( aCommand.aSfxItemFile, aTmpSfxItemFile, bDoMove );
333 			if( bErr ) {
334 				aErrFile = aCommand.aSfxItemFile;
335 				aErrFile2 = aTmpSfxItemFile;
336 			}
337 		}
338 		if( !bErr && aCommand.aDataBaseFile.Len() )
339 		{
340 			bErr |= !FileMove_Impl( aCommand.aDataBaseFile, aTmpDataBaseFile, bDoMove );
341 			if( bErr ) {
342 				aErrFile = aCommand.aDataBaseFile;
343 				aErrFile2 = aTmpDataBaseFile;
344 			}
345 		}
346 		if( !bErr && aCommand.aCallingFile.Len() )
347 		{
348 			bErr |= !FileMove_Impl( aCommand.aCallingFile, aTmpCallingFile, bDoMove );
349 			if( bErr ) {
350 				aErrFile = aCommand.aCallingFile;
351 				aErrFile2 = aTmpCallingFile;
352 			}
353 		}
354 		if( !bErr && aCommand.aCxxFile.Len() )
355 		{
356 			bErr |= !FileMove_Impl( aCommand.aCxxFile, aTmpCxxFile, bDoMove );
357 			if( bErr ) {
358 				aErrFile = aCommand.aCxxFile;
359 				aErrFile2 = aTmpCxxFile;
360 			}
361 		}
362 		if( !bErr && aCommand.aHxxFile.Len() )
363 		{
364 			bErr |= !FileMove_Impl( aCommand.aHxxFile, aTmpHxxFile, bDoMove );
365 			if( bErr ) {
366 				aErrFile = aCommand.aHxxFile;
367 				aErrFile2 = aTmpHxxFile;
368 			}
369 		}
370 		if( !bErr && aCommand.aHelpIdFile.Len() )
371 		{
372 			bErr |= !FileMove_Impl( aCommand.aHelpIdFile, aTmpHelpIdFile, bDoMove );
373 			if( bErr ) {
374 				aErrFile = aCommand.aHelpIdFile;
375 				aErrFile2 = aTmpHelpIdFile;
376 			}
377 		}
378 		if( !bErr && aCommand.aCSVFile.Len() )
379 		{
380 			bErr |= !FileMove_Impl( aCommand.aCSVFile, aTmpCSVFile, bDoMove );
381 			if( bErr ) {
382 				aErrFile = aCommand.aCSVFile;
383 				aErrFile2 = aTmpCSVFile;
384 			}
385 		}
386 		if( !bErr && aCommand.aDocuFile.Len() )
387 		{
388 			bErr |= !FileMove_Impl( aCommand.aDocuFile, aTmpDocuFile, bDoMove );
389 			if( bErr ) {
390 				aErrFile = aCommand.aDocuFile;
391 				aErrFile2 = aTmpDocuFile;
392 			}
393 		}
394 
395 		if( bErr )
396 		{
397 			nExit = -1;
398 			ByteString aStr = "cannot move file from: ";
399             aStr += ByteString( aErrFile2, RTL_TEXTENCODING_UTF8 );
400             aStr += "\n              to file: ";
401             aStr += ByteString( aErrFile, RTL_TEXTENCODING_UTF8 );
402             fprintf( stderr, "%s\n", aStr.GetBuffer() );
403 		}
404 		else
405 		{
406 			if( aCommand.aTargetFile.Len() )
407 			{
408 #ifdef ICC
409 				DirEntry aT(aCommand.aTargetFile);
410 				aT.Kill();
411 #endif
412 				// Datei stempeln, da idl korrekt durchlaufen wurde
413 				SvFileStream aOutStm( aCommand.aTargetFile,
414 								STREAM_READWRITE | STREAM_TRUNC );
415 			}
416 		}
417 	}
418 
419 	if( nExit != 0 )
420 	{
421 		if( aCommand.aListFile.Len() )
422 			DirEntry( aTmpListFile ).Kill();
423 		if( aCommand.aSlotMapFile.Len() )
424 			DirEntry( aTmpSlotMapFile ).Kill();
425 		if( aCommand.aSfxItemFile.Len() )
426 			DirEntry( aTmpSfxItemFile ).Kill();
427 		if( aCommand.aDataBaseFile.Len() )
428 			DirEntry( aTmpDataBaseFile ).Kill();
429 		if( aCommand.aCallingFile.Len() )
430 			DirEntry( aTmpCallingFile ).Kill();
431 		if( aCommand.aCxxFile.Len() )
432 			DirEntry( aTmpCxxFile ).Kill();
433 		if( aCommand.aHxxFile.Len() )
434 			DirEntry( aTmpHxxFile ).Kill();
435 	}
436 
437 	delete pDataBase;
438 	DeInit();
439 	if( nExit != 0 )
440 		fprintf( stderr, "svidl terminated with errors\n" );
441 	return nExit;
442 }
443 
444