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 : #ifndef _TOOLS_RESMGR_HXX
20 : #define _TOOLS_RESMGR_HXX
21 :
22 : #include "tools/toolsdllapi.h"
23 : #include <i18nlangtag/languagetag.hxx>
24 : #include <tools/string.hxx>
25 : #include <tools/resid.hxx>
26 :
27 : #include <vector>
28 :
29 : class SvStream;
30 : class InternalResMgr;
31 :
32 : /// Defines structure used to build resource
33 : struct RSHEADER_TYPE
34 : {
35 : private:
36 : sal_uInt32 nId; ///< Identifier of resource
37 : RESOURCE_TYPE nRT; ///< Resource type
38 : sal_uInt32 nGlobOff; ///< Global offset
39 : sal_uInt32 nLocalOff; ///< Local offset
40 :
41 : public:
42 : inline sal_uInt32 GetId(); ///< Identifier of resource
43 : inline RESOURCE_TYPE GetRT(); ///< Resource type
44 : inline sal_uInt32 GetGlobOff(); ///< Global offset
45 : inline sal_uInt32 GetLocalOff(); ///< Local offset
46 : };
47 :
48 : typedef OUString (*ResHookProc)( const OUString& rStr );
49 :
50 : // Initialization
51 : #define RC_NOTYPE 0x00
52 : // Global resource
53 : #define RC_GLOBAL 0x01
54 : #define RC_AUTORELEASE 0x02
55 : #define RC_NOTFOUND 0x04
56 : #define RC_FALLBACK_DOWN 0x08
57 : #define RC_FALLBACK_UP 0x10
58 :
59 : class Resource;
60 : class ResMgr;
61 :
62 : struct ImpRCStack
63 : {
64 : // pResource and pClassRes equal NULL: resource was not loaded
65 : RSHEADER_TYPE * pResource; ///< pointer to resource
66 : void * pClassRes; ///< pointer to class specified init data
67 : short Flags; ///< resource status
68 : void * aResHandle; ///< Resource-Identifier from InternalResMgr
69 : const Resource* pResObj; ///< pointer to Resource object
70 : sal_uInt32 nId; ///< ResId used for error message
71 : ResMgr* pResMgr; ///< ResMgr for Resource pResObj
72 :
73 : void Clear();
74 : void Init( ResMgr * pMgr, const Resource * pObj, sal_uInt32 nId );
75 : };
76 :
77 : class TOOLS_DLLPUBLIC ResMgr
78 : {
79 : private:
80 : InternalResMgr* pImpRes;
81 : std::vector< ImpRCStack > aStack; ///< resource context stack
82 : int nCurStack;
83 : ResMgr* pFallbackResMgr; ///< fallback ResMgr in case the Resource
84 : ///< was not contained in this ResMgr
85 : ResMgr* pOriginalResMgr; ///< the res mgr that fell back to this
86 : ///< stack level
87 :
88 : TOOLS_DLLPRIVATE void incStack();
89 : TOOLS_DLLPRIVATE void decStack();
90 :
91 4014 : TOOLS_DLLPRIVATE const ImpRCStack * StackTop( sal_uInt32 nOff = 0 ) const
92 : {
93 4014 : return (((int)nOff >= nCurStack) ? NULL : &aStack[nCurStack-nOff]);
94 : }
95 : TOOLS_DLLPRIVATE void Init( const OUString& rFileName );
96 :
97 : TOOLS_DLLPRIVATE ResMgr( InternalResMgr * pImp );
98 :
99 : #ifdef DBG_UTIL
100 : TOOLS_DLLPRIVATE static void RscError_Impl( const sal_Char* pMessage,
101 : ResMgr* pResMgr,
102 : RESOURCE_TYPE nRT,
103 : sal_uInt32 nId,
104 : std::vector< ImpRCStack >& rResStack,
105 : int nDepth );
106 : #endif
107 :
108 : // called from within GetResource() if a resource could not be found
109 : TOOLS_DLLPRIVATE ResMgr* CreateFallbackResMgr( const ResId& rId,
110 : const Resource* pResource );
111 : // creates a 1k sized buffer set to zero for unfound resources
112 : // used in case RC_NOTFOUND
113 : static void* pEmptyBuffer;
114 : TOOLS_DLLPRIVATE static void* getEmptyBuffer();
115 :
116 : // the next two methods are needed to prevent the string hook called
117 : // with the res mgr mutex locked
118 : // like GetString, but doesn't call the string hook
119 : TOOLS_DLLPRIVATE static sal_uInt32 GetStringWithoutHook( OUString& rStr,
120 : const sal_uInt8* pStr );
121 : // like ReadString but doesn't call the string hook
122 : TOOLS_DLLPRIVATE OUString ReadStringWithoutHook();
123 :
124 : static ResMgr* ImplCreateResMgr( InternalResMgr* pImpl ) { return new ResMgr( pImpl ); }
125 :
126 : // no copying
127 : ResMgr(const ResMgr&);
128 : ResMgr& operator=(const ResMgr&);
129 :
130 : public:
131 : static void DestroyAllResMgr(); ///< Called upon app shutdown
132 :
133 : ~ResMgr();
134 :
135 : /// Language-dependent resource library
136 : static const sal_Char* GetLang( LanguageType& eLanguage, sal_uInt16 nPrio = 0 ); ///< @deprecated see "tools/source/rc/resmgr.cxx"
137 : static ResMgr* SearchCreateResMgr( const sal_Char* pPrefixName,
138 : LanguageTag& rLocale );
139 : static ResMgr* CreateResMgr( const sal_Char* pPrefixName,
140 : LanguageTag aLocale = LanguageTag( LANGUAGE_SYSTEM) );
141 :
142 : /// Test whether resource still exists
143 : void TestStack( const Resource * );
144 :
145 : /// Check whether resource is available
146 : bool IsAvailable( const ResId& rId,
147 : const Resource* = NULL) const;
148 :
149 : /// Search and load resource, given its ID
150 : bool GetResource( const ResId& rId, const Resource * = NULL );
151 : static void * GetResourceSkipHeader( const ResId& rResId, ResMgr ** ppResMgr );
152 : /// Free resource context
153 : void PopContext( const Resource* = NULL );
154 :
155 : /// Increment resource pointer
156 : void* Increment( sal_uInt32 nSize );
157 :
158 : /// Size of an object within the resource
159 29670 : static sal_uInt32 GetObjSize( RSHEADER_TYPE* pHT )
160 29670 : { return( pHT->GetGlobOff() ); }
161 :
162 : /// Return a string and its length out of the resource
163 : static sal_uInt32 GetString( OUString& rStr, const sal_uInt8* pStr );
164 : /// Return a byte string and its length out of the resource
165 : static sal_uInt32 GetByteString( OString& rStr, const sal_uInt8* pStr );
166 :
167 : /// Return the size of a string in the resource
168 125835 : static sal_uInt32 GetStringSize( sal_uInt32 nLen )
169 125835 : { nLen++; return (nLen + nLen%2); }
170 : static sal_uInt32 GetStringSize( const sal_uInt8* pStr, sal_uInt32& nLen );
171 :
172 : /// Return a int64
173 : static sal_uInt64 GetUInt64( void* pDatum );
174 : /// Return a long
175 : static sal_Int32 GetLong( void * pLong );
176 : /// Return a short
177 : static sal_Int16 GetShort( void * pShort );
178 :
179 : /// Return a pointer to the resource
180 : void * GetClass();
181 :
182 : RSHEADER_TYPE * CreateBlock( const ResId & rId );
183 :
184 : sal_uInt32 GetRemainSize();
185 :
186 : const OUString& GetFileName() const;
187 :
188 : sal_Int16 ReadShort();
189 : sal_Int32 ReadLong();
190 : OUString ReadString();
191 : OString ReadByteString();
192 :
193 : /// Generate auto help ID for current resource stack
194 : OString GetAutoHelpId();
195 :
196 : static void SetReadStringHook( ResHookProc pProc );
197 : static ResHookProc GetReadStringHook();
198 : static void SetDefaultLocale( const LanguageTag& rLocale );
199 : };
200 :
201 50528249 : inline sal_uInt32 RSHEADER_TYPE::GetId()
202 : {
203 50528249 : return (sal_uInt32)ResMgr::GetLong( &nId );
204 : }
205 :
206 50681033 : inline RESOURCE_TYPE RSHEADER_TYPE::GetRT()
207 : {
208 50681033 : return (RESOURCE_TYPE)ResMgr::GetLong( &nRT );
209 : }
210 :
211 52009664 : inline sal_uInt32 RSHEADER_TYPE::GetGlobOff()
212 : {
213 52009664 : return (sal_uInt32)ResMgr::GetLong( &nGlobOff );
214 : }
215 :
216 1665347 : inline sal_uInt32 RSHEADER_TYPE::GetLocalOff()
217 : {
218 1665347 : return (sal_uInt32)ResMgr::GetLong( &nLocalOff );
219 : }
220 :
221 : #endif
222 :
223 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|