Cantera  2.0
stringUtils.h
Go to the documentation of this file.
1 /**
2  * @file stringUtils.h
3  * Contains declarations for string manipulation functions
4  * within Cantera.
5  */
6 // Copyright 2001 California Institute of Technology
7 
8 #ifndef CT_STRINGUTILS_H
9 #define CT_STRINGUTILS_H
10 
11 #include "ct_defs.h"
12 
13 #include <string>
14 
15 namespace Cantera
16 {
17 
18 class Phase;
19 class ThermoPhase;
20 
21 //! Convert a double into a c++ string
22 /*!
23  * This routine doesn't assume a formatting. You
24  * must supply the formatting
25  *
26  * @param x double to be converted
27  * @param fmt Format to be used (printf style)
28  */
29 std::string fp2str(const double x, const std::string& fmt);
30 
31 //! Convert a double into a c++ string
32 /*!
33  * The default format to use is equivalent to the default
34  * format used by printf's %g formatting.
35  *
36  * @param x double to be converted
37  */
38 std::string fp2str(const double x);
39 
40 //! Convert an int to a string using a format converter
41 /*!
42  * @param n int to be converted
43  * @param fmt format converter for an int int the printf command
44  */
45 std::string int2str(const int n, const std::string& fmt);
46 
47 //! Convert an int to a string
48 /*!
49  * @param n int to be converted
50  */
51 std::string int2str(const int n);
52 
53 //! Convert an unsigned integer to a string
54 /*!
55  * @param n int to be converted
56  */
57 std::string int2str(const size_t n);
58 
59 //! Strip the leading and trailing white space
60 //! from a string
61 /*!
62  * The command isprint() is used to determine printable
63  * characters.
64  *
65  * @param s Input string
66  * @return Returns a copy of the string, stripped
67  * of leading and trailing white space
68  */
69 std::string stripws(const std::string& s);
70 
71 //! Strip non-printing characters wherever they are
72 /*!
73  * @param s Input string
74  * @return Returns a copy of the string,
75  * stripped of all non-printing characters.
76  */
77 std::string stripnonprint(const std::string& s);
78 
79 //! Cast a copy of a string to lower case
80 /*!
81  * @param s Input string
82  * @return Returns a copy of the string,
83  * with all characters lowercase.
84  */
85 std::string lowercase(const std::string& s);
86 
87 //! Parse a composition string into a map consisting of individual key:composition
88 //! pairs.
89 /*!
90  * The composition is a double.
91  * Example
92  *
93  * Input is
94  *
95  * "fire:0 ice:1 snow:2"
96  *
97  * Output is
98  * x["fire"] = 0
99  * x["ice"] = 1
100  * x["snow"] = 2
101  *
102  * @param ss original string consisting of multiple key:composition
103  * pairs on multiple lines
104  * @param x Output map consisting of a composition
105  * map, which is a string to double map
106  */
107 void parseCompString(const std::string& ss, Cantera::compositionMap& x);
108 
109 
110 //! Parse a composition string into individual key:composition
111 //! pairs
112 /*!
113  *
114  * @param ss original string consisting of multiple key:composition
115  * pairs on multiple lines
116  * @param w Output vector consisting of single key:composition
117  * items in each index.
118  */
119 void split(const std::string& ss, std::vector<std::string>& w);
120 
121 //! Interpret a string as a list of floats, and convert it to a vector
122 //! of floats
123 /*!
124  * @param str String input vector
125  * @param a Output pointer to a vector of floats
126  * @param delim character delimiter. Defaults to a space
127  * @return Returns the number of floats found and converted
128  */
129 int fillArrayFromString(const std::string& str, doublereal* const a,
130  const char delim = ' ');
131 
132 
133 //! Generate a logfile name based on an input file name
134 /*!
135  * It tries to find the basename. Then, it appends a .log
136  * to it.
137  *
138  * @param infile Input file name
139  *
140  * @return Returns a logfile name
141  */
142 std::string logfileName(const std::string& infile);
143 
144 
145 //! Get the file name without the path or extension
146 /*!
147  * @param fullPath Input file name consisting
148  * of the full file name
149  *
150  * @return Returns the basename
151  */
152 std::string getBaseName(const std::string& fullPath);
153 
154 //! Translate a string into one integer value
155 /*!
156  * No error checking is done on the conversion. The c stdlib function
157  * atoi() is used.
158  *
159  * @param val String value of the integer
160  *
161  * @return Returns an integer
162  */
163 int intValue(std::string val);
164 
165 //! Translate a string into one doublereal value
166 /*!
167  * No error checking is done on the conversion. The c stdlib function
168  * atof() is used.
169  *
170  * @param val String value of the double
171  *
172  * @return Returns a doublereal value
173  */
174 doublereal fpValue(std::string val);
175 
176 //! Translate a string into one doublereal value
177 /*!
178  * Error checking is carried on the conversion.
179  *
180  * @param val String value of the double
181  *
182  * @return Returns a doublereal value
183  */
184 doublereal fpValueCheck(std::string val);
185 
186 //! Parse a name string, separating out the phase name from the species name
187 /*!
188  * Name strings must not contain these internal characters "; \n \t ,"
189  * Only one colon is allowed, the one separating the phase name from the
190  * species name. Therefore, names may not include a colon.
191  *
192  * @param nameStr (input) Name string containing the phase name and the species
193  * name separated by a colon. The phase name is optional.
194  * example: "silane:SiH4"
195  * @param phaseName (output) Name of the phase, if specified. If not specified,
196  * a blank string is returned.
197  * @return (output) Species name is returned. If nameStr is blank
198  * an empty string is returned.
199  */
200 std::string parseSpeciesName(const std::string& nameStr, std::string& phaseName);
201 
202 //! Line wrap a string via a copy operation
203 /*!
204  * @param s Input string to be line wrapped
205  * @param len Length at which to wrap. The
206  * default is 70.
207  */
208 std::string wrapString(const std::string& s,
209  const int len=70);
210 
211 //! Routine strips off white space from a c character string
212 /*!
213  * This routine strips off blanks and tabs (only leading and trailing
214  * characters) in 'str'. On return, it returns the number of
215  * characters still included in the string (excluding the null character).
216  *
217  * Comments are excluded -> All instances of the comment character, '!',
218  * are replaced by NULL character thereby terminating
219  * the string
220  *
221  * Parameter list:
222  *
223  * @param str On output 'str' contains the same characters as on
224  * input except the leading and trailing white space and
225  * comments have been removed.
226  */
227 int stripLTWScstring(char str[]);
228 
229 //! Translate a char string into a single double
230 /*!
231  * atofCheck is a wrapper around the C stdlib routine atof().
232  * It does quite a bit more error checking than atof() or
233  * strtod(), and is quite a bit more restrictive.
234  *
235  * First it interprets both E, e, d, and D as exponents.
236  * atof() only interprets e or E as an exponent character.
237  *
238  * It only accepts a string as well formed if it consists as a
239  * single token. Multiple words will produce an error message
240  *
241  * It will produce an error for NAN and inf entries as well,
242  * in contrast to atof() or strtod().
243  * The user needs to know that a serious numerical issue
244  * has occurred.
245  *
246  * It does not accept hexadecimal numbers.
247  *
248  * @param dptr pointer to the input c string
249  * @return Returns the double
250  *
251  * On any error, it will throw a CanteraError signal.
252  */
253 doublereal atofCheck(const char* const dptr);
254 
255 
256 //! Interpret one or two token string as a single double
257 /*!
258  * This is similar to atof(). However, the second token
259  * is interpreted as an MKS units string and a conversion
260  * factor to MKS is applied.
261  *
262  * Example
263  * " 1.0 atm"
264  *
265  * results in the number 1.01325e5
266  *
267  * @param strSI string to be converted. One or two tokens
268  *
269  * @return returns a converted double
270  */
271 doublereal strSItoDbl(const std::string& strSI);
272 
273 //! This function separates a string up into tokens
274 //! according to the location of white space.
275 /*!
276  * White space includes the new line character. tokens
277  * are stripped of leading and trailing white space.
278  *
279  * The separate tokens are returned in a string vector, v.
280  *
281  * @param oval String to be broken up
282  * @param v Output vector of tokens.
283  */
284 void tokenizeString(const std::string& oval,
285  std::vector<std::string>& v);
286 
287 }
288 
289 #endif