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_tools.hxx"
26
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <vos/signal.hxx>
31 #include <tools/debug.hxx>
32 #ifndef _TABLE_HXX
33 #include <tools/table.hxx>
34 #endif
35 #include <tools/stream.hxx>
36 #include <tools/resmgr.hxx>
37 #include <tools/rc.hxx>
38 #include <tools/rcid.h>
39 #include <osl/endian.h>
40 #include <osl/process.h>
41 #include <osl/thread.h>
42 #include <osl/file.hxx>
43 #include <osl/mutex.hxx>
44 #include <rtl/ustrbuf.hxx>
45 #include <rtl/strbuf.hxx>
46 #include <tools/urlobj.hxx>
47 #include <rtl/instance.hxx>
48 #include <rtl/bootstrap.hxx>
49 #include <i18npool/mslangid.hxx>
50 #include <tools/simplerm.hxx>
51
52 #include <tools/isofallback.hxx>
53
54 #include <functional>
55 #include <algorithm>
56 #include <unordered_map>
57 #include <list>
58 #include <set>
59
60 #ifdef UNX
61 #define SEARCH_PATH_DELIMITER_CHAR_STRING ":"
62 #define SEARCH_PATH_DELIMITER ':'
63 #else
64 #define SEARCH_PATH_DELIMITER_CHAR_STRING ";"
65 #define SEARCH_PATH_DELIMITER ';'
66 #endif
67
68 #define SEARCH_PATH_DELIMITER_STRING ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SEARCH_PATH_DELIMITER_CHAR_STRING ) )
69
70 using namespace rtl;
71 using namespace osl;
72
73 // for thread safety
74 static osl::Mutex* pResMgrMutex = NULL;
getResMgrMutex()75 static osl::Mutex& getResMgrMutex()
76 {
77 if( !pResMgrMutex )
78 {
79 osl::Guard<osl::Mutex> aGuard( *osl::Mutex::getGlobalMutex() );
80 if( ! pResMgrMutex )
81 pResMgrMutex = new osl::Mutex();
82 }
83 return *pResMgrMutex;
84 }
85
86 struct ImpContent;
87 class InternalResMgr
88 {
89 friend class ResMgr;
90 friend class SimpleResMgr;
91 friend class ResMgrContainer;
92
93 ImpContent * pContent;
94 sal_uInt32 nOffCorrection;
95 sal_uInt8 * pStringBlock;
96 SvStream * pStm;
97 sal_Bool bEqual2Content;
98 sal_uInt32 nEntries;
99 OUString aFileName;
100 OUString aPrefix;
101 OUString aResName;
102 bool bSingular;
103 com::sun::star::lang::Locale aLocale;
104 std::unordered_map<sal_uInt64, int>* pResUseDump;
105
106 InternalResMgr( const OUString& rFileURL,
107 const OUString& aPrefix,
108 const OUString& aResName,
109 const com::sun::star::lang::Locale& rLocale );
110 ~InternalResMgr();
111 sal_Bool Create();
112
113 sal_Bool IsGlobalAvailable( RESOURCE_TYPE nRT, sal_uInt32 nId ) const;
114 void * LoadGlobalRes( RESOURCE_TYPE nRT, sal_uInt32 nId,
115 void **pResHandle );
116 public:
117 void FreeGlobalRes( void *, void * );
118
119 SvStream * GetBitmapStream( sal_uInt32 nResId );
120 };
121
122 // =======================================================================
123
124 class ResMgrContainer
125 {
126 static ResMgrContainer* pOneInstance;
127
128 struct ContainerElement
129 {
130 InternalResMgr* pResMgr;
131 OUString aFileURL;
132 int nRefCount;
133 int nLoadCount;
134
ContainerElementResMgrContainer::ContainerElement135 ContainerElement() :
136 pResMgr( NULL ),
137 nRefCount( 0 ),
138 nLoadCount( 0 )
139 {}
140 };
141
142 std::unordered_map< OUString, ContainerElement, OUStringHash> m_aResFiles;
143 com::sun::star::lang::Locale m_aDefLocale;
144
ResMgrContainer()145 ResMgrContainer() { init(); }
146 ~ResMgrContainer();
147
148 void init();
149 public:
150
151 static ResMgrContainer& get();
152 static void release();
153
154 InternalResMgr* getResMgr( const OUString& rPrefix,
155 com::sun::star::lang::Locale& rLocale,
156 bool bForceNewInstance = false
157 );
158 InternalResMgr* getNextFallback( InternalResMgr* pResMgr );
159
160 void freeResMgr( InternalResMgr* pResMgr );
161
setDefLocale(const com::sun::star::lang::Locale & rLocale)162 void setDefLocale( const com::sun::star::lang::Locale& rLocale )
163 { m_aDefLocale = rLocale; }
getDefLocale() const164 const com::sun::star::lang::Locale& getDefLocale() const
165 { return m_aDefLocale; }
166 };
167
168 ResMgrContainer* ResMgrContainer::pOneInstance = NULL;
169
get()170 ResMgrContainer& ResMgrContainer::get()
171 {
172 if( ! pOneInstance )
173 pOneInstance = new ResMgrContainer();
174 return *pOneInstance;
175 }
176
~ResMgrContainer()177 ResMgrContainer::~ResMgrContainer()
178 {
179 for( std::unordered_map< OUString, ContainerElement, OUStringHash >::iterator it =
180 m_aResFiles.begin(); it != m_aResFiles.end(); ++it )
181 {
182 OSL_TRACE( "Resource file %s loaded %d times\n",
183 OUStringToOString( it->second.aFileURL, osl_getThreadTextEncoding() ).getStr(),
184 it->second.nLoadCount );
185 delete it->second.pResMgr;
186 }
187 }
188
release()189 void ResMgrContainer::release()
190 {
191 delete pOneInstance;
192 pOneInstance = NULL;
193 }
194
init()195 void ResMgrContainer::init()
196 {
197 // get resource path
198 std::list< OUString > aDirs;
199 sal_Int32 nIndex = 0;
200
201 // 1. fixed locations
202 rtl::OUString uri(
203 RTL_CONSTASCII_USTRINGPARAM("$OOO_BASE_DIR/program/resource"));
204 rtl::Bootstrap::expandMacros(uri);
205 aDirs.push_back(uri);
206
207 // 2. in STAR_RESOURCEPATH
208 const sal_Char* pEnv = getenv( "STAR_RESOURCEPATH" );
209 if( pEnv )
210 {
211 OUString aEnvPath( OStringToOUString( OString( pEnv ), osl_getThreadTextEncoding() ) );
212 nIndex = 0;
213 while( nIndex >= 0 )
214 {
215 OUString aPathElement( aEnvPath.getToken( 0, SEARCH_PATH_DELIMITER, nIndex ) );
216 if( aPathElement.getLength() )
217 {
218 OUString aFileURL;
219 File::getFileURLFromSystemPath( aPathElement, aFileURL );
220 aDirs.push_back( aFileURL);
221 }
222 }
223 }
224
225 // collect all possible resource files
226 for( std::list< OUString >::const_iterator dir_it = aDirs.begin(); dir_it != aDirs.end(); ++dir_it )
227 {
228 Directory aDir( *dir_it );
229 if( aDir.open() == FileBase::E_None )
230 {
231 DirectoryItem aItem;
232 while( aDir.getNextItem( aItem ) == FileBase::E_None )
233 {
234 FileStatus aStatus(FileStatusMask_FileName);
235 if( aItem.getFileStatus( aStatus ) == FileBase::E_None )
236 {
237 OUString aFileName = aStatus.getFileName();
238 if( aFileName.getLength() < 5 )
239 continue;
240 if( ! aFileName.endsWithIgnoreAsciiCaseAsciiL( ".res", 4 ) )
241 continue;
242 OUString aResName = aFileName.copy( 0, aFileName.getLength()-4 );
243 if( m_aResFiles.find( aResName ) != m_aResFiles.end() )
244 continue;
245 OUStringBuffer aURL( dir_it->getLength() + aFileName.getLength() + 1 );
246 aURL.append( *dir_it );
247 if( !dir_it->endsWithIgnoreAsciiCaseAsciiL( "/", 1 ) )
248 aURL.append( sal_Unicode('/') );
249 aURL.append( aFileName );
250 m_aResFiles[ aResName ].aFileURL = aURL.makeStringAndClear();
251 }
252 }
253 }
254 #if OSL_DEBUG_LEVEL > 1
255 else
256 OSL_TRACE( "opening dir %s failed\n", OUStringToOString( *dir_it, osl_getThreadTextEncoding() ).getStr() );
257 #endif
258 }
259 #if OSL_DEBUG_LEVEL > 1
260 for( std::unordered_map< OUString, ContainerElement, OUStringHash >::const_iterator it =
261 m_aResFiles.begin(); it != m_aResFiles.end(); ++it )
262 {
263 OSL_TRACE( "ResMgrContainer: %s -> %s\n",
264 OUStringToOString( it->first, osl_getThreadTextEncoding() ).getStr(),
265 OUStringToOString( it->second.aFileURL, osl_getThreadTextEncoding() ).getStr() );
266 }
267 #endif
268
269 // set default language
270 LanguageType nLang = MsLangId::getSystemUILanguage();
271 MsLangId::convertLanguageToLocale(nLang, m_aDefLocale);
272 }
273
getResMgr(const OUString & rPrefix,com::sun::star::lang::Locale & rLocale,bool bForceNewInstance)274 InternalResMgr* ResMgrContainer::getResMgr( const OUString& rPrefix,
275 com::sun::star::lang::Locale& rLocale,
276 bool bForceNewInstance
277 )
278 {
279 com::sun::star::lang::Locale aLocale( rLocale );
280 OUStringBuffer aSearch( rPrefix.getLength() + 16 );
281 std::unordered_map< OUString, ContainerElement, OUStringHash >::iterator it = m_aResFiles.end();
282
283 int nTries = 0;
284 if( aLocale.Language.getLength() > 0 )
285 nTries = 1;
286 if( aLocale.Country.getLength() > 0 )
287 nTries = 2;
288 if( aLocale.Variant.getLength() > 0 )
289 nTries = 3;
290 while( nTries-- )
291 {
292 aSearch.append( rPrefix );
293 if( nTries > -1 )
294 {
295 aSearch.append( aLocale.Language );
296 }
297 if( nTries > 0 )
298 {
299 aSearch.append( sal_Unicode('-') );
300 aSearch.append( aLocale.Country );
301 }
302 if( nTries > 1 )
303 {
304 aSearch.append( sal_Unicode('-') );
305 aSearch.append( aLocale.Variant );
306 }
307 it = m_aResFiles.find( aSearch.makeStringAndClear() );
308 if( it != m_aResFiles.end() )
309 {
310 // ensure InternalResMgr existence
311 if( ! it->second.pResMgr )
312 {
313 InternalResMgr* pImp =
314 new InternalResMgr( it->second.aFileURL, rPrefix, it->first, aLocale );
315 if( ! pImp->Create() )
316 {
317 delete pImp;
318 continue;
319 }
320 it->second.pResMgr = pImp;
321 }
322 break;
323 }
324 if( nTries == 0 && !aLocale.Language.equalsIgnoreAsciiCaseAscii( "en" ) )
325 {
326 // locale fallback failed
327 // fallback to en-US locale
328 nTries = 2;
329 aLocale.Language = OUString( RTL_CONSTASCII_USTRINGPARAM( "en" ) );
330 aLocale.Country = OUString( RTL_CONSTASCII_USTRINGPARAM( "US" ) );
331 aLocale.Variant = OUString();
332 }
333 }
334 // try if there is anything with this prefix at all
335 if( it == m_aResFiles.end() )
336 {
337 aLocale = com::sun::star::lang::Locale();
338 it = m_aResFiles.find( rPrefix );
339 if( it == m_aResFiles.end() )
340 {
341 for( it = m_aResFiles.begin(); it != m_aResFiles.end(); ++it )
342 {
343 if( it->first.matchIgnoreAsciiCase( rPrefix ) )
344 {
345 // ensure InternalResMgr existence
346 if( ! it->second.pResMgr )
347 {
348 InternalResMgr* pImp =
349 new InternalResMgr( it->second.aFileURL,
350 rPrefix,
351 it->first,
352 aLocale );
353 if( ! pImp->Create() )
354 {
355 delete pImp;
356 continue;
357 }
358 it->second.pResMgr = pImp;
359 }
360 // try to guess locale
361 sal_Int32 nIndex = rPrefix.getLength();
362 aLocale.Language = it->first.getToken( 0, '-', nIndex );
363 if( nIndex > 0 )
364 aLocale.Country = it->first.getToken( 0, '-', nIndex );
365 if( nIndex > 0 )
366 aLocale.Variant = it->first.getToken( 0, '-', nIndex );
367 break;
368 }
369 }
370 }
371 }
372 // give up
373 if( it == m_aResFiles.end() )
374 {
375 OUStringBuffer sKey = rPrefix;
376 sKey.append( rLocale.Language );
377 if( rLocale.Country.getLength() )
378 {
379 sKey.append( sal_Unicode('-') );
380 sKey.append( rLocale.Country );
381 }
382 if( rLocale.Variant.getLength() )
383 {
384 sKey.append( sal_Unicode('-') );
385 sKey.append( rLocale.Variant );
386 } // if( aLocale.Variant.getLength() )
387 ::rtl::OUString sURL = sKey.makeStringAndClear();
388 sURL += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".res"));
389 if ( m_aResFiles.find(sURL) == m_aResFiles.end() )
390 {
391 m_aResFiles[ sURL ].aFileURL = sURL;
392 return getResMgr(rPrefix,rLocale,bForceNewInstance);
393 } // if ( m_aResFiles.find(sURL) == m_aResFiles.end() )
394 return NULL;
395 }
396
397 rLocale = aLocale;
398 // at this point it->second.pResMgr must be filled either by creating a new one
399 // (then the refcount is still 0) or because we already had one
400 InternalResMgr* pImp = it->second.pResMgr;
401
402 if( it->second.nRefCount == 0 )
403 it->second.nLoadCount++;
404
405 // for SimpleResMgr
406 if( bForceNewInstance )
407 {
408 if( it->second.nRefCount == 0 )
409 {
410 // shortcut: the match algorithm already created the InternalResMgr
411 // take it instead of creating yet another one
412 it->second.pResMgr = NULL;
413 pImp->bSingular = true;
414 }
415 else
416 {
417 pImp = new InternalResMgr( it->second.aFileURL, rPrefix, it->first, aLocale );
418 pImp->bSingular = true;
419 if( !pImp->Create() )
420 {
421 delete pImp;
422 pImp = NULL;
423 }
424 else
425 it->second.nLoadCount++;
426 }
427 }
428 else
429 it->second.nRefCount++;
430
431 return pImp;
432 }
433
getNextFallback(InternalResMgr * pMgr)434 InternalResMgr* ResMgrContainer::getNextFallback( InternalResMgr* pMgr )
435 {
436 com::sun::star::lang::Locale aLocale = pMgr->aLocale;
437 if( aLocale.Variant.getLength() )
438 aLocale.Variant = OUString();
439 else if( aLocale.Country.getLength() )
440 aLocale.Country = OUString();
441 else if( ! aLocale.Language.equalsIgnoreAsciiCaseAscii( "en" ) )
442 {
443 aLocale.Language = OUString( RTL_CONSTASCII_USTRINGPARAM( "en" ) );
444 aLocale.Country = OUString( RTL_CONSTASCII_USTRINGPARAM( "US" ) );
445 }
446 InternalResMgr* pNext = getResMgr( pMgr->aPrefix, aLocale, pMgr->bSingular );
447 // prevent recursion
448 if( pNext == pMgr || pNext->aResName.equals( pMgr->aResName ) )
449 {
450 if( pNext->bSingular )
451 delete pNext;
452 pNext = NULL;
453 }
454 return pNext;
455 }
456
freeResMgr(InternalResMgr * pResMgr)457 void ResMgrContainer::freeResMgr( InternalResMgr* pResMgr )
458 {
459 if( pResMgr->bSingular )
460 delete pResMgr;
461 else
462 {
463 std::unordered_map< OUString, ContainerElement, OUStringHash >::iterator it =
464 m_aResFiles.find( pResMgr->aResName );
465 if( it != m_aResFiles.end() )
466 {
467 DBG_ASSERT( it->second.nRefCount > 0, "InternalResMgr freed too often" );
468 if( it->second.nRefCount > 0 )
469 it->second.nRefCount--;
470 if( it->second.nRefCount == 0 )
471 {
472 delete it->second.pResMgr;
473 it->second.pResMgr = NULL;
474 }
475 }
476 }
477 }
478
479 // =======================================================================
480
TestRes()481 void Resource::TestRes()
482 {
483 if( m_pResMgr )
484 m_pResMgr->TestStack( this );
485 }
486
487 struct ImpContent
488 {
489 sal_uInt64 nTypeAndId;
490 sal_uInt32 nOffset;
491 };
492
493 struct ImpContentLessCompare
494 {
495 typedef ImpContent first_argument_type;
496 typedef ImpContent second_argument_type;
497 typedef bool result_type;
operator ()ImpContentLessCompare498 inline bool operator() (const ImpContent& lhs, const ImpContent& rhs) const
499 {
500 return lhs.nTypeAndId < rhs.nTypeAndId;
501 }
502 };
503
504 struct ImpContentMixLessCompare
505 {
506 typedef ImpContent first_argument_type;
507 typedef sal_uInt64 second_argument_type;
508 typedef bool result_type;
operator ()ImpContentMixLessCompare509 inline bool operator() (const ImpContent& lhs, const sal_uInt64& rhs) const
510 {
511 return lhs.nTypeAndId < rhs;
512 }
operator ()ImpContentMixLessCompare513 inline bool operator() (const sal_uInt64& lhs, const ImpContent& rhs) const
514 {
515 return lhs < rhs.nTypeAndId;
516 }
517 };
518
519
520 // =======================================================================
521
522 static ResHookProc pImplResHookProc = 0;
523
524 // =======================================================================
525
GetBitmapStream(sal_uInt32 nId)526 SvStream * InternalResMgr::GetBitmapStream( sal_uInt32 nId )
527 {
528 // Anfang der Strings suchen
529 ImpContent * pFind = ::std::lower_bound(pContent,
530 pContent + nEntries,
531 ((sal_uInt64(RT_SYS_BITMAP) << 32) | nId),
532 ImpContentMixLessCompare());
533 if ( (pFind != (pContent + nEntries)) && (pFind->nTypeAndId == ((sal_uInt64(RT_SYS_BITMAP) << 32) | nId)) )
534 {
535 pStm->Seek( pFind->nOffset );
536 return pStm;
537 }
538 return NULL;
539 }
540
541 // -----------------------------------------------------------------------
542
InternalResMgr(const OUString & rFileURL,const OUString & rPrefix,const OUString & rResName,const com::sun::star::lang::Locale & rLocale)543 InternalResMgr::InternalResMgr( const OUString& rFileURL,
544 const OUString& rPrefix,
545 const OUString& rResName,
546 const com::sun::star::lang::Locale& rLocale )
547 : pContent( NULL )
548 , pStringBlock( NULL )
549 , pStm( NULL )
550 , bEqual2Content( sal_True )
551 , nEntries( 0 )
552 , aFileName( rFileURL )
553 , aPrefix( rPrefix )
554 , aResName( rResName )
555 , bSingular( false )
556 , aLocale( rLocale )
557 , pResUseDump( 0 )
558 {
559 }
560
561 // -----------------------------------------------------------------------
562
~InternalResMgr()563 InternalResMgr::~InternalResMgr()
564 {
565 rtl_freeMemory(pContent);
566 rtl_freeMemory(pStringBlock);
567 delete pStm;
568
569 #ifdef DBG_UTIL
570 if( pResUseDump )
571 {
572 const sal_Char* pLogFile = getenv( "STAR_RESOURCE_LOGGING" );
573 if ( pLogFile )
574 {
575 SvFileStream aStm( UniString( pLogFile, RTL_TEXTENCODING_ASCII_US ), STREAM_WRITE );
576 aStm.Seek( STREAM_SEEK_TO_END );
577 ByteString aLine( "FileName: " );
578 aLine.Append( ByteString( OUStringToOString( aFileName, RTL_TEXTENCODING_UTF8 ) ) );
579 aStm.WriteLine( aLine );
580
581 for( std::unordered_map<sal_uInt64, int>::const_iterator it = pResUseDump->begin();
582 it != pResUseDump->end(); ++it )
583 {
584 sal_uInt64 nKeyId = it->first;
585 aLine.Assign( "Type/Id: " );
586 aLine.Append( ByteString::CreateFromInt32( sal::static_int_cast< sal_Int32 >((nKeyId >> 32) & 0xFFFFFFFF) ) );
587 aLine.Append( '/' );
588 aLine.Append( ByteString::CreateFromInt32( sal::static_int_cast< sal_Int32 >(nKeyId & 0xFFFFFFFF) ) );
589 aStm.WriteLine( aLine );
590 }
591 }
592 }
593 #endif
594
595 delete pResUseDump;
596 }
597
598 // -----------------------------------------------------------------------
599
600
Create()601 sal_Bool InternalResMgr::Create()
602 {
603 ResMgrContainer::get();
604 sal_Bool bDone = sal_False;
605
606 pStm = new SvFileStream( aFileName, (STREAM_READ | STREAM_SHARE_DENYWRITE | STREAM_NOCREATE) );
607 if( pStm->GetError() == 0 )
608 {
609 sal_Int32 lContLen = 0;
610
611 pStm->Seek( STREAM_SEEK_TO_END );
612 /*
613 if( ( pInternalResMgr->pHead = (RSHEADER_TYPE *)mmap( 0, nResourceFileSize,
614 PROT_READ, MAP_PRIVATE,
615 fRes, 0 ) ) != (RSHEADER_TYPE *)-1)
616 */
617 pStm->SeekRel( - (int)sizeof( lContLen ) );
618 pStm->Read( &lContLen, sizeof( lContLen ) );
619 // is bigendian, swab to the right endian
620 lContLen = ResMgr::GetLong( &lContLen );
621 pStm->SeekRel( -lContLen );
622 // allocate stored ImpContent data (12 bytes per unit)
623 sal_uInt8* pContentBuf = (sal_uInt8*)rtl_allocateMemory( lContLen );
624 pStm->Read( pContentBuf, lContLen );
625 // allocate ImpContent space (sizeof(ImpContent) per unit, not necessarily 12)
626 pContent = (ImpContent *)rtl_allocateMemory( sizeof(ImpContent)*lContLen/12 );
627 // Auf die Anzahl der ImpContent k�rzen
628 nEntries = (sal_uInt32)lContLen / 12;
629 bEqual2Content = sal_True; // Die Daten der Resourcen liegen
630 // genauso wie das Inhaltsverzeichnis
631 sal_Bool bSorted = sal_True;
632 if( nEntries )
633 {
634 #ifdef DBG_UTIL
635 const sal_Char* pLogFile = getenv( "STAR_RESOURCE_LOGGING" );
636 if ( pLogFile )
637 {
638 pResUseDump = new std::unordered_map<sal_uInt64, int>;
639 for( sal_uInt32 i = 0; i < nEntries; ++i )
640 (*pResUseDump)[pContent[i].nTypeAndId] = 1;
641 }
642 #endif
643 // swap the content to the right endian
644 pContent[0].nTypeAndId = ResMgr::GetUInt64( pContentBuf );
645 pContent[0].nOffset = ResMgr::GetLong( pContentBuf+8 );
646 sal_uInt32 nCount = nEntries - 1;
647 for( sal_uInt32 i = 0,j=1; i < nCount; ++i,++j )
648 {
649 // swap the content to the right endian
650 pContent[j].nTypeAndId = ResMgr::GetUInt64( pContentBuf + (12*j) );
651 pContent[j].nOffset = ResMgr::GetLong( pContentBuf + (12*j+8) );
652 if( pContent[i].nTypeAndId >= pContent[j].nTypeAndId )
653 bSorted = sal_False;
654 if( (pContent[i].nTypeAndId & 0xFFFFFFFF00000000LL) == (pContent[j].nTypeAndId & 0xFFFFFFFF00000000LL)
655 && pContent[i].nOffset >= pContent[j].nOffset )
656 bEqual2Content = sal_False;
657 }
658 }
659 rtl_freeMemory( pContentBuf );
660 #ifndef OS2
661 OSL_ENSURE( bSorted, "content not sorted" );
662 #endif
663 OSL_ENSURE( bEqual2Content, "resource structure wrong" );
664 if( !bSorted )
665 ::std::sort(pContent,pContent+nEntries,ImpContentLessCompare());
666 // qsort( pContent, nEntries, sizeof( ImpContent ), Compare );
667
668 bDone = sal_True;
669 }
670
671 return bDone;
672 }
673
674 // -----------------------------------------------------------------------
675
IsGlobalAvailable(RESOURCE_TYPE nRT,sal_uInt32 nId) const676 sal_Bool InternalResMgr::IsGlobalAvailable( RESOURCE_TYPE nRT, sal_uInt32 nId ) const
677 {
678 // Anfang der Strings suchen
679 sal_uInt64 nValue = ((sal_uInt64(nRT) << 32) | nId);
680 ImpContent * pFind = ::std::lower_bound(pContent,
681 pContent + nEntries,
682 nValue,
683 ImpContentMixLessCompare());
684 return (pFind != (pContent + nEntries)) && (pFind->nTypeAndId == nValue);
685 }
686
687 // -----------------------------------------------------------------------
688
LoadGlobalRes(RESOURCE_TYPE nRT,sal_uInt32 nId,void ** pResHandle)689 void* InternalResMgr::LoadGlobalRes( RESOURCE_TYPE nRT, sal_uInt32 nId,
690 void **pResHandle )
691 {
692 #ifdef DBG_UTIL
693 if( pResUseDump )
694 pResUseDump->erase( (sal_uInt64(nRT) << 32) | nId );
695 #endif
696 // Anfang der Strings suchen
697 sal_uInt64 nValue = ((sal_uInt64(nRT) << 32) | nId);
698 ImpContent* pEnd = (pContent + nEntries);
699 ImpContent* pFind = ::std::lower_bound( pContent,
700 pEnd,
701 nValue,
702 ImpContentMixLessCompare());
703 if( pFind && (pFind != pEnd) && (pFind->nTypeAndId == nValue) )
704 {
705 if( nRT == RSC_STRING && bEqual2Content )
706 {
707 // String Optimierung
708 if( !pStringBlock )
709 {
710 // Anfang der Strings suchen
711 ImpContent * pFirst = pFind;
712 ImpContent * pLast = pFirst;
713 while( pFirst > pContent && ((pFirst -1)->nTypeAndId >> 32) == RSC_STRING )
714 --pFirst;
715 while( pLast < pEnd && (pLast->nTypeAndId >> 32) == RSC_STRING )
716 ++pLast;
717 nOffCorrection = pFirst->nOffset;
718 sal_uInt32 nSize;
719 --pLast;
720 pStm->Seek( pLast->nOffset );
721 RSHEADER_TYPE aHdr;
722 pStm->Read( &aHdr, sizeof( aHdr ) );
723 nSize = pLast->nOffset + aHdr.GetGlobOff() - nOffCorrection;
724 pStringBlock = (sal_uInt8*)rtl_allocateMemory( nSize );
725 pStm->Seek( pFirst->nOffset );
726 pStm->Read( pStringBlock, nSize );
727 }
728 *pResHandle = pStringBlock;
729 return (sal_uInt8*)pStringBlock + pFind->nOffset - nOffCorrection;
730 } // if( nRT == RSC_STRING && bEqual2Content )
731 else
732 {
733 *pResHandle = 0;
734 RSHEADER_TYPE aHeader;
735 pStm->Seek( pFind->nOffset );
736 pStm->Read( &aHeader, sizeof( RSHEADER_TYPE ) );
737 void * pRes = rtl_allocateMemory( aHeader.GetGlobOff() );
738 memcpy( pRes, &aHeader, sizeof( RSHEADER_TYPE ) );
739 pStm->Read( (sal_uInt8*)pRes + sizeof( RSHEADER_TYPE ),
740 aHeader.GetGlobOff() - sizeof( RSHEADER_TYPE ) );
741 return pRes;
742 }
743 } // if( pFind && (pFind != pEnd) && (pFind->nTypeAndId == nValue) )
744 *pResHandle = 0;
745 //Resource holen
746 return NULL;
747 }
748
749 // -----------------------------------------------------------------------
750
FreeGlobalRes(void * pResHandle,void * pResource)751 void InternalResMgr::FreeGlobalRes( void * pResHandle, void * pResource )
752 {
753 if ( !pResHandle )
754 // REsource wurde extra allokiert
755 rtl_freeMemory(pResource);
756 }
757
758 // =======================================================================
759
760 #ifdef DBG_UTIL
761
GetTypeRes_Impl(const ResId & rTypeId)762 UniString GetTypeRes_Impl( const ResId& rTypeId )
763 {
764 // Funktion verlassen, falls Resourcefehler in dieser Funktion
765 static int bInUse = sal_False;
766 UniString aTypStr( UniString::CreateFromInt32( rTypeId.GetId() ) );
767
768 if ( !bInUse )
769 {
770 bInUse = sal_True;
771
772 ResId aResId( sal_uInt32(RSCVERSION_ID), *rTypeId.GetResMgr() );
773 aResId.SetRT( RSC_VERSIONCONTROL );
774
775 if ( rTypeId.GetResMgr()->GetResource( aResId ) )
776 {
777 rTypeId.SetRT( RSC_STRING );
778 if ( rTypeId.GetResMgr()->IsAvailable( rTypeId ) )
779 {
780 aTypStr = UniString( rTypeId );
781 // Versions Resource Klassenzeiger ans Ende setzen
782 rTypeId.GetResMgr()->Increment( sizeof( RSHEADER_TYPE ) );
783 }
784 }
785 bInUse = sal_False;
786 }
787
788 return aTypStr;
789 }
790
791 // -----------------------------------------------------------------------
792
RscError_Impl(const sal_Char * pMessage,ResMgr * pResMgr,RESOURCE_TYPE nRT,sal_uInt32 nId,std::vector<ImpRCStack> & rResStack,int nDepth)793 void ResMgr::RscError_Impl( const sal_Char* pMessage, ResMgr* pResMgr,
794 RESOURCE_TYPE nRT, sal_uInt32 nId,
795 std::vector< ImpRCStack >& rResStack, int nDepth )
796 {
797 // create a separate ResMgr with its own stack
798 // first get a second reference of the InternalResMgr
799 InternalResMgr* pImp =
800 ResMgrContainer::get().getResMgr( pResMgr->pImpRes->aPrefix,
801 pResMgr->pImpRes->aLocale,
802 true );
803
804 ResMgr* pNewResMgr = new ResMgr( pImp );
805
806 ByteString aStr = OUStringToOString( pResMgr->GetFileName(), RTL_TEXTENCODING_UTF8 );
807 if ( aStr.Len() )
808 aStr += '\n';
809
810 aStr.Append( "Class: " );
811 aStr.Append( ByteString( GetTypeRes_Impl( ResId( nRT, *pNewResMgr ) ), RTL_TEXTENCODING_UTF8 ) );
812 aStr.Append( ", Id: " );
813 aStr.Append( ByteString::CreateFromInt32( (long)nId ) );
814 aStr.Append( ". " );
815 aStr.Append( pMessage );
816
817 aStr.Append( "\nResource Stack\n" );
818 while( nDepth > 0 )
819 {
820 aStr.Append( "Class: " );
821 aStr.Append( ByteString( GetTypeRes_Impl( ResId( rResStack[nDepth].pResource->GetRT(), *pNewResMgr ) ), RTL_TEXTENCODING_UTF8 ) );
822 aStr.Append( ", Id: " );
823 aStr.Append( ByteString::CreateFromInt32( (long)rResStack[nDepth].pResource->GetId() ) );
824 nDepth--;
825 }
826
827 // clean up
828 delete pNewResMgr;
829
830 DBG_ERROR( aStr.GetBuffer() );
831 }
832
833 #endif
834
835 // =======================================================================
836
RscException_Impl()837 static void RscException_Impl()
838 {
839 switch ( vos::OSignalHandler::raise( OSL_SIGNAL_USER_RESOURCEFAILURE, (void*)"" ) )
840 {
841 case vos::OSignalHandler::TAction_CallNextHandler:
842 abort();
843
844 case vos::OSignalHandler::TAction_Ignore:
845 return;
846
847 case vos::OSignalHandler::TAction_AbortApplication:
848 abort();
849
850 case vos::OSignalHandler::TAction_KillApplication:
851 exit(-1);
852 }
853 }
854
855 // =======================================================================
856
Init(ResMgr * pMgr,const Resource * pObj,sal_uInt32 Id)857 void ImpRCStack::Init( ResMgr* pMgr, const Resource* pObj, sal_uInt32 Id )
858 {
859 pResource = NULL;
860 pClassRes = NULL;
861 Flags = RC_NOTYPE;
862 aResHandle = NULL;
863 pResObj = pObj;
864 nId = Id & ~RSC_DONTRELEASE; //TLX: Besser Init aendern
865 pResMgr = pMgr;
866 if ( !(Id & RSC_DONTRELEASE) )
867 Flags |= RC_AUTORELEASE;
868 }
869
870 // -----------------------------------------------------------------------
871
Clear()872 void ImpRCStack::Clear()
873 {
874 pResource = NULL;
875 pClassRes = NULL;
876 Flags = RC_NOTYPE;
877 aResHandle = NULL;
878 pResObj = NULL;
879 nId = 0;
880 pResMgr = NULL;
881 }
882
883 // -----------------------------------------------------------------------
884
LocalResource(const ImpRCStack * pStack,RESOURCE_TYPE nRTType,sal_uInt32 nId)885 static RSHEADER_TYPE* LocalResource( const ImpRCStack* pStack,
886 RESOURCE_TYPE nRTType,
887 sal_uInt32 nId )
888 {
889 // Gibt die Position der Resource zurueck, wenn sie gefunden wurde.
890 // Ansonsten gibt die Funktion Null zurueck.
891 RSHEADER_TYPE* pTmp; // Zeiger auf Kind-Resourceobjekte
892 RSHEADER_TYPE* pEnd; // Zeiger auf das Ende der Resource
893
894 if ( pStack->pResource && pStack->pClassRes )
895 {
896 pTmp = (RSHEADER_TYPE*)
897 ((sal_uInt8*)pStack->pResource + pStack->pResource->GetLocalOff());
898 pEnd = (RSHEADER_TYPE*)
899 ((sal_uInt8*)pStack->pResource + pStack->pResource->GetGlobOff());
900 while ( pTmp != pEnd )
901 {
902 if ( pTmp->GetRT() == nRTType && pTmp->GetId() == nId )
903 return pTmp;
904 pTmp = (RSHEADER_TYPE*)((sal_uInt8*)pTmp + pTmp->GetGlobOff());
905 }
906 }
907
908 return NULL;
909 }
910
911 // =======================================================================
912
913 void* ResMgr::pEmptyBuffer = NULL;
914
getEmptyBuffer()915 void* ResMgr::getEmptyBuffer()
916 {
917 if( ! pEmptyBuffer )
918 pEmptyBuffer = rtl_allocateZeroMemory( 1024 );
919 return pEmptyBuffer;
920 }
921
DestroyAllResMgr()922 void ResMgr::DestroyAllResMgr()
923 {
924 {
925 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
926 if( pEmptyBuffer )
927 {
928 rtl_freeMemory( pEmptyBuffer );
929 pEmptyBuffer = NULL;
930 }
931 ResMgrContainer::release();
932 }
933 delete pResMgrMutex;
934 pResMgrMutex = NULL;
935 }
936
937 // -----------------------------------------------------------------------
938
Init(const OUString & rFileName)939 void ResMgr::Init( const OUString& rFileName )
940 {
941 (void) rFileName; // avoid warning about unused parameter
942 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
943
944 if ( !pImpRes )
945 {
946 #ifdef DBG_UTIL
947 ByteString aStr( "Resourcefile not found:\n" );
948 aStr += ByteString( OUStringToOString( rFileName, RTL_TEXTENCODING_UTF8 ) );
949 DBG_ERROR( aStr.GetBuffer() );
950 #endif
951 RscException_Impl();
952 }
953 #ifdef DBG_UTIL
954 else
955 {
956 void* aResHandle = 0; // Hilfvariable fuer Resource
957 void* pVoid; // Zeiger auf die Resource
958
959 pVoid = pImpRes->LoadGlobalRes( RSC_VERSIONCONTROL, RSCVERSION_ID,
960 &aResHandle );
961 if ( pVoid )
962 pImpRes->FreeGlobalRes( aResHandle, pVoid );
963 else
964 {
965 ByteString aStr( "Wrong version:\n" );
966 aStr += ByteString( OUStringToOString( pImpRes->aFileName, RTL_TEXTENCODING_UTF8 ) );
967 DbgError( aStr.GetBuffer() );
968 }
969 }
970 #endif
971 nCurStack = -1;
972 aStack.clear();
973 pFallbackResMgr = pOriginalResMgr = NULL;
974 incStack();
975 }
976
977 // -----------------------------------------------------------------------
978
ResMgr(InternalResMgr * pImpMgr)979 ResMgr::ResMgr( InternalResMgr * pImpMgr )
980 {
981 pImpRes = pImpMgr;
982 Init( pImpMgr->aFileName );
983 }
984
985 // -----------------------------------------------------------------------
986
~ResMgr()987 ResMgr::~ResMgr()
988 {
989 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
990
991 ResMgrContainer::get().freeResMgr( pImpRes );
992
993 // clean up possible left rc stack frames
994 while( nCurStack > 0 )
995 {
996 if( ( aStack[nCurStack].Flags & (RC_GLOBAL | RC_NOTFOUND) ) == RC_GLOBAL )
997 pImpRes->FreeGlobalRes( aStack[nCurStack].aResHandle,
998 aStack[nCurStack].pResource );
999 nCurStack--;
1000 }
1001 }
1002
1003
incStack()1004 void ResMgr::incStack()
1005 {
1006 nCurStack++;
1007 if( nCurStack >= int(aStack.size()) )
1008 aStack.push_back( ImpRCStack() );
1009 aStack[nCurStack].Clear();
1010
1011 DBG_ASSERT( nCurStack < 32, "Resource stack unreasonably large" );
1012 }
1013
decStack()1014 void ResMgr::decStack()
1015 {
1016 DBG_ASSERT( nCurStack > 0, "resource stack underrun !" );
1017 if( (aStack[nCurStack].Flags & RC_FALLBACK_UP) )
1018 {
1019 nCurStack--;
1020 // warning: this will delete *this, see below
1021 pOriginalResMgr->decStack();
1022 }
1023 else
1024 {
1025 ImpRCStack& rTop = aStack[nCurStack];
1026 if( (rTop.Flags & RC_FALLBACK_DOWN) )
1027 {
1028 #if OSL_DEBUG_LEVEL > 1
1029 OSL_TRACE( "returning from fallback %s\n",
1030 OUStringToOString(pFallbackResMgr->GetFileName(), osl_getThreadTextEncoding() ).getStr() );
1031 #endif
1032 delete pFallbackResMgr;
1033 pFallbackResMgr = NULL;
1034 }
1035 nCurStack--;
1036 }
1037 }
1038
1039 #ifdef DBG_UTIL
1040
TestStack(const Resource * pResObj)1041 void ResMgr::TestStack( const Resource* pResObj )
1042 {
1043 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1044
1045 if ( DbgIsResource() )
1046 {
1047 for( int i = 1; i <= nCurStack; ++i )
1048 {
1049 if ( aStack[i].pResObj == pResObj )
1050 {
1051 #ifdef DBG_UTIL
1052 RscError_Impl( "Resource not freed! ", this,
1053 aStack[i].pResource->GetRT(),
1054 aStack[i].pResource->GetId(),
1055 aStack, i-1 );
1056 #endif
1057 }
1058 }
1059 }
1060 }
1061
1062 #else
1063
TestStack(const Resource *)1064 void ResMgr::TestStack( const Resource* )
1065 {
1066 }
1067
1068 #endif
1069
1070 // -----------------------------------------------------------------------
IsAvailable(const ResId & rId,const Resource * pResObj) const1071 sal_Bool ResMgr::IsAvailable( const ResId& rId, const Resource* pResObj ) const
1072 {
1073 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1074
1075 sal_Bool bAvailable = sal_False;
1076 RSHEADER_TYPE* pClassRes = rId.GetpResource();
1077 RESOURCE_TYPE nRT = rId.GetRT2();
1078 sal_uInt32 nId = rId.GetId();
1079 const ResMgr* pMgr = rId.GetResMgr();
1080
1081 if ( !pMgr )
1082 pMgr = this;
1083
1084 if( pMgr->pFallbackResMgr )
1085 {
1086 ResId aId( rId );
1087 aId.SetResMgr( NULL );
1088 return pMgr->pFallbackResMgr->IsAvailable( aId, pResObj );
1089 }
1090
1091 if ( !pResObj || pResObj == pMgr->aStack[pMgr->nCurStack].pResObj )
1092 {
1093 if ( !pClassRes )
1094 pClassRes = LocalResource( &pMgr->aStack[pMgr->nCurStack], nRT, nId );
1095 if ( pClassRes )
1096 {
1097 if ( pClassRes->GetRT() == nRT )
1098 bAvailable = sal_True;
1099 }
1100 }
1101
1102 // vieleicht globale Resource
1103 if ( !pClassRes )
1104 bAvailable = pMgr->pImpRes->IsGlobalAvailable( nRT, nId );
1105
1106 return bAvailable;
1107 }
1108
1109 // -----------------------------------------------------------------------
1110
GetClass()1111 void* ResMgr::GetClass()
1112 {
1113 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1114
1115 if( pFallbackResMgr )
1116 return pFallbackResMgr->GetClass();
1117
1118 return aStack[nCurStack].pClassRes;
1119 }
1120
1121 // -----------------------------------------------------------------------
1122
GetResource(const ResId & rId,const Resource * pResObj)1123 sal_Bool ResMgr::GetResource( const ResId& rId, const Resource* pResObj )
1124 {
1125 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1126
1127 if( pFallbackResMgr )
1128 {
1129 ResId aId( rId );
1130 aId.SetResMgr( NULL );
1131 return pFallbackResMgr->GetResource( aId, pResObj );
1132 }
1133
1134 ResMgr* pMgr = rId.GetResMgr();
1135 if ( pMgr && (this != pMgr) )
1136 return pMgr->GetResource( rId, pResObj );
1137
1138 // normally Increment will pop the context; this is
1139 // not possible in RC_NOTFOUND case, so pop a frame here
1140 ImpRCStack* pTop = &aStack[nCurStack];
1141 if( (pTop->Flags & RC_NOTFOUND) )
1142 {
1143 decStack();
1144 }
1145
1146 RSHEADER_TYPE* pClassRes = rId.GetpResource();
1147 RESOURCE_TYPE nRT = rId.GetRT2();
1148 sal_uInt32 nId = rId.GetId();
1149
1150 incStack();
1151 pTop = &aStack[nCurStack];
1152 pTop->Init( pMgr, pResObj, nId |
1153 (rId.IsAutoRelease() ? 0 : RSC_DONTRELEASE) );
1154
1155 if ( pClassRes )
1156 {
1157 if ( pClassRes->GetRT() == nRT )
1158 pTop->pClassRes = pClassRes;
1159 else
1160 {
1161 #ifdef DBG_UTIL
1162 RscError_Impl( "Different class and resource type!",
1163 this, nRT, nId, aStack, nCurStack-1 );
1164 #endif
1165 pTop->Flags |= RC_NOTFOUND;
1166 pTop->pClassRes = getEmptyBuffer();
1167 pTop->pResource = (RSHEADER_TYPE*)pTop->pClassRes;
1168 return sal_False;
1169 }
1170 }
1171 else
1172 {
1173 OSL_ENSURE( nCurStack > 0, "stack of 1 to shallow" );
1174 pTop->pClassRes = LocalResource( &aStack[nCurStack-1], nRT, nId );
1175 }
1176
1177 if ( pTop->pClassRes )
1178 // lokale Resource, nicht system Resource
1179 pTop->pResource = (RSHEADER_TYPE *)pTop->pClassRes;
1180 else
1181 {
1182 pTop->pClassRes = pImpRes->LoadGlobalRes( nRT, nId, &pTop->aResHandle );
1183 if ( pTop->pClassRes )
1184 {
1185 pTop->Flags |= RC_GLOBAL;
1186 pTop->pResource = (RSHEADER_TYPE *)pTop->pClassRes;
1187 }
1188 else
1189 {
1190 // try to get a fallback resource
1191 pFallbackResMgr = CreateFallbackResMgr( rId, pResObj );
1192 if( pFallbackResMgr )
1193 {
1194 pTop->Flags |= RC_FALLBACK_DOWN;
1195 #ifdef DBG_UTIL
1196 ByteString aMess( "found resource " );
1197 aMess.Append( ByteString::CreateFromInt32( nId ) );
1198 aMess.Append( " in fallback " );
1199 aMess.Append( ByteString( OUStringToOString( pFallbackResMgr->GetFileName(), osl_getThreadTextEncoding() ) ) );
1200 aMess.Append( "\n" );
1201 RscError_Impl( aMess.GetBuffer(),
1202 this, nRT, nId, aStack, nCurStack-1 );
1203 #endif
1204 }
1205 else
1206 {
1207 #ifdef DBG_UTIL
1208 RscError_Impl( "Cannot load resource! ",
1209 this, nRT, nId, aStack, nCurStack-1 );
1210 #endif
1211 pTop->Flags |= RC_NOTFOUND;
1212 pTop->pClassRes = getEmptyBuffer();
1213 pTop->pResource = (RSHEADER_TYPE*)pTop->pClassRes;
1214 return sal_False;
1215 }
1216 }
1217 }
1218
1219 return sal_True;
1220 }
1221
1222 // -----------------------------------------------------------------------
1223
GetResourceSkipHeader(const ResId & rResId,ResMgr ** ppResMgr)1224 void * ResMgr::GetResourceSkipHeader( const ResId& rResId, ResMgr ** ppResMgr )
1225 {
1226 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1227
1228 DBG_ASSERT( rResId.GetResMgr(), "illegal ResId without ResMgr" );
1229 *ppResMgr = rResId.GetResMgr();
1230 if( *ppResMgr )
1231 {
1232 (*ppResMgr)->GetResource( rResId );
1233 (*ppResMgr)->Increment( sizeof( RSHEADER_TYPE ) );
1234 return (*ppResMgr)->GetClass();
1235 }
1236 return getEmptyBuffer();
1237 }
1238
1239 // -----------------------------------------------------------------------
1240
PopContext(const Resource * pResObj)1241 void ResMgr::PopContext( const Resource* pResObj )
1242 {
1243 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1244
1245 if( pFallbackResMgr )
1246 {
1247 pFallbackResMgr->PopContext( pResObj );
1248 return;
1249 }
1250
1251 #ifdef DBG_UTIL
1252 if ( DbgIsResource() )
1253 {
1254 if ( (aStack[nCurStack].pResObj != pResObj) || nCurStack == 0 )
1255 {
1256 RscError_Impl( "Cannot free resource! ", this,
1257 RSC_NOTYPE, 0, aStack, nCurStack );
1258 }
1259 }
1260 #endif
1261
1262 if ( nCurStack > 0 )
1263 {
1264 ImpRCStack* pTop = &aStack[nCurStack];
1265 #ifdef DBG_UTIL
1266 if ( DbgIsResource() && !(pTop->Flags & RC_NOTFOUND) )
1267 {
1268 void* pRes = (sal_uInt8*)pTop->pResource +
1269 pTop->pResource->GetLocalOff();
1270
1271 if ( pTop->pClassRes != pRes )
1272 {
1273 RscError_Impl( "Classpointer not at the end!",
1274 this, pTop->pResource->GetRT(),
1275 pTop->pResource->GetId(),
1276 aStack, nCurStack-1 );
1277 }
1278 }
1279 #endif
1280
1281 // Resource freigeben
1282 if( (pTop->Flags & (RC_GLOBAL | RC_NOTFOUND)) == RC_GLOBAL )
1283 // kann auch Fremd-Ressource sein
1284 pImpRes->FreeGlobalRes( pTop->aResHandle, pTop->pResource );
1285 decStack();
1286 }
1287 }
1288
1289 // -----------------------------------------------------------------------
1290
CreateBlock(const ResId & rId)1291 RSHEADER_TYPE* ResMgr::CreateBlock( const ResId& rId )
1292 {
1293 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1294
1295 if( pFallbackResMgr )
1296 {
1297 ResId aId( rId );
1298 aId.SetResMgr( NULL );
1299 return pFallbackResMgr->CreateBlock( aId );
1300 }
1301
1302 RSHEADER_TYPE* pHeader = NULL;
1303 if ( GetResource( rId ) )
1304 {
1305 // Der Zeiger steht am Anfang, deswegen zeigt der Klassen-Pointer
1306 // auf den Header und die restliche Groesse ist die Gesammte.
1307 pHeader = (RSHEADER_TYPE*)rtl_allocateMemory( GetRemainSize() );
1308 memcpy( pHeader, GetClass(), GetRemainSize() );
1309 Increment( pHeader->GetLocalOff() ); //ans Ende setzen
1310 if ( pHeader->GetLocalOff() != pHeader->GetGlobOff() )
1311 // Hat Sub-Ressourcen, deshalb extra freigeben
1312 PopContext();
1313 }
1314
1315 return pHeader;
1316 }
1317
1318 // ------------------------------------------------------------------
1319
GetShort(void * pShort)1320 sal_Int16 ResMgr::GetShort( void * pShort )
1321 {
1322 return ((*((sal_uInt8*)pShort + 0) << 8) |
1323 (*((sal_uInt8*)pShort + 1) << 0) );
1324 }
1325
1326 // ------------------------------------------------------------------
1327
GetLong(void * pLong)1328 sal_Int32 ResMgr::GetLong( void * pLong )
1329 {
1330 return ((*((sal_uInt8*)pLong + 0) << 24) |
1331 (*((sal_uInt8*)pLong + 1) << 16) |
1332 (*((sal_uInt8*)pLong + 2) << 8) |
1333 (*((sal_uInt8*)pLong + 3) << 0) );
1334 }
1335
1336 // ------------------------------------------------------------------
1337
GetUInt64(void * pDatum)1338 sal_uInt64 ResMgr::GetUInt64( void* pDatum )
1339 {
1340 return ((sal_uInt64(*((sal_uInt8*)pDatum + 0)) << 56) |
1341 (sal_uInt64(*((sal_uInt8*)pDatum + 1)) << 48) |
1342 (sal_uInt64(*((sal_uInt8*)pDatum + 2)) << 40) |
1343 (sal_uInt64(*((sal_uInt8*)pDatum + 3)) << 32) |
1344 (sal_uInt64(*((sal_uInt8*)pDatum + 4)) << 24) |
1345 (sal_uInt64(*((sal_uInt8*)pDatum + 5)) << 16) |
1346 (sal_uInt64(*((sal_uInt8*)pDatum + 6)) << 8) |
1347 (sal_uInt64(*((sal_uInt8*)pDatum + 7)) << 0) );
1348 }
1349
1350 // -----------------------------------------------------------------------
GetStringWithoutHook(UniString & rStr,const sal_uInt8 * pStr)1351 sal_uInt32 ResMgr::GetStringWithoutHook( UniString& rStr, const sal_uInt8* pStr )
1352 {
1353 sal_uInt32 nLen=0;
1354 sal_uInt32 nRet = GetStringSize( pStr, nLen );
1355 UniString aString( (sal_Char*)pStr, RTL_TEXTENCODING_UTF8,
1356 RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE |
1357 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT |
1358 RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT );
1359 rStr = aString;
1360 return nRet;
1361 }
1362
GetString(UniString & rStr,const sal_uInt8 * pStr)1363 sal_uInt32 ResMgr::GetString( UniString& rStr, const sal_uInt8* pStr )
1364 {
1365 UniString aString;
1366 sal_uInt32 nRet = GetStringWithoutHook( aString, pStr );
1367 if ( pImplResHookProc )
1368 pImplResHookProc( aString );
1369 rStr = aString;
1370 return nRet;
1371 }
1372
GetByteString(rtl::OString & rStr,const sal_uInt8 * pStr)1373 sal_uInt32 ResMgr::GetByteString( rtl::OString& rStr, const sal_uInt8* pStr )
1374 {
1375 sal_uInt32 nLen=0;
1376 sal_uInt32 nRet = GetStringSize( pStr, nLen );
1377 rStr = rtl::OString( (const sal_Char*)pStr, nLen );
1378 return nRet;
1379 }
1380
1381 // ------------------------------------------------------------------
1382
GetStringSize(const sal_uInt8 * pStr,sal_uInt32 & nLen)1383 sal_uInt32 ResMgr::GetStringSize( const sal_uInt8* pStr, sal_uInt32& nLen )
1384 {
1385 nLen = static_cast< sal_uInt32 >( strlen( (const char*)pStr ) );
1386 return GetStringSize( nLen );
1387 }
1388
1389 // -----------------------------------------------------------------------
1390
GetRemainSize()1391 sal_uInt32 ResMgr::GetRemainSize()
1392 {
1393 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1394
1395 if( pFallbackResMgr )
1396 return pFallbackResMgr->GetRemainSize();
1397
1398 const ImpRCStack& rTop = aStack[nCurStack];
1399 return (sal_uInt32)((sal_IntPtr)(sal_uInt8 *)rTop.pResource +
1400 rTop.pResource->GetLocalOff() -
1401 (sal_IntPtr)(sal_uInt8 *)rTop.pClassRes);
1402 }
1403
1404 // -----------------------------------------------------------------------
1405
Increment(sal_uInt32 nSize)1406 void* ResMgr::Increment( sal_uInt32 nSize )
1407 {
1408 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1409
1410 if( pFallbackResMgr )
1411 return pFallbackResMgr->Increment( nSize );
1412
1413 ImpRCStack& rStack = aStack[nCurStack];
1414 if( (rStack.Flags & RC_NOTFOUND) )
1415 return rStack.pClassRes;
1416
1417 sal_uInt8* pClassRes = (sal_uInt8*)rStack.pClassRes + nSize;
1418
1419 rStack.pClassRes = pClassRes;
1420
1421 RSHEADER_TYPE* pRes = rStack.pResource;
1422
1423 sal_uInt32 nLocalOff = pRes->GetLocalOff();
1424 if ( (pRes->GetGlobOff() == nLocalOff) &&
1425 (((char*)pRes + nLocalOff) == rStack.pClassRes) &&
1426 (rStack.Flags & RC_AUTORELEASE))
1427 {
1428 PopContext( rStack.pResObj );
1429 }
1430
1431 return pClassRes;
1432 }
1433
CreateFallbackResMgr(const ResId & rId,const Resource * pResource)1434 ResMgr* ResMgr::CreateFallbackResMgr( const ResId& rId, const Resource* pResource )
1435 {
1436 ResMgr *pFallback = NULL;
1437 if( nCurStack > 0 )
1438 {
1439 // get the next fallback level in resource file scope
1440 InternalResMgr* pRes = ResMgrContainer::get().getNextFallback( pImpRes );
1441 if( pRes )
1442 {
1443 // check that the fallback locale is not already in the chain of
1444 // fallbacks - prevent fallback loops
1445 ResMgr* pResMgr = this;
1446 while( pResMgr &&
1447 ( pResMgr->pImpRes->aLocale.Language != pRes->aLocale.Language ||
1448 pResMgr->pImpRes->aLocale.Country != pRes->aLocale.Country ||
1449 pResMgr->pImpRes->aLocale.Variant != pRes->aLocale.Variant )
1450 )
1451 {
1452 pResMgr = pResMgr->pOriginalResMgr;
1453 }
1454 if( pResMgr )
1455 {
1456 // found a recursion, no fallback possible
1457 ResMgrContainer::get().freeResMgr( pRes );
1458 return NULL;
1459 }
1460 OSL_TRACE( "trying fallback: %s\n", OUStringToOString( pRes->aFileName, osl_getThreadTextEncoding() ).getStr() );
1461 pFallback = new ResMgr( pRes );
1462 pFallback->pOriginalResMgr = this;
1463 // try to recreate the resource stack
1464 bool bHaveStack = true;
1465 for( int i = 1; i < nCurStack; i++ )
1466 {
1467 if( !aStack[i].pResource )
1468 {
1469 bHaveStack = false;
1470 break;
1471 }
1472 ResId aId( aStack[i].pResource->GetId(), *pFallbackResMgr );
1473 aId.SetRT( aStack[i].pResource->GetRT() );
1474 if( !pFallback->GetResource( aId ) )
1475 {
1476 bHaveStack = false;
1477 break;
1478 }
1479 }
1480 if( bHaveStack )
1481 {
1482 ResId aId( rId.GetId(), *pFallback );
1483 aId.SetRT( rId.GetRT() );
1484 if( !pFallback->GetResource( aId, pResource ) )
1485 bHaveStack = false;
1486 else
1487 pFallback->aStack[pFallback->nCurStack].Flags |= RC_FALLBACK_UP;
1488 }
1489 if( !bHaveStack )
1490 {
1491 delete pFallback;
1492 pFallback = NULL;
1493 }
1494 }
1495 }
1496 return pFallback;
1497 }
1498
1499 //---------------------------------------------------------------------------
1500 //
1501 // method left here for SDK compatibility,
1502 // used in "framework/source/services/substitutepathvars.cxx"
1503 //
1504 // phone numbers no longer in use for resource files
1505 //
1506 //---------------------------------------------------------------------------
1507
GetLang(LanguageType & nType,sal_uInt16 nPrio)1508 const char* ResMgr::GetLang( LanguageType& nType, sal_uInt16 nPrio )
1509 {
1510 if ( nType == LANGUAGE_SYSTEM || nType == LANGUAGE_DONTKNOW )
1511 nType = MsLangId::getSystemUILanguage();
1512
1513 if ( nPrio == 0 )
1514 {
1515 switch ( nType )
1516 {
1517 case LANGUAGE_DANISH:
1518 return "45";
1519
1520 case LANGUAGE_DUTCH:
1521 case LANGUAGE_DUTCH_BELGIAN:
1522 return "31";
1523
1524 case LANGUAGE_ENGLISH:
1525 case LANGUAGE_ENGLISH_UK:
1526 case LANGUAGE_ENGLISH_EIRE:
1527 case LANGUAGE_ENGLISH_SAFRICA:
1528 case LANGUAGE_ENGLISH_JAMAICA:
1529 case LANGUAGE_ENGLISH_BELIZE:
1530 case LANGUAGE_ENGLISH_TRINIDAD:
1531 case LANGUAGE_ENGLISH_ZIMBABWE:
1532 case LANGUAGE_ENGLISH_PHILIPPINES:
1533 return "44";
1534
1535 case LANGUAGE_ENGLISH_US:
1536 case LANGUAGE_ENGLISH_CAN:
1537 return "01";
1538
1539 case LANGUAGE_ENGLISH_AUS:
1540 case LANGUAGE_ENGLISH_NZ:
1541 return "61";
1542 case LANGUAGE_ESTONIAN:
1543 return "77";
1544
1545
1546 case LANGUAGE_FINNISH:
1547 return "35";
1548
1549 case LANGUAGE_FRENCH_CANADIAN:
1550 return "02";
1551
1552 case LANGUAGE_FRENCH:
1553 case LANGUAGE_FRENCH_BELGIAN:
1554 case LANGUAGE_FRENCH_SWISS:
1555 case LANGUAGE_FRENCH_LUXEMBOURG:
1556 case LANGUAGE_FRENCH_MONACO:
1557 return "33";
1558
1559 case LANGUAGE_GERMAN:
1560 case LANGUAGE_GERMAN_SWISS:
1561 case LANGUAGE_GERMAN_AUSTRIAN:
1562 case LANGUAGE_GERMAN_LUXEMBOURG:
1563 case LANGUAGE_GERMAN_LIECHTENSTEIN:
1564 return "49";
1565
1566 case LANGUAGE_ITALIAN:
1567 case LANGUAGE_ITALIAN_SWISS:
1568 return "39";
1569
1570 case LANGUAGE_NORWEGIAN:
1571 case LANGUAGE_NORWEGIAN_BOKMAL:
1572 return "47";
1573
1574 case LANGUAGE_PORTUGUESE:
1575 return "03";
1576
1577 case LANGUAGE_PORTUGUESE_BRAZILIAN:
1578 return "55";
1579
1580 case LANGUAGE_SPANISH_DATED:
1581 case LANGUAGE_SPANISH_MEXICAN:
1582 case LANGUAGE_SPANISH_MODERN:
1583 case LANGUAGE_SPANISH_GUATEMALA:
1584 case LANGUAGE_SPANISH_COSTARICA:
1585 case LANGUAGE_SPANISH_PANAMA:
1586 case LANGUAGE_SPANISH_DOMINICAN_REPUBLIC:
1587 case LANGUAGE_SPANISH_VENEZUELA:
1588 case LANGUAGE_SPANISH_COLOMBIA:
1589 case LANGUAGE_SPANISH_PERU:
1590 case LANGUAGE_SPANISH_ARGENTINA:
1591 case LANGUAGE_SPANISH_ECUADOR:
1592 case LANGUAGE_SPANISH_CHILE:
1593 case LANGUAGE_SPANISH_URUGUAY:
1594 case LANGUAGE_SPANISH_PARAGUAY:
1595 case LANGUAGE_SPANISH_BOLIVIA:
1596 return "34";
1597
1598 case LANGUAGE_SWEDISH:
1599 return "46";
1600
1601 case LANGUAGE_POLISH:
1602 return "48";
1603 case LANGUAGE_CZECH:
1604 return "42";
1605 case LANGUAGE_SLOVENIAN:
1606 return "50";
1607 case LANGUAGE_HUNGARIAN:
1608 return "36";
1609 case LANGUAGE_RUSSIAN:
1610 return "07";
1611 case LANGUAGE_SLOVAK:
1612 return "43";
1613 case LANGUAGE_GREEK:
1614 return "30";
1615 case LANGUAGE_TURKISH:
1616 return "90";
1617
1618 case LANGUAGE_CHINESE_SIMPLIFIED:
1619 return "86";
1620 case LANGUAGE_CHINESE_TRADITIONAL:
1621 return "88";
1622 case LANGUAGE_JAPANESE:
1623 return "81";
1624 case LANGUAGE_KOREAN:
1625 case LANGUAGE_KOREAN_JOHAB:
1626 return "82";
1627 case LANGUAGE_THAI:
1628 return "66";
1629 case LANGUAGE_HINDI:
1630 return "91";
1631
1632 case LANGUAGE_ARABIC_PRIMARY_ONLY:
1633 case LANGUAGE_ARABIC_IRAQ:
1634 case LANGUAGE_ARABIC_EGYPT:
1635 case LANGUAGE_ARABIC_LIBYA:
1636 case LANGUAGE_ARABIC_ALGERIA:
1637 case LANGUAGE_ARABIC_MOROCCO:
1638 case LANGUAGE_ARABIC_TUNISIA:
1639 case LANGUAGE_ARABIC_OMAN:
1640 case LANGUAGE_ARABIC_YEMEN:
1641 case LANGUAGE_ARABIC_SYRIA:
1642 case LANGUAGE_ARABIC_JORDAN:
1643 case LANGUAGE_ARABIC_LEBANON:
1644 case LANGUAGE_ARABIC_KUWAIT:
1645 case LANGUAGE_ARABIC_UAE:
1646 case LANGUAGE_ARABIC_BAHRAIN:
1647 case LANGUAGE_ARABIC_QATAR:
1648 return "96";
1649
1650 case LANGUAGE_HEBREW:
1651 return "97";
1652
1653 case LANGUAGE_CATALAN:
1654 return "37";
1655
1656 default:
1657 return "99";
1658 }
1659 }
1660 else if ( nPrio == 1 )
1661 {
1662 switch ( nType )
1663 {
1664 case LANGUAGE_FRENCH_CANADIAN:
1665 return "33";
1666
1667 case LANGUAGE_PORTUGUESE_BRAZILIAN:
1668 return "03";
1669
1670 default:
1671 return NULL;
1672 }
1673 }
1674 else if ( nPrio == 2 )
1675 return "01";
1676 else if ( nPrio == 3 )
1677 return "44";
1678 else if ( nPrio == 4 )
1679 return "49";
1680 else
1681 return "99";
1682 }
1683
1684 // -----------------------------------------------------------------------
1685
CreateResMgr(const sal_Char * pPrefixName,com::sun::star::lang::Locale aLocale)1686 ResMgr* ResMgr::CreateResMgr( const sal_Char* pPrefixName,
1687 com::sun::star::lang::Locale aLocale )
1688 {
1689 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1690
1691 OUString aPrefix( pPrefixName, strlen( pPrefixName ), osl_getThreadTextEncoding() );
1692
1693 if( ! aLocale.Language.getLength() )
1694 aLocale = ResMgrContainer::get().getDefLocale();
1695
1696 InternalResMgr* pImp = ResMgrContainer::get().getResMgr( aPrefix, aLocale );
1697 return pImp ? new ResMgr( pImp ) : NULL;
1698 }
1699
1700 // -----------------------------------------------------------------------
1701
SearchCreateResMgr(const sal_Char * pPrefixName,com::sun::star::lang::Locale & rLocale)1702 ResMgr* ResMgr::SearchCreateResMgr(
1703 const sal_Char* pPrefixName,
1704 com::sun::star::lang::Locale& rLocale )
1705 {
1706 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1707
1708 OUString aPrefix( pPrefixName, strlen( pPrefixName ), osl_getThreadTextEncoding() );
1709
1710 if( ! rLocale.Language.getLength() )
1711 rLocale = ResMgrContainer::get().getDefLocale();
1712
1713 InternalResMgr* pImp = ResMgrContainer::get().getResMgr( aPrefix, rLocale );
1714 return pImp ? new ResMgr( pImp ) : NULL;
1715 }
1716
1717 // -----------------------------------------------------------------------
1718
ReadShort()1719 sal_Int16 ResMgr::ReadShort()
1720 {
1721 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1722
1723 if( pFallbackResMgr )
1724 return pFallbackResMgr->ReadShort();
1725
1726 sal_Int16 n = GetShort( GetClass() );
1727 Increment( sizeof( sal_Int16 ) );
1728 return n;
1729 }
1730
1731 // -----------------------------------------------------------------------
1732
ReadLong()1733 sal_Int32 ResMgr::ReadLong()
1734 {
1735 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1736
1737 if( pFallbackResMgr )
1738 return pFallbackResMgr->ReadLong();
1739
1740 sal_Int32 n = GetLong( GetClass() );
1741 Increment( sizeof( sal_Int32 ) );
1742 return n;
1743 }
1744
1745 // -----------------------------------------------------------------------
1746
ReadStringWithoutHook()1747 UniString ResMgr::ReadStringWithoutHook()
1748 {
1749 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1750
1751 if( pFallbackResMgr )
1752 return pFallbackResMgr->ReadStringWithoutHook();
1753
1754 UniString aRet;
1755
1756 const ImpRCStack& rTop = aStack[nCurStack];
1757 if( (rTop.Flags & RC_NOTFOUND) )
1758 {
1759 #if OSL_DEBUG_LEVEL > 0
1760 aRet = OUString( RTL_CONSTASCII_USTRINGPARAM( "<resource not found>" ) );
1761 #endif
1762 }
1763 else
1764 Increment( GetStringWithoutHook( aRet, (const sal_uInt8*)GetClass() ) );
1765
1766 return aRet;
1767 }
1768
ReadString()1769 UniString ResMgr::ReadString()
1770 {
1771 UniString aRet = ReadStringWithoutHook();
1772 if ( pImplResHookProc )
1773 pImplResHookProc( aRet );
1774 return aRet;
1775 }
1776
ReadByteString()1777 rtl::OString ResMgr::ReadByteString()
1778 {
1779 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1780
1781 if( pFallbackResMgr )
1782 return pFallbackResMgr->ReadByteString();
1783
1784 rtl::OString aRet;
1785
1786 const ImpRCStack& rTop = aStack[nCurStack];
1787 if( (rTop.Flags & RC_NOTFOUND) )
1788 {
1789 #if OSL_DEBUG_LEVEL > 0
1790 aRet = OString( "<resource not found>" );
1791 #endif
1792 }
1793 else
1794 Increment( GetByteString( aRet, (const sal_uInt8*)GetClass() ) );
1795
1796 return aRet;
1797 }
1798
1799 // -----------------------------------------------------------------------
1800
GetAutoHelpId()1801 rtl::OString ResMgr::GetAutoHelpId()
1802 {
1803 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1804
1805 if( pFallbackResMgr )
1806 return pFallbackResMgr->GetAutoHelpId();
1807
1808 OSL_ENSURE( nCurStack, "resource stack empty in Auto help id generation" );
1809 if( nCurStack < 1 || nCurStack > 2 )
1810 return rtl::OString();
1811
1812 // prepare HID, start with resource prefix
1813 rtl::OStringBuffer aHID( 32 );
1814 aHID.append( rtl::OUStringToOString( pImpRes->aPrefix, RTL_TEXTENCODING_UTF8 ) );
1815 aHID.append( '.' );
1816
1817 // append type
1818 const ImpRCStack *pRC = StackTop();
1819 OSL_ENSURE( pRC, "missing resource stack level" );
1820
1821 if ( nCurStack == 1 )
1822 {
1823 // auto help ids for top level windows
1824 switch( pRC->pResource->GetRT() ) {
1825 case RSC_DOCKINGWINDOW: aHID.append( "DockingWindow" ); break;
1826 case RSC_WORKWIN: aHID.append( "WorkWindow" ); break;
1827 case RSC_MODELESSDIALOG: aHID.append( "ModelessDialog" ); break;
1828 case RSC_FLOATINGWINDOW: aHID.append( "FloatingWindow" ); break;
1829 case RSC_MODALDIALOG: aHID.append( "ModalDialog" ); break;
1830 case RSC_TABPAGE: aHID.append( "TabPage" ); break;
1831 default: return rtl::OString();
1832 }
1833 }
1834 else
1835 {
1836 // only controls with the following parents get auto help ids
1837 const ImpRCStack *pRC1 = StackTop(1);
1838 switch( pRC1->pResource->GetRT() ) {
1839 case RSC_DOCKINGWINDOW:
1840 case RSC_WORKWIN:
1841 case RSC_MODELESSDIALOG:
1842 case RSC_FLOATINGWINDOW:
1843 case RSC_MODALDIALOG:
1844 case RSC_TABPAGE:
1845 // intentionally no breaks!
1846 // auto help ids for controls
1847 switch( pRC->pResource->GetRT() ) {
1848 case RSC_TABCONTROL: aHID.append( "TabControl" ); break;
1849 case RSC_RADIOBUTTON: aHID.append( "RadioButton" ); break;
1850 case RSC_CHECKBOX: aHID.append( "CheckBox" ); break;
1851 case RSC_TRISTATEBOX: aHID.append( "TriStateBox" ); break;
1852 case RSC_EDIT: aHID.append( "Edit" ); break;
1853 case RSC_MULTILINEEDIT: aHID.append( "MultiLineEdit" ); break;
1854 case RSC_MULTILISTBOX: aHID.append( "MultiListBox" ); break;
1855 case RSC_LISTBOX: aHID.append( "ListBox" ); break;
1856 case RSC_COMBOBOX: aHID.append( "ComboBox" ); break;
1857 case RSC_PUSHBUTTON: aHID.append( "PushButton" ); break;
1858 case RSC_SPINFIELD: aHID.append( "SpinField" ); break;
1859 case RSC_PATTERNFIELD: aHID.append( "PatternField" ); break;
1860 case RSC_NUMERICFIELD: aHID.append( "NumericField" ); break;
1861 case RSC_METRICFIELD: aHID.append( "MetricField" ); break;
1862 case RSC_CURRENCYFIELD: aHID.append( "CurrencyField" ); break;
1863 case RSC_DATEFIELD: aHID.append( "DateField" ); break;
1864 case RSC_TIMEFIELD: aHID.append( "TimeField" ); break;
1865 case RSC_IMAGERADIOBUTTON: aHID.append( "ImageRadioButton" ); break;
1866 case RSC_NUMERICBOX: aHID.append( "NumericBox" ); break;
1867 case RSC_METRICBOX: aHID.append( "MetricBox" ); break;
1868 case RSC_CURRENCYBOX: aHID.append( "CurrencyBox" ); break;
1869 case RSC_DATEBOX: aHID.append( "DateBox" ); break;
1870 case RSC_TIMEBOX: aHID.append( "TimeBox" ); break;
1871 case RSC_IMAGEBUTTON: aHID.append( "ImageButton" ); break;
1872 case RSC_MENUBUTTON: aHID.append( "MenuButton" ); break;
1873 case RSC_MOREBUTTON: aHID.append( "MoreButton" ); break;
1874 default:
1875 // no type, no auto HID
1876 return rtl::OString();
1877 }
1878 break;
1879 default:
1880 return rtl::OString();
1881 }
1882 }
1883
1884 // append resource id hierarchy
1885 for( int nOff = nCurStack-1; nOff >= 0; nOff-- )
1886 {
1887 aHID.append( '.' );
1888 pRC = StackTop( nOff );
1889
1890 OSL_ENSURE( pRC->pResource, "missing resource in resource stack level !" );
1891 if( pRC->pResource )
1892 aHID.append( sal_Int32( pRC->pResource->GetId() ) );
1893 }
1894
1895 return aHID.makeStringAndClear();
1896 }
1897
1898 // -----------------------------------------------------------------------
1899
SetReadStringHook(ResHookProc pProc)1900 void ResMgr::SetReadStringHook( ResHookProc pProc )
1901 {
1902 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1903 pImplResHookProc = pProc;
1904 }
1905
1906 // -----------------------------------------------------------------------
1907
GetReadStringHook()1908 ResHookProc ResMgr::GetReadStringHook()
1909 {
1910 return pImplResHookProc;
1911 }
1912
1913 // -----------------------------------------------------------------------
1914
SetDefaultLocale(const com::sun::star::lang::Locale & rLocale)1915 void ResMgr::SetDefaultLocale( const com::sun::star::lang::Locale& rLocale )
1916 {
1917 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1918 ResMgrContainer::get().setDefLocale( rLocale );
1919 }
1920
1921 // -----------------------------------------------------------------------
1922
GetFileName() const1923 const OUString& ResMgr::GetFileName() const
1924 {
1925 return pImpRes->aFileName;
1926 }
1927
1928 // =======================================================================
1929
SimpleResMgr(const sal_Char * pPrefixName,const::com::sun::star::lang::Locale & rLocale)1930 SimpleResMgr::SimpleResMgr( const sal_Char* pPrefixName,
1931 const ::com::sun::star::lang::Locale& rLocale )
1932 {
1933 OUString aPrefix( pPrefixName, strlen( pPrefixName ), osl_getThreadTextEncoding() );
1934 com::sun::star::lang::Locale aLocale( rLocale );
1935
1936 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1937 if( ! aLocale.Language.getLength() )
1938 aLocale = ResMgrContainer::get().getDefLocale();
1939
1940 m_pResImpl = ResMgrContainer::get().getResMgr( aPrefix, aLocale, true );
1941 DBG_ASSERT( m_pResImpl, "SimpleResMgr::SimpleResMgr : have no impl class !" );
1942 }
1943
1944 // -----------------------------------------------------------------------
SimpleResMgr(const::rtl::OUString & _rPrefixName,::com::sun::star::lang::Locale & _inout_Locale)1945 SimpleResMgr::SimpleResMgr( const ::rtl::OUString& _rPrefixName, ::com::sun::star::lang::Locale& _inout_Locale )
1946 {
1947 osl::Guard<osl::Mutex> aGuard( getResMgrMutex() );
1948 m_pResImpl = ResMgrContainer::get().getResMgr( _rPrefixName, _inout_Locale, true );
1949 }
1950
1951 // -----------------------------------------------------------------------
~SimpleResMgr()1952 SimpleResMgr::~SimpleResMgr()
1953 {
1954 delete m_pResImpl;
1955 }
1956
1957 // -----------------------------------------------------------------------
Create(const sal_Char * pPrefixName,com::sun::star::lang::Locale aLocale)1958 SimpleResMgr* SimpleResMgr::Create( const sal_Char* pPrefixName, com::sun::star::lang::Locale aLocale )
1959 {
1960 return new SimpleResMgr( pPrefixName, aLocale );
1961 }
1962
1963 // -----------------------------------------------------------------------
IsAvailable(RESOURCE_TYPE _resourceType,sal_uInt32 _resourceId)1964 bool SimpleResMgr::IsAvailable( RESOURCE_TYPE _resourceType, sal_uInt32 _resourceId )
1965 {
1966 vos::OGuard aGuard(m_aAccessSafety);
1967
1968 if ( ( RSC_STRING != _resourceType ) && ( RSC_RESOURCE != _resourceType ) )
1969 return false;
1970
1971 DBG_ASSERT( m_pResImpl, "SimpleResMgr::IsAvailable: have no impl class !" );
1972 return m_pResImpl->IsGlobalAvailable( _resourceType, _resourceId );
1973 }
1974
1975 // -----------------------------------------------------------------------
ReadString(sal_uInt32 nId)1976 UniString SimpleResMgr::ReadString( sal_uInt32 nId )
1977 {
1978 vos::OGuard aGuard(m_aAccessSafety);
1979
1980 DBG_ASSERT( m_pResImpl, "SimpleResMgr::ReadString : have no impl class !" );
1981 // perhaps constructed with an invalid filename ?
1982
1983 UniString sReturn;
1984 if ( !m_pResImpl )
1985 return sReturn;
1986
1987 void* pResHandle = NULL;
1988 InternalResMgr* pFallback = m_pResImpl;
1989 RSHEADER_TYPE* pResHeader = (RSHEADER_TYPE*)m_pResImpl->LoadGlobalRes( RSC_STRING, nId, &pResHandle );
1990 if ( !pResHeader )
1991 {
1992 osl::Guard<osl::Mutex> aGuard2( getResMgrMutex() );
1993
1994 // try fallback
1995 while( ! pResHandle && pFallback )
1996 {
1997 InternalResMgr* pOldFallback = pFallback;
1998 pFallback = ResMgrContainer::get().getNextFallback( pFallback );
1999 if( pOldFallback != m_pResImpl )
2000 ResMgrContainer::get().freeResMgr( pOldFallback );
2001 if( pFallback )
2002 {
2003 // handle possible recursion
2004 if( pFallback->aLocale.Language != m_pResImpl->aLocale.Language ||
2005 pFallback->aLocale.Country != m_pResImpl->aLocale.Country ||
2006 pFallback->aLocale.Variant != m_pResImpl->aLocale.Variant )
2007 {
2008 pResHeader = (RSHEADER_TYPE*)pFallback->LoadGlobalRes( RSC_STRING, nId, &pResHandle );
2009 }
2010 else
2011 {
2012 ResMgrContainer::get().freeResMgr( pFallback );
2013 pFallback = NULL;
2014 }
2015 }
2016 }
2017 if( ! pResHandle )
2018 // no such resource
2019 return sReturn;
2020 }
2021
2022 // sal_uIntPtr nLen = pResHeader->GetLocalOff() - sizeof(RSHEADER_TYPE);
2023 ResMgr::GetString( sReturn, (const sal_uInt8*)(pResHeader+1) );
2024
2025 // not necessary with te current implementation which holds the string table permanently, but to be sure ....
2026 // note: pFallback cannot be NULL here and is either the fallback or m_pResImpl
2027 pFallback->FreeGlobalRes( pResHeader, pResHandle );
2028 if( m_pResImpl != pFallback )
2029 {
2030 osl::Guard<osl::Mutex> aGuard2( getResMgrMutex() );
2031
2032 ResMgrContainer::get().freeResMgr( pFallback );
2033 }
2034 return sReturn;
2035 }
2036
2037 // -----------------------------------------------------------------------
2038
GetLocale() const2039 const ::com::sun::star::lang::Locale& SimpleResMgr::GetLocale() const
2040 {
2041 DBG_ASSERT( IsValid(), "SimpleResMgr::ReadBlob: invalid, this will crash!" );
2042 return m_pResImpl->aLocale;
2043 }
2044
2045 // -----------------------------------------------------------------------
2046
ReadBlob(sal_uInt32 nId,void ** pBuffer)2047 sal_uInt32 SimpleResMgr::ReadBlob( sal_uInt32 nId, void** pBuffer )
2048 {
2049 vos::OGuard aGuard(m_aAccessSafety);
2050
2051 DBG_ASSERT( m_pResImpl, "SimpleResMgr::ReadBlob : have no impl class !" );
2052
2053 // perhaps constructed with an invalid filename ?
2054 DBG_ASSERT( pBuffer, "SimpleResMgr::ReadBlob : invalid argument !" );
2055 *pBuffer = NULL;
2056
2057 void* pResHandle = NULL;
2058 InternalResMgr* pFallback = m_pResImpl;
2059 RSHEADER_TYPE* pResHeader = (RSHEADER_TYPE*)m_pResImpl->LoadGlobalRes( RSC_RESOURCE, nId, &pResHandle );
2060 DBG_ASSERT( pResHeader, "SimpleResMgr::ReadBlob : couldn't find the resource with the given id !" );
2061
2062 if ( !pResHeader )
2063 {
2064 osl::Guard<osl::Mutex> aGuard2( getResMgrMutex() );
2065
2066 // try fallback
2067 while( ! pResHandle && pFallback )
2068 {
2069 InternalResMgr* pOldFallback = pFallback;
2070 pFallback = ResMgrContainer::get().getNextFallback( pFallback );
2071 if( pOldFallback != m_pResImpl )
2072 ResMgrContainer::get().freeResMgr( pOldFallback );
2073 if( pFallback )
2074 {
2075 // handle possible recursion
2076 if( pFallback->aLocale.Language != m_pResImpl->aLocale.Language ||
2077 pFallback->aLocale.Country != m_pResImpl->aLocale.Country ||
2078 pFallback->aLocale.Variant != m_pResImpl->aLocale.Variant )
2079 {
2080 pResHeader = (RSHEADER_TYPE*)pFallback->LoadGlobalRes( RSC_RESOURCE, nId, &pResHandle );
2081 }
2082 else
2083 {
2084 ResMgrContainer::get().freeResMgr( pFallback );
2085 pFallback = NULL;
2086 }
2087 }
2088 }
2089 if( ! pResHandle )
2090 // no exception handling, this would require the locking of the solar mutex which isn't allowed within this class
2091 return 0;
2092 }
2093
2094 DBG_ASSERT( pResHandle == NULL, "SimpleResMgr::ReadBlob : behaviour of LoadGlobalRes changed !" );
2095 // if pResHandle is not NULL the FreeBlob wouldn't have to delete the pointer given as pBuffer, but
2096 // FreeBlob doesn't know that so it would probably crash ....
2097
2098 sal_uInt32 nRemaining = pResHeader->GetLocalOff() - sizeof(RSHEADER_TYPE);
2099 *pBuffer = (void*)(((sal_uInt8*)pResHeader) + sizeof(RSHEADER_TYPE));
2100
2101 // free an eventual fallback InternalResMgr
2102 if( m_pResImpl != pFallback )
2103 {
2104 osl::Guard<osl::Mutex> aGuard2( getResMgrMutex() );
2105
2106 ResMgrContainer::get().freeResMgr( pFallback );
2107 }
2108
2109 return nRemaining;
2110 }
2111
2112 // -----------------------------------------------------------------------
2113
FreeBlob(void * pBuffer)2114 void SimpleResMgr::FreeBlob( void* pBuffer )
2115 {
2116 void* pCompleteBuffer = (void*)(((sal_uInt8*)pBuffer) - sizeof(RSHEADER_TYPE));
2117 rtl_freeMemory(pCompleteBuffer);
2118 }
2119