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