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 _TOOLS_RESMGR_HXX
20 #define _TOOLS_RESMGR_HXX
22 #include "tools/toolsdllapi.h"
23 #include <i18nlangtag/languagetag.hxx>
24 #include <tools/string.hxx>
25 #include <tools/resid.hxx>
32 /// Defines structure used to build resource
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
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
);
51 #define RC_NOTYPE 0x00
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
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
74 void Init( ResMgr
* pMgr
, const Resource
* pObj
, sal_uInt32 nId
);
77 class TOOLS_DLLPUBLIC ResMgr
80 InternalResMgr
* pImpRes
;
81 std::vector
< ImpRCStack
> aStack
; ///< resource context stack
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
88 TOOLS_DLLPRIVATE
void incStack();
89 TOOLS_DLLPRIVATE
void decStack();
91 TOOLS_DLLPRIVATE
const ImpRCStack
* StackTop( sal_uInt32 nOff
= 0 ) const
93 return (((int)nOff
>= nCurStack
) ? NULL
: &aStack
[nCurStack
-nOff
]);
95 TOOLS_DLLPRIVATE
void Init( const OUString
& rFileName
);
97 TOOLS_DLLPRIVATE
ResMgr( InternalResMgr
* pImp
);
100 TOOLS_DLLPRIVATE
static void RscError_Impl( const sal_Char
* pMessage
,
104 std::vector
< ImpRCStack
>& rResStack
,
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();
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();
124 static ResMgr
* ImplCreateResMgr( InternalResMgr
* pImpl
) { return new ResMgr( pImpl
); }
127 ResMgr(const ResMgr
&);
128 ResMgr
& operator=(const ResMgr
&);
131 static void DestroyAllResMgr(); ///< Called upon app shutdown
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
) );
142 /// Test whether resource still exists
143 void TestStack( const Resource
* );
145 /// Check whether resource is available
146 sal_Bool
IsAvailable( const ResId
& rId
,
147 const Resource
* = NULL
) const;
149 /// Search and load resource, given its ID
150 sal_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
);
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
);
173 static sal_uInt64
GetUInt64( void* pDatum
);
175 static sal_Int32
GetLong( void * pLong
);
177 static sal_Int16
GetShort( void * pShort
);
179 /// Return a pointer to the resource
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 /// Generate auto help ID for current resource stack
194 OString
GetAutoHelpId();
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
);
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */