strutils.h

00001 /*
00002  * strutils.h
00003  * This file is part of dbPager Classes Library (DCL)
00004  *
00005  * Copyright (c) 2008 Dennis Prochko <wolfsoft@mail.ru>
00006  *
00007  * DCL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation version 3.
00010  *
00011  * DCL is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with DCL; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor,
00019  * Boston, MA  02110-1301  USA
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 Regular expression string iterator
00193 class regexp {
00194 public:
00195         class iterator: public std::iterator<std::input_iterator_tag,
00196           int> {
00197         public:
00198                 int pos;
00199                 std::string found;
00200                 explicit iterator(regexp &expression): expr(expression) { };
00201         private:
00202                 regexp &expr;
00203         };
00204         iterator operator()(const std::string &source,
00205           const std::string &expression) {
00206                 _source = source;
00207                 _expression = expression;
00208                 return iterator(*this);
00209         };
00210 private:
00211         strings _regexp;
00212         std::string _source, _expression;
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 } // namespace
00329 
00330 #endif /*STRUTILS_H_*/

 
Support This Project
SourceForge.net Logo