Synesis Software STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ...

fastformat/bundles/windows_resource_bundle.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        fastformat/bundles/windows_resource_bundle.hpp
00003  *
00004  * Purpose:     Windows resource bundle.
00005  *
00006  * Created:     24th April 2009
00007  * Updated:     30th April 2010
00008  *
00009  * Home:        http://www.fastformat.org/
00010  *
00011  * Copyright (c) 2009-2010, Matthew Wilson and Synesis Software
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are
00016  * met:
00017  *
00018  * - Redistributions of source code must retain the above copyright notice,
00019  *   this list of conditions and the following disclaimer.
00020  * - Redistributions in binary form must reproduce the above copyright
00021  *   notice, this list of conditions and the following disclaimer in the
00022  *   documentation and/or other materials provided with the distribution.
00023  * - Neither the names of Matthew Wilson and Synesis Software nor the names
00024  *   of any contributors may be used to endorse or promote products derived
00025  *   from this software without specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00028  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00029  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00030  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00031  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00032  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00033  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00034  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00035  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00036  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00037  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00038  *
00039  * ////////////////////////////////////////////////////////////////////// */
00040 
00041 
00047 #ifndef FASTFORMAT_INCL_FASTFORMAT_BUNDLES_HPP_WINDOWS_RESOURCE_BUNDLE
00048 #define FASTFORMAT_INCL_FASTFORMAT_BUNDLES_HPP_WINDOWS_RESOURCE_BUNDLE
00049 
00050 /* /////////////////////////////////////////////////////////////////////////
00051  * Version information
00052  */
00053 
00054 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
00055 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_WINDOWS_RESOURCE_BUNDLE_MAJOR    1
00056 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_WINDOWS_RESOURCE_BUNDLE_MINOR    0
00057 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_WINDOWS_RESOURCE_BUNDLE_REVISION 2
00058 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_WINDOWS_RESOURCE_BUNDLE_EDIT     5
00059 #endif /* !FASTFORMAT_DOCUMENTATION_SKIP_SECTION */
00060 
00061 /* /////////////////////////////////////////////////////////////////////////
00062  * Language
00063  */
00064 
00065 #ifndef __cplusplus
00066 # error This file can only be included in C++ compilation units
00067 #endif /* !__cplusplus */
00068 
00069 /* /////////////////////////////////////////////////////////////////////////
00070  * Includes
00071  */
00072 
00073 /* FastFormat Header Files */
00074 #include <fastformat/fastformat.h>
00075 #include <fastformat/exceptions.hpp>
00076 #include <fastformat/util/bundle/windows_replacement_translation_functions.hpp>
00077 
00078 /* WinSTL Header Files */
00079 #include <winstl/winstl.h>
00080 
00081 /* STLSoft Header Files */
00082 #include <stlsoft/conversion/integer_to_string.hpp>
00083 #include <stlsoft/shims/access/string.hpp>
00084 #include <stlsoft/util/minmax.hpp>
00085 #include <winstl/error/error_desc.hpp>
00086 #include <winstl/string/resource_string.hpp>
00087 
00088 /* C++ Standard Header Files */
00089 #include <stdexcept>
00090 #include <string>
00091 
00092 /* /////////////////////////////////////////////////////////////////////////
00093  * Namespace
00094  */
00095 
00096 #if !defined(FASTFORMAT_NO_NAMESPACE)
00097 namespace fastformat
00098 {
00099 #endif /* !FASTFORMAT_NO_NAMESPACE */
00100 
00101 /* /////////////////////////////////////////////////////////////////////////
00102  * Classes
00103  */
00104 
00107 class windows_resource_bundle
00108 {
00111 public:
00113     typedef ff_char_t                       char_type;
00115     typedef std::basic_string<char_type>    string_type;
00117     typedef windows_resource_bundle         class_type;
00119     class missing_resource_id_exception
00120         : public bundle_exception
00121     {
00122     public: // Member Types
00124         typedef bundle_exception                    parent_class_type;
00126         typedef missing_resource_id_exception       class_type;
00128         typedef parent_class_type::string_type      string_type;
00129 
00130     public: // Construction
00135         explicit missing_resource_id_exception(int id)
00136             : parent_class_type(create_message_(NULL, 0u, id, NULL).c_str())
00137             , id(id)
00138             , code(-1)
00139         {
00140             FASTFORMAT_COVER_MARK_ENTRY();
00141         }
00153         missing_resource_id_exception(char const* msg, DWORD code, ff_char_t const* id, ff_char_t const* resourceType)
00154             : parent_class_type(create_message_(msg, code, reinterpret_cast<int>(id), resourceType).c_str())
00155             , id(reinterpret_cast<int>(id))
00156             , code(code)
00157         {
00158             FASTFORMAT_COVER_MARK_ENTRY();
00159         }
00171         missing_resource_id_exception(char const* msg, DWORD code, int id, ff_char_t const* resourceType)
00172             : parent_class_type(create_message_(msg, code, id, resourceType).c_str())
00173             , id(id)
00174             , code(code)
00175         {
00176             FASTFORMAT_COVER_MARK_ENTRY();
00177         }
00178 
00179     private:
00181         class_type& operator =(class_type const&);
00182 
00183     public: // Attributes
00185         const int   id;
00187         const DWORD code;
00188 
00189     private:
00190         static multibyte_string_type create_message_(char const* msg, DWORD code, int id, ff_char_t const* resourceType)
00191         {
00192             static const char       string0[]   =   "could not load bundle resource corresponding to identifier ";
00193             static const char       string1[]   =   ": ";
00194             char                    num_[21];
00195             char const*             num         =   stlsoft::integer_to_string(&num_[0], STLSOFT_NUM_ELEMENTS(num_), id);
00196             winstl::error_desc_a    reason(code);
00197             multibyte_string_type   message;
00198 
00199             if(0 != code)
00200             {
00201                 message.reserve(STLSOFT_NUM_ELEMENTS(string0) + 21 + STLSOFT_NUM_ELEMENTS(string1) + reason.size());
00202             }
00203             else
00204             {
00205                 message.reserve(STLSOFT_NUM_ELEMENTS(string0) + 21);
00206             }
00207 
00208             message += string0;
00209             message += num;
00210 
00211             if(0 != code)
00212             {
00213                 message += string1;
00214                 message += reason;
00215             }
00216 
00217             return message;
00218         }
00219     };
00220     typedef missing_resource_id_exception   exception_type;
00221 private:
00222     typedef winstl::basic_resource_string<
00223         string_type
00224     ,   stlsoft::throw_exception_policy<exception_type>
00225     >                                       resource_string_type_;
00227 
00230 public:
00231     explicit windows_resource_bundle(HINSTANCE hinst);
00232     ~windows_resource_bundle() throw();
00233 private:
00234     windows_resource_bundle(class_type const&);
00235     class_type& operator =(class_type const&);
00237 
00240 public:
00241     string_type operator [](int id) const throw(missing_resource_id_exception);
00243 
00246 private:
00248 
00251 private:
00252     HINSTANCE   m_instance;
00254 };
00255 
00256 /* /////////////////////////////////////////////////////////////////////////
00257  * Namespace
00258  */
00259 
00260 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
00261 
00262 inline /* explicit */ windows_resource_bundle::windows_resource_bundle(HINSTANCE hinst)
00263     : m_instance(hinst)
00264 {}
00265 
00266 inline windows_resource_bundle::~windows_resource_bundle() throw()
00267 {
00268     ::FreeLibrary(m_instance);
00269 }
00270 
00271 inline windows_resource_bundle::string_type windows_resource_bundle::operator [](int id) const throw(windows_resource_bundle::missing_resource_id_exception)
00272 {
00273     resource_string_type_   str(m_instance, id);
00274     string_type             s2 = str;
00275 
00276     // Now we attempt some "smarts" by looking to see if
00277     //
00278     // 1. it contains no {D} replacement parameters, where D is a digit
00279     // 2. it contains one or more %D replacement parameters, where D is a digit
00280     //
00281     // If BOTH of these conditions are true, then it assumes that this is an
00282     // MFC / message resource file, and to do the following transformations:
00283     //
00284     // a. all { instances are to be replaced with {{
00285     // b. all %D parameters are to be replaced with {D}
00286 
00287     if( 0 == fastformat::util::calculate_number_of_fastformat_replacement_parameters(s2.data(), s2.length()) &&
00288         0 != fastformat::util::calculate_number_of_windows_replacement_parameters(s2.data(), s2.length()))
00289     {
00290         s2 = fastformat::util::escape_fastformat_replacement_parameters(s2);
00291 
00292         s2 = fastformat::util::change_windows_replacement_parameters_to_fastformat(s2);
00293     }
00294 
00295     return s2;
00296 }
00297 
00298 #endif /* FASTFORMAT_DOCUMENTATION_SKIP_SECTION */
00299 
00300 /* /////////////////////////////////////////////////////////////////////////
00301  * Namespace
00302  */
00303 
00304 #if !defined(FASTFORMAT_NO_NAMESPACE)
00305 } /* namespace fastformat */
00306 #endif /* !FASTFORMAT_NO_NAMESPACE */
00307 
00308 /* ////////////////////////////////////////////////////////////////////// */
00309 
00310 #endif /* FASTFORMAT_INCL_FASTFORMAT_BUNDLES_HPP_WINDOWS_RESOURCE_BUNDLE */
00311 
00312 /* ///////////////////////////// end of file //////////////////////////// */

FastFormat Library documentation © Matthew Wilson, 2006-2009 SourceForge.net Logo