appkit  1.5.1
RoadNarrows Robotics Application Kit
StringTheory.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_appkit
8 //
9 // File: StringTheory.cxx
10 //
11 /*! \file
12  *
13  * \brief Of string spaces and their strangian operators.
14  *
15  * \author Robin Knight (robin.knight@roadnarrows.com)
16  *
17  * \par Copyright
18  * \h_copy 2016-2017. RoadNarrows LLC.\n
19  * http://www.roadnarrows.com\n
20  * All Rights Reserved
21  *
22  * \par License:
23  * MIT
24  */
25 /*
26  * @EulaBegin@
27  *
28  * Permission is hereby granted, without written agreement and without
29  * license or royalty fees, to use, copy, modify, and distribute this
30  * software and its documentation for any purpose, provided that
31  * (1) The above copyright notice and the following two paragraphs
32  * appear in all copies of the source code and (2) redistributions
33  * including binaries reproduces these notices in the supporting
34  * documentation. Substantial modifications to this software may be
35  * copyrighted by their authors and need not follow the licensing terms
36  * described here, provided that the new terms are clearly indicated in
37  * all files where they apply.
38  *
39  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
40  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
41  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
42  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
43  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
44  * THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
47  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
48  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
49  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
50  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
51  *
52  * @EulaEnd@
53  */
54 ////////////////////////////////////////////////////////////////////////////////
55 
56 #include <stdio.h>
57 #include <unistd.h>
58 #include <stdlib.h>
59 #include <ctype.h>
60 
61 #include <sstream>
62 #include <string>
63 #include <locale>
64 #include <vector>
65 
66 #include "rnr/rnrconfig.h"
67 #include "rnr/log.h"
68 
70 
71 using namespace std;
72 
73 namespace rnr
74 {
75  namespace str
76  {
77  const char *FalseHood[] =
78  {
79  "0", "false", "f", "off", "low", "disable", "no", NULL
80  };
81 
82  const char *TruthHood[] =
83  {
84  "1", "true", "t", "on", "high", "enable", "yes", NULL
85  };
86 
87  size_t split(const string &str, const char delim, StringVec &elems)
88  {
89  size_t n = elems.size();
90  stringstream ss;
91  string item;
92 
93  ss.str(str);
94 
95  while(std::getline(ss, item, delim) )
96  {
97  elems.push_back(item);
98  }
99 
100  return elems.size() - n;
101  }
102 
103  StringVec split(const string &str, const char delim)
104  {
105  StringVec elems;
106 
107  split(str, delim, elems);
108 
109  return elems;
110  }
111 
112  string &replace(const string &what, const string &with, string &str)
113  {
114  size_t len = what.length();
115  size_t n;
116 
117  // no search for string or with has a what
118  if( (len == 0) || (with.find(what) != string::npos) )
119  {
120  return str;
121  }
122 
123  // replace all whats
124  while( (n = str.find(what)) != string::npos )
125  {
126  str.replace(n, len, with);
127  }
128 
129  return str;
130  }
131 
132  string lowercase(const string &str)
133  {
134  std::locale loc;
135  string lower;
136 
137  for(size_t i = 0; i < str.size(); ++i)
138  {
139  lower.push_back(std::tolower(str[i], loc));
140  }
141 
142  return lower;
143  }
144 
145  string uppercase(const string &str)
146  {
147  std::locale loc;
148  string upper;
149 
150  for(size_t i = 0; i < str.size(); ++i)
151  {
152  upper.push_back(std::toupper(str[i], loc));
153  }
154 
155  return upper;
156  }
157 
158  size_t gcss(const string &str1, const string &str2, const size_t pos)
159  {
160  size_t i;
161  size_t len = 0;
162 
163  for(i = pos; (i < str1.size()) && (i < str2.size()); ++i)
164  {
165  if( str1[i] == str2[i] )
166  {
167  ++len;
168  }
169  else
170  {
171  break;
172  }
173  }
174 
175  return len;
176  }
177 
178  int tobool(const string &str, bool &val)
179  {
180  size_t i;
181 
182  for(i = 0; FalseHood[i] != NULL; ++i)
183  {
184  if( str == FalseHood[i] )
185  {
186  val = false;
187  return OK;
188  }
189  }
190 
191  for(i = 0; TruthHood[i] != NULL; ++i)
192  {
193  if( str == TruthHood[i] )
194  {
195  val = true;
196  return OK;
197  }
198  }
199 
200  return RC_ERROR;
201  }
202 
203  int tolong(const string &str, long &val)
204  {
205  char sniff;
206 
207  return sscanf(str.c_str(), "%li%c", &val, &sniff) == 1? OK: RC_ERROR;
208  }
209 
210  int todouble(const string &str, double &val)
211  {
212  char sniff;
213 
214  return sscanf(str.c_str(), "%lf%c", &val, &sniff) == 1? OK: RC_ERROR;
215  }
216 
217  string prettify(const string &str)
218  {
219  string realpurdy;
220  bool quoteit = false;
221 
222  for(size_t i = 0; i < str.length(); ++i)
223  {
224  switch( str[i] )
225  {
226  case '"':
227  quoteit = true;
228  realpurdy.append("\\\"");
229  break;
230  case ' ':
231  quoteit = true;
232  realpurdy.append(" ");
233  break;
234  case '\t':
235  quoteit = true;
236  realpurdy.append("\\t");
237  break;
238  case '\n':
239  quoteit = true;
240  realpurdy.append("\\n");
241  break;
242  case '\r':
243  quoteit = true;
244  realpurdy.append("\\r");
245  break;
246  case '\v':
247  quoteit = true;
248  realpurdy.append("\\v");
249  break;
250  case '\f':
251  quoteit = true;
252  realpurdy.append("\\f");
253  break;
254  case '\\':
255  quoteit = true;
256  realpurdy.append("\\");
257  break;
258  default:
259  if( (str[i] < 0x21) || (str[i] > 0x7e) )
260  {
261  unsigned hh = (unsigned)str[i];
262  char buf[8];
263 
264  sprintf(buf, "\\x%02x", hh);
265  realpurdy.append(buf);
266  quoteit = true;
267  }
268  else
269  {
270  realpurdy.push_back(str[i]);
271  }
272  break;
273  }
274  }
275 
276  if( quoteit )
277  {
278  realpurdy.insert(realpurdy.begin(), '"');
279  realpurdy.insert(realpurdy.end(), '"');
280  }
281 
282  return realpurdy;
283  }
284 
285  string c14n(const string &str)
286  {
287  string strc14n(str);
288  size_t pos;
289 
290  // strip leading and trailing whitespace
291  trim(strc14n);
292 
293  return prettify(strc14n);
294  }
295 
296  string c14n(const StringVec &tokens)
297  {
298  stringstream ss;
299  string sep;
300 
301  for(size_t i = 0; i < tokens.size(); ++i)
302  {
303  ss << sep << prettify(tokens[i]);
304 
305  if( i == 0 )
306  {
307  sep = " ";
308  }
309  }
310 
311  return ss.str();
312  }
313 
314  } // namespace str
315 } // namespace rnr
int tolong(const std::string &str, long &val)
Convert string to a long integer.
std::vector< std::string > StringVec
Useful types.
Definition: StringTheory.h:83
int tobool(const std::string &str, bool &val)
Convert string to boolean.
std::string lowercase(const std::string &str)
Convert in-place string to lower case.
const char * TruthHood[]
strings that equate to true
Of string spaces and their strangian operators.
size_t split(const std::string &str, const char delim, StringVec &elems)
Split string.
int todouble(const std::string &str, double &val)
Convert string to a double-precision floating-point number.
std::string & trim(std::string &str)
Trim string in-place of leading and trailing whitespace.
Definition: StringTheory.h:197
std::string prettify(const std::string &str)
Prettify string.
std::string uppercase(const std::string &str)
Convert string to upper case.
std::string & replace(const std::string &what, const std::string &with, std::string &str)
In-place replace all whats in string with with.
std::string c14n(const std::string &str)
Simple canonicalization of a string.
RoadNarrows Robotics.
Definition: Camera.h:74
size_t gcss(const std::string &str1, const std::string &str2, const size_t pos=0)
Find the length of the Greatest Common SubString.
const char * FalseHood[]
Falsehood and truthhood strings. Each list termintate with a NULL.