00001 /** 00002 * @file stringUtils.h 00003 * Contains declarations for string manipulation functions 00004 * within Cantera. 00005 */ 00006 00007 /* 00008 * $Revision: 368 $ 00009 * $Date: 2010-01-03 19:46:26 -0500 (Sun, 03 Jan 2010) $ 00010 */ 00011 00012 // Copyright 2001 California Institute of Technology 00013 00014 #ifndef CT_STRINGUTILS_H 00015 #define CT_STRINGUTILS_H 00016 00017 #include "ct_defs.h" 00018 00019 #include <string> 00020 00021 namespace Cantera { 00022 00023 class Phase; 00024 class ThermoPhase; 00025 00026 //! Convert a double into a c++ string 00027 /*! 00028 * This routine doesn't assume a formatting. You 00029 * must supply the formatting 00030 * 00031 * @param x double to be converted 00032 * @param fmt Format to be used (printf style) 00033 */ 00034 std::string fp2str(const double x, const std::string &fmt); 00035 00036 //! Convert a double into a c++ string 00037 /*! 00038 * The default format to use is equivalent to the default 00039 * format used by printf's %g formatting. 00040 * 00041 * @param x double to be converted 00042 */ 00043 std::string fp2str(const double x); 00044 00045 //! Convert an int to a string using a format converter 00046 /*! 00047 * @param n int to be converted 00048 * @param fmt format converter for an int int the printf command 00049 */ 00050 std::string int2str(const int n, const std::string &fmt); 00051 00052 //! Convert an int to a string 00053 /*! 00054 * @param n int to be converted 00055 */ 00056 std::string int2str(const int n); 00057 00058 //! Strip the leading and trailing white space 00059 //! from a string 00060 /*! 00061 * The command isprint() is used to determine printable 00062 * characters. 00063 * 00064 * @param s Input string 00065 * @return Returns a copy of the string, stripped 00066 * of leading and trailing white space 00067 */ 00068 std::string stripws(const std::string &s); 00069 00070 //! Strip non-printing characters wherever they are 00071 /*! 00072 * @param s Input string 00073 * @return Returns a copy of the string, 00074 * stripped of all non-printing characters. 00075 */ 00076 std::string stripnonprint(const std::string &s); 00077 00078 //! Cast a copy of a string to lower case 00079 /*! 00080 * @param s Input string 00081 * @return Returns a copy of the string, 00082 * with all characters lowercase. 00083 */ 00084 std::string lowercase(const std::string &s); 00085 00086 //! Parse a composition string into a map consisting of individual key:composition 00087 //! pairs. 00088 /*! 00089 * The composition is a double. 00090 * Example 00091 * 00092 * Input is 00093 * 00094 * "fire:0 ice:1 snow:2" 00095 * 00096 * Output is 00097 * x["fire"] = 0 00098 * x["ice"] = 1 00099 * x["snow"] = 2 00100 * 00101 * @param ss original string consisting of multiple key:composition 00102 * pairs on multiple lines 00103 * @param x Output map consisting of a composition 00104 * map, which is a string to double map 00105 */ 00106 void parseCompString(const std::string &ss, Cantera::compositionMap& x); 00107 00108 00109 //! Parse a composition string into individual key:composition 00110 //! pairs 00111 /*! 00112 * 00113 * @param ss original string consisting of multiple key:composition 00114 * pairs on multiple lines 00115 * @param w Output vector consisting of single key:composition 00116 * items in each index. 00117 */ 00118 void split(const std::string &ss, std::vector<std::string>& w); 00119 00120 //! Interpret a string as a list of floats, and convert it to a vector 00121 //! of floats 00122 /*! 00123 * @param str String input vector 00124 * @param a Output pointer to a vector of floats 00125 * @param delim character delimiter. Defaults to a space 00126 * @return Returns the number of floats found and converted 00127 */ 00128 int fillArrayFromString(const std::string& str, doublereal* const a, 00129 const char delim = ' '); 00130 00131 00132 //! Generate a logfile name based on an input file name 00133 /*! 00134 * It tries to find the basename. Then, it appends a .log 00135 * to it. 00136 * 00137 * @param infile Input file name 00138 * 00139 * @return Returns a logfile name 00140 */ 00141 std::string logfileName(const std::string& infile); 00142 00143 00144 //! Get the file name without the path or extension 00145 /*! 00146 * @param fullPath Input file name consisting 00147 * of the full file name 00148 * 00149 * @return Returns the basename 00150 */ 00151 std::string getBaseName(const std::string& fullPath); 00152 00153 //! Translate a string into one integer value 00154 /*! 00155 * No error checking is done on the conversion. The c stdlib function 00156 * atoi() is used. 00157 * 00158 * @param val String value of the integer 00159 * 00160 * @return Returns an integer 00161 */ 00162 int intValue(std::string val); 00163 00164 //! Translate a string into one doublereal value 00165 /*! 00166 * No error checking is done on the conversion. The c stdlib function 00167 * atof() is used. 00168 * 00169 * @param val String value of the double 00170 * 00171 * @return Returns a doublereal value 00172 */ 00173 doublereal fpValue(std::string val); 00174 00175 //! Translate a string into one doublereal value 00176 /*! 00177 * Error checking is carried on the conversion. 00178 * 00179 * @param val String value of the double 00180 * 00181 * @return Returns a doublereal value 00182 */ 00183 doublereal fpValueCheck(std::string val); 00184 00185 //! Line wrap a string via a copy operation 00186 /*! 00187 * @param s Input string to be line wrapped 00188 * @param len Length at which to wrap. The 00189 * default is 70. 00190 */ 00191 std::string wrapString(const std::string &s, 00192 const int len=70); 00193 00194 //! Routine strips off white space from a c character string 00195 /*! 00196 * This routine strips off blanks and tabs (only leading and trailing 00197 * characters) in 'str'. On return, it returns the number of 00198 * characters still included in the string (excluding the null character). 00199 * 00200 * Comments are excluded -> All instances of the comment character, '!', 00201 * are replaced by NULL character thereby terminating 00202 * the string 00203 * 00204 * Parameter list: 00205 * 00206 * @param str On output 'str' contains the same characters as on 00207 * input except the leading and trailing white space and 00208 * comments have been removed. 00209 */ 00210 int stripLTWScstring(char str[]); 00211 00212 //! Translate a char string into a single double 00213 /*! 00214 * atofCheck is a wrapper around the C stdlib routine atof(). 00215 * It does quite a bit more error checking than atof() or 00216 * strtod(), and is quite a bit more restrictive. 00217 * 00218 * First it interprets both E, e, d, and D as exponents. 00219 * atof() only interprets e or E as an exponent character. 00220 * 00221 * It only accepts a string as well formed if it consists as a 00222 * single token. Multiple words will produce an error message 00223 * 00224 * It will produce an error for NAN and inf entries as well, 00225 * in contrast to atof() or strtod(). 00226 * The user needs to know that a serious numerical issue 00227 * has occurred. 00228 * 00229 * It does not accept hexadecimal numbers. 00230 * 00231 * @param dptr pointer to the input c string 00232 * @return Returns the double 00233 * 00234 * On any error, it will throw a CanteraError signal. 00235 */ 00236 doublereal atofCheck(const char * const dptr); 00237 00238 00239 //! Interpret one or two token string as a single double 00240 /*! 00241 * This is similar to atof(). However, the second token 00242 * is interpreted as an MKS units string and a conversion 00243 * factor to MKS is applied. 00244 * 00245 * Example 00246 * " 1.0 atm" 00247 * 00248 * results in the number 1.01325e5 00249 * 00250 * @param strSI string to be converted. One or two tokens 00251 * 00252 * @return returns a converted double 00253 */ 00254 doublereal strSItoDbl(const std::string& strSI); 00255 00256 //! This function separates a string up into tokens 00257 //! according to the location of white space. 00258 /*! 00259 * White space includes the new line character. tokens 00260 * are stripped of leading and trailing white space. 00261 * 00262 * The separate tokens are returned in a string vector, v. 00263 * 00264 * @param oval String to be broken up 00265 * @param v Output vector of tokens. 00266 */ 00267 void tokenizeString(const std::string& oval, 00268 std::vector<std::string>& v); 00269 00270 } 00271 00272 #endif