UTResource.h

Go to the documentation of this file.
00001 //==================================================================================================
00002 // Copyright (C) 2010  Brian Tietz    sdbtietz at yahoo dot com
00003 //
00004 // This program is free software; you can redistribute it and/or modify it under the terms of the
00005 // GNU General Public License as published by the Free Software Foundation, version 2.0 of the
00006 // License.
00007 //
00008 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
00009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00010 // General Public License for more details.
00011 //
00012 // You should have received a copy of the GNU General Public License along with this program; if
00013 // not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00014 // 02110-1301, USA.
00015 //
00016 // For commercial software, the copyright holder (Brian Tietz, email sdbtietz at yahoo dot com)
00017 // reserves the right and is willing to waive the proprietary source code disclosure aspects of that
00018 // license as applied to the UT library in exchange for either substantial contributions to the
00019 // development of the UT library or other forms of compensation.  Any such waiver must be
00020 // established in writing between the copyright holder and the commercial entity obtaining such a
00021 // waiver.
00022 //==================================================================================================
00023 
00024 
00025 #ifndef _UT_RESOURCE_H_
00026 #define _UT_RESOURCE_H_
00027 
00028 
00029 //==================================================================================================
00030 //=== Project headers
00031 //==================================================================================================
00032 #include "UTVersionInfo.h"
00033 #include "UTLists.h"
00034 #include "UTString.h"
00035 #include "UTMutex.h"
00036 
00037 
00038 //==================================================================================================
00039 //=== Forward name declarations
00040 //==================================================================================================
00041 class Buffer_t;
00042 class ResourcePlatformInfo_t;
00043 class ModuleLocaleStrings_t;
00044 class ModuleLocales_t;
00045 class InputStream_t;
00046 
00047 
00048 //==================================================================================================
00049 class UT_EXPORT Resource_t
00050 //==================================================================================================
00052 {
00053     //----------------------------------------------------------------------------------------------
00054     public:
00055     //----------------------------------------------------------------------------------------------
00056                             Resource_t(stringliteral* library_name = NULL);
00066 
00067                             ~Resource_t();
00069 
00070     inline const String_t&  ModuleName() const;
00075 
00076     int                     Load( int resource_id, out byte* contents, int max_size ) const;
00083 
00084     void                    Load( int resource_id, out Buffer_t* contents ) const;
00088 
00089     void                    LoadText( int resource_id, out String_t* string ) const;
00094 
00095     void        SetLocale( stringliteral* locale, bool global );
00160 
00161     void        GetLocalizedString( int string_id, out String_t* string ) const;
00165 
00166     bool        IsSystemDefaultLocaleSupported(bool support_language_only = true) const;
00173 
00174     int         CountSupportedLocales() const;
00177 
00178     void        GetSupportedLocale( int index, out String_t* string ) const;
00181 
00182     // \cond DOXYGEN_DOCUMENT_NEVER
00183     static void InitSystemDefaultLocale();
00184                     // Called once at startup by the UT library to set up the system default locale.
00185     static void TerminateResourceSupport();
00186                     // Called once at exit by the UT library to tear down resource support.
00187     static bool IsValidLocale(stringliteral* locale);
00188     // \endcond
00189 
00190     //----------------------------------------------------------------------------------------------
00191     private:
00192     //----------------------------------------------------------------------------------------------
00193     void                FindBestLocale(stringliteral* locale);
00194     static void         PlatformGetVersionInfo( stringliteral* module,
00195                                                 out InternalVersionInfo_t* version_info );
00196     ModuleLocales_t*    AddModuleLocales();
00197 
00198     //----------------------------------------------------------------------------------------------
00199     private:
00200     //----------------------------------------------------------------------------------------------
00201     String_t                                                m_module_name;
00202     mutable ResourcePlatformInfo_t*                         m_platform_info;
00203     mutable ModuleLocaleStrings_t*                          m_current_locale;
00204 
00205     static bool                                             ms_resource_system_initialized;
00206     static int32                                            ms_num_resource_objects;
00207 
00208     static Mutex_t                                          ms_resource_mutex;
00209     static String_t                                         ms_system_default_locale;
00210     static String_t                                         ms_global_locale;
00211     static OwnedBTree_t< ModuleLocales_t, stringliteral* >  ms_module_locales;
00212 
00213     friend class ResourcePlatformInfo_t;
00214     friend class VersionInfo_t;
00215     friend class ModuleLocales_t;
00216     friend class ModuleLocaleStrings_t;
00217     friend class ModuleResources_t;
00218 };
00219 
00220 
00221 // \cond DOXYGEN_DOCUMENT_NEVER
00222 
00223 
00224 //==================================================================================================
00225 class ModuleLocales_t
00226 //==================================================================================================
00227 : public BTreeNode_t< ModuleLocales_t, stringliteral* >
00228 {
00229     //----------------------------------------------------------------------------------------------
00230     public:
00231     //----------------------------------------------------------------------------------------------
00232                             ModuleLocales_t( const String_t& module_name, Resource_t* load_from );
00233     ModuleLocaleStrings_t*  FindBestLocale( stringliteral* locale,
00234                                             const String_t& global_locale,
00235                                             const String_t& system_default_locale,
00236                                             Resource_t* load_from );
00237 
00238     inline int              Compare(stringliteral* other) const;
00239     inline                  operator stringliteral*() const;
00240 
00241     //----------------------------------------------------------------------------------------------
00242     private:
00243     //----------------------------------------------------------------------------------------------
00244     String_t                            m_module_name;
00245     OwnedList_t<ModuleLocaleStrings_t>  m_locales;
00246     int                                 m_num_strings_per_locale;
00247 
00248     friend class Resource_t;
00249 };
00250 
00251 
00252 //==================================================================================================
00253 class ModuleLocaleStrings_t
00254 //==================================================================================================
00255 {
00256     //----------------------------------------------------------------------------------------------
00257     public:
00258     //----------------------------------------------------------------------------------------------
00259     inline                  ModuleLocaleStrings_t( const utf8* name, int resource_id );
00260     inline                  ~ModuleLocaleStrings_t();
00261 
00262     inline String_t*        AllocateStrings(int num_strings);
00263 
00264     inline const String_t&  Name() const;
00265     inline int              ResourceID() const;
00266     inline bool             IsPopulated() const;
00267     void                    Populate( const Resource_t* load_from, int num_strings );
00268 
00269     inline void             GetString( int string_id, out String_t* string ) const;
00270 
00271     //----------------------------------------------------------------------------------------------
00272     private:
00273     //----------------------------------------------------------------------------------------------
00274     String_t                m_name;
00275     int                     m_resource_id;
00276     volatile bool           m_is_populated;
00277     String_t*               m_strings;
00278     int                     m_num_strings;
00279 };
00280 
00281 
00282 
00283 
00284 //==================================================================================================
00285 //==================================================================================================
00286 //===
00287 //=== Inline function implementations
00288 //===
00289 //==================================================================================================
00290 //==================================================================================================
00291 inline const String_t&
00292 Resource_t::ModuleName() const
00293 {
00294     return m_module_name;
00295 }
00296 
00297 
00298 inline int
00299 ModuleLocales_t::Compare(stringliteral* other) const
00300 {
00301     return m_module_name.Compare(other);
00302 }
00303 
00304 
00305 inline
00306 ModuleLocales_t::operator stringliteral*() const
00307 {
00308     return m_module_name.ConstChars();
00309 }
00310 
00311 
00312 inline
00313 ModuleLocaleStrings_t::ModuleLocaleStrings_t( const utf8* name, int resource_id )
00314 {
00315     m_name = name;
00316     m_resource_id = resource_id;
00317     m_is_populated = false;
00318     m_strings = NULL;
00319     m_num_strings = 0;
00320 }
00321 
00322 
00323 inline
00324 ModuleLocaleStrings_t::~ModuleLocaleStrings_t()
00325 {
00326     if(m_strings)
00327         delete[] m_strings;
00328 }
00329 
00330 
00331 inline String_t*
00332 ModuleLocaleStrings_t::AllocateStrings(int num_strings)
00333 {
00334     rel_assert(m_num_strings == 0 && !m_strings);
00335     m_num_strings = num_strings;
00336     m_strings = new String_t[num_strings];
00337     return m_strings;
00338 }
00339 
00340 
00341 inline const String_t&
00342 ModuleLocaleStrings_t::Name() const
00343 {
00344     return m_name;
00345 }
00346 
00347 
00348 inline int
00349 ModuleLocaleStrings_t::ResourceID() const
00350 {
00351     return m_resource_id;
00352 }
00353 
00354 
00355 inline bool
00356 ModuleLocaleStrings_t::IsPopulated() const
00357 {
00358     return m_is_populated;
00359 };
00360 
00361 
00362 inline void
00363 ModuleLocaleStrings_t::GetString( int string_id, out String_t* string ) const
00364 {
00365     rel_assert(string_id < m_num_strings);
00366     string->Set(m_strings[string_id]);
00367 }
00368 
00369 
00370 // \endcond
00371 
00372 
00373 #endif // _UT_RESOURCE_H_

Generated on Tue Dec 14 22:35:05 2010 for UT library by  doxygen 1.6.1