1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 #ifndef _TOOLS_RESMGR_HXX 28 #define _TOOLS_RESMGR_HXX 29 30 #include "tools/toolsdllapi.h" 31 #include <i18npool/lang.h> 32 #include <tools/string.hxx> 33 #include <tools/ref.hxx> 34 #include <tools/resid.hxx> 35 #include <com/sun/star/lang/Locale.hpp> 36 37 #define CREATEVERSIONRESMGR_NAME( Name ) #Name 38 #define CREATEVERSIONRESMGR( Name ) ResMgr::CreateResMgr( CREATEVERSIONRESMGR_NAME( Name ) ) 39 40 #define LOCALE_MAX_FALLBACK 6 41 42 #include <vector> 43 44 class SvStream; 45 class InternalResMgr; 46 47 // ----------------- 48 // - RSHEADER_TYPE - 49 // ----------------- 50 51 // Definition der Struktur, aus denen die Resource aufgebaut ist 52 struct RSHEADER_TYPE 53 { 54 private: 55 sal_uInt32 nId; // Identifier der Resource 56 RESOURCE_TYPE nRT; // Resource Typ 57 sal_uInt32 nGlobOff; // Globaler Offset 58 sal_uInt32 nLocalOff; // Lokaler Offset 59 60 public: 61 inline sal_uInt32 GetId(); // Identifier der Resource 62 inline RESOURCE_TYPE GetRT(); // Resource Typ 63 inline sal_uInt32 GetGlobOff(); // Globaler Offset 64 inline sal_uInt32 GetLocalOff(); // Lokaler Offset 65 }; 66 67 // ---------- 68 // - ResMgr - 69 // ---------- 70 71 typedef void (*ResHookProc)( UniString& rStr ); 72 73 // ---------- 74 // - ResMgr - 75 // ---------- 76 77 // Initialisierung 78 #define RC_NOTYPE 0x00 79 // Globale Resource 80 #define RC_GLOBAL 0x01 81 #define RC_AUTORELEASE 0x02 82 #define RC_NOTFOUND 0x04 83 #define RC_FALLBACK_DOWN 0x08 84 #define RC_FALLBACK_UP 0x10 85 86 class Resource; 87 class ResMgr; 88 struct ImpRCStack 89 { 90 // pResource and pClassRes equal NULL: resource was not loaded 91 RSHEADER_TYPE * pResource; // pointer to resource 92 void * pClassRes; // pointer to class specified init data 93 short Flags; // resource status 94 void * aResHandle; // Resource-Identifier from InternalResMgr 95 const Resource* pResObj; // pointer to Resource object 96 sal_uInt32 nId; // ResId used for error message 97 ResMgr* pResMgr; // ResMgr for Resource pResObj 98 99 void Clear(); 100 void Init( ResMgr * pMgr, const Resource * pObj, sal_uInt32 nId ); 101 }; 102 103 class TOOLS_DLLPUBLIC ResMgr 104 { 105 private: 106 InternalResMgr* pImpRes; 107 std::vector< ImpRCStack > aStack; // resource context stack 108 int nCurStack; 109 ResMgr* pFallbackResMgr; // fallback ResMgr in case the Resource 110 // was not contained in this ResMgr 111 ResMgr* pOriginalResMgr; // the res mgr that fell back to this 112 // stack level 113 114 TOOLS_DLLPRIVATE void incStack(); 115 TOOLS_DLLPRIVATE void decStack(); 116 117 TOOLS_DLLPRIVATE const ImpRCStack * StackTop( sal_uInt32 nOff = 0 ) const 118 { 119 return (((int)nOff >= nCurStack) ? NULL : &aStack[nCurStack-nOff]); 120 } 121 TOOLS_DLLPRIVATE void Init( const rtl::OUString& rFileName ); 122 123 TOOLS_DLLPRIVATE ResMgr( InternalResMgr * pImp ); 124 125 #ifdef DBG_UTIL 126 TOOLS_DLLPRIVATE static void RscError_Impl( const sal_Char* pMessage, ResMgr* pResMgr, 127 RESOURCE_TYPE nRT, sal_uInt32 nId, 128 std::vector< ImpRCStack >& rResStack, int nDepth ); 129 #endif 130 131 // called from within GetResource() if a resource could not be found 132 TOOLS_DLLPRIVATE ResMgr* CreateFallbackResMgr( const ResId& rId, const Resource* pResource ); 133 // creates a 1k sized buffer set to zero for unfound resources 134 // used in case RC_NOTFOUND 135 static void* pEmptyBuffer; 136 TOOLS_DLLPRIVATE static void* getEmptyBuffer(); 137 138 // the next two methods are needed to prevent the string hook called 139 // with the res mgr mutex locked 140 // like GetString, but doesn't call the string hook 141 TOOLS_DLLPRIVATE static sal_uInt32 GetStringWithoutHook( UniString& rStr, const sal_uInt8* pStr ); 142 // like ReadString but doesn't call the string hook 143 TOOLS_DLLPRIVATE UniString ReadStringWithoutHook(); 144 145 static ResMgr* ImplCreateResMgr( InternalResMgr* pImpl ) { return new ResMgr( pImpl ); } 146 147 //No copying 148 ResMgr(const ResMgr&); 149 ResMgr& operator=(const ResMgr&); 150 public: 151 static void DestroyAllResMgr(); // Wird gerufen, wenn App beendet wird 152 153 ~ResMgr(); 154 155 // Sprachabhaengige Ressource Library 156 static const sal_Char* GetLang( LanguageType& eLanguage, sal_uInt16 nPrio = 0 ); //depricated! see "tools/source/rc/resmgr.cxx" 157 static ResMgr* SearchCreateResMgr( const sal_Char* pPrefixName, 158 com::sun::star::lang::Locale& rLocale ); 159 static ResMgr* CreateResMgr( const sal_Char* pPrefixName, 160 com::sun::star::lang::Locale aLocale = com::sun::star::lang::Locale( rtl::OUString(), 161 rtl::OUString(), 162 rtl::OUString())); 163 164 // Testet ob Resource noch da ist 165 void TestStack( const Resource * ); 166 167 // ist Resource verfuegbar 168 sal_Bool IsAvailable( const ResId& rId, 169 const Resource* = NULL) const; 170 171 // Resource suchen und laden 172 sal_Bool GetResource( const ResId& rId, const Resource * = NULL ); 173 static void * GetResourceSkipHeader( const ResId& rResId, ResMgr ** ppResMgr ); 174 // Kontext freigeben 175 void PopContext( const Resource* = NULL ); 176 177 // Resourcezeiger erhoehen 178 void* Increment( sal_uInt32 nSize ); 179 180 // Groesse ein Objektes in der Resource 181 static sal_uInt32 GetObjSize( RSHEADER_TYPE* pHT ) 182 { return( pHT->GetGlobOff() ); } 183 184 // returns a string and its length out of the resource 185 static sal_uInt32 GetString( UniString& rStr, const sal_uInt8* pStr ); 186 // returns a byte string and its length out of the resource 187 static sal_uInt32 GetByteString( rtl::OString& rStr, const sal_uInt8* pStr ); 188 189 // Groesse eines Strings in der Resource 190 static sal_uInt32 GetStringSize( sal_uInt32 nLen ) 191 { nLen++; return (nLen + nLen%2); } 192 static sal_uInt32 GetStringSize( const sal_uInt8* pStr, sal_uInt32& nLen ); 193 194 // return a int64 195 static sal_uInt64 GetUInt64( void* pDatum ); 196 // Gibt einen long zurueck 197 static sal_Int32 GetLong( void * pLong ); 198 // return a short 199 static sal_Int16 GetShort( void * pShort ); 200 201 // Gibt einen Zeiger auf die Resource zurueck 202 void * GetClass(); 203 204 RSHEADER_TYPE * CreateBlock( const ResId & rId ); 205 206 // Gibt die verbleibende Groesse zurueck 207 sal_uInt32 GetRemainSize(); 208 209 const rtl::OUString&GetFileName() const; 210 211 sal_Int16 ReadShort(); 212 sal_Int32 ReadLong(); 213 UniString ReadString(); 214 rtl::OString ReadByteString(); 215 216 // generate auto help id for current resource stack 217 rtl::OString GetAutoHelpId(); 218 219 static void SetReadStringHook( ResHookProc pProc ); 220 static ResHookProc GetReadStringHook(); 221 static void SetDefaultLocale( const com::sun::star::lang::Locale& rLocale ); 222 }; 223 224 inline sal_uInt32 RSHEADER_TYPE::GetId() 225 { 226 return (sal_uInt32)ResMgr::GetLong( &nId ); 227 } 228 inline RESOURCE_TYPE RSHEADER_TYPE::GetRT() 229 { 230 return (RESOURCE_TYPE)ResMgr::GetLong( &nRT ); 231 } 232 inline sal_uInt32 RSHEADER_TYPE::GetGlobOff() 233 { 234 return (sal_uInt32)ResMgr::GetLong( &nGlobOff ); 235 } 236 inline sal_uInt32 RSHEADER_TYPE::GetLocalOff() 237 { 238 return (sal_uInt32)ResMgr::GetLong( &nLocalOff ); 239 } 240 241 #endif // _SV_RESMGR_HXX 242