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

fastformat/bundles/winini_bundle.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        fastformat/bundles/winini_bundle.hpp
00003  *
00004  * Purpose:     Windows INI file bundle.
00005  *
00006  * Created:     19th September 2006
00007  * Updated:     11th August 2009
00008  *
00009  * Home:        http://www.fastformat.org/
00010  *
00011  * Copyright (c) 2006-2009, 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_WININI_BUNDLE
00048 #define FASTFORMAT_INCL_FASTFORMAT_BUNDLES_HPP_WININI_BUNDLE
00049 
00050 /* /////////////////////////////////////////////////////////////////////////
00051  * Version information
00052  */
00053 
00054 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
00055 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_WININI_BUNDLE_MAJOR      1
00056 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_WININI_BUNDLE_MINOR      1
00057 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_WININI_BUNDLE_REVISION   3
00058 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_WININI_BUNDLE_EDIT       14
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 
00076 /* WinSTL Header Files */
00077 #include <winstl/winstl.h>
00078 
00079 /* STLSoft Header Files */
00080 #include <stlsoft/shims/access/string.hpp>
00081 #include <stlsoft/util/minmax.hpp>
00082 
00083 /* C++ Standard Header Files */
00084 #include <stdexcept>
00085 #include <string>
00086 
00087 /* /////////////////////////////////////////////////////////////////////////
00088  * Namespace
00089  */
00090 
00091 #if !defined(FASTFORMAT_NO_NAMESPACE)
00092 namespace fastformat
00093 {
00094 #endif /* !FASTFORMAT_NO_NAMESPACE */
00095 
00096 /* /////////////////////////////////////////////////////////////////////////
00097  * Classes
00098  */
00099 
00102 class winini_bundle
00103 {
00106 public:
00107     typedef ff_char_t                       char_type;
00108     typedef std::basic_string<char_type>    string_type;
00109     typedef winini_bundle                   class_type;
00111 
00114 public:
00115     explicit winini_bundle(char_type const *source, char_type const *section);
00116 
00117     template <typename S0, typename S1>
00118     explicit winini_bundle(S0 const& source, S1 const& section)
00119         : m_iniFileName(::stlsoft::c_str_data(source), ::stlsoft::c_str_len(source))
00120         , m_iniSection(::stlsoft::c_str_data(section), ::stlsoft::c_str_len(section))
00121     {}
00122 
00123     ~winini_bundle() throw();
00125 
00128 public:
00129     string_type operator [](char_type const *name) const throw(std::out_of_range);
00130     template <typename S>
00131     string_type operator [](S const& name) const throw(std::out_of_range)
00132     {
00133         FASTFORMAT_DECLARE_c_str_ptr_();
00134 
00135         return operator [](FASTFORMAT_INVOKE_c_str_ptr_(FASTFORMAT_INVOKE_c_str_ptr_(name)));
00136     }
00138 
00141 private:
00143 
00146 private:
00147     const string_type   m_iniFileName;
00148     const string_type   m_iniSection;
00150 
00153 private:
00154     winini_bundle(class_type const&);
00155     class_type& operator =(class_type const&);
00157 };
00158 
00159 /* /////////////////////////////////////////////////////////////////////////
00160  * Namespace
00161  */
00162 
00163 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
00164 
00165 inline winini_bundle::winini_bundle(char_type const *source, char_type const *section)
00166     : m_iniFileName(source)
00167     , m_iniSection(section)
00168 {}
00169 
00170 inline winini_bundle::~winini_bundle() throw()
00171 {}
00172 
00173 inline winini_bundle::string_type winini_bundle::operator [](char_type const *name) const throw(std::out_of_range)
00174 {
00175     static const char_type sentinel[] = FASTFORMAT_LITERAL_STRING("{5DDAADE5-5134-4734-ACB8-A6D0FA3BD0EA}-{D875F19B-8279-4c44-9517-F16366BC13E3}");
00176 
00177     ::SetLastError(0);
00178 
00179     char_type   result[1001];
00180     DWORD       dw = ::GetPrivateProfileString(
00181                                             m_iniSection.c_str()
00182                                         ,   name
00183                                         ,   sentinel
00184                                         ,   &result[0]
00185                                         ,   STLSOFT_NUM_ELEMENTS(result)
00186                                         ,   m_iniFileName.c_str());
00187 
00188     if(dw == STLSOFT_NUM_ELEMENTS(sentinel) - 1)    /* element not found */
00189     {
00190         if(0 == string_type::traits_type::compare(sentinel, result, stlsoft::minimum(size_t(dw), STLSOFT_NUM_ELEMENTS(sentinel) - 1)))
00191         {
00192             throw std::out_of_range("section/key combination does not exist in the given INI file");
00193         }
00194     }
00195 
00196     ::GetLastError();
00197 
00198     return string_type(result, dw);
00199 }
00200 
00201 #endif /* FASTFORMAT_DOCUMENTATION_SKIP_SECTION */
00202 
00203 /* /////////////////////////////////////////////////////////////////////////
00204  * Namespace
00205  */
00206 
00207 #if !defined(FASTFORMAT_NO_NAMESPACE)
00208 } /* namespace fastformat */
00209 #endif /* !FASTFORMAT_NO_NAMESPACE */
00210 
00211 /* ////////////////////////////////////////////////////////////////////// */
00212 
00213 #endif /* FASTFORMAT_INCL_FASTFORMAT_BUNDLES_HPP_WININI_BUNDLE */
00214 
00215 /* ///////////////////////////// end of file //////////////////////////// */

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