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/fsys.hxx>
28 : #include <tools/debug.hxx>
29 :
30 15111 : 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 8 : SvMetaModule::SvMetaModule( const String & rIdlFileName, sal_Bool bImp )
39 : : aIdlFileName( rIdlFileName )
40 8 : , bImported( bImp ), bIsModified( sal_False )
41 : {
42 8 : }
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 >> 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_lenPrefixed_uInt8s_ToOString<sal_uInt16>(rStm));
61 0 : aSlotIdFile.setString(read_lenPrefixed_uInt8s_ToOString<sal_uInt16>(rStm));
62 0 : aModulePrefix.setString(read_lenPrefixed_uInt8s_ToOString<sal_uInt16>(rStm));
63 :
64 : // read compiler data
65 : sal_uInt16 nCmpLen;
66 0 : rStm >> 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 8 : void SvMetaModule::Save( SvPersistStream & rStm )
75 : {
76 8 : SvMetaExtern::Save( rStm );
77 :
78 8 : rStm << (sal_uInt16)(MODULE_VER | IDL_WRITE_COMPILER); // Version
79 :
80 8 : rStm << aClassList;
81 8 : rStm << aTypeList;
82 8 : rStm << aAttrList;
83 : // browser
84 8 : rStm.WriteUniOrByteString( aIdlFileName, rStm.GetStreamCharSet() );
85 8 : write_lenPrefixed_uInt8s_FromOString<sal_uInt16>(rStm, aHelpFileName.getString());
86 8 : write_lenPrefixed_uInt8s_FromOString<sal_uInt16>(rStm, aSlotIdFile.getString());
87 8 : write_lenPrefixed_uInt8s_FromOString<sal_uInt16>(rStm, aModulePrefix.getString());
88 :
89 : // write compiler data
90 8 : sal_uInt16 nCmpLen = 0;
91 8 : sal_uLong nLenPos = rStm.Tell();
92 8 : rStm << nCmpLen;
93 8 : rStm << aBeginName;
94 8 : rStm << aEndName;
95 8 : rStm << aNextName;
96 : // write length of compiler data
97 8 : sal_uLong nPos = rStm.Tell();
98 8 : rStm.Seek( nLenPos );
99 8 : rStm << (sal_uInt16)( nPos - nLenPos - sizeof( sal_uInt16 ) );
100 8 : rStm.Seek( nPos );
101 8 : }
102 :
103 8 : sal_Bool SvMetaModule::SetName( const rtl::OString& rName, SvIdlDataBase * pBase )
104 : {
105 8 : if( pBase )
106 : {
107 8 : if( pBase->GetModule( rName ) )
108 0 : return sal_False;
109 : }
110 8 : return SvMetaExtern::SetName( rName );
111 : }
112 :
113 1452 : sal_Bool SvMetaModule::FillNextName( SvGlobalName * pName )
114 : {
115 1452 : *pName = aNextName;
116 :
117 1452 : if( aNextName < aEndName )
118 : {
119 1452 : ++aNextName;
120 1452 : bIsModified = sal_True;
121 1452 : return sal_True;
122 : }
123 0 : return sal_False;
124 : }
125 :
126 21 : void SvMetaModule::ReadAttributesSvIdl( SvIdlDataBase & rBase,
127 : SvTokenStream & rInStm )
128 : {
129 21 : SvMetaExtern::ReadAttributesSvIdl( rBase, rInStm );
130 :
131 21 : aHelpFileName.ReadSvIdl( SvHash_HelpFile(), rInStm );
132 21 : if( aSlotIdFile.ReadSvIdl( SvHash_SlotIdFile(), rInStm ) )
133 : {
134 11 : sal_uInt32 nTokPos = rInStm.Tell();
135 11 : if( !rBase.ReadIdFile( rtl::OStringToOUString(aSlotIdFile.getString(), RTL_TEXTENCODING_ASCII_US)) )
136 : {
137 0 : rtl::OStringBuffer aStr(RTL_CONSTASCII_STRINGPARAM("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 21 : aTypeLibFile.ReadSvIdl( SvHash_TypeLibFile(), rInStm );
146 21 : aModulePrefix.ReadSvIdl( SvHash_ModulePrefix(), rInStm );
147 21 : }
148 :
149 8 : void SvMetaModule::WriteAttributesSvIdl( SvIdlDataBase & rBase,
150 : SvStream & rOutStm,
151 : sal_uInt16 nTab )
152 : {
153 8 : SvMetaExtern::WriteAttributesSvIdl( rBase, rOutStm, nTab );
154 8 : if( !aTypeLibFile.getString().isEmpty() || !aSlotIdFile.getString().isEmpty() || !aTypeLibFile.getString().isEmpty() )
155 : {
156 8 : if( !aHelpFileName.getString().isEmpty() )
157 : {
158 0 : WriteTab( rOutStm, nTab );
159 0 : aHelpFileName.WriteSvIdl( SvHash_HelpFile(), rOutStm, nTab +1 );
160 0 : rOutStm << ';' << endl;
161 : }
162 8 : if( !aSlotIdFile.getString().isEmpty() )
163 : {
164 8 : WriteTab( rOutStm, nTab );
165 8 : aSlotIdFile.WriteSvIdl( SvHash_SlotIdFile(), rOutStm, nTab +1 );
166 8 : rOutStm << ';' << endl;
167 : }
168 8 : if( !aTypeLibFile.getString().isEmpty() )
169 : {
170 0 : WriteTab( rOutStm, nTab );
171 0 : aTypeLibFile.WriteSvIdl( SvHash_TypeLibFile(), rOutStm, nTab +1 );
172 0 : rOutStm << ';' << endl;
173 : }
174 : }
175 8 : }
176 :
177 10136 : void SvMetaModule::ReadContextSvIdl( SvIdlDataBase & rBase,
178 : SvTokenStream & rInStm )
179 : {
180 10136 : sal_uInt32 nTokPos = rInStm.Tell();
181 20164 : if( rInStm.GetToken()->Is( SvHash_interface() )
182 10028 : || rInStm.GetToken()->Is( SvHash_shell() ) )
183 : {
184 194 : SvMetaClassRef aClass = new SvMetaClass();
185 194 : if( aClass->ReadSvIdl( rBase, rInStm ) )
186 : {
187 194 : aClassList.push_back( aClass );
188 : // announce globally
189 194 : rBase.GetClassList().push_back( aClass );
190 194 : }
191 : }
192 9942 : else if( rInStm.GetToken()->Is( SvHash_enum() ) )
193 : {
194 205 : SvMetaTypeEnumRef aEnum = new SvMetaTypeEnum();
195 :
196 205 : if( aEnum->ReadSvIdl( rBase, rInStm ) )
197 : {
198 : // declared in module
199 205 : aTypeList.push_back( aEnum );
200 : // announce globally
201 205 : rBase.GetTypeList().push_back( aEnum );
202 205 : }
203 : }
204 26614 : else if( rInStm.GetToken()->Is( SvHash_item() )
205 8592 : || rInStm.GetToken()->Is( SvHash_struct() )
206 8285 : || rInStm.GetToken()->Is( SvHash_typedef() ) )
207 : {
208 1452 : SvMetaTypeRef xItem = new SvMetaType();
209 :
210 1452 : if( xItem->ReadSvIdl( rBase, rInStm ) )
211 : {
212 : // declared in module
213 1452 : aTypeList.push_back( xItem );
214 : // announce globally
215 1452 : rBase.GetTypeList().push_back( xItem );
216 1452 : }
217 : }
218 8285 : else if( rInStm.GetToken()->Is( SvHash_include() ) )
219 : {
220 133 : sal_Bool bOk = sal_False;
221 133 : rInStm.GetToken_Next();
222 133 : SvToken * pTok = rInStm.GetToken_Next();
223 133 : if( pTok->IsString() )
224 : {
225 133 : DirEntry aFullName( rtl::OStringToOUString(pTok->GetString(), RTL_TEXTENCODING_ASCII_US) );
226 133 : rBase.StartNewFile( aFullName.GetFull() );
227 133 : if( aFullName.Find( rBase.GetPath() ) )
228 : {
229 133 : rBase.AddDepFile(aFullName.GetFull());
230 133 : SvTokenStream aTokStm( aFullName.GetFull() );
231 133 : if( SVSTREAM_OK == aTokStm.GetStream().GetError() )
232 : {
233 : // rescue error from old file
234 133 : SvIdlError aOldErr = rBase.GetError();
235 : // reset error
236 133 : rBase.SetError( SvIdlError() );
237 :
238 133 : sal_uInt32 nBeginPos = 0xFFFFFFFF; // can not happen with Tell
239 10234 : while( nBeginPos != aTokStm.Tell() )
240 : {
241 9968 : nBeginPos = aTokStm.Tell();
242 9968 : ReadContextSvIdl( rBase, aTokStm );
243 9968 : aTokStm.ReadDelemiter();
244 : }
245 133 : bOk = aTokStm.GetToken()->IsEof();
246 133 : if( !bOk )
247 : {
248 0 : rBase.WriteError( aTokStm );
249 : }
250 : // recover error from old file
251 133 : rBase.SetError( aOldErr );
252 : }
253 : else
254 : {
255 : rtl::OStringBuffer aStr(RTL_CONSTASCII_STRINGPARAM(
256 0 : "cannot open file: "));
257 : aStr.append(rtl::OUStringToOString(aFullName.GetFull(),
258 0 : RTL_TEXTENCODING_UTF8));
259 0 : rBase.SetError(aStr.makeStringAndClear(), pTok);
260 133 : }
261 : }
262 : else
263 : {
264 : rtl::OStringBuffer aStr(RTL_CONSTASCII_STRINGPARAM(
265 0 : "cannot find file:"));
266 : aStr.append(rtl::OUStringToOString(aFullName.GetFull(),
267 0 : RTL_TEXTENCODING_UTF8));
268 0 : rBase.SetError(aStr.makeStringAndClear(), pTok);
269 133 : }
270 : }
271 133 : if( !bOk )
272 0 : rInStm.Seek( nTokPos );
273 : }
274 : else
275 : {
276 8152 : SvMetaSlotRef xSlot = new SvMetaSlot();
277 :
278 8152 : if( xSlot->ReadSvIdl( rBase, rInStm ) )
279 : {
280 8011 : if( xSlot->Test( rBase, rInStm ) )
281 : {
282 : // declared in module
283 8011 : aAttrList.push_back( xSlot );
284 : // announce globally
285 8011 : rBase.AppendAttr( xSlot );
286 : }
287 8152 : }
288 : }
289 10136 : }
290 :
291 8 : void SvMetaModule::WriteContextSvIdl( SvIdlDataBase & rBase,
292 : SvStream & rOutStm,
293 : sal_uInt16 nTab )
294 : {
295 8 : SvMetaExtern::WriteContextSvIdl( rBase, rOutStm, nTab );
296 : sal_uLong n;
297 1665 : for( n = 0; n < aTypeList.size(); n++ )
298 : {
299 1657 : WriteTab( rOutStm, nTab );
300 1657 : aTypeList[n]->WriteSvIdl( rBase, rOutStm, nTab );
301 : }
302 8 : rOutStm << endl;
303 8019 : for( n = 0; n < aAttrList.size(); n++ )
304 : {
305 8011 : WriteTab( rOutStm, nTab );
306 8011 : aAttrList[n]->WriteSvIdl( rBase, rOutStm, nTab );
307 : }
308 8 : rOutStm << endl;
309 202 : for( n = 0; n < aClassList.size(); n++ )
310 : {
311 194 : WriteTab( rOutStm, nTab );
312 194 : aClassList[n]->WriteSvIdl( rBase, rOutStm, nTab );
313 : }
314 8 : }
315 :
316 8 : sal_Bool SvMetaModule::ReadSvIdl( SvIdlDataBase & rBase, SvTokenStream & rInStm )
317 : {
318 8 : bIsModified = sal_True; // up to now always when compiler running
319 :
320 8 : sal_uInt32 nTokPos = rInStm.Tell();
321 8 : SvToken * pTok = rInStm.GetToken_Next();
322 8 : sal_Bool bOk = sal_False;
323 8 : bOk = pTok->Is( SvHash_module() );
324 8 : if( bOk )
325 : {
326 8 : pTok = rInStm.GetToken_Next();
327 8 : if( pTok->IsString() )
328 8 : bOk = aBeginName.MakeId(rtl::OStringToOUString(pTok->GetString(), RTL_TEXTENCODING_ASCII_US));
329 : }
330 8 : rInStm.ReadDelemiter();
331 8 : if( bOk )
332 : {
333 8 : pTok = rInStm.GetToken_Next();
334 8 : if( pTok->IsString() )
335 8 : bOk = aEndName.MakeId(rtl::OStringToOUString(pTok->GetString(), RTL_TEXTENCODING_ASCII_US));
336 : }
337 8 : rInStm.ReadDelemiter();
338 8 : if( bOk )
339 : {
340 8 : aNextName = aBeginName;
341 :
342 8 : rBase.Push( this ); // onto the context stack
343 :
344 8 : if( ReadNameSvIdl( rBase, rInStm ) )
345 : {
346 : // set pointer to itself
347 8 : SetModule( rBase );
348 8 : bOk = SvMetaName::ReadSvIdl( rBase, rInStm );
349 : }
350 8 : rBase.GetStack().Pop(); // remove from stack
351 : }
352 8 : if( !bOk )
353 0 : rInStm.Seek( nTokPos );
354 8 : return bOk;
355 : }
356 :
357 8 : void SvMetaModule::WriteSvIdl( SvIdlDataBase & rBase, SvStream & rOutStm,
358 : sal_uInt16 nTab )
359 : {
360 8 : rOutStm << SvHash_module()->GetName().getStr() << endl
361 8 : << '\"';
362 8 : rOutStm.WriteUniOrByteString( aBeginName.GetHexName(), rOutStm.GetStreamCharSet() );
363 8 : rOutStm << '\"' << endl << '\"';
364 8 : rOutStm.WriteUniOrByteString( aEndName.GetHexName(), rOutStm.GetStreamCharSet() );
365 8 : rOutStm << '\"' << endl;
366 8 : SvMetaExtern::WriteSvIdl( rBase, rOutStm, nTab );
367 8 : }
368 :
369 8 : void SvMetaModule::WriteSfx( SvIdlDataBase & rBase, SvStream & rOutStm )
370 : {
371 202 : for( sal_uLong n = 0; n < aClassList.size(); n++ )
372 : {
373 194 : SvMetaClass * pClass = aClassList[n];
374 194 : pClass->WriteSfx( rBase, rOutStm );
375 : }
376 8 : }
377 :
378 8 : void SvMetaModule::WriteHelpIds( SvIdlDataBase & rBase, SvStream & rOutStm,
379 : HelpIdTable& rTable )
380 : {
381 202 : for( sal_uLong n = 0; n < aClassList.size(); n++ )
382 : {
383 194 : SvMetaClass * pClass = aClassList[n];
384 194 : pClass->WriteHelpIds( rBase, rOutStm, rTable );
385 : }
386 8 : }
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 << "// class SvMetaModule" << endl;
398 0 : WriteTab( rOutStm, nTab );
399 0 : rOutStm << "helpfile(\"" << aHelpFileName.getString().getStr() << "\");" << 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 << "#include \"" << aSlotIdFile.getString().getStr() << '"' << endl << endl;
415 : }
416 0 : SvMetaExtern::Write( rBase, rOutStm, nTab, nT, nA );
417 0 : rOutStm << endl;
418 0 : WriteTab( rOutStm, nTab );
419 0 : rOutStm << "library " << GetName().getString().getStr() << endl;
420 0 : WriteTab( rOutStm, nTab );
421 0 : rOutStm << '{' << endl;
422 0 : WriteTab( rOutStm, nTab );
423 0 : rOutStm << "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 << '}' << endl;
439 : }
440 0 : break;
441 : case WRITE_DOCU:
442 : {
443 0 : rOutStm << "SvIDL interface documentation" << endl << endl;
444 0 : rOutStm << "<MODULE>" << endl << GetName().getString().getStr() << endl;
445 0 : WriteDescription( rOutStm );
446 0 : rOutStm << "</MODULE>" << endl << endl;
447 :
448 0 : rOutStm << "<CLASSES>" << endl;
449 0 : for( sal_uLong n = 0; n < aClassList.size(); n++ )
450 : {
451 0 : SvMetaClass * pClass = aClassList[n];
452 0 : if( !pClass->IsShell() )
453 : {
454 0 : rOutStm << pClass->GetName().getString().getStr();
455 0 : SvMetaClass* pSC = pClass->GetSuperClass();
456 0 : if( pSC )
457 0 : rOutStm << " : " << pSC->GetName().getString().getStr();
458 :
459 : // imported classes
460 0 : const SvClassElementMemberList& rClassList = pClass->GetClassList();
461 0 : if ( !rClassList.empty() )
462 : {
463 0 : rOutStm << " ( ";
464 :
465 0 : for( sal_uLong m=0; m<rClassList.size(); ++m )
466 : {
467 0 : SvClassElement *pEle = rClassList[m];
468 0 : SvMetaClass *pCl = pEle->GetClass();
469 0 : rOutStm << pCl->GetName().getString().getStr();
470 0 : if ( m+1 == rClassList.size() )
471 0 : rOutStm << " )";
472 : else
473 0 : rOutStm << " , ";
474 : }
475 : }
476 :
477 0 : rOutStm << endl;
478 : }
479 : }
480 0 : rOutStm << "</CLASSES>" << endl << endl;
481 : // no break!
482 : }
483 :
484 : case WRITE_C_SOURCE:
485 : case WRITE_C_HEADER:
486 : {
487 0 : for( sal_uLong n = 0; n < aClassList.size(); n++ )
488 : {
489 0 : SvMetaClass * pClass = aClassList[n];
490 0 : if( !pClass->IsShell() )
491 0 : pClass->Write( rBase, rOutStm, nTab, nT, nA );
492 : }
493 : }
494 0 : break;
495 :
496 : default:
497 0 : break;
498 : }
499 0 : }
500 :
501 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|