build fix
[LibreOffice.git] / include / tools / resmgr.hxx
blob0679818a0b8a01882633c3d1ebd96062fad4c309
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_TOOLS_RESMGR_HXX
20 #define INCLUDED_TOOLS_RESMGR_HXX
22 #include <tools/toolsdllapi.h>
23 #include <i18nlangtag/languagetag.hxx>
24 #include <tools/resid.hxx>
25 #include <o3tl/typed_flags_set.hxx>
27 #include <vector>
29 class SvStream;
30 class InternalResMgr;
32 /// Defines structure used to build resource
33 struct RSHEADER_TYPE
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
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
48 typedef OUString (*ResHookProc)( const OUString& rStr );
50 // Initialization
51 enum class RCFlags
53 NONE = 0x00,
54 GLOBAL = 0x01, // Global resource
55 AUTORELEASE = 0x02,
56 NOTFOUND = 0x04,
57 FALLBACK_DOWN = 0x08,
58 FALLBACK_UP = 0x10,
60 namespace o3tl
62 template<> struct typed_flags<RCFlags> : is_typed_flags<RCFlags, 0x1f> {};
65 class Resource;
66 class ResMgr;
68 struct ImpRCStack
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
79 void Clear();
80 void Init( ResMgr * pMgr, const Resource * pObj, sal_uInt32 nId );
83 class TOOLS_DLLPUBLIC ResMgr
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
94 TOOLS_DLLPRIVATE void incStack();
95 TOOLS_DLLPRIVATE void decStack();
97 TOOLS_DLLPRIVATE void Init( const OUString& rFileName );
99 TOOLS_DLLPRIVATE ResMgr( InternalResMgr * pImp );
101 #ifdef DBG_UTIL
102 TOOLS_DLLPRIVATE static void RscError_Impl( const sal_Char* pMessage,
103 ResMgr* pResMgr,
104 RESOURCE_TYPE nRT,
105 sal_uInt32 nId,
106 std::vector< ImpRCStack >& rResStack,
107 int nDepth );
108 #endif
110 // called from within GetResource() if a resource could not be found
111 TOOLS_DLLPRIVATE ResMgr* CreateFallbackResMgr( const ResId& rId,
112 const Resource* pResource );
113 // creates a 1k sized buffer set to zero for unfound resources
114 // used in case RC_NOTFOUND
115 static void* pEmptyBuffer;
116 TOOLS_DLLPRIVATE static void* getEmptyBuffer();
118 // the next two methods are needed to prevent the string hook called
119 // with the res mgr mutex locked
120 // like GetString, but doesn't call the string hook
121 TOOLS_DLLPRIVATE static sal_uInt32 GetStringWithoutHook( OUString& rStr,
122 const sal_uInt8* pStr );
123 // like ReadString but doesn't call the string hook
124 TOOLS_DLLPRIVATE OUString ReadStringWithoutHook();
126 ResMgr(const ResMgr&) = delete;
127 ResMgr& operator=(const ResMgr&) = delete;
129 public:
130 static void DestroyAllResMgr(); ///< Called upon app shutdown
132 ~ResMgr();
134 /// Language-dependent resource library
135 static ResMgr* SearchCreateResMgr( const sal_Char* pPrefixName,
136 LanguageTag& rLocale );
137 static ResMgr* CreateResMgr( const sal_Char* pPrefixName,
138 const LanguageTag& aLocale = LanguageTag( LANGUAGE_SYSTEM) );
140 #ifdef DBG_UTIL
141 /// Test whether resource still exists
142 void TestStack();
143 #endif
145 /// Check whether resource is available
146 bool IsAvailable( const ResId& rId,
147 const Resource* = nullptr) const;
149 /// Search and load resource, given its ID
150 bool GetResource( const ResId& rId, const Resource * = nullptr );
151 static void * GetResourceSkipHeader( const ResId& rResId, ResMgr ** ppResMgr );
152 /// Free resource context
153 void PopContext( const Resource* = nullptr );
155 /// Increment resource pointer
156 void* Increment( sal_uInt32 nSize );
158 /// Size of an object within the resource
159 static sal_uInt32 GetObjSize( RSHEADER_TYPE* pHT )
160 { return( pHT->GetGlobOff() ); }
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 );
167 /// Return the size of a string in the resource
168 static sal_uInt32 GetStringSize( sal_uInt32 nLen )
169 { nLen++; return (nLen + nLen%2); }
170 static sal_uInt32 GetStringSize( const sal_uInt8* pStr, sal_uInt32& nLen );
172 /// Return a int64
173 static sal_uInt64 GetUInt64( void const * pDatum );
174 /// Return a long
175 static sal_Int32 GetLong( void const * pLong );
176 /// Return a short
177 static sal_Int16 GetShort( void const * pShort );
179 /// Return a pointer to the resource
180 void * GetClass();
182 RSHEADER_TYPE * CreateBlock( const ResId & rId );
184 sal_uInt32 GetRemainSize();
186 const OUString& GetFileName() const;
188 sal_Int16 ReadShort();
189 sal_Int32 ReadLong();
190 OUString ReadString();
191 OString ReadByteString();
193 /// The locale of this ResMgr.
194 const LanguageTag& GetLocale() const;
196 static void SetReadStringHook( ResHookProc pProc );
197 static ResHookProc GetReadStringHook();
198 static void SetDefaultLocale( const LanguageTag& rLocale );
201 inline sal_uInt32 RSHEADER_TYPE::GetId()
203 return (sal_uInt32)ResMgr::GetLong( &nId );
206 inline RESOURCE_TYPE RSHEADER_TYPE::GetRT()
208 return (RESOURCE_TYPE)ResMgr::GetLong( &nRT );
211 inline sal_uInt32 RSHEADER_TYPE::GetGlobOff()
213 return (sal_uInt32)ResMgr::GetLong( &nGlobOff );
216 inline sal_uInt32 RSHEADER_TYPE::GetLocalOff()
218 return (sal_uInt32)ResMgr::GetLong( &nLocalOff );
221 #endif
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */