Pol  Revision:cb584c9
strutil.cpp
Go to the documentation of this file.
1 
11 #include "strutil.h"
12 
13 #include <boost/algorithm/string/case_conv.hpp>
14 
15 #include "logfacility.h"
16 #include "stlutil.h"
17 #include "unittest.h"
18 
19 namespace Pol
20 {
21 namespace Clib
22 {
23 std::string hexint( unsigned short v )
24 {
25  OSTRINGSTREAM os;
26  os << "0x" << std::hex << v;
27  return OSTRINGSTREAM_STR( os );
28 }
29 
30 std::string hexint( signed int v )
31 {
32  OSTRINGSTREAM os;
33  os << "0x" << std::hex << v;
34  return OSTRINGSTREAM_STR( os );
35 }
36 std::string hexint( unsigned int v )
37 {
38  OSTRINGSTREAM os;
39  os << "0x" << std::hex << v;
40  return OSTRINGSTREAM_STR( os );
41 }
42 
43 std::string hexint( signed long v )
44 {
45  OSTRINGSTREAM os;
46  os << "0x" << std::hex << v;
47  return OSTRINGSTREAM_STR( os );
48 }
49 std::string hexint( unsigned long v )
50 {
51  OSTRINGSTREAM os;
52  os << "0x" << std::hex << v;
53  return OSTRINGSTREAM_STR( os );
54 }
55 #ifdef _WIN64
56 std::string hexint( size_t v )
57 {
58  OSTRINGSTREAM os;
59  os << "0x" << std::hex << v;
60  return OSTRINGSTREAM_STR( os );
61 }
62 #endif
63 
64 std::string decint( unsigned short v )
65 {
66  OSTRINGSTREAM os;
67  os << v;
68  return OSTRINGSTREAM_STR( os );
69 }
70 
71 std::string decint( signed int v )
72 {
73  OSTRINGSTREAM os;
74  os << v;
75  return OSTRINGSTREAM_STR( os );
76 }
77 
78 std::string decint( unsigned int v )
79 {
80  OSTRINGSTREAM os;
81  os << v;
82  return OSTRINGSTREAM_STR( os );
83 }
84 
85 std::string decint( signed long v )
86 {
87  OSTRINGSTREAM os;
88  os << v;
89  return OSTRINGSTREAM_STR( os );
90 }
91 std::string decint( unsigned long v )
92 {
93  OSTRINGSTREAM os;
94  os << v;
95  return OSTRINGSTREAM_STR( os );
96 }
97 
98 #ifdef _WIN64
99 std::string decint( size_t v )
100 {
101  OSTRINGSTREAM os;
102  os << v;
103  return OSTRINGSTREAM_STR( os );
104 }
105 #endif
106 
107 void splitnamevalue( const std::string& istr, std::string& propname, std::string& propvalue )
108 {
109  std::string::size_type start = istr.find_first_not_of( " \t\r\n" );
110  if ( start != std::string::npos )
111  {
112  std::string::size_type delimpos = istr.find_first_of( " \t\r\n=", start + 1 );
113  if ( delimpos != std::string::npos )
114  {
115  std::string::size_type valuestart = istr.find_first_not_of( " \t\r\n", delimpos + 1 );
116  std::string::size_type valueend = istr.find_last_not_of( " \t\r\n" );
117  propname = istr.substr( start, delimpos - start );
118  if ( valuestart != std::string::npos && valueend != std::string::npos )
119  {
120  propvalue = istr.substr( valuestart, valueend - valuestart + 1 );
121  }
122  else
123  {
124  propvalue = "";
125  }
126  }
127  else
128  {
129  propname = istr.substr( start, std::string::npos );
130  propvalue = "";
131  }
132  }
133  else
134  {
135  propname = "";
136  propvalue = "";
137  }
138 }
139 
140 void test_splitnamevalue( const std::string& istr, const std::string& exp_pn,
141  const std::string& exp_pv )
142 {
143  std::string pn, pv;
144  splitnamevalue( istr, pn, pv );
145  if ( pn != exp_pn || pv != exp_pv )
146  {
147  INFO_PRINT << "splitnamevalue( \"" << istr << "\" ) fails!\n";
148  }
149 }
150 
152 {
153  test_splitnamevalue( "a b", "a", "b" );
154  test_splitnamevalue( "av bx", "av", "bx" );
155  test_splitnamevalue( "nm=valu", "nm", "valu" );
156  test_splitnamevalue( "nm:valu", "nm:valu", "" );
157  test_splitnamevalue( "nm", "nm", "" );
158  test_splitnamevalue( " nm", "nm", "" );
159  test_splitnamevalue( " nm ", "nm", "" );
160  test_splitnamevalue( " nm valu", "nm", "valu" );
161  test_splitnamevalue( " nm value ", "nm", "value" );
162  test_splitnamevalue( " nm value is multiple words", "nm", "value is multiple words" );
163  test_splitnamevalue( " nm value is multiple words\t ", "nm", "value is multiple words" );
164 }
166 
167 void decodequotedstring( std::string& str )
168 {
169  std::string tmp;
170  tmp.swap( str );
171  const char* s = tmp.c_str();
172  str.reserve( tmp.size() );
173  ++s;
174  while ( *s )
175  {
176  char ch = *s++;
177 
178  switch ( ch )
179  {
180  case '\\':
181  ch = *s++;
182  switch ( ch )
183  {
184  case '\0':
185  return;
186  case 'n': // newline
187  str += "\n";
188  break;
189  default: // slash, quote, etc
190  str += ch;
191  break;
192  }
193  break;
194 
195  case '\"':
196  return;
197 
198  default:
199  str += ch;
200  break;
201  }
202  }
203 }
204 void encodequotedstring( std::string& str )
205 {
206  std::string tmp;
207  tmp.swap( str );
208  const char* s = tmp.c_str();
209  str.reserve( tmp.size() + 2 );
210  str += "\"";
211 
212  while ( *s )
213  {
214  char ch = *s++;
215  switch ( ch )
216  {
217  case '\\':
218  str += "\\\\";
219  break;
220  case '\"':
221  str += "\\\"";
222  break;
223  case '\n':
224  str += "\\n";
225  break;
226  default:
227  str += ch;
228  break;
229  }
230  }
231 
232  str += "\"";
233 }
234 
235 std::string getencodedquotedstring( const std::string& in )
236 {
237  std::string tmp = in;
238  encodequotedstring( tmp );
239  return tmp;
240 }
241 void test_dqs( const std::string& in, const std::string& out )
242 {
243  std::string tmp = in;
244  decodequotedstring( tmp );
245  if ( tmp != out )
246  {
247  INFO_PRINT << "decodequotedstring( " << in << " ) fails!\n";
248  }
249  encodequotedstring( tmp );
250  if ( tmp != in )
251  {
252  INFO_PRINT << "encodequotedstring( " << out << " ) fails!\n";
253  }
254 }
255 
257 {
258  test_dqs( "\"hi\"", "hi" );
259  test_dqs( "\"hi \"", "hi " );
260  test_dqs( "\" hi \"", " hi " );
261  test_dqs( "\" \\\"hi\"", " \"hi" );
262 }
264 
265 // If we have boost, I think we should use it...
266 void mklower( std::string& str )
267 {
268  boost::to_lower( str );
269 }
270 
271 void mkupper( std::string& str )
272 {
273  boost::to_upper( str );
274 }
275 
276 std::string strlower( const std::string& str )
277 {
278  return boost::to_lower_copy( str );
279 }
280 
281 std::string strupper( const std::string& str )
282 {
283  return boost::to_upper_copy( str );
284 }
285 }
286 }
UnitTest test_convertquotedstring_obj(test_convertquotedstring)
void splitnamevalue(const std::string &istr, std::string &propname, std::string &propvalue)
Definition: strutil.cpp:107
void mkupper(std::string &str)
Definition: strutil.cpp:271
void encodequotedstring(std::string &str)
Definition: strutil.cpp:204
std::string decint(unsigned short v)
Definition: strutil.cpp:64
#define OSTRINGSTREAM_STR(x)
Definition: stlutil.h:76
std::string hexint(unsigned short v)
Definition: strutil.cpp:23
#define OSTRINGSTREAM
Definition: stlutil.h:75
std::string getencodedquotedstring(const std::string &in)
Definition: strutil.cpp:235
void decodequotedstring(std::string &str)
Definition: strutil.cpp:167
void test_dqs(const std::string &in, const std::string &out)
Definition: strutil.cpp:241
void test_splitnamevalue(const std::string &istr, const std::string &exp_pn, const std::string &exp_pv)
Definition: strutil.cpp:140
std::string strlower(const std::string &str)
Definition: strutil.cpp:276
void mklower(std::string &str)
Definition: strutil.cpp:266
UnitTest test_splitnamevalue_obj(test_splitnamevalue)
std::string strupper(const std::string &str)
Definition: strutil.cpp:281
#define INFO_PRINT
Definition: logfacility.h:223
Definition: berror.cpp:12
void test_convertquotedstring()
Definition: strutil.cpp:256