00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
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
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 3
00058 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_PROPERTIES_BUNDLE_EDIT 14
00059 #endif
00060
00061
00062
00063
00064
00065 #ifndef __cplusplus
00066 # error This file can only be included in C++ compilation units
00067 #endif
00068
00069
00070
00071
00072
00073
00074 #include <fastformat/fastformat.h>
00075
00076
00077
00078 #include <stlsoft/shims/access/string.hpp>
00079 #include <stlsoft/string/split_functions.hpp>
00080 #include <stlsoft/string/trim_functions.hpp>
00081
00082
00083 #include <fstream>
00084 #include <map>
00085 #include <stdexcept>
00086 #include <string>
00087
00088
00089
00090
00091
00092 #if !defined(FASTFORMAT_NO_NAMESPACE)
00093 namespace fastformat
00094 {
00095 #endif
00096
00097
00098
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_map(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
00154
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 const map_type m_map;
00171
00174 private:
00175 properties_bundle(class_type const&);
00176 class_type& operator =(class_type const&);
00178 };
00179
00180
00181
00182
00183
00184 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
00185
00186 inline const properties_bundle::map_type properties_bundle::open_(char const *name)
00187 {
00188
00189
00190 std::string line;
00191 std::ifstream stm(name);
00192 map_type map;
00193
00194 if(!stm)
00195 {
00196
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_map(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_map.find(name);
00231
00232 if(it == m_map.end())
00233 {
00234 throw std::out_of_range("format string not found");
00235 }
00236
00237 return (*it).second;
00238 }
00239
00240 #endif
00241
00242
00243
00244
00245
00246 #if !defined(FASTFORMAT_NO_NAMESPACE)
00247 }
00248 #endif
00249
00250
00251
00252 #endif
00253
00254