00001
00002
00003 #ifndef ARCLIB_STRINGCONV
00004 #define ARCLIB_STRINGCONV
00005
00006 #include <iomanip>
00007 #include <sstream>
00008 #include <string>
00009 #include <vector>
00010
00011 #include <arc/Logger.h>
00012
00013 namespace Arc {
00014 extern Logger stringLogger;
00015
00017 template<typename T>
00018 T stringto(const std::string& s) {
00019 T t;
00020 if (s.empty()) {
00021 stringLogger.msg(ERROR, "Empty string");
00022 return 0;
00023 }
00024 std::stringstream ss(s);
00025 ss >> t;
00026 if (ss.fail()) {
00027 stringLogger.msg(ERROR, "Conversion failed: %s", s);
00028 return 0;
00029 }
00030 if (!ss.eof())
00031 stringLogger.msg(WARNING, "Full string not used: %s", s);
00032 return t;
00033 }
00034
00036 template<typename T>
00037 bool stringto(const std::string& s, T& t) {
00038 t = 0;
00039 if (s.empty())
00040 return false;
00041 std::stringstream ss(s);
00042 ss >> t;
00043 if (ss.fail())
00044 return false;
00045 if (!ss.eof())
00046 return false;
00047 return true;
00048 }
00049
00050
00051
00052 #define stringtoi(A) stringto < int > ((A))
00053 #define stringtoui(A) stringto < unsigned int > ((A))
00054 #define stringtol(A) stringto < long > ((A))
00055 #define stringtoll(A) stringto < long long > ((A))
00056 #define stringtoul(A) stringto < unsigned long > ((A))
00057 #define stringtoull(A) stringto < unsigned long long > ((A))
00058 #define stringtof(A) stringto < float > ((A))
00059 #define stringtod(A) stringto < double > ((A))
00060 #define stringtold(A) stringto < long double > ((A))
00061
00064 bool strtoint(const std::string& s, signed int& t, int base = 10);
00065
00068 bool strtoint(const std::string& s, unsigned int& t, int base = 10);
00069
00072 bool strtoint(const std::string& s, signed long& t, int base = 10);
00073
00076 bool strtoint(const std::string& s, unsigned long& t, int base = 10);
00077
00080 bool strtoint(const std::string& s, signed long long& t, int base = 10);
00081
00084 bool strtoint(const std::string& s, unsigned long long& t, int base = 10);
00085
00087 template<typename T>
00088 std::string tostring(T t, int width = 0, int precision = 0) {
00089 std::stringstream ss;
00090 if (precision)
00091 ss << std::setprecision(precision);
00092 ss << std::setw(width) << t;
00093 return ss.str();
00094 }
00095
00098 std::string inttostr(signed long long t, int base = 10, int width = 0);
00099
00102 std::string inttostr(unsigned long long t, int base = 10, int width = 0);
00103
00106 inline std::string inttostr(signed int t, int base = 10, int width = 0) {
00107 return inttostr((signed long long)t,base,width);
00108 }
00109
00112 inline std::string inttostr(unsigned int t, int base = 10, int width = 0) {
00113 return inttostr((unsigned long long)t,base,width);
00114 }
00115
00118 inline std::string inttostr(signed long t, int base = 10, int width = 0) {
00119 return inttostr((signed long long)t,base,width);
00120 }
00121
00124 inline std::string inttostr(unsigned long t, int base = 10, int width = 0) {
00125 return inttostr((unsigned long long)t,base,width);
00126 }
00127
00129 inline std::string booltostr(bool b) {
00130 return b ? "true" : "false";
00131 }
00132
00135 inline bool strtobool(const std::string& s) {
00136 return s == "true" || s == "1";
00137 }
00138
00143 inline bool strtobool(const std::string& s, bool& b) {
00144 if (s == "true" || s == "1" || s == "false" || s == "0") {
00145 b = (s == "true" || s == "1");
00146 return true;
00147 }
00148 return false;
00149 }
00150
00152 std::string lower(const std::string& s);
00153
00155 std::string upper(const std::string& s);
00156
00158 void tokenize(const std::string& str, std::vector<std::string>& tokens,
00159 const std::string& delimiters = " ",
00160 const std::string& start_quotes = "", const std::string& end_quotes = "");
00161
00163 void tokenize(const std::string& str, std::list<std::string>& tokens,
00164 const std::string& delimiters = " ",
00165 const std::string& start_quotes = "", const std::string& end_quotes = "");
00166
00168 std::string::size_type get_token(std::string& token,
00169 const std::string& str, std::string::size_type pos,
00170 const std::string& delimiters = " ",
00171 const std::string& start_quotes = "", const std::string& end_quotes = "");
00172
00173
00175 std::string trim(const std::string& str, const char *sep = NULL);
00176
00178 std::string strip(const std::string& str);
00179
00181
00184
00185 std::string uri_encode(const std::string& str, bool encode_slash);
00186
00188 std::string uri_unencode(const std::string& str);
00189
00191 std::string convert_to_rdn(const std::string& dn);
00192
00194 typedef enum {
00195 escape_char,
00196 escape_octal,
00197 escape_hex
00198 } escape_type;
00199
00202 std::string escape_chars(const std::string& str, const std::string& chars, char esc, bool excl, escape_type type = escape_char);
00203
00205 std::string unescape_chars(const std::string& str, char esc, escape_type type = escape_char);
00206
00207 }
00208
00209 #endif // ARCLIB_STRINGCONV