COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
Collaboration diagram for String handling functions:

Classes

class  StringParser
 

Functions

vector< string > string_split (string in, string delimeters)
 Parse a string. More...
 
uint16_t string_parse (char *string, char *words[], uint16_t wmax)
 Parse a string into words. More...
 
int string_cmp (const char *wild, const char *string)
 
string to_hex_string (vector< uint8_t > buffer, bool ascii)
 
string to_string (char *value)
 
string to_hex (int64_t value, uint16_t digits, bool zerofill)
 
string to_signed (int64_t value, uint16_t digits, bool zerofill)
 
string to_unsigned (uint64_t value, uint16_t digits, bool zerofill)
 
string to_double (double value, uint16_t precision)
 
string to_mjd (double value)
 
string to_temperature (double value, char units, uint8_t precision)
 
string to_angle (double value, char units, uint8_t precision)
 
string to_bool (bool value)
 
string to_json (string key, string value)
 
string to_json (string key, double value)
 
string to_json (string key, int64_t value)
 
string to_json (string key, int32_t value)
 
string to_json (string key, int16_t value)
 
string to_json (string key, int8_t value)
 
string to_json (string key, uint64_t value)
 
string to_json (string key, uint32_t value)
 
string to_json (string key, uint16_t value)
 
string to_json (string key, uint8_t value)
 
string to_label (string label, double value, uint16_t precision, bool mjd)
 
string to_label (string label, uint64_t value, uint16_t digits, bool hex)
 
string to_label (string label, uint32_t value, uint16_t digits, bool hex)
 
string to_label (string label, uint16_t value, uint16_t digits, bool hex)
 
string to_label (string label, uint8_t value, uint16_t digits, bool hex)
 
string to_label (string label, int64_t value, uint16_t digits, bool hex)
 
string to_label (string label, int32_t value, uint16_t digits, bool hex)
 
string to_label (string label, int16_t value, uint16_t digits, bool hex)
 
string to_label (string label, int8_t value, uint16_t digits, bool hex)
 
string to_label (string label, bool value)
 
string to_label (string label, string value)
 
string clean_string (string value)
 
 StringParser::StringParser (string str)
 
 StringParser::StringParser (string str, char delimiter)
 
void StringParser::splitString (string str, char delimiter)
 
string StringParser::getFieldNumber (uint32_t index)
 
double StringParser::getFieldNumberAsDouble (uint32_t index)
 
int StringParser::getFieldNumberAsInteger (uint32_t index)
 

Detailed Description

Function Documentation

vector< string > string_split ( string  in,
string  delimeters 
)

Parse a string.

Divide a string into substrings based on a delimeter and return a vector of the results.

Parameters
inZero terminated character string.
delimeterstring of delimeters.
Returns
vector of sub strings.
47  {
48  vector<string> result;
49  const char *str = in.data();
50  do {
51  const char *begin = str;
52  while(*str) {
53  bool match = false;
54  for (size_t i=0; i<delimeters.size(); ++i) {
55  if (*str == delimeters[i]) {
56  match = true;
57  break;
58  }
59  }
60  if (match) {
61  break;
62  }
63  str++;
64  }
65  result.push_back(string(begin, str));
66  } while (0 != *str++);
67  return result;
68 }
int i
Definition: rw_test.cpp:37
const string & str
Definition: json11.cpp:360
uint16_t string_parse ( char *  string,
char *  words[],
uint16_t  wmax 
)

Parse a string into words.

Divide a string into words separated by white space and return an array of the results.

Parameters
stringZero terminated character string.
wordsEmpty array for storage of substrings.
wmaxmaximum number of words that can be stored in words array.
Returns
Number of words
78  {
79  uint16_t wcount, ccount, i;
80  if (string == NULL) return 0;
81 
82  wcount = ccount = i = 0;
83  while (string[i] == ' ' || string[i] == '\t') {
84  i++;
85  }
86 
87  if (string[i] == 0) return 0;
88 
89  while (string[i] != 0) {
90  if (string[i] == ' ' || string[i] == '\t') {
91  words[wcount][ccount] = 0;
92  ccount = 0;
93  wcount++;
94  if (wcount == wmax-1) break;
95  do {
96  i++;
97  } while (string[i] == ' ' || string[i] == '\t');
98  } else {
99  if (!ccount)
100  words[wcount] = &string[i];
101  ccount++;
102  i++;
103  }
104  }
105  if (ccount) wcount++;
106  words[wcount] = (char *)nullptr;
107  return (wcount);
108 }
int i
Definition: rw_test.cpp:37
int string_cmp ( const char *  wild,
const char *  string 
)
110  {
111  // Written by Jack Handy - <A href="mailto:jakkhandy@hotmail.com">jakkhandy@hotmail.com</A>
112  const char *cp = NULL, *mp = NULL;
113 
114  while ((*string) && (*wild != '*')) {
115  if ((*wild != *string) && (*wild != '?')) { return 0; }
116  wild++;
117  string++;
118  }
119 
120  while (*string) {
121  if (*wild == '*') {
122  if (!*++wild) { return 1; }
123  mp = wild;
124  cp = string+1;
125  } else if ((*wild == *string) || (*wild == '?')) {
126  wild++;
127  string++;
128  } else {
129  wild = mp;
130  string = cp++;
131  }
132  }
133 
134  while (*wild == '*') {
135  wild++;
136  }
137  return !*wild;
138 }
string to_hex_string ( vector< uint8_t >  buffer,
bool  ascii 
)
204  {
205  string output;
206  output.resize(buffer.size() * 4);
207  for (uint16_t i=0; i<buffer.size(); ++i) {
208  if (ascii && buffer[i] > 31 && buffer[i] < 127)
209  {
210  sprintf(&output[strlen(output.c_str())], " %02x(%c)", buffer[i], buffer[i]);
211  }
212  else
213  {
214  sprintf(&output[strlen(output.c_str())], " %02x", buffer[i]);
215  }
216  }
217  return output;
218 }
int i
Definition: rw_test.cpp:37
string output
Definition: agent-2-0.cpp:56
static char buffer[255]
Definition: propagator_simple.cpp:60
string to_string ( char *  value)
220  {
221  string output = value;
222  return output;
223 }
string output
Definition: agent-2-0.cpp:56
string to_hex ( int64_t  value,
uint16_t  digits,
bool  zerofill 
)
225  {
226  string output="";
227  output.resize(digits?digits+2:20);
228  if (zerofill) {
229  if (digits) {
230  sprintf(&output[0], "%0*lx", digits, value);
231  } else {
232  sprintf(&output[0], "%0lx", value);
233  }
234  } else {
235  if (digits) {
236  sprintf(&output[0], "%*lx", digits, value);
237  } else {
238  sprintf(&output[0], "%lx", value);
239  }
240  }
241  output.resize(strlen(&output[0]));
242  return output;
243 }
string output
Definition: agent-2-0.cpp:56
string to_signed ( int64_t  value,
uint16_t  digits,
bool  zerofill 
)
245  {
246  string output="";
247  output.resize((value==0?0:size_t(log10(std::abs(value))))+digits+5);
248  if (zerofill) {
249  if (digits) {
250  sprintf(&output[0], "%0*ld", digits, value);
251  } else {
252  sprintf(&output[0], "%0ld", value);
253  }
254  } else {
255  if (digits) {
256  sprintf(&output[0], "%*ld", digits, value);
257  } else {
258  sprintf(&output[0], "%ld", value);
259  }
260  }
261  output.resize(strlen(&output[0]));
262  return output;
263 }
string output
Definition: agent-2-0.cpp:56
string to_unsigned ( uint64_t  value,
uint16_t  digits,
bool  zerofill 
)
265  {
266  string output="";
267  output.resize((value==0?0:size_t(log10((value))))+digits+5);
268  if (zerofill) {
269  if (digits) {
270  sprintf(&output[0], "%0*lu", digits, value);
271  } else {
272  sprintf(&output[0], "%0lu", value);
273  }
274  } else {
275  if (digits) {
276  sprintf(&output[0], "%*lu", digits, value);
277  } else {
278  sprintf(&output[0], "%lu", value);
279  }
280  }
281  output.resize(strlen(&output[0]));
282  return output;
283 }
string output
Definition: agent-2-0.cpp:56
string to_double ( double  value,
uint16_t  precision 
)
285  {
286  string output="";
287 // output.resize((value==0.?0:size_t(log10(std::abs(value))))+precision+5);
288  output.resize(17+precision);
289  if (precision) {
290  sprintf(&output[0], "%.*g", precision, value);
291  } else {
292  sprintf(&output[0], "%g", value);
293  }
294  output.resize(strlen(&output[0]));
295  return output;
296 }
string output
Definition: agent-2-0.cpp:56
string to_mjd ( double  value)
298 { return to_double(value, 13); }
string to_double(double value, uint16_t precision)
Definition: stringlib.cpp:285
string to_temperature ( double  value,
char  units,
uint8_t  precision 
)
301 {
302  switch (units) {
303  case 'K':
304  return to_double(value, precision) + " K";
305  case 'C':
306  return to_double(value - 273.15, precision) + " C";
307  case 'F':
308  return to_double((value - 273.15) / 1.8 + 32., precision) + " F";
309  }
310  return "";
311 }
string to_double(double value, uint16_t precision)
Definition: stringlib.cpp:285
string to_angle ( double  value,
char  units,
uint8_t  precision 
)
313  {
314  switch (units)
315  {
316  case 'R':
317  return to_double(value, precision);
318  case 'D':
319  return to_double(DEGOF(value), precision) + 'D';
320  case 'A':
321  return to_double(value / DAS2R, precision) + '\'';
322  }
323  return "";
324 }
const double DAS2R
Multiplicand for Seconds of Arc to Radians.
Definition: math/constants.h:22
#define DEGOF(rad)
Degrees of a Radian value.
Definition: math/constants.h:33
string to_double(double value, uint16_t precision)
Definition: stringlib.cpp:285
string to_bool ( bool  value)
326  {
327  string output="";
328  output.resize(2);
329  output[0] = value?'1':'0';
330  output.resize(strlen(&output[0]));
331  return output;
332 }
string output
Definition: agent-2-0.cpp:56
string to_json ( string  key,
string  value 
)
334  {
335  JSONObject jobject;
336  jobject.addElement(key, value);
337  return jobject.to_json_object();
338 }
Definition: jsonobject.h:5
string to_json_object()
Definition: jsonobject.cpp:80
void addElement(string key, JSONValue value)
Definition: jsonobject.cpp:10
string to_json ( string  key,
double  value 
)
340  {
341  JSONObject jobject;
342  jobject.addElement(key, value);
343  return jobject.to_json_object();
344 }
Definition: jsonobject.h:5
string to_json_object()
Definition: jsonobject.cpp:80
void addElement(string key, JSONValue value)
Definition: jsonobject.cpp:10
string to_json ( string  key,
int64_t  value 
)
346  {
347  JSONObject jobject;
348  jobject.addElement(key, value);
349  return jobject.to_json_object();
350 }
Definition: jsonobject.h:5
string to_json_object()
Definition: jsonobject.cpp:80
void addElement(string key, JSONValue value)
Definition: jsonobject.cpp:10
string to_json ( string  key,
int32_t  value 
)
352  {
353  return to_json(key, static_cast<int64_t>(value));
354 }
string to_json(string key, string value)
Definition: stringlib.cpp:334
string to_json ( string  key,
int16_t  value 
)
356  {
357  return to_json(key, static_cast<int64_t>(value));
358 }
string to_json(string key, string value)
Definition: stringlib.cpp:334
string to_json ( string  key,
int8_t  value 
)
360  {
361  return to_json(key, static_cast<int64_t>(value));
362 }
string to_json(string key, string value)
Definition: stringlib.cpp:334
string to_json ( string  key,
uint64_t  value 
)
364  {
365  JSONObject jobject;
366  jobject.addElement(key, value);
367  return jobject.to_json_object();
368 }
Definition: jsonobject.h:5
string to_json_object()
Definition: jsonobject.cpp:80
void addElement(string key, JSONValue value)
Definition: jsonobject.cpp:10
string to_json ( string  key,
uint32_t  value 
)
370 { return to_json(key, static_cast<uint64_t>(value)); }
string to_json(string key, string value)
Definition: stringlib.cpp:334
string to_json ( string  key,
uint16_t  value 
)
372 { return to_json(key, static_cast<uint64_t>(value)); }
string to_json(string key, string value)
Definition: stringlib.cpp:334
string to_json ( string  key,
uint8_t  value 
)
374 { return to_json(key, static_cast<uint64_t>(value)); }
string to_json(string key, string value)
Definition: stringlib.cpp:334
string to_label ( string  label,
double  value,
uint16_t  precision,
bool  mjd 
)
376  {
377  if (mjd) {
378  return label + ": " + to_mjd(value);
379  } else {
380  return label + ": " + to_double(value, precision);
381  }
382 }
double mjd
Definition: udp_send.cpp:41
string to_mjd(double value)
Definition: stringlib.cpp:298
string to_double(double value, uint16_t precision)
Definition: stringlib.cpp:285
string to_label ( string  label,
uint64_t  value,
uint16_t  digits,
bool  hex 
)
384  {
385  if (hex) {
386  return label + ": " + to_hex(value, digits);
387  } else {
388  return label + ": " + to_unsigned(value, digits);
389  }
390 }
string to_unsigned(uint64_t value, uint16_t digits, bool zerofill)
Definition: stringlib.cpp:265
string to_hex(int64_t value, uint16_t digits, bool zerofill)
Definition: stringlib.cpp:225
string to_label ( string  label,
uint32_t  value,
uint16_t  digits,
bool  hex 
)
392  {
393  return to_label(label, static_cast<uint64_t>(value), digits, hex);
394 }
string to_label(string label, double value, uint16_t precision, bool mjd)
Definition: stringlib.cpp:376
string to_label ( string  label,
uint16_t  value,
uint16_t  digits,
bool  hex 
)
396  {
397  return to_label(label, static_cast<uint64_t>(value), digits, hex);
398 }
string to_label(string label, double value, uint16_t precision, bool mjd)
Definition: stringlib.cpp:376
string to_label ( string  label,
uint8_t  value,
uint16_t  digits,
bool  hex 
)
400  {
401  return to_label(label, static_cast<uint64_t>(value), digits, hex);
402 }
string to_label(string label, double value, uint16_t precision, bool mjd)
Definition: stringlib.cpp:376
string to_label ( string  label,
int64_t  value,
uint16_t  digits,
bool  hex 
)
404  {
405  if (hex) {
406  return label + ": " + to_hex(value, digits);
407  } else {
408  return label + ": " + to_signed(value, digits);
409  }
410 }
string to_signed(int64_t value, uint16_t digits, bool zerofill)
Definition: stringlib.cpp:245
string to_hex(int64_t value, uint16_t digits, bool zerofill)
Definition: stringlib.cpp:225
string to_label ( string  label,
int32_t  value,
uint16_t  digits,
bool  hex 
)
412  {
413  return to_label(label, static_cast<int64_t>(value), digits, hex);
414 }
string to_label(string label, double value, uint16_t precision, bool mjd)
Definition: stringlib.cpp:376
string to_label ( string  label,
int16_t  value,
uint16_t  digits,
bool  hex 
)
416  {
417  return to_label(label, static_cast<int64_t>(value), digits, hex);
418 }
string to_label(string label, double value, uint16_t precision, bool mjd)
Definition: stringlib.cpp:376
string to_label ( string  label,
int8_t  value,
uint16_t  digits,
bool  hex 
)
420  {
421  return to_label(label, static_cast<int64_t>(value), digits, hex);
422 }
string to_label(string label, double value, uint16_t precision, bool mjd)
Definition: stringlib.cpp:376
string to_label ( string  label,
bool  value 
)
424  {
425  return label + ": " + to_bool(value);
426 }
string to_bool(bool value)
Definition: stringlib.cpp:326
string to_label ( string  label,
string  value 
)
428  {
429  return label + ": " + (value);
430 }
string clean_string ( string  value)
432  {
433  string output;
434  for (uint16_t i=0; i<value.length(); ++i) {
435  if (value[i] != 0) {
436  output.push_back(value[i]);
437  printf("%c", value[i]);
438  } else {
439  printf(" [0] ");
440  }
441  }
442  printf("\n");
443  output.push_back(0);
444  return output;
445 }
int i
Definition: rw_test.cpp:37
string output
Definition: agent-2-0.cpp:56
StringParser::StringParser ( string  str)
143 { splitString(str, ','); }
void splitString(string str, char delimiter)
Definition: stringlib.cpp:150
const string & str
Definition: json11.cpp:360
StringParser::StringParser ( string  str,
char  delimiter 
)
void splitString(string str, char delimiter)
Definition: stringlib.cpp:150
const string & str
Definition: json11.cpp:360
delimiter
Definition: inputfile.py:6
void StringParser::splitString ( string  str,
char  delimiter 
)
151 {
152  std::stringstream ss(str);
153  string token;
154 
155  while(std::getline(ss, token, delimiter)) {
156  //std::cout << token << '\n';
157  vect.push_back(token);
158  }
159 
160  numberOfFields = vect.size();
161 
162  // default offset so that when we want to get the 1st entry we just to getFieldNumber(1)
163  offset = -1;
164 }
const string & str
Definition: json11.cpp:360
vector< string > vect
Definition: stringlib.h:91
size_t numberOfFields
Definition: stringlib.h:103
delimiter
Definition: inputfile.py:6
int offset
Definition: stringlib.h:97
string StringParser::getFieldNumber ( uint32_t  index)
170  {
171  string out;
172  uint32_t real_offset = index + offset;
173 
174  if ( index>0 && numberOfFields >= (real_offset) && real_offset < vect.size() ){
175  out = vect.at(real_offset);
176  } else { // fail safe
177  return "";
178  }
179  return out;
180 }
vector< string > vect
Definition: stringlib.h:91
size_t numberOfFields
Definition: stringlib.h:103
int offset
Definition: stringlib.h:97
double StringParser::getFieldNumberAsDouble ( uint32_t  index)
185 {
186  double out;
187  if ( (index > 0) && (numberOfFields >= index)){
188  try {
189  out = stod(vect.at(index + offset));
190  //std::cout <<out << std::endl;
191  } catch (const std::exception& e){
192  std::cout << "error parsing string:" << e.what() << std::endl;
193  return 0;
194  }
195  } else { // fail safe
196  return 0;
197  }
198 
199  return out;
200 }
Definition: eci2kep_test.cpp:33
vector< string > vect
Definition: stringlib.h:91
size_t numberOfFields
Definition: stringlib.h:103
int offset
Definition: stringlib.h:97
int StringParser::getFieldNumberAsInteger ( uint32_t  index)
202 { return getFieldNumberAsDouble(index); }
double getFieldNumberAsDouble(uint32_t index)
Definition: stringlib.cpp:184