byteman  1.3 (Build #225)
Bitstream relocation and manipulation tool
parse.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2022 Kristiyan Manev (University of Manchester)
3  *
4  * Licensed under the Apache License, Version 2.0(the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #ifndef STR_PARSE_H
18 #define STR_PARSE_H
19 
20 #include<cstdint> //uint
21 #include<string>
22 #include<iomanip> //quoted
23 #include<sstream>
24 
25 /**************************************************************************/
30 namespace str{
31  namespace parse {
33  inline std::string nthStringWord(std::string s, int n)
34  {
35  std::stringstream ss(s);
36  std::string temp;
37  int intFound, i = 0;
38  while (!ss.eof()) {
39  ss >> quoted(temp);
40  if(temp.empty()) break;
41  try{
42  size_t sz;
43  intFound = stoi(temp, &sz, 0);
44  if(temp.size() != sz)
45  throw std::exception();
46  } catch (const std::exception &e) {
47  if(i == n)
48  return temp;
49  i++;
50  e;
51  }
52  temp.clear();
53  }
54  return std::string("");
55  }
57  inline std::string allStringWords(std::string s)
58  {
59  std::stringstream ss(s);
60  std::string temp, ret = "";
61  int intFound;
62  while (!ss.eof()) {
63  ss >> quoted(temp);
64  if(temp.empty()) break;
65  try{
66  size_t sz;
67  intFound = stoi(temp, &sz, 0);
68  if(temp.size() != sz)
69  throw std::exception();
70  } catch (const std::exception &e) {
71  if(!ret.empty())
72  ret.append(" ");
73  ret.append(temp);
74  e;
75  }
76  temp.clear();
77  }
78  return ret;
79  }
81  inline std::string lastStringWord(std::string s)
82  {
83  std::stringstream ss(s);
84  std::string ret;
85  ret.clear();
86  std::string temp;
87  int intFound;
88  while (!ss.eof()) {
89  ss >> quoted(temp);
90  if(temp.empty()) break;
91  try{
92  size_t sz;
93  intFound = stoi(temp, &sz, 0);
94  if(temp.size() != sz)
95  throw std::exception();
96  } catch (const std::exception &e) {
97  ret = temp;
98  e;
99  }
100  temp.clear();
101  }
102  return ret;
103  }
105  inline std::string allStringWordsWithoutLastStringWord(std::string s)
106  {
107  std::stringstream ss(s);
108  std::string ret;
109  ret.clear();
110  std::string temp;
111  std::string temp2;
112  int intFound;
113  while (!ss.eof()) {
114  ss >> quoted(temp);
115  if(temp.empty()) break;
116  try{
117  size_t sz;
118  intFound = stoi(temp, &sz, 0);
119  if(temp.size() != sz)
120  throw std::exception();
121  } catch (const std::exception &e) {
122  if(!ret.empty())
123  ret.append(" ");
124  ret.append(temp2);
125  temp2 = temp;
126  e;
127  }
128  temp.clear();
129  }
130  return ret;
131  }
133  inline bool nthInteger(std::string s, int n, int &x)
134  {
135  std::stringstream ss(s);
136  std::string temp;
137  int intFound;
138  int i = 0;
139  while (!ss.eof()) {
140  ss >> quoted(temp);
141  if(temp.empty()) break;
142  try{
143  size_t sz;
144  intFound = stoi(temp, &sz, 0);
145  if(temp.size() != sz)
146  throw std::exception();
147  if(i == n) {
148  x = intFound;
149  return true;
150  }
151  i++;
152  } catch (const std::exception &e) {
153  e;
154  }
155  temp.clear();
156  }
157  return false;
158  }
160  inline bool arrayOfUints(std::string s, int arrsize, uint32_t* arr)
161  {
162  if (arrsize == 0)
163  return true;
164  std::stringstream ss(s);
165  std::string temp;
166  uint32_t intFound;
167  bool intWasFound = false;
168  for (int i = 0 ; i < arrsize ; i++)
169  {
170  intWasFound = false;
171  while ((!ss.eof()) && (!intWasFound)) {
172  ss >> quoted(temp);
173  if(temp.empty()) break;
174  try{
175  size_t sz;
176  intFound = (uint32_t)(stoll(temp, &sz, 0));
177  if(temp.size() != sz)
178  throw std::exception();
179  arr[i] = intFound;
180  intWasFound = true;
181  } catch (const std::exception &e) {
182  e;
183  }
184  temp.clear();
185  }
186  if(ss.eof() && (!intWasFound))
187  return false;
188  }
189  return true; //success
190  }
191 
192  template<typename ... Rest> inline bool multipleInts(std::stringstream & ss)
193  {
194  return true;
195  }
197  template<typename ... Rest> inline bool multipleInts(std::stringstream & ss, int & x, Rest & ... args)
198  {
199  int numArgs = sizeof...(args);
200  std::string temp;
201  int intFound;
202  bool intWasFound = false;
203  while ((!ss.eof()) && (!intWasFound)) {
204  ss >> quoted(temp);
205  if(temp.empty()) break;
206  try{
207  size_t sz;
208  intFound = stoi(temp, &sz, 0);
209  if(temp.size() != sz)
210  throw std::exception();
211  x = intFound;
212  intWasFound = true;
213  } catch (const std::exception &e) {
214  e;
215  }
216  temp.clear();
217  }
218  if(ss.eof() && (!intWasFound))
219  return false;
220  return multipleInts(ss, args...);
221  }
223  template<typename ... Args> inline bool multipleInts(std::string s, Args & ... args)
224  {
225  std::stringstream ss(s);
226  return multipleInts(ss, args...);
227  }
228 
229  template<typename ... Rest> inline bool multipleUints(std::stringstream & ss)
230  {
231  return true;
232  }
234  template<typename ... Rest> inline bool multipleUints(std::stringstream & ss, uint32_t & x, Rest & ... args)
235  {
236  int numArgs = sizeof...(args);
237  std::string temp;
238  unsigned long uintFound;
239  bool intWasFound = false;
240  while ((!ss.eof()) && (!intWasFound)) {
241  ss >> quoted(temp);
242  if(temp.empty()) break;
243  try{
244  size_t sz;
245  uintFound = stoul(temp, &sz, 0);
246  if(temp.size() != sz)
247  throw std::exception();
248  x = (uint32_t)uintFound;
249  intWasFound = true;
250  } catch (const std::exception &e) {
251  e;
252  }
253  temp.clear();
254  }
255  if(ss.eof() && (!intWasFound))
256  return false;
257  return multipleUints(ss, args...);
258  }
260  template<typename ... Args> inline bool multipleUints(std::string s, Args & ... args)
261  {
262  std::stringstream ss(s);
263  return multipleUints(ss, args...);
264  }
265  }
266 }
267 #endif //STR_PARSE_H
std::string lastStringWord(std::string s)
Parses a string s, removes all integers and returns the last of all string words.
Definition: parse.h:81
bool arrayOfUints(std::string s, int arrsize, uint32_t *arr)
Removes all string words from a given string s and returns the parsed arrsize number of integers into...
Definition: parse.h:160
bool multipleUints(std::stringstream &ss)
Definition: parse.h:229
bool nthInteger(std::string s, int n, int &x)
Parses a string s, returns the n-th integer.
Definition: parse.h:133
std::string allStringWordsWithoutLastStringWord(std::string s)
Parses a string s, removes all integers and the last string word. Returns the rest.
Definition: parse.h:105
std::string allStringWords(std::string s)
Parses a string s, removes all integers and returns the rest.
Definition: parse.h:57
bool multipleInts(std::stringstream &ss)
Definition: parse.h:192
std::string nthStringWord(std::string s, int n)
Parses a string s, returns the n-th string word that is not an integer.
Definition: parse.h:33
Definition: iff.h:29