antioch-0.4.0
string_utils.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // Antioch - A Gas Dynamics Thermochemistry Library
5 //
6 // Copyright (C) 2014-2016 Paul T. Bauman, Benjamin S. Kirk,
7 // Sylvain Plessis, Roy H. Stonger
8 //
9 // Copyright (C) 2013 The PECOS Development Team
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the Version 2.1 GNU Lesser General
13 // Public License as published by the Free Software Foundation.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
23 // Boston, MA 02110-1301 USA
24 //
25 //-----------------------------------------------------------------------el-
26 
27 #ifndef ANTIOCH_STRING_UTILS_H
28 #define ANTIOCH_STRING_UTILS_H
29 
30 // Antioch
32 // some enum
33 #include "antioch/kinetics_enum.h"
34 #include "antioch/reaction_enum.h"
35 
36 // C++
37 #include <iostream>
38 #include <string>
39 #include <vector>
40 #include <sstream>
41 
42 namespace Antioch
43 {
45  void split_string( const std::string& input,
46  const std::string& delimiter,
47  std::vector<std::string>& results );
48 
49 
51 
53  void remove_newline_from_strings( std::vector<std::string>& strings );
54 
55  template <typename T>
56  inline
57  T string_to_T(const std::string& input)
58  {
59  std::istringstream converter(input);
60  T returnval;
61  converter >> returnval;
62  if (converter.fail())
63  antioch_error();
64  return returnval;
65  }
66 
67  template <typename Type>
68  inline
69  std::pair<std::string, Type> split_string_on_colon(const std::string &token)
70  {
71  std::pair<std::string, Type> ret = std::make_pair(std::string(), 0);
72  std::string::size_type colon_position = token.find(":");
73  antioch_assert (colon_position != std::string::npos);
74  ret.first = token.substr(0, colon_position);
75  ret.second = string_to_T<Type>(token.substr(colon_position + 1));
76  return ret;
77  }
78 
82  inline
83  int SplitString(const std::string& input,
84  const std::string& delimiter,
85  std::vector<std::string>& results,
86  bool includeEmpties = true)
87  {
88  using std::vector;
89  using std::string;
90 
91  int iPos = 0;
92  int newPos = -1;
93  int sizeS2 = (int)delimiter.size();
94  int isize = (int)input.size();
95 
96  if(
97  ( isize == 0 )
98  ||
99  ( sizeS2 == 0 )
100  )
101  {
102  return 0;
103  }
104 
105  vector<int> positions;
106 
107  newPos = input.find (delimiter, 0);
108 
109  if( newPos < 0 )
110  {
111  return 0;
112  }
113 
114  int numFound = 0;
115 
116  while( newPos >= iPos )
117  {
118  numFound++;
119  positions.push_back(newPos);
120  iPos = newPos;
121  newPos = input.find (delimiter, iPos+sizeS2);
122  }
123 
124  if( numFound == 0 )
125  {
126  return 0;
127  }
128 
129  for( int i=0; i <= static_cast<int>(positions.size()); ++i )
130  {
131  string s("");
132  if( i == 0 )
133  {
134  s = input.substr( i, positions[i] );
135  }
136  else
137  {
138  int offset = positions[i-1] + sizeS2;
139  if( offset < isize )
140  {
141  if( i == static_cast<int>(positions.size()) )
142  {
143  s = input.substr(offset);
144  }
145  else if( i > 0 )
146  {
147  s = input.substr( positions[i-1] + sizeS2,
148  positions[i] - positions[i-1] - sizeS2 );
149  }
150  }
151  }
152  if( includeEmpties || ( s.size() > 0 ) )
153  {
154  results.push_back(s);
155  }
156  }
157  return numFound;
158  }
159 
168  inline
169  int ascii_getline(std::istream & buf, std::string & line)
170  {
171  char c('a');
172  line.clear();
173  while(!buf.eof())
174  {
175  c = buf.get();
176  if(c == '\n' || c == '\r')break;
177  line += c;
178  }
179  char n = buf.peek();
180 
181  /* never trust ascii files, they may come from
182  Windows, suppodedly \n\r, but let's not
183  underestimate Windows's viciousness
184  */
185  if((c == '\n' && n == '\r') ||
186  (n == '\n' && c == '\r'))c = buf.get();
187 
188  return buf.good();
189  }
190 
191  inline
193  {
194 // kinetics
195  if(str == "A")
196  {
198  }else if(str == "E")
199  {
201  }else if(str == "B")
202  {
204  }else if(str == "D")
205  {
207  }else if(str == "Tref")
208  {
210  }else if(str == "Rscale")
211  {
213  }else if(str == "sigma")
214  {
216  }else if(str == "lambda")
217  {
219  }else if(str == "0")
220  {
222  }else if(str == "inf")
223  {
225  }else
226  {
228  }
229  }
230 
231  inline
233  {
234 // chemical
235  if(str == "efficiencies")
236  {
238  }else if(str == "alpha")
239  {
241  }else if(str == "T1")
242  {
244  }else if(str == "T2")
245  {
247  }else if(str == "T3")
248  {
250  }else
251  {
253  }
254  }
255 
256 } // end namespace Antioch
257 
258 #endif // ANTIOCH_STRING_UTILS_H
void remove_newline_from_strings(std::vector< std::string > &strings)
Strips newline characters from strings in the input vector, strings.
Definition: string_utils.C:56
#define antioch_assert(asserted)
void split_string(const std::string &input, const std::string &delimiter, std::vector< std::string > &results)
All characters in delimiter will be treated as a delimiter.
Definition: string_utils.C:34
std::pair< std::string, Type > split_string_on_colon(const std::string &token)
Definition: string_utils.h:69
#define antioch_error()
T string_to_T(const std::string &input)
Definition: string_utils.h:57
int ascii_getline(std::istream &buf, std::string &line)
adapted getline, never believe ascii file for the formatting of end-of-line.
Definition: string_utils.h:169
a int
Definition: eigen_utils.h:67
KineticsModel::Parameters string_to_kin_enum(const std::string &str)
Definition: string_utils.h:192
int SplitString(const std::string &input, const std::string &delimiter, std::vector< std::string > &results, bool includeEmpties=true)
Taken from FIN-S for XML parsing.
Definition: string_utils.h:83
The parameters are reduced parameters.
ReactionType::Parameters string_to_chem_enum(const std::string &str)
Definition: string_utils.h:232

Generated on Thu Jul 7 2016 11:09:46 for antioch-0.4.0 by  doxygen 1.8.8