strutils.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef STRUTILS_H_
00023 #define STRUTILS_H_
00024
00025 #include <string>
00026 #include <vector>
00027 #include <sstream>
00028 #include <locale>
00029 #include <cwchar>
00030
00031 #include <dcl/exception.h>
00032 #include <dcl/i18n.h>
00033
00034 namespace dbp {
00035
00036 typedef std::vector<std::string> strings;
00037
00039 const std::string nullstr;
00040
00042
00052 class trim: public std::binary_function<std::string, char*, std::string> {
00053 public:
00055
00060 std::string operator()(const std::string &source,
00061 const char *delims = " \t\r\n") const;
00062 };
00063
00065
00072 class char_compare: public std::binary_function<char, char, bool> {
00073 public:
00075 char_compare(const std::ctype<char> &c): _ct(c) {};
00077
00082 bool operator()(char x, char y) const {
00083 return _ct.toupper(x) == _ct.toupper(y);
00084 };
00085 private:
00086 const std::ctype<char> &_ct;
00087 };
00088
00090
00097 class compare: public std::binary_function<std::string, std::string, bool> {
00098 public:
00100
00103 compare(const std::locale &L = std::locale::classic()): loc(L),
00104 ct(std::use_facet<std::ctype<char> >(loc)) { };
00106
00111 bool operator()(const std::string &x, const std::string &y) const;
00112 private:
00113 std::locale loc;
00114 const std::ctype<char> &ct;
00115 };
00116
00118
00122 class tokenize {
00123 public:
00125
00130 strings operator()(const std::string &source,
00131 const char *delims = ",;");
00133
00142 void operator()(const std::string &source, std::string &left,
00143 std::string &right, const char *delims = "/\\");
00144 };
00145
00147
00158 class format {
00159 private:
00160 std::string str_;
00161 strings params_;
00162 public:
00164
00167 format(const std::string &source): str_(source) {};
00169
00172 std::string str() const;
00174
00177 template<class T>
00178 format& operator%(const T& x) {
00179 std::stringstream s;
00180 s << x;
00181 params_.push_back(s.str());
00182 return *this;
00183 }
00185
00188 friend std::ostream& operator<<(std::ostream&, format&);
00189 };
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00217
00221 class string2wstring: public std::binary_function<std::string, std::locale const &,
00222 std::wstring> {
00223 public:
00225
00230 std::wstring operator()(const std::string &source,
00231 std::locale const &loc = std::locale()) const;
00232 };
00233
00235
00239 class wstring2string: public std::binary_function<std::wstring, std::locale const &,
00240 std::string> {
00241 public:
00243
00248 std::string operator()(const std::wstring &text,
00249 std::locale const &loc = std::locale()) const;
00250 };
00251
00253
00257 class char_length: public std::binary_function<std::string, std::locale&,
00258 std::size_t> {
00259 public:
00261
00266 std::size_t operator()(std::string const &text,
00267 std::locale const &loc = std::locale("")) const;
00268 };
00269
00271
00275 class type_conversion_exception: public dbp::exception { };
00276
00278
00281 template <class TYPE>
00282 std::string to_string(const TYPE value) throw(type_conversion_exception) {
00283 std::ostringstream s;
00284 s.imbue(std::locale::classic());
00285 try {
00286 s << value;
00287 }
00288 catch (...) {
00289 throw type_conversion_exception();
00290 }
00291 return s.str();
00292 }
00293
00294 template <>
00295 std::string to_string<const char*>(const char *value) throw(type_conversion_exception);
00296
00297 template <>
00298 std::string to_string<const std::string&>(const std::string &value) throw(type_conversion_exception);
00299
00301
00305 template <class TYPE>
00306 TYPE from_string(const std::string &value) throw(type_conversion_exception) {
00307 std::istringstream s(value);
00308 s.imbue(std::locale::classic());
00309 TYPE rslt;
00310 try {
00311 if (!(s >> rslt))
00312 throw type_conversion_exception();
00313 }
00314 catch (...) {
00315 throw type_conversion_exception();
00316 }
00317 return rslt;
00318 }
00319
00320 template <>
00321 const char* from_string<const char*>(const std::string &value)
00322 throw(type_conversion_exception);
00323
00324 template <>
00325 std::string from_string<std::string>(const std::string &value)
00326 throw(type_conversion_exception);
00327
00328 }
00329
00330 #endif