- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages
- Examples

# fastformat/bundles/properties_bundle.hpp

Go to the documentation of this file.

    00001 /* /////////////////////////////////////////////////////////////////////////
    00002  * File:        fastformat/bundles/properties_bundle.hpp
    00003  *
    00004  * Purpose:     .properties file bundle.
    00005  *
    00006  * Created:     19th September 2006
    00007  * Updated:     19th May 2010
    00008  *
    00009  * Home:        http://www.fastformat.org/
    00010  *
    00011  * Copyright (c) 2007-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_PROPERTIES_BUNDLE
    00048 #define FASTFORMAT_INCL_FASTFORMAT_BUNDLES_HPP_PROPERTIES_BUNDLE
    00049
    00050 /* /////////////////////////////////////////////////////////////////////////
    00051  * Version information
    00052  */
    00053
    00054 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
    00055 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_PROPERTIES_BUNDLE_MAJOR      1
    00056 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_PROPERTIES_BUNDLE_MINOR      1
    00057 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_PROPERTIES_BUNDLE_REVISION   4
    00058 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_PROPERTIES_BUNDLE_EDIT       16
    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
    00077 /* STLSoft Header Files */
    00078 #include <stlsoft/shims/access/string.hpp>
    00079 #include <stlsoft/string/split_functions.hpp>
    00080 #include <stlsoft/string/trim_functions.hpp>
    00081
    00082 /* C++ Standard Header Files */
    00083 #include <fstream>
    00084 #include <map>
    00085 #include <stdexcept>
    00086 #include <string>
    00087
    00088 /* /////////////////////////////////////////////////////////////////////////
    00089  * Namespace
    00090  */
    00091
    00092 #if !defined(FASTFORMAT_NO_NAMESPACE)
    00093 namespace fastformat
    00094 {
    00095 #endif /* !FASTFORMAT_NO_NAMESPACE */
    00096
    00097 /* /////////////////////////////////////////////////////////////////////////
    00098  * Classes
    00099  */
    00100
    00103 class properties_bundle
    00104 {
    00107 public:
    00108     typedef std::string                         string_type;
    00109     typedef properties_bundle                   class_type;
    00110 private:
    00111     typedef std::map<string_type, string_type>  map_type;
    00113
    00116 public:
    00117     enum
    00118     {
    00119             ignoreCaseOnLookup  =   0x01
    00120         ,   treatSourceAsMemory =   0x02
    00121     };
    00123
    00126 public:
    00127     explicit properties_bundle(char const *source);
    00128
    00129     template <typename S>
    00130     explicit properties_bundle(S const& source)
    00131         : m_properties(open_ns_(source))
    00132     {}
    00133
    00134     ~properties_bundle() throw();
    00136
    00139 public:
    00140     string_type operator [](char const* name) const throw(std::out_of_range);
    00141     template <typename S>
    00142     string_type operator [](S const& name) const throw(std::out_of_range)
    00143     {
    00144         FASTFORMAT_DECLARE_c_str_ptr_();
    00145
    00146         return operator [](FASTFORMAT_INVOKE_c_str_ptr_(FASTFORMAT_INVOKE_c_str_ptr_(name)));
    00147     }
    00149
    00152 private:
    00153     // We have to have this funky level of indirection due to DMC++ and GCC compilers'
    00154     // weird behaviour wrt handling shims' fully qualified names in templates.
    00155     template <typename S>
    00156     static const map_type open_ns_(S const& source)
    00157     {
    00158         FASTFORMAT_DECLARE_c_str_ptr_();
    00159
    00160         return open_(FASTFORMAT_INVOKE_c_str_ptr_(source));
    00161     }
    00162
    00163     static const map_type open_(char const *name);
    00165
    00168 private:
    00169     map_type const m_properties;
    00171
    00174 private:
    00175     properties_bundle(class_type const&);
    00176     class_type& operator =(class_type const&);
    00178 };
    00179
    00180 /* /////////////////////////////////////////////////////////////////////////
    00181  * Implementation
    00182  */
    00183
    00184 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
    00185
    00186 inline /* static */ const properties_bundle::map_type properties_bundle::open_(char const *name)
    00187 {
    00188 // TODO: Change this to use the new STLSoft properties_file class, when 1.10 comes out
    00189
    00190     std::string     line;
    00191     std::ifstream   stm(name);
    00192     map_type        map;
    00193
    00194     if(!stm)
    00195     {
    00196 // TODO: Define (and thoroughly unit-test) a FastFormat-specific exception, and use that instead
    00197
    00198         throw std::runtime_error("properties file not found");
    00199     }
    00200
    00201     for(; std::getline(stm, line); )
    00202     {
    00203         std::string name;
    00204         std::string value;
    00205
    00206         stlsoft::trim_all(line);
    00207
    00208         if(stlsoft::split(line, '=', name, value))
    00209         {
    00210             stlsoft::trim_all(name);
    00211             stlsoft::trim_left(value);
    00212
    00213             map[name] = value;
    00214         }
    00215     }
    00216
    00217     return map;
    00218 }
    00219
    00220
    00221 inline properties_bundle::properties_bundle(char const *source)
    00222     : m_properties(open_(source))
    00223 {}
    00224
    00225 inline properties_bundle::~properties_bundle() throw()
    00226 {}
    00227
    00228 inline properties_bundle::string_type properties_bundle::operator [](char const *name) const throw(std::out_of_range)
    00229 {
    00230     map_type::const_iterator it = m_properties.find(name);
    00231
    00232     if(it == m_properties.end())
    00233     {
    00234         throw std::out_of_range("format string not found");
    00235     }
    00236
    00237     return (*it).second;
    00238 }
    00239
    00240 #endif /* FASTFORMAT_DOCUMENTATION_SKIP_SECTION */
    00241
    00242 /* /////////////////////////////////////////////////////////////////////////
    00243  * Namespace
    00244  */
    00245
    00246 #if !defined(FASTFORMAT_NO_NAMESPACE)
    00247 } /* namespace fastformat */
    00248 #endif /* !FASTFORMAT_NO_NAMESPACE */
    00249
    00250 /* ////////////////////////////////////////////////////////////////////////// */
    00251
    00252 #endif /* FASTFORMAT_INCL_FASTFORMAT_BUNDLES_HPP_PROPERTIES_BUNDLE */
    00253
    00254 /* ///////////////////////////// end of file //////////////////////////// */

FastFormat Library documentation © Matthew Wilson, 2006-2009 |