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