ARC SDK
StringConv.h
1 // -*- indent-tabs-mode: nil -*-
2 
3 #ifndef ARCLIB_STRINGCONV
4 #define ARCLIB_STRINGCONV
5 
6 #include <iomanip>
7 #include <sstream>
8 #include <string>
9 #include <vector>
10 
11 #include <arc/Logger.h>
12 
13 namespace Arc {
14 
18  extern Logger stringLogger;
19 
21  template<typename T>
22  T stringto(const std::string& s) {
23  T t;
24  if (s.empty()) {
25  stringLogger.msg(ERROR, "Empty string");
26  return 0;
27  }
28  std::stringstream ss(s);
29  ss >> t;
30  if (ss.fail()) {
31  stringLogger.msg(ERROR, "Conversion failed: %s", s);
32  return 0;
33  }
34  if (!ss.eof())
35  stringLogger.msg(WARNING, "Full string not used: %s", s);
36  return t;
37  }
38 
40  template<typename T>
41  bool stringto(const std::string& s, T& t) {
42  t = 0;
43  if (s.empty())
44  return false;
45  std::stringstream ss(s);
46  ss >> t;
47  if (ss.fail())
48  return false;
49  if (!ss.eof())
50  return false;
51  return true;
52  }
53 
54 
55 
56 #define stringtoi(A) stringto < int > ((A))
57 #define stringtoui(A) stringto < unsigned int > ((A))
58 #define stringtol(A) stringto < long > ((A))
59 #define stringtoll(A) stringto < long long > ((A))
60 #define stringtoul(A) stringto < unsigned long > ((A))
61 #define stringtoull(A) stringto < unsigned long long > ((A))
62 #define stringtof(A) stringto < float > ((A))
63 #define stringtod(A) stringto < double > ((A))
64 #define stringtold(A) stringto < long double > ((A))
65 
67 
68  bool strtoint(const std::string& s, signed int& t, int base = 10);
69 
71 
72  bool strtoint(const std::string& s, unsigned int& t, int base = 10);
73 
75 
76  bool strtoint(const std::string& s, signed long& t, int base = 10);
77 
79 
80  bool strtoint(const std::string& s, unsigned long& t, int base = 10);
81 
83 
84  bool strtoint(const std::string& s, signed long long& t, int base = 10);
85 
87 
88  bool strtoint(const std::string& s, unsigned long long& t, int base = 10);
89 
91  template<typename T>
92  std::string tostring(T t, int width = 0, int precision = 0) {
93  std::stringstream ss;
94  if (precision)
95  ss << std::setprecision(precision);
96  ss << std::setw(width) << t;
97  return ss.str();
98  }
99 
101 
102  std::string inttostr(signed long long t, int base = 10, int width = 0);
103 
105 
106  std::string inttostr(unsigned long long t, int base = 10, int width = 0);
107 
109 
110  inline std::string inttostr(signed int t, int base = 10, int width = 0) {
111  return inttostr((signed long long)t,base,width);
112  }
113 
115 
116  inline std::string inttostr(unsigned int t, int base = 10, int width = 0) {
117  return inttostr((unsigned long long)t,base,width);
118  }
119 
121 
122  inline std::string inttostr(signed long t, int base = 10, int width = 0) {
123  return inttostr((signed long long)t,base,width);
124  }
125 
127 
128  inline std::string inttostr(unsigned long t, int base = 10, int width = 0) {
129  return inttostr((unsigned long long)t,base,width);
130  }
131 
133  inline std::string booltostr(bool b) {
134  return b ? "true" : "false";
135  }
136 
138  inline bool strtobool(const std::string& s) {
139  return s == "true" || s == "1";
140  }
141 
143 
146  inline bool strtobool(const std::string& s, bool& b) {
147  if (s == "true" || s == "1" || s == "false" || s == "0") {
148  b = (s == "true" || s == "1");
149  return true;
150  }
151  return false;
152  }
153 
155  std::string lower(const std::string& s);
156 
158  std::string upper(const std::string& s);
159 
161  void tokenize(const std::string& str, std::vector<std::string>& tokens,
162  const std::string& delimiters = " ",
163  const std::string& start_quotes = "", const std::string& end_quotes = "");
164 
166  void tokenize(const std::string& str, std::list<std::string>& tokens,
167  const std::string& delimiters = " ",
168  const std::string& start_quotes = "", const std::string& end_quotes = "");
169 
171  std::string::size_type get_token(std::string& token,
172  const std::string& str, std::string::size_type pos,
173  const std::string& delimiters = " ",
174  const std::string& start_quotes = "", const std::string& end_quotes = "");
175 
176 
178  std::string trim(const std::string& str, const char *sep = NULL);
179 
181  std::string strip(const std::string& str);
182 
184 
187  std::string uri_encode(const std::string& str, bool encode_slash);
188 
190  std::string uri_unencode(const std::string& str);
191 
193  std::string convert_to_rdn(const std::string& dn);
194 
196  typedef enum {
200  } escape_type;
201 
203 
204  std::string escape_chars(const std::string& str, const std::string& chars, char esc, bool excl, escape_type type = escape_char);
205 
207  std::string unescape_chars(const std::string& str, char esc, escape_type type = escape_char);
208 
210 } // namespace Arc
211 
212 #endif // ARCLIB_STRINGCONV