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 <ctype.h>
22 : #include <stdio.h>
23 :
24 : #include <module.hxx>
25 : #include <globals.hxx>
26 : #include <database.hxx>
27 : #include <tools/debug.hxx>
28 : #include <osl/file.hxx>
29 :
30 0 : SV_IMPL_META_FACTORY1( SvMetaModule, SvMetaExtern );
31 :
32 0 : SvMetaModule::SvMetaModule()
33 : : bImported( sal_False )
34 0 : , bIsModified( sal_False )
35 : {
36 0 : }
37 :
38 0 : SvMetaModule::SvMetaModule( const OUString & rIdlFileName, sal_Bool bImp )
39 : : aIdlFileName( rIdlFileName )
40 0 : , bImported( bImp ), bIsModified( sal_False )
41 : {
42 0 : }
43 :
44 : #define MODULE_VER 0x0001
45 0 : void SvMetaModule::Load( SvPersistStream & rStm )
46 : {
47 0 : bImported = sal_True; // import always
48 0 : SvMetaExtern::Load( rStm );
49 :
50 : sal_uInt16 nVer;
51 :
52 0 : rStm.ReadUInt16( nVer ); // version
53 : DBG_ASSERT( (nVer & ~IDL_WRITE_MASK) == MODULE_VER, "false version" );
54 :
55 0 : rStm >> aClassList;
56 0 : rStm >> aTypeList;
57 0 : rStm >> aAttrList;
58 : // browser
59 0 : aIdlFileName = rStm.ReadUniOrByteString( rStm.GetStreamCharSet() );
60 0 : aHelpFileName.setString(read_uInt16_lenPrefixed_uInt8s_ToOString(rStm));
61 0 : aSlotIdFile.setString(read_uInt16_lenPrefixed_uInt8s_ToOString(rStm));
62 0 : aModulePrefix.setString(read_uInt16_lenPrefixed_uInt8s_ToOString(rStm));
63 :
64 : // read compiler data
65 : sal_uInt16 nCmpLen;
66 0 : rStm.ReadUInt16( nCmpLen );
67 : DBG_ASSERT( (nVer & IDL_WRITE_MASK) == IDL_WRITE_COMPILER,
68 : "no idl compiler format" );
69 0 : rStm >> aBeginName;
70 0 : rStm >> aEndName;
71 0 : rStm >> aNextName;
72 0 : }
73 :
74 0 : void SvMetaModule::Save( SvPersistStream & rStm )
75 : {
76 0 : SvMetaExtern::Save( rStm );
77 :
78 0 : rStm.WriteUInt16( (sal_uInt16)(MODULE_VER | IDL_WRITE_COMPILER) ); // Version
79 :
80 0 : WriteSvDeclPersistList( rStm, aClassList );
81 0 : WriteSvDeclPersistList( rStm, aTypeList );
82 0 : WriteSvDeclPersistList( rStm, aAttrList );
83 : // browser
84 0 : rStm.WriteUniOrByteString( aIdlFileName, rStm.GetStreamCharSet() );
85 0 : write_uInt16_lenPrefixed_uInt8s_FromOString(rStm, aHelpFileName.getString());
86 0 : write_uInt16_lenPrefixed_uInt8s_FromOString(rStm, aSlotIdFile.getString());
87 0 : write_uInt16_lenPrefixed_uInt8s_FromOString(rStm, aModulePrefix.getString());
88 :
89 : // write compiler data
90 0 : sal_uInt16 nCmpLen = 0;
91 0 : sal_uLong nLenPos = rStm.Tell();
92 0 : rStm.WriteUInt16( nCmpLen );
93 0 : WriteSvGlobalName( rStm, aBeginName );
94 0 : WriteSvGlobalName( rStm, aEndName );
95 0 : WriteSvGlobalName( rStm, aNextName );
96 : // write length of compiler data
97 0 : sal_uLong nPos = rStm.Tell();
98 0 : rStm.Seek( nLenPos );
99 0 : rStm.WriteUInt16( (sal_uInt16)( nPos - nLenPos - sizeof( sal_uInt16 ) ) );
100 0 : rStm.Seek( nPos );
101 0 : }
102 :
103 0 : sal_Bool SvMetaModule::SetName( const OString& rName, SvIdlDataBase * pBase )
104 : {
105 0 : if( pBase )
106 : {
107 0 : if( pBase->GetModule( rName ) )
108 0 : return sal_False;
109 : }
110 0 : return SvMetaExtern::SetName( rName );
111 : }
112 :
113 0 : sal_Bool SvMetaModule::FillNextName( SvGlobalName * pName )
114 : {
115 0 : *pName = aNextName;
116 :
117 0 : if( aNextName < aEndName )
118 : {
119 0 : ++aNextName;
120 0 : bIsModified = sal_True;
121 0 : return sal_True;
122 : }
123 0 : return sal_False;
124 : }
125 :
126 0 : void SvMetaModule::ReadAttributesSvIdl( SvIdlDataBase & rBase,
127 : SvTokenStream & rInStm )
128 : {
129 0 : SvMetaExtern::ReadAttributesSvIdl( rBase, rInStm );
130 :
131 0 : aHelpFileName.ReadSvIdl( SvHash_HelpFile(), rInStm );
132 0 : if( aSlotIdFile.ReadSvIdl( SvHash_SlotIdFile(), rInStm ) )
133 : {
134 0 : sal_uInt32 nTokPos = rInStm.Tell();
135 0 : if( !rBase.ReadIdFile( OStringToOUString(aSlotIdFile.getString(), RTL_TEXTENCODING_ASCII_US)) )
136 : {
137 0 : OStringBuffer aStr("cannot read file: ");
138 0 : aStr.append(aSlotIdFile.getString());
139 0 : rBase.SetError( aStr.makeStringAndClear(), rInStm.GetToken() );
140 0 : rBase.WriteError( rInStm );
141 :
142 0 : rInStm.Seek( nTokPos );
143 : }
144 : }
145 0 : aTypeLibFile.ReadSvIdl( SvHash_TypeLibFile(), rInStm );
146 0 : aModulePrefix.ReadSvIdl( SvHash_ModulePrefix(), rInStm );
147 0 : }
148 :
149 0 : void SvMetaModule::WriteAttributesSvIdl( SvIdlDataBase & rBase,
150 : SvStream & rOutStm,
151 : sal_uInt16 nTab )
152 : {
153 0 : SvMetaExtern::WriteAttributesSvIdl( rBase, rOutStm, nTab );
154 0 : if( !aHelpFileName.getString().isEmpty() || !aSlotIdFile.getString().isEmpty() || !aTypeLibFile.getString().isEmpty() )
155 : {
156 0 : if( !aHelpFileName.getString().isEmpty() )
157 : {
158 0 : WriteTab( rOutStm, nTab );
159 0 : aHelpFileName.WriteSvIdl( SvHash_HelpFile(), rOutStm, nTab +1 );
160 0 : rOutStm.WriteChar( ';' ) << endl;
161 : }
162 0 : if( !aSlotIdFile.getString().isEmpty() )
163 : {
164 0 : WriteTab( rOutStm, nTab );
165 0 : aSlotIdFile.WriteSvIdl( SvHash_SlotIdFile(), rOutStm, nTab +1 );
166 0 : rOutStm.WriteChar( ';' ) << endl;
167 : }
168 0 : if( !aTypeLibFile.getString().isEmpty() )
169 : {
170 0 : WriteTab( rOutStm, nTab );
171 0 : aTypeLibFile.WriteSvIdl( SvHash_TypeLibFile(), rOutStm, nTab +1 );
172 0 : rOutStm.WriteChar( ';' ) << endl;
173 : }
174 : }
175 0 : }
176 :
177 0 : void SvMetaModule::ReadContextSvIdl( SvIdlDataBase & rBase,
178 : SvTokenStream & rInStm )
179 : {
180 0 : sal_uInt32 nTokPos = rInStm.Tell();
181 0 : if( rInStm.GetToken()->Is( SvHash_interface() )
182 0 : || rInStm.GetToken()->Is( SvHash_shell() ) )
183 : {
184 0 : SvMetaClassRef aClass = new SvMetaClass();
185 0 : if( aClass->ReadSvIdl( rBase, rInStm ) )
186 : {
187 0 : aClassList.push_back( aClass );
188 : // announce globally
189 0 : rBase.GetClassList().push_back( aClass );
190 0 : }
191 : }
192 0 : else if( rInStm.GetToken()->Is( SvHash_enum() ) )
193 : {
194 0 : SvMetaTypeEnumRef aEnum = new SvMetaTypeEnum();
195 :
196 0 : if( aEnum->ReadSvIdl( rBase, rInStm ) )
197 : {
198 : // declared in module
199 0 : aTypeList.push_back( aEnum );
200 : // announce globally
201 0 : rBase.GetTypeList().push_back( aEnum );
202 0 : }
203 : }
204 0 : else if( rInStm.GetToken()->Is( SvHash_item() )
205 0 : || rInStm.GetToken()->Is( SvHash_struct() )
206 0 : || rInStm.GetToken()->Is( SvHash_typedef() ) )
207 : {
208 0 : SvMetaTypeRef xItem = new SvMetaType();
209 :
210 0 : if( xItem->ReadSvIdl( rBase, rInStm ) )
211 : {
212 : // declared in module
213 0 : aTypeList.push_back( xItem );
214 : // announce globally
215 0 : rBase.GetTypeList().push_back( xItem );
216 0 : }
217 : }
218 0 : else if( rInStm.GetToken()->Is( SvHash_include() ) )
219 : {
220 0 : sal_Bool bOk = sal_False;
221 0 : rInStm.GetToken_Next();
222 0 : SvToken * pTok = rInStm.GetToken_Next();
223 0 : if( pTok->IsString() )
224 : {
225 0 : OUString aFullName(OStringToOUString(pTok->GetString(), RTL_TEXTENCODING_ASCII_US));
226 0 : rBase.StartNewFile( aFullName );
227 0 : osl::FileBase::RC searchError = osl::File::searchFileURL(aFullName, rBase.GetPath(), aFullName);
228 0 : osl::FileBase::getSystemPathFromFileURL( aFullName, aFullName );
229 :
230 0 : if( osl::FileBase::E_None == searchError )
231 : {
232 0 : rBase.AddDepFile( aFullName );
233 0 : SvTokenStream aTokStm( aFullName );
234 :
235 0 : if( SVSTREAM_OK == aTokStm.GetStream().GetError() )
236 : {
237 : // rescue error from old file
238 0 : SvIdlError aOldErr = rBase.GetError();
239 : // reset error
240 0 : rBase.SetError( SvIdlError() );
241 :
242 0 : sal_uInt32 nBeginPos = 0xFFFFFFFF; // can not happen with Tell
243 0 : while( nBeginPos != aTokStm.Tell() )
244 : {
245 0 : nBeginPos = aTokStm.Tell();
246 0 : ReadContextSvIdl( rBase, aTokStm );
247 0 : aTokStm.ReadDelemiter();
248 : }
249 0 : bOk = aTokStm.GetToken()->IsEof();
250 0 : if( !bOk )
251 : {
252 0 : rBase.WriteError( aTokStm );
253 : }
254 : // recover error from old file
255 0 : rBase.SetError( aOldErr );
256 : }
257 : else
258 : {
259 0 : OStringBuffer aStr("cannot open file: ");
260 0 : aStr.append(OUStringToOString(aFullName, RTL_TEXTENCODING_UTF8));
261 0 : rBase.SetError(aStr.makeStringAndClear(), pTok);
262 0 : }
263 : }
264 : else
265 : {
266 0 : OStringBuffer aStr("cannot find file:");
267 0 : aStr.append(OUStringToOString(aFullName, RTL_TEXTENCODING_UTF8));
268 0 : rBase.SetError(aStr.makeStringAndClear(), pTok);
269 0 : }
270 : }
271 0 : if( !bOk )
272 0 : rInStm.Seek( nTokPos );
273 : }
274 : else
275 : {
276 0 : SvMetaSlotRef xSlot = new SvMetaSlot();
277 :
278 0 : if( xSlot->ReadSvIdl( rBase, rInStm ) )
279 : {
280 0 : if( xSlot->Test( rBase, rInStm ) )
281 : {
282 : // declared in module
283 0 : aAttrList.push_back( xSlot );
284 : // announce globally
285 0 : rBase.AppendAttr( xSlot );
286 : }
287 0 : }
288 : }
289 0 : }
290 :
291 0 : void SvMetaModule::WriteContextSvIdl( SvIdlDataBase & rBase,
292 : SvStream & rOutStm,
293 : sal_uInt16 nTab )
294 : {
295 0 : SvMetaExtern::WriteContextSvIdl( rBase, rOutStm, nTab );
296 : sal_uLong n;
297 0 : for( n = 0; n < aTypeList.size(); n++ )
298 : {
299 0 : WriteTab( rOutStm, nTab );
300 0 : aTypeList[n]->WriteSvIdl( rBase, rOutStm, nTab );
301 : }
302 0 : rOutStm << endl;
303 0 : for( n = 0; n < aAttrList.size(); n++ )
304 : {
305 0 : WriteTab( rOutStm, nTab );
306 0 : aAttrList[n]->WriteSvIdl( rBase, rOutStm, nTab );
307 : }
308 0 : rOutStm << endl;
309 0 : for( n = 0; n < aClassList.size(); n++ )
310 : {
311 0 : WriteTab( rOutStm, nTab );
312 0 : aClassList[n]->WriteSvIdl( rBase, rOutStm, nTab );
313 : }
314 0 : }
315 :
316 0 : sal_Bool SvMetaModule::ReadSvIdl( SvIdlDataBase & rBase, SvTokenStream & rInStm )
317 : {
318 0 : bIsModified = sal_True; // up to now always when compiler running
319 :
320 0 : sal_uInt32 nTokPos = rInStm.Tell();
321 0 : SvToken * pTok = rInStm.GetToken_Next();
322 0 : sal_Bool bOk = pTok->Is( SvHash_module() );
323 0 : if( bOk )
324 : {
325 0 : pTok = rInStm.GetToken_Next();
326 0 : if( pTok->IsString() )
327 0 : bOk = aBeginName.MakeId(OStringToOUString(pTok->GetString(), RTL_TEXTENCODING_ASCII_US));
328 : }
329 0 : rInStm.ReadDelemiter();
330 0 : if( bOk )
331 : {
332 0 : pTok = rInStm.GetToken_Next();
333 0 : if( pTok->IsString() )
334 0 : bOk = aEndName.MakeId(OStringToOUString(pTok->GetString(), RTL_TEXTENCODING_ASCII_US));
335 : }
336 0 : rInStm.ReadDelemiter();
337 0 : if( bOk )
338 : {
339 0 : aNextName = aBeginName;
340 :
341 0 : rBase.Push( this ); // onto the context stack
342 :
343 0 : if( ReadNameSvIdl( rBase, rInStm ) )
344 : {
345 : // set pointer to itself
346 0 : SetModule( rBase );
347 0 : bOk = SvMetaName::ReadSvIdl( rBase, rInStm );
348 : }
349 0 : rBase.GetStack().Pop(); // remove from stack
350 : }
351 0 : if( !bOk )
352 0 : rInStm.Seek( nTokPos );
353 0 : return bOk;
354 : }
355 :
356 0 : void SvMetaModule::WriteSvIdl( SvIdlDataBase & rBase, SvStream & rOutStm,
357 : sal_uInt16 nTab )
358 : {
359 0 : rOutStm.WriteCharPtr( SvHash_module()->GetName().getStr() ) << endl;
360 0 : rOutStm.WriteChar( '\"' );
361 0 : rOutStm.WriteUniOrByteString( aBeginName.GetHexName(), rOutStm.GetStreamCharSet() );
362 0 : rOutStm.WriteChar( '\"' ) << endl;
363 0 : rOutStm.WriteChar( '\"' );
364 0 : rOutStm.WriteUniOrByteString( aEndName.GetHexName(), rOutStm.GetStreamCharSet() );
365 0 : rOutStm.WriteChar( '\"' ) << endl;
366 0 : SvMetaExtern::WriteSvIdl( rBase, rOutStm, nTab );
367 0 : }
368 :
369 0 : void SvMetaModule::WriteSfx( SvIdlDataBase & rBase, SvStream & rOutStm )
370 : {
371 0 : for( sal_uLong n = 0; n < aClassList.size(); n++ )
372 : {
373 0 : SvMetaClass * pClass = aClassList[n];
374 0 : pClass->WriteSfx( rBase, rOutStm );
375 : }
376 0 : }
377 :
378 0 : void SvMetaModule::WriteHelpIds( SvIdlDataBase & rBase, SvStream & rOutStm,
379 : HelpIdTable& rTable )
380 : {
381 0 : for( sal_uLong n = 0; n < aClassList.size(); n++ )
382 : {
383 0 : SvMetaClass * pClass = aClassList[n];
384 0 : pClass->WriteHelpIds( rBase, rOutStm, rTable );
385 : }
386 0 : }
387 :
388 0 : void SvMetaModule::WriteAttributes( SvIdlDataBase & rBase,
389 : SvStream & rOutStm,
390 : sal_uInt16 nTab,
391 : WriteType nT, WriteAttribute nA )
392 : {
393 0 : SvMetaExtern::WriteAttributes( rBase, rOutStm, nTab, nT, nA );
394 0 : if( !aHelpFileName.getString().isEmpty() )
395 : {
396 0 : WriteTab( rOutStm, nTab );
397 0 : rOutStm.WriteCharPtr( "// class SvMetaModule" ) << endl;
398 0 : WriteTab( rOutStm, nTab );
399 0 : rOutStm.WriteCharPtr( "helpfile(\"" ).WriteCharPtr( aHelpFileName.getString().getStr() ).WriteCharPtr( "\");" ) << endl;
400 : }
401 0 : }
402 :
403 0 : void SvMetaModule::Write( SvIdlDataBase & rBase, SvStream & rOutStm,
404 : sal_uInt16 nTab,
405 : WriteType nT, WriteAttribute nA )
406 : {
407 0 : switch ( nT )
408 : {
409 : case WRITE_ODL:
410 : {
411 0 : if( !aSlotIdFile.getString().isEmpty() )
412 : {
413 0 : WriteTab( rOutStm, nTab );
414 0 : rOutStm.WriteCharPtr( "#include \"" ).WriteCharPtr( aSlotIdFile.getString().getStr() ).WriteChar( '"' ) << endl << endl;
415 : }
416 0 : SvMetaExtern::Write( rBase, rOutStm, nTab, nT, nA );
417 0 : rOutStm << endl;
418 0 : WriteTab( rOutStm, nTab );
419 0 : rOutStm.WriteCharPtr( "library " ).WriteCharPtr( GetName().getString().getStr() ) << endl;
420 0 : WriteTab( rOutStm, nTab );
421 0 : rOutStm.WriteChar( '{' ) << endl;
422 0 : WriteTab( rOutStm, nTab );
423 0 : rOutStm.WriteCharPtr( "importlib(\"STDOLE.TLB\");" ) << endl;
424 :
425 0 : for( sal_uLong n = 0; n < aClassList.size(); n++ )
426 : {
427 0 : SvMetaClass * pClass = aClassList[n];
428 0 : if( !pClass->IsShell() && pClass->GetAutomation() )
429 : {
430 0 : WriteTab( rOutStm, nTab );
431 0 : WriteStars( rOutStm );
432 0 : pClass->Write( rBase, rOutStm, nTab +1, nT, nA );
433 0 : if( n +1 < aClassList.size() )
434 0 : rOutStm << endl;
435 : }
436 : }
437 :
438 0 : rOutStm.WriteChar( '}' ) << endl;
439 : }
440 0 : break;
441 : case WRITE_DOCU:
442 : {
443 0 : rOutStm.WriteCharPtr( "SvIDL interface documentation" ) << endl << endl;
444 0 : rOutStm.WriteCharPtr( "<MODULE>" ) << endl;
445 0 : rOutStm.WriteCharPtr( GetName().getString().getStr() ) << endl;
446 0 : WriteDescription( rOutStm );
447 0 : rOutStm.WriteCharPtr( "</MODULE>" ) << endl << endl;
448 :
449 0 : rOutStm.WriteCharPtr( "<CLASSES>" ) << endl;
450 0 : for( sal_uLong n = 0; n < aClassList.size(); n++ )
451 : {
452 0 : SvMetaClass * pClass = aClassList[n];
453 0 : if( !pClass->IsShell() )
454 : {
455 0 : rOutStm.WriteCharPtr( pClass->GetName().getString().getStr() );
456 0 : SvMetaClass* pSC = pClass->GetSuperClass();
457 0 : if( pSC )
458 0 : rOutStm.WriteCharPtr( " : " ).WriteCharPtr( pSC->GetName().getString().getStr() );
459 :
460 : // imported classes
461 0 : const SvClassElementMemberList& rClassList = pClass->GetClassList();
462 0 : if ( !rClassList.empty() )
463 : {
464 0 : rOutStm.WriteCharPtr( " ( " );
465 :
466 0 : for( sal_uLong m=0; m<rClassList.size(); ++m )
467 : {
468 0 : SvClassElement *pEle = rClassList[m];
469 0 : SvMetaClass *pCl = pEle->GetClass();
470 0 : rOutStm.WriteCharPtr( pCl->GetName().getString().getStr() );
471 0 : if ( m+1 == rClassList.size() )
472 0 : rOutStm.WriteCharPtr( " )" );
473 : else
474 0 : rOutStm.WriteCharPtr( " , " );
475 : }
476 : }
477 :
478 0 : rOutStm << endl;
479 : }
480 : }
481 0 : rOutStm.WriteCharPtr( "</CLASSES>" ) << endl << endl;
482 : // no break!
483 : }
484 :
485 : case WRITE_C_SOURCE:
486 : case WRITE_C_HEADER:
487 : {
488 0 : for( sal_uLong n = 0; n < aClassList.size(); n++ )
489 : {
490 0 : SvMetaClass * pClass = aClassList[n];
491 0 : if( !pClass->IsShell() )
492 0 : pClass->Write( rBase, rOutStm, nTab, nT, nA );
493 : }
494 : }
495 0 : break;
496 :
497 : default:
498 0 : break;
499 : }
500 0 : }
501 :
502 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|