1*8b851043SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*8b851043SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*8b851043SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*8b851043SAndrew Rist * distributed with this work for additional information
6*8b851043SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*8b851043SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*8b851043SAndrew Rist * "License"); you may not use this file except in compliance
9*8b851043SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*8b851043SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*8b851043SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*8b851043SAndrew Rist * software distributed under the License is distributed on an
15*8b851043SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*8b851043SAndrew Rist * KIND, either express or implied. See the License for the
17*8b851043SAndrew Rist * specific language governing permissions and limitations
18*8b851043SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*8b851043SAndrew Rist *************************************************************/
21*8b851043SAndrew Rist
22*8b851043SAndrew Rist
23cdf0e10cSrcweir #ifndef _TOOLS_RESMGR_HXX
24cdf0e10cSrcweir #define _TOOLS_RESMGR_HXX
25cdf0e10cSrcweir
26cdf0e10cSrcweir #include "tools/toolsdllapi.h"
27cdf0e10cSrcweir #include <i18npool/lang.h>
28cdf0e10cSrcweir #include <tools/string.hxx>
29cdf0e10cSrcweir #include <tools/ref.hxx>
30cdf0e10cSrcweir #include <tools/resid.hxx>
31cdf0e10cSrcweir #include <com/sun/star/lang/Locale.hpp>
32cdf0e10cSrcweir
33cdf0e10cSrcweir #define CREATEVERSIONRESMGR_NAME( Name ) #Name
34cdf0e10cSrcweir #define CREATEVERSIONRESMGR( Name ) ResMgr::CreateResMgr( CREATEVERSIONRESMGR_NAME( Name ) )
35cdf0e10cSrcweir
36cdf0e10cSrcweir #define LOCALE_MAX_FALLBACK 6
37cdf0e10cSrcweir
38cdf0e10cSrcweir #include <vector>
39cdf0e10cSrcweir
40cdf0e10cSrcweir class SvStream;
41cdf0e10cSrcweir class InternalResMgr;
42cdf0e10cSrcweir
43cdf0e10cSrcweir // -----------------
44cdf0e10cSrcweir // - RSHEADER_TYPE -
45cdf0e10cSrcweir // -----------------
46cdf0e10cSrcweir
47cdf0e10cSrcweir // Definition der Struktur, aus denen die Resource aufgebaut ist
48cdf0e10cSrcweir struct RSHEADER_TYPE
49cdf0e10cSrcweir {
50cdf0e10cSrcweir private:
51cdf0e10cSrcweir sal_uInt32 nId; // Identifier der Resource
52cdf0e10cSrcweir RESOURCE_TYPE nRT; // Resource Typ
53cdf0e10cSrcweir sal_uInt32 nGlobOff; // Globaler Offset
54cdf0e10cSrcweir sal_uInt32 nLocalOff; // Lokaler Offset
55cdf0e10cSrcweir
56cdf0e10cSrcweir public:
57cdf0e10cSrcweir inline sal_uInt32 GetId(); // Identifier der Resource
58cdf0e10cSrcweir inline RESOURCE_TYPE GetRT(); // Resource Typ
59cdf0e10cSrcweir inline sal_uInt32 GetGlobOff(); // Globaler Offset
60cdf0e10cSrcweir inline sal_uInt32 GetLocalOff(); // Lokaler Offset
61cdf0e10cSrcweir };
62cdf0e10cSrcweir
63cdf0e10cSrcweir // ----------
64cdf0e10cSrcweir // - ResMgr -
65cdf0e10cSrcweir // ----------
66cdf0e10cSrcweir
67cdf0e10cSrcweir typedef void (*ResHookProc)( UniString& rStr );
68cdf0e10cSrcweir
69cdf0e10cSrcweir // ----------
70cdf0e10cSrcweir // - ResMgr -
71cdf0e10cSrcweir // ----------
72cdf0e10cSrcweir
73cdf0e10cSrcweir // Initialisierung
74cdf0e10cSrcweir #define RC_NOTYPE 0x00
75cdf0e10cSrcweir // Globale Resource
76cdf0e10cSrcweir #define RC_GLOBAL 0x01
77cdf0e10cSrcweir #define RC_AUTORELEASE 0x02
78cdf0e10cSrcweir #define RC_NOTFOUND 0x04
79cdf0e10cSrcweir #define RC_FALLBACK_DOWN 0x08
80cdf0e10cSrcweir #define RC_FALLBACK_UP 0x10
81cdf0e10cSrcweir
82cdf0e10cSrcweir class Resource;
83cdf0e10cSrcweir class ResMgr;
84cdf0e10cSrcweir struct ImpRCStack
85cdf0e10cSrcweir {
86cdf0e10cSrcweir // pResource and pClassRes equal NULL: resource was not loaded
87cdf0e10cSrcweir RSHEADER_TYPE * pResource; // pointer to resource
88cdf0e10cSrcweir void * pClassRes; // pointer to class specified init data
89cdf0e10cSrcweir short Flags; // resource status
90cdf0e10cSrcweir void * aResHandle; // Resource-Identifier from InternalResMgr
91cdf0e10cSrcweir const Resource* pResObj; // pointer to Resource object
92cdf0e10cSrcweir sal_uInt32 nId; // ResId used for error message
93cdf0e10cSrcweir ResMgr* pResMgr; // ResMgr for Resource pResObj
94cdf0e10cSrcweir
95cdf0e10cSrcweir void Clear();
96cdf0e10cSrcweir void Init( ResMgr * pMgr, const Resource * pObj, sal_uInt32 nId );
97cdf0e10cSrcweir };
98cdf0e10cSrcweir
99cdf0e10cSrcweir class TOOLS_DLLPUBLIC ResMgr
100cdf0e10cSrcweir {
101cdf0e10cSrcweir private:
102cdf0e10cSrcweir InternalResMgr* pImpRes;
103cdf0e10cSrcweir std::vector< ImpRCStack > aStack; // resource context stack
104cdf0e10cSrcweir int nCurStack;
105cdf0e10cSrcweir ResMgr* pFallbackResMgr; // fallback ResMgr in case the Resource
106cdf0e10cSrcweir // was not contained in this ResMgr
107cdf0e10cSrcweir ResMgr* pOriginalResMgr; // the res mgr that fell back to this
108cdf0e10cSrcweir // stack level
109cdf0e10cSrcweir
110cdf0e10cSrcweir TOOLS_DLLPRIVATE void incStack();
111cdf0e10cSrcweir TOOLS_DLLPRIVATE void decStack();
112cdf0e10cSrcweir
StackTop(sal_uInt32 nOff=0) const113cdf0e10cSrcweir TOOLS_DLLPRIVATE const ImpRCStack * StackTop( sal_uInt32 nOff = 0 ) const
114cdf0e10cSrcweir {
115cdf0e10cSrcweir return (((int)nOff >= nCurStack) ? NULL : &aStack[nCurStack-nOff]);
116cdf0e10cSrcweir }
117cdf0e10cSrcweir TOOLS_DLLPRIVATE void Init( const rtl::OUString& rFileName );
118cdf0e10cSrcweir
119cdf0e10cSrcweir TOOLS_DLLPRIVATE ResMgr( InternalResMgr * pImp );
120cdf0e10cSrcweir
121cdf0e10cSrcweir #ifdef DBG_UTIL
122cdf0e10cSrcweir TOOLS_DLLPRIVATE static void RscError_Impl( const sal_Char* pMessage, ResMgr* pResMgr,
123cdf0e10cSrcweir RESOURCE_TYPE nRT, sal_uInt32 nId,
124cdf0e10cSrcweir std::vector< ImpRCStack >& rResStack, int nDepth );
125cdf0e10cSrcweir #endif
126cdf0e10cSrcweir
127cdf0e10cSrcweir // called from within GetResource() if a resource could not be found
128cdf0e10cSrcweir TOOLS_DLLPRIVATE ResMgr* CreateFallbackResMgr( const ResId& rId, const Resource* pResource );
129cdf0e10cSrcweir // creates a 1k sized buffer set to zero for unfound resources
130cdf0e10cSrcweir // used in case RC_NOTFOUND
131cdf0e10cSrcweir static void* pEmptyBuffer;
132cdf0e10cSrcweir TOOLS_DLLPRIVATE static void* getEmptyBuffer();
133cdf0e10cSrcweir
134cdf0e10cSrcweir // the next two methods are needed to prevent the string hook called
135cdf0e10cSrcweir // with the res mgr mutex locked
136cdf0e10cSrcweir // like GetString, but doesn't call the string hook
137cdf0e10cSrcweir TOOLS_DLLPRIVATE static sal_uInt32 GetStringWithoutHook( UniString& rStr, const sal_uInt8* pStr );
138cdf0e10cSrcweir // like ReadString but doesn't call the string hook
139cdf0e10cSrcweir TOOLS_DLLPRIVATE UniString ReadStringWithoutHook();
140cdf0e10cSrcweir
ImplCreateResMgr(InternalResMgr * pImpl)141cdf0e10cSrcweir static ResMgr* ImplCreateResMgr( InternalResMgr* pImpl ) { return new ResMgr( pImpl ); }
142cdf0e10cSrcweir
143cdf0e10cSrcweir //No copying
144cdf0e10cSrcweir ResMgr(const ResMgr&);
145cdf0e10cSrcweir ResMgr& operator=(const ResMgr&);
146cdf0e10cSrcweir public:
147cdf0e10cSrcweir static void DestroyAllResMgr(); // Wird gerufen, wenn App beendet wird
148cdf0e10cSrcweir
149cdf0e10cSrcweir ~ResMgr();
150cdf0e10cSrcweir
151cdf0e10cSrcweir // Sprachabhaengige Ressource Library
152cdf0e10cSrcweir static const sal_Char* GetLang( LanguageType& eLanguage, sal_uInt16 nPrio = 0 ); //depricated! see "tools/source/rc/resmgr.cxx"
153cdf0e10cSrcweir static ResMgr* SearchCreateResMgr( const sal_Char* pPrefixName,
154cdf0e10cSrcweir com::sun::star::lang::Locale& rLocale );
155cdf0e10cSrcweir static ResMgr* CreateResMgr( const sal_Char* pPrefixName,
156cdf0e10cSrcweir com::sun::star::lang::Locale aLocale = com::sun::star::lang::Locale( rtl::OUString(),
157cdf0e10cSrcweir rtl::OUString(),
158cdf0e10cSrcweir rtl::OUString()));
159cdf0e10cSrcweir
160cdf0e10cSrcweir // Testet ob Resource noch da ist
161cdf0e10cSrcweir void TestStack( const Resource * );
162cdf0e10cSrcweir
163cdf0e10cSrcweir // ist Resource verfuegbar
164cdf0e10cSrcweir sal_Bool IsAvailable( const ResId& rId,
165cdf0e10cSrcweir const Resource* = NULL) const;
166cdf0e10cSrcweir
167cdf0e10cSrcweir // Resource suchen und laden
168cdf0e10cSrcweir sal_Bool GetResource( const ResId& rId, const Resource * = NULL );
169cdf0e10cSrcweir static void * GetResourceSkipHeader( const ResId& rResId, ResMgr ** ppResMgr );
170cdf0e10cSrcweir // Kontext freigeben
171cdf0e10cSrcweir void PopContext( const Resource* = NULL );
172cdf0e10cSrcweir
173cdf0e10cSrcweir // Resourcezeiger erhoehen
174cdf0e10cSrcweir void* Increment( sal_uInt32 nSize );
175cdf0e10cSrcweir
176cdf0e10cSrcweir // Groesse ein Objektes in der Resource
GetObjSize(RSHEADER_TYPE * pHT)177cdf0e10cSrcweir static sal_uInt32 GetObjSize( RSHEADER_TYPE* pHT )
178cdf0e10cSrcweir { return( pHT->GetGlobOff() ); }
179cdf0e10cSrcweir
180cdf0e10cSrcweir // returns a string and its length out of the resource
181cdf0e10cSrcweir static sal_uInt32 GetString( UniString& rStr, const sal_uInt8* pStr );
182cdf0e10cSrcweir // returns a byte string and its length out of the resource
183cdf0e10cSrcweir static sal_uInt32 GetByteString( rtl::OString& rStr, const sal_uInt8* pStr );
184cdf0e10cSrcweir
185cdf0e10cSrcweir // Groesse eines Strings in der Resource
GetStringSize(sal_uInt32 nLen)186cdf0e10cSrcweir static sal_uInt32 GetStringSize( sal_uInt32 nLen )
187cdf0e10cSrcweir { nLen++; return (nLen + nLen%2); }
188cdf0e10cSrcweir static sal_uInt32 GetStringSize( const sal_uInt8* pStr, sal_uInt32& nLen );
189cdf0e10cSrcweir
190cdf0e10cSrcweir // return a int64
191cdf0e10cSrcweir static sal_uInt64 GetUInt64( void* pDatum );
192cdf0e10cSrcweir // Gibt einen long zurueck
193cdf0e10cSrcweir static sal_Int32 GetLong( void * pLong );
194cdf0e10cSrcweir // return a short
195cdf0e10cSrcweir static sal_Int16 GetShort( void * pShort );
196cdf0e10cSrcweir
197cdf0e10cSrcweir // Gibt einen Zeiger auf die Resource zurueck
198cdf0e10cSrcweir void * GetClass();
199cdf0e10cSrcweir
200cdf0e10cSrcweir RSHEADER_TYPE * CreateBlock( const ResId & rId );
201cdf0e10cSrcweir
202cdf0e10cSrcweir // Gibt die verbleibende Groesse zurueck
203cdf0e10cSrcweir sal_uInt32 GetRemainSize();
204cdf0e10cSrcweir
205cdf0e10cSrcweir const rtl::OUString&GetFileName() const;
206cdf0e10cSrcweir
207cdf0e10cSrcweir sal_Int16 ReadShort();
208cdf0e10cSrcweir sal_Int32 ReadLong();
209cdf0e10cSrcweir UniString ReadString();
210cdf0e10cSrcweir rtl::OString ReadByteString();
211cdf0e10cSrcweir
212cdf0e10cSrcweir // generate auto help id for current resource stack
213cdf0e10cSrcweir rtl::OString GetAutoHelpId();
214cdf0e10cSrcweir
215cdf0e10cSrcweir static void SetReadStringHook( ResHookProc pProc );
216cdf0e10cSrcweir static ResHookProc GetReadStringHook();
217cdf0e10cSrcweir static void SetDefaultLocale( const com::sun::star::lang::Locale& rLocale );
218cdf0e10cSrcweir };
219cdf0e10cSrcweir
GetId()220cdf0e10cSrcweir inline sal_uInt32 RSHEADER_TYPE::GetId()
221cdf0e10cSrcweir {
222cdf0e10cSrcweir return (sal_uInt32)ResMgr::GetLong( &nId );
223cdf0e10cSrcweir }
GetRT()224cdf0e10cSrcweir inline RESOURCE_TYPE RSHEADER_TYPE::GetRT()
225cdf0e10cSrcweir {
226cdf0e10cSrcweir return (RESOURCE_TYPE)ResMgr::GetLong( &nRT );
227cdf0e10cSrcweir }
GetGlobOff()228cdf0e10cSrcweir inline sal_uInt32 RSHEADER_TYPE::GetGlobOff()
229cdf0e10cSrcweir {
230cdf0e10cSrcweir return (sal_uInt32)ResMgr::GetLong( &nGlobOff );
231cdf0e10cSrcweir }
GetLocalOff()232cdf0e10cSrcweir inline sal_uInt32 RSHEADER_TYPE::GetLocalOff()
233cdf0e10cSrcweir {
234cdf0e10cSrcweir return (sal_uInt32)ResMgr::GetLong( &nLocalOff );
235cdf0e10cSrcweir }
236cdf0e10cSrcweir
237cdf0e10cSrcweir #endif // _SV_RESMGR_HXX
238