1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
32 /// Defines structure used to build resource
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
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
);
54 GLOBAL
= 0x01, // Global resource
62 template<> struct typed_flags
<RCFlags
> : is_typed_flags
<RCFlags
, 0x1f> {};
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
80 void Init( ResMgr
* pMgr
, const Resource
* pObj
, sal_uInt32 nId
);
83 class TOOLS_DLLPUBLIC ResMgr
86 InternalResMgr
* pImpRes
;
87 std::vector
< ImpRCStack
> aStack
; ///< resource context stack
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
94 TOOLS_DLLPRIVATE
void incStack();
95 TOOLS_DLLPRIVATE
void decStack();
97 TOOLS_DLLPRIVATE
const ImpRCStack
* StackTop( sal_uInt32 nOff
= 0 ) const
99 return (((int)nOff
>= nCurStack
) ? NULL
: &aStack
[nCurStack
-nOff
]);
101 TOOLS_DLLPRIVATE
void Init( const OUString
& rFileName
);
103 TOOLS_DLLPRIVATE
ResMgr( InternalResMgr
* pImp
);
106 TOOLS_DLLPRIVATE
static void RscError_Impl( const sal_Char
* pMessage
,
110 std::vector
< ImpRCStack
>& rResStack
,
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();
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();
130 static ResMgr
* ImplCreateResMgr( InternalResMgr
* pImpl
) { return new ResMgr( pImpl
); }
132 ResMgr(const ResMgr
&) SAL_DELETED_FUNCTION
;
133 ResMgr
& operator=(const ResMgr
&) SAL_DELETED_FUNCTION
;
136 static void DestroyAllResMgr(); ///< Called upon app shutdown
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
) );
147 /// Test whether resource still exists
148 void TestStack( const Resource
* );
151 /// Check whether resource is available
152 bool IsAvailable( const ResId
& rId
,
153 const Resource
* = NULL
) const;
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
);
161 /// Increment resource pointer
162 void* Increment( sal_uInt32 nSize
);
164 /// Size of an object within the resource
165 static sal_uInt32
GetObjSize( RSHEADER_TYPE
* pHT
)
166 { return( pHT
->GetGlobOff() ); }
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
);
173 /// Return the size of a string in the resource
174 static sal_uInt32
GetStringSize( sal_uInt32 nLen
)
175 { nLen
++; return (nLen
+ nLen
%2); }
176 static sal_uInt32
GetStringSize( const sal_uInt8
* pStr
, sal_uInt32
& nLen
);
179 static sal_uInt64
GetUInt64( void* pDatum
);
181 static sal_Int32
GetLong( void * pLong
);
183 static sal_Int16
GetShort( void * pShort
);
185 /// Return a pointer to the resource
188 RSHEADER_TYPE
* CreateBlock( const ResId
& rId
);
190 sal_uInt32
GetRemainSize();
192 const OUString
& GetFileName() const;
194 sal_Int16
ReadShort();
195 sal_Int32
ReadLong();
196 OUString
ReadString();
197 OString
ReadByteString();
199 /// Generate auto help ID for current resource stack
200 OString
GetAutoHelpId();
202 static void SetReadStringHook( ResHookProc pProc
);
203 static ResHookProc
GetReadStringHook();
204 static void SetDefaultLocale( const LanguageTag
& rLocale
);
207 inline sal_uInt32
RSHEADER_TYPE::GetId()
209 return (sal_uInt32
)ResMgr::GetLong( &nId
);
212 inline RESOURCE_TYPE
RSHEADER_TYPE::GetRT()
214 return (RESOURCE_TYPE
)ResMgr::GetLong( &nRT
);
217 inline sal_uInt32
RSHEADER_TYPE::GetGlobOff()
219 return (sal_uInt32
)ResMgr::GetLong( &nGlobOff
);
222 inline sal_uInt32
RSHEADER_TYPE::GetLocalOff()
224 return (sal_uInt32
)ResMgr::GetLong( &nLocalOff
);
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */