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