Cantera  2.0
ckr_utils.cpp
Go to the documentation of this file.
1 /**
2  * @file ckr_utils.cpp
3  *
4  */
5 
6 // Copyright 2001 California Institute of Technology
7 
8 #include <ctype.h>
9 
10 #include <math.h>
11 #include "ckr_utils.h"
12 #include <string.h>
13 
14 using namespace std;
15 
16 namespace ckr
17 {
18 
19 
20 bool match(const std::string& s1, const std::string& s2)
21 {
22  size_t n = s2.size();
23  if (s1.size() < n) {
24  return false;
25  }
26  for (size_t i = 0; i < n; i++)
27  if (s2[i] != '*' && (toupper(s1[i]) != toupper(s2[i]))) {
28  return false;
29  }
30  return true;
31 }
32 
33 
34 void removeWhiteSpace(std::string& s)
35 {
36  string r;
37  int ssize = static_cast<int>(s.size());
38  for (int n = 0; n < ssize; n++) if (s[n] != ' '
39  && s[n] != '\t' && s[n] != '\n') {
40  r += s[n];
41  }
42  s = r;
43 }
44 
45 /**
46  * Get tokens from char array of length n beginning at 'begin'. Tokens are
47  * delimited by character 'delim' (space by default), with the exception of
48  * text contained within forward slashes ('/'), which is treated literally.
49  *
50  * code
51  * vector<string> tokens;
52  * char line[] = "a b/3.0 txt/ c /4.0 6/ d";
53  * int n = strlen(line);
54  * getTokens(line, n, tokens);
55  * for (int i = 0; i < tokens.size(); i++) cout << tokens[i] << endl;
56  * endcode
57  *
58  */
59 
60 void getTokens(std::string& s, int n, std::vector<std::string>& toks, char delim)
61 {
62  string::iterator q, p = s.begin(), end = p + n;
63  vector<string> tokk;
64  int inslash = -1;
65 
66  //p = begin;
67  while (1 > 0) {
68  for (; p < end; p++) {
69  if (*p != delim) {
70  break;
71  }
72  }
73  q = p;
74  for (; q < end; q++) {
75  if (*q == '/') {
76  inslash *= -1;
77  }
78  if (inslash < 0 && *q == delim) {
79  break;
80  }
81  }
82  if (p != q) {
83  tokk.push_back(s.substr(p - s.begin(), q - p));
84  }
85  p = q;
86  if (p == end) {
87  break;
88  }
89  }
90 
91  toks.clear();
92  int nt = static_cast<int>(tokk.size());
93  string t = "";
94  for (int i = 0; i < nt; i++) {
95  if (tokk[i][0] == '/') {
96  t += tokk[i];
97  } else {
98  if (t != "") {
99  toks.push_back(t);
100  }
101  t = tokk[i];
102  }
103  }
104  if (t != "") {
105  toks.push_back(t);
106  }
107 }
108 
109 
110 /**
111  * Look for a slash-delimited number in string s,
112  * and if found set v to the numerical value, and
113  * set s to the portion of the string before the
114  * first slash. Return true if slash data found,
115  * false otherwise.
116  */
117 bool extractSlashData(std::string& s, std::string& name, std::string& data)
118 {
119  int slen = static_cast<int>(s.size());
120  string::size_type n = s.find_first_of("/");
121  if (n != string::npos && (static_cast<int>(n) < slen)) {
122  int m;
123  for (m = static_cast<int>(n)+1; m < slen; m++) if (s[m] == '/') {
124  break;
125  }
126  if (m < slen) {
127  data = s.substr(n+1,m-n-1);
128  name = s.substr(0,n);
129  removeWhiteSpace(name);
130  s = s.substr(m+1,1000);
131  return true;
132  } else {
133  name = s;
134  removeWhiteSpace(name);
135  data = "";
136  s = "";
137  return false;
138  }
139  } else {
140  name = s;
141  removeWhiteSpace(name);
142  data = "";
143  s = "";
144  return false;
145  }
146 }
147 
148 /**
149  * Return a modified version of string word, in which
150  * the first letter is upper case, and the rest are
151  * lower case.
152  */
153 string capitalize(const std::string& word)
154 {
155  string cap = word;
156  int n = static_cast<int>(word.size());
157  if (n > 0) {
158  cap[0] = (char) toupper(word[0]);
159  for (int m = 1; m < n; m++) {
160  cap[m] = (char) tolower(word[m]);
161  }
162  }
163  return cap;
164 }
165 
166 }
167