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