From a44c7650c5fa754fed9e21df24964b80527439ca Mon Sep 17 00:00:00 2001 From: Harry Moffat Date: Sat, 27 Dec 2008 00:25:04 +0000 Subject: [PATCH] Added fpValueCheck and added doxygen info --- Cantera/src/base/ctml.cpp | 128 ++++-- Cantera/src/base/ctml.h | 212 ++++++---- Cantera/src/base/stringUtils.cpp | 669 ++++++++++++++++--------------- Cantera/src/base/stringUtils.h | 10 + Cantera/src/base/xml.cpp | 5 +- Cantera/src/base/xml.h | 9 +- 6 files changed, 584 insertions(+), 449 deletions(-) diff --git a/Cantera/src/base/ctml.cpp b/Cantera/src/base/ctml.cpp index dd5b56ae0..5db74519a 100755 --- a/Cantera/src/base/ctml.cpp +++ b/Cantera/src/base/ctml.cpp @@ -5,7 +5,7 @@ * */ -/* $Author$ +/* * $Revision$ * $Date$ */ @@ -135,10 +135,10 @@ namespace ctml { else return 0; } - string getString(const XML_Node& parent, string name) { - if (!parent.hasChild(name)) return ""; - return parent(name); - } + string getString(const XML_Node& parent, string name) { + if (!parent.hasChild(name)) return ""; + return parent(name); + } void getString(XML_Node& node, string title, string& val, string& type) { @@ -256,7 +256,7 @@ namespace ctml { * and "" , for no conversion. The default value is "" * which implies that no conversion is allowed. */ - doublereal getFloat(const XML_Node& parent, string name, string type) { + doublereal getFloat(const XML_Node& parent, std::string name, std::string type) { if (!parent.hasChild(name)) throw CanteraError("getFloat (called from XML Node \"" + parent.name() + "\"): ", @@ -315,40 +315,92 @@ namespace ctml { return fctr*x; } + + doublereal getFloatDefaultUnits(const Cantera::XML_Node& parent, std::string name, + std::string defaultUnits, std::string type) { - /** - * Get an integer value from a child element. Returns an - * integer value for the child named 'name' of element 'parent'. - */ - int getInteger(const XML_Node& parent, string name) { - if (!parent.hasChild(name)) - throw CanteraError("getInteger (called from XML Node \"" + - parent.name() + "\"): ", - "no child XML element named " + name); - const XML_Node& node = parent.child(name); - int x, x0, x1; - string units, vmin, vmax; - x = atoi(node().c_str()); - x0 = -9999999; - x1 = 9999999; - vmin = node["min"]; - vmax = node["max"]; - if (vmin != "") { - x0 = atoi(vmin.c_str()); - if (x < x0) { - writelog("\nWarning: value "+node()+" is below lower limit of " - +vmin+".\n"); - } - } - if (node["max"] != "") { - x1 = atoi(vmax.c_str()); - if (x > x1) { - writelog("\nWarning: value "+node()+" is above upper limit of " - +vmax+".\n"); - } - } - return x; + doublereal fctr = 1.0; + if (defaultUnits == "") { + throw CanteraError("getFloatDefaultUnits", + "need to supply an actual value of defaultUnits"); } + if (type == "actEnergy") { + fctr = actEnergyToSI(defaultUnits); + } else if (type == "toSI") { + fctr = toSI(defaultUnits); + } else if (defaultUnits == "temperature") { + fctr = toSI(defaultUnits); + } else if (type == "density") { + fctr = toSI(defaultUnits); + } else if (type == "pressure") { + fctr = toSI(defaultUnits); + } else { + throw CanteraError("getFloatDefaultUnits", + "type of units must be supplied and understood"); + } + doublereal val = getFloat(parent, name, type); + val /= fctr; + return val; + } + + + // Get an integer value from a child element. + /* + * Returns an integer value for the child named 'name' of element 'parent'. + * + * Note, it's an error for the child element not to exist. + * + * Example: + * + * Code snipet: + * @verbatum + const XML_Node &State_XMLNode; + int number = 1; + if (state_XMLNode.hasChild("NumProcs")) { + number = getInteger(State_XMLNode, "numProcs"); + } + @endverbatum + * + * reads the corresponding XML file: + * @verbatum + + 10 + <\state> + @endverbatum + * + * @param parent reference to the XML_Node object of the parent XML element + * @param name Name of the XML child element + */ + int getInteger(const XML_Node& parent, string name) { + if (!parent.hasChild(name)) { + throw CanteraError("getInteger (called from XML Node \"" + + parent.name() + "\"): ", + "no child XML element named " + name); + } + const XML_Node& node = parent.child(name); + int x, x0, x1; + string units, vmin, vmax; + x = atoi(node().c_str()); + x0 = -9999999; + x1 = 9999999; + vmin = node["min"]; + vmax = node["max"]; + if (vmin != "") { + x0 = atoi(vmin.c_str()); + if (x < x0) { + writelog("\nWarning: value "+node()+" is below lower limit of " + +vmin+".\n"); + } + } + if (node["max"] != "") { + x1 = atoi(vmax.c_str()); + if (x > x1) { + writelog("\nWarning: value "+node()+" is above upper limit of " + +vmax+".\n"); + } + } + return x; + } /* * getFloatArray(): diff --git a/Cantera/src/base/ctml.h b/Cantera/src/base/ctml.h index 04ea2b5e8..fa82accf1 100755 --- a/Cantera/src/base/ctml.h +++ b/Cantera/src/base/ctml.h @@ -24,57 +24,57 @@ namespace ctml { - const std::string CTML_Version = "1.4.1"; + const std::string CTML_Version = "1.4.1"; - bool isBuiltin(std::string nm); + bool isBuiltin(std::string nm); - void addBool(Cantera::XML_Node& node, - std::string title, - bool val); + void addBool(Cantera::XML_Node& node, + std::string title, + bool val); - void addInteger(Cantera::XML_Node& node, - std::string title, - int val, - std::string units="", - std::string type=""); + void addInteger(Cantera::XML_Node& node, + std::string title, + int val, + std::string units="", + std::string type=""); - void addFloat(Cantera::XML_Node& node, - std::string title, - doublereal val, - std::string units="", - std::string type="", - doublereal minval = Cantera::Undef, - doublereal maxval = Cantera::Undef); + void addFloat(Cantera::XML_Node& node, + std::string title, + doublereal val, + std::string units="", + std::string type="", + doublereal minval = Cantera::Undef, + doublereal maxval = Cantera::Undef); - void addIntegerArray(Cantera::XML_Node& node, - std::string title, - int n, - const int* vals, - std::string units="", - std::string type="", - doublereal minval=Cantera::Undef, - doublereal maxval=Cantera::Undef); + void addIntegerArray(Cantera::XML_Node& node, + std::string title, + int n, + const int* vals, + std::string units="", + std::string type="", + doublereal minval=Cantera::Undef, + doublereal maxval=Cantera::Undef); - void addFloatArray(Cantera::XML_Node& node, - std::string title, - int n, - const double* vals, - std::string units="", - std::string type="", - doublereal minval = Cantera::Undef, - doublereal maxval = Cantera::Undef); + void addFloatArray(Cantera::XML_Node& node, + std::string title, + int n, + const double* vals, + std::string units="", + std::string type="", + doublereal minval = Cantera::Undef, + doublereal maxval = Cantera::Undef); - void addString(Cantera::XML_Node& node, - std::string title, - std::string val, - std::string type=""); + void addString(Cantera::XML_Node& node, + std::string title, + std::string val, + std::string type=""); - void getFloatArray(const Cantera::XML_Node& node, Cantera::vector_fp& v, - bool convert=true, std::string type="", - std::string nodeName = "floatArray"); + void getFloatArray(const Cantera::XML_Node& node, Cantera::vector_fp& v, + bool convert=true, std::string type="", + std::string nodeName = "floatArray"); - void getStringArray(const Cantera::XML_Node& node, std::vector& v); - void getStringArray(const std::string& val, std::vector& v); + void getStringArray(const Cantera::XML_Node& node, std::vector& v); + void getStringArray(const std::string& val, std::vector& v); void getMap(const Cantera::XML_Node& node, std::map& m); //! This function interprets the value portion of an XML element @@ -90,9 +90,9 @@ namespace ctml { * of the string. * Example: @verbatum * - red:112 blue:34 - green:banana - @endverbatum + red:112 blue:34 + green:banana + @endverbatum * * Returns: * key val @@ -109,9 +109,9 @@ namespace ctml { Cantera::Array2D &returnValues, bool convert = true, bool matrixSymmetric = false); - void getIntegers(const Cantera::XML_Node& node, std::map& v); - void getFloats(const Cantera::XML_Node& node, std::map& v, - bool convert=true); + void getIntegers(const Cantera::XML_Node& node, std::map& v); + void getFloats(const Cantera::XML_Node& node, std::map& v, + bool convert=true); //! Get a floating-point value from a child element. @@ -127,19 +127,19 @@ namespace ctml { * * Code snipet: * @verbatum - const XML_Node &State_XMLNode; - doublereal pres = OneAtm; - if (state_XMLNode.hasChild("pressure")) { - pres = getFloat(State_XMLNode, "pressure", "toSI"); - } - @endverbatum + const XML_Node &State_XMLNode; + doublereal pres = OneAtm; + if (state_XMLNode.hasChild("pressure")) { + pres = getFloat(State_XMLNode, "pressure", "toSI"); + } + @endverbatum * * reads the corresponding XML file: * @verbatum - + 101325.0 - <\state> - @endverbatum + <\state> + @endverbatum * * @param parent reference to the XML_Node object of the parent XML element * @param name Name of the XML child element @@ -149,28 +149,92 @@ namespace ctml { */ doublereal getFloat(const Cantera::XML_Node& parent, std::string name, std::string type=""); - + + //! Get a floating-point value from a child element with a defined units field + /*! + * Returns a double value for the child named 'name' of element 'parent'. + * 'type' must be supplied and match a known unit type. + * + * Note, it's an error for the child element not to exist. + * + * Example: + * + * Code snipet: + * @verbatum + const XML_Node &State_XMLNode; + doublereal pres = OneAtm; + if (state_XMLNode.hasChild("pressure")) { + pres = getFloatDefaultUnits(State_XMLNode, "pressure", "Pa", "toSI"); + } + @endverbatum + * + * reads the corresponding XML file: + * @verbatum + + 101325.0 + <\state> + @endverbatum + * + * @param parent reference to the XML_Node object of the parent XML element + * @param name Name of the XML child element + * @param defaultUnits Default units string to be found in the units attribute. + * If the units string in the XML field is equal to defaultUnits, + * no units conversion will be carried out. + * @param type String type. Currently known types are "toSI" and "actEnergy", + * and "" , for no conversion. The default value is "", + * which implies that no conversion is allowed. + */ + doublereal getFloatDefaultUnits(const Cantera::XML_Node& parent, std::string name, + std::string defaultUnits, std::string type="toSI"); + + //! Get an integer value from a child element. + /*! + * Returns an integer value for the child named 'name' of element 'parent'. + * + * Note, it's an error for the child element not to exist. + * + * Example: + * + * Code snipet: + * @verbatum + const XML_Node &State_XMLNode; + int number = 1; + if (state_XMLNode.hasChild("NumProcs")) { + number = getInteger(State_XMLNode, "numProcs"); + } + @endverbatum + * + * reads the corresponding XML file: + * @verbatum + + 10 + <\state> + @endverbatum + * + * @param parent reference to the XML_Node object of the parent XML element + * @param name Name of the XML child element + */ int getInteger(const Cantera::XML_Node& parent, std::string name); - void getStrings(const Cantera::XML_Node& node, std::map& v); - void getFunction(const Cantera::XML_Node& node, std::string& type, - doublereal& xmin, doublereal& xmax, Cantera::vector_fp& coeffs); - Cantera::XML_Node* getByTitle(Cantera::XML_Node& node, std::string title); - void getString(Cantera::XML_Node& node, std::string title, - std::string& val, std::string& type); + void getStrings(const Cantera::XML_Node& node, std::map& v); + void getFunction(const Cantera::XML_Node& node, std::string& type, + doublereal& xmin, doublereal& xmax, Cantera::vector_fp& coeffs); + Cantera::XML_Node* getByTitle(Cantera::XML_Node& node, std::string title); + void getString(Cantera::XML_Node& node, std::string title, + std::string& val, std::string& type); - std::string getString(const Cantera::XML_Node& parent, std::string name); + std::string getString(const Cantera::XML_Node& parent, std::string name); - // these are defined in ct2ctml.cpp - void get_CTML_Tree(Cantera::XML_Node* node, std::string file, int debug = 0); + // these are defined in ct2ctml.cpp + void get_CTML_Tree(Cantera::XML_Node* node, std::string file, int debug = 0); - //! Convert a cti file into a ctml file - /*! - * - * @ingroup inputfiles - */ - void ct2ctml(const char* file, int debug = 0); + //! Convert a cti file into a ctml file + /*! + * + * @ingroup inputfiles + */ + void ct2ctml(const char* file, int debug = 0); } #endif diff --git a/Cantera/src/base/stringUtils.cpp b/Cantera/src/base/stringUtils.cpp index bd63ac288..7e14f00a6 100755 --- a/Cantera/src/base/stringUtils.cpp +++ b/Cantera/src/base/stringUtils.cpp @@ -27,207 +27,207 @@ namespace Cantera { - /** - * Convert a floating point number to a std::string using sprintf. - */ - std::string fp2str(double x, std::string fmt) { - char buf[64]; - int n = SNPRINTF(buf, 63, fmt.c_str(), x); - if (n > 0) { - buf[63] = '\0'; - return std::string(buf); - } - return std::string(" "); - } - std::string fp2str(double x) { - char buf[64]; - int n = SNPRINTF(buf, 64, "%g" , x); - if (n > 0) { - buf[29] = '\0'; - return std::string(buf); - } - return std::string(" "); - } + /** + * Convert a floating point number to a std::string using sprintf. + */ + std::string fp2str(double x, std::string fmt) { + char buf[64]; + int n = SNPRINTF(buf, 63, fmt.c_str(), x); + if (n > 0) { + buf[63] = '\0'; + return std::string(buf); + } + return std::string(" "); + } + std::string fp2str(double x) { + char buf[64]; + int n = SNPRINTF(buf, 64, "%g" , x); + if (n > 0) { + buf[29] = '\0'; + return std::string(buf); + } + return std::string(" "); + } - /** - * Convert an integer number to a std::string using sprintf. - */ - std::string int2str(int n, std::string fmt) { - char buf[30]; - int m = SNPRINTF(buf, 30, fmt.c_str(), n); - //sprintf(buf, fmt.c_str(), n); - if (m > 0) { - buf[29] = '\0'; - return std::string(buf); - } - return std::string(" "); + /** + * Convert an integer number to a std::string using sprintf. + */ + std::string int2str(int n, std::string fmt) { + char buf[30]; + int m = SNPRINTF(buf, 30, fmt.c_str(), n); + //sprintf(buf, fmt.c_str(), n); + if (m > 0) { + buf[29] = '\0'; + return std::string(buf); } + return std::string(" "); + } - std::string int2str(int n) { - char buf[30]; - int m = SNPRINTF(buf, 30, "%d", n); - if (m > 0) { - buf[29] = '\0'; - return std::string(buf); + std::string int2str(int n) { + char buf[30]; + int m = SNPRINTF(buf, 30, "%d", n); + if (m > 0) { + buf[29] = '\0'; + return std::string(buf); + } + return std::string(" "); + } + + std::string lowercase(const std::string &s) { + int n = static_cast(s.size()); + std::string lc(s); + for (int i = 0; i < n; i++) lc[i] = tolower(s[i]); + return lc; + } + + static int firstChar(std::string s) { + int i; + int n = static_cast(s.size()); + for (i = 0; i < n; i++) + if (s[i] != ' ' && isprint(s[i])) break; + return i; + } + + static int lastChar(std::string s) { + int i; + int n = static_cast(s.size()); + for (i = n-1; i >= 0; i--) + if (s[i] != ' ' && isprint(s[i])) break; + return i; + } + + /** + * Strip leading and trailing white space. + */ + std::string stripws(std::string s) { + int ifirst = firstChar(s); + int ilast = lastChar(s); + return s.substr(ifirst, ilast - ifirst + 1); + } + + /** + * Strip non-printing characters. + */ + std::string stripnonprint(std::string s) { + int i; + int n = static_cast(s.size()); + std::string ss = ""; + for (i = 0; i < n; i++) { + if (isprint(s[i])) { + ss += s[i]; } - return std::string(" "); - } - - std::string lowercase(const std::string &s) { - int n = static_cast(s.size()); - std::string lc(s); - for (int i = 0; i < n; i++) lc[i] = tolower(s[i]); - return lc; - } - - static int firstChar(std::string s) { - int i; - int n = static_cast(s.size()); - for (i = 0; i < n; i++) - if (s[i] != ' ' && isprint(s[i])) break; - return i; - } - - static int lastChar(std::string s) { - int i; - int n = static_cast(s.size()); - for (i = n-1; i >= 0; i--) - if (s[i] != ' ' && isprint(s[i])) break; - return i; - } - - /** - * Strip leading and trailing white space. - */ - std::string stripws(std::string s) { - int ifirst = firstChar(s); - int ilast = lastChar(s); - return s.substr(ifirst, ilast - ifirst + 1); - } - - /** - * Strip non-printing characters. - */ - std::string stripnonprint(std::string s) { - int i; - int n = static_cast(s.size()); - std::string ss = ""; - for (i = 0; i < n; i++) { - if (isprint(s[i])) { - ss += s[i]; - } - } - return ss; } + return ss; + } - /** - * Parse a composition string. - */ - void parseCompString(const std::string ss, compositionMap& x) { - std::string s = ss; - std::string::size_type icolon, ibegin, iend; - std::string name, num, nm; - do { - ibegin = s.find_first_not_of(", ;\n\t"); - if (ibegin != std::string::npos) { - s = s.substr(ibegin,s.size()); - icolon = s.find(':'); - iend = s.find_first_of(", ;\n\t"); - //icomma = s.find(','); - if (icolon != std::string::npos) { - name = s.substr(0, icolon); - if (iend != std::string::npos) { - num = s.substr(icolon+1, iend-icolon); - s = s.substr(iend+1, s.size()); - } - else { - num = s.substr(icolon+1, s.size()); - s = ""; - } - nm = stripws(name); - if (x.find(nm) == x.end()) { - //if (x[nm] == 0.0) { - throw CanteraError("parseCompString", - "unknown species " + nm); - } - x[nm] = atof(num.c_str()); - } - else s = ""; - } - } - while (s != ""); + /** + * Parse a composition string. + */ + void parseCompString(const std::string ss, compositionMap& x) { + std::string s = ss; + std::string::size_type icolon, ibegin, iend; + std::string name, num, nm; + do { + ibegin = s.find_first_not_of(", ;\n\t"); + if (ibegin != std::string::npos) { + s = s.substr(ibegin,s.size()); + icolon = s.find(':'); + iend = s.find_first_of(", ;\n\t"); + //icomma = s.find(','); + if (icolon != std::string::npos) { + name = s.substr(0, icolon); + if (iend != std::string::npos) { + num = s.substr(icolon+1, iend-icolon); + s = s.substr(iend+1, s.size()); + } + else { + num = s.substr(icolon+1, s.size()); + s = ""; + } + nm = stripws(name); + if (x.find(nm) == x.end()) { + //if (x[nm] == 0.0) { + throw CanteraError("parseCompString", + "unknown species " + nm); + } + x[nm] = atof(num.c_str()); + } + else s = ""; + } } + while (s != ""); + } - /** - * Parse a composition string. - */ - void split(const std::string ss, std::vector& w) { - std::string s = ss; - std::string::size_type ibegin, iend; - std::string name, num, nm; - do { - ibegin = s.find_first_not_of(", ;\n\t"); - if (ibegin != std::string::npos) { - s = s.substr(ibegin,s.size()); - iend = s.find_first_of(", ;\n\t"); - if (iend != std::string::npos) { - w.push_back(s.substr(0, iend)); - s = s.substr(iend+1, s.size()); - } - else { - w.push_back(s.substr(0, s.size())); - return; - } - } - } - while (s != ""); + /** + * Parse a composition string. + */ + void split(const std::string ss, std::vector& w) { + std::string s = ss; + std::string::size_type ibegin, iend; + std::string name, num, nm; + do { + ibegin = s.find_first_not_of(", ;\n\t"); + if (ibegin != std::string::npos) { + s = s.substr(ibegin,s.size()); + iend = s.find_first_of(", ;\n\t"); + if (iend != std::string::npos) { + w.push_back(s.substr(0, iend)); + s = s.substr(iend+1, s.size()); + } + else { + w.push_back(s.substr(0, s.size())); + return; + } + } } + while (s != ""); + } - int fillArrayFromString(const std::string& str, doublereal* a, char delim) { - std::string::size_type iloc; - int count = 0; - std::string num, s; - s = str; - while (s.size() > 0) { - iloc = s.find(delim); - if (iloc > 0) { - num = s.substr(0, iloc); - s = s.substr(iloc+1,s.size()); - } - else { - num = s; - s = ""; - } - a[count] = atof(num.c_str()); - count++; - } - return count; + int fillArrayFromString(const std::string& str, doublereal* a, char delim) { + std::string::size_type iloc; + int count = 0; + std::string num, s; + s = str; + while (s.size() > 0) { + iloc = s.find(delim); + if (iloc > 0) { + num = s.substr(0, iloc); + s = s.substr(iloc+1,s.size()); + } + else { + num = s; + s = ""; + } + a[count] = atof(num.c_str()); + count++; } + return count; + } - /** - * Get the file name without the path or extension - */ - std::string getFileName(const std::string& path) { - std::string file; - size_t idot = path.find_last_of('.'); - size_t islash = path.find_last_of('/'); - if (idot > 0 && idot < path.size()) { - if (islash > 0 && islash < idot) { - file = path.substr(islash+1, idot-islash-1); - } - else { - file = path.substr(0,idot); - } - } - else { - file = path; - } - return file; + /** + * Get the file name without the path or extension + */ + std::string getFileName(const std::string& path) { + std::string file; + size_t idot = path.find_last_of('.'); + size_t islash = path.find_last_of('/'); + if (idot > 0 && idot < path.size()) { + if (islash > 0 && islash < idot) { + file = path.substr(islash+1, idot-islash-1); + } + else { + file = path.substr(0,idot); + } } + else { + file = path; + } + return file; + } int intValue(std::string val) { @@ -237,164 +237,169 @@ namespace Cantera { doublereal fpValue(std::string val) { return std::atof(stripws(val).c_str()); } - /** - * Generate a logfile name based on an input file name - */ - std::string logfileName(const std::string& infile) { - std::string logfile; - size_t idot = infile.find_last_of('.'); - size_t islash = infile.find_last_of('/'); - if (idot > 0 && idot < infile.size()) { - if (islash > 0 && islash < idot) { - logfile = infile.substr(islash+1, idot-islash-1) + ".log"; - } - else { - logfile = infile.substr(0,idot) + ".log"; - } - } - else { - logfile = infile + ".log"; - } - return logfile; + + doublereal fpValueCheck(std::string val) { + return atofCheck(stripws(val).c_str()); + } + + /** + * Generate a logfile name based on an input file name + */ + std::string logfileName(const std::string& infile) { + std::string logfile; + size_t idot = infile.find_last_of('.'); + size_t islash = infile.find_last_of('/'); + if (idot > 0 && idot < infile.size()) { + if (islash > 0 && islash < idot) { + logfile = infile.substr(islash+1, idot-islash-1) + ".log"; + } + else { + logfile = infile.substr(0,idot) + ".log"; + } } + else { + logfile = infile + ".log"; + } + return logfile; + } - std::string wrapString(const std::string& s, int len) { - int nc = s.size(); - int n, count=0; - std::string r; - for (n = 0; n < nc; n++) { - if (s[n] == '\n') count = 0; - else count++; - if (count > len && s[n] == ' ') { - r += "\n "; - count = 0; - } - r += s[n]; - } - return r; + std::string wrapString(const std::string& s, int len) { + int nc = s.size(); + int n, count=0; + std::string r; + for (n = 0; n < nc; n++) { + if (s[n] == '\n') count = 0; + else count++; + if (count > len && s[n] == ' ') { + r += "\n "; + count = 0; + } + r += s[n]; } + return r; + } + /* + * This routine strips off blanks and tabs (only leading and trailing + * characters) in 'str'. On return, it returns the number of + * characters still included in the string (excluding the null character). + * + * Comments are excluded -> All instances of the comment character, '!', + * are replaced by '\0' thereby terminating + * the string + * + * Parameter list: + * + * str == On output 'str' contains the same characters as on + * input except the leading and trailing white space and + * comments have been removed. + */ + int stripLTWScstring(char str[]) { + int i = 0, j = 0; + char ch; + const char COM_CHAR='\0'; /* - * This routine strips off blanks and tabs (only leading and trailing - * characters) in 'str'. On return, it returns the number of - * characters still included in the string (excluding the null character). - * - * Comments are excluded -> All instances of the comment character, '!', - * are replaced by '\0' thereby terminating - * the string - * - * Parameter list: - * - * str == On output 'str' contains the same characters as on - * input except the leading and trailing white space and - * comments have been removed. + * Quick Returns */ - int stripLTWScstring(char str[]) { - int i = 0, j = 0; - char ch; - const char COM_CHAR='\0'; - /* - * Quick Returns - */ - if ((str == 0) || (str[0] == '\0')) return (0); + if ((str == 0) || (str[0] == '\0')) return (0); - /* Find first non-space character character */ - while(((ch = str[i]) != '\0') && isspace(ch)) i++; + /* Find first non-space character character */ + while(((ch = str[i]) != '\0') && isspace(ch)) i++; - /* - * Move real part of str to the front by copying the string - * - Comments are handled here, by terminating the copy at the - * first comment indicator, and inserting the null character at - * that point. - */ - - while ( (ch = str[j+i]) != '\0' && - (ch != COM_CHAR)) { - str[j] = ch; - j++; - } - str[j] = '\0'; - j--; - /* Remove trailing white space by inserting a null character */ - while( (j != -1 ) && isspace(str[j])) j--; - j++; - str[j] = '\0'; - return (j); - } - - /* - * atofCheck is a wrapper around the C stdlib routine atof(). - * It does quite a bit more error checking than atof() or - * strtod(), and is quite a bit more restrictive. - * - * First it interprets both E, e, d, and D as exponents. - * atof() only interprets e or E as an exponent character. - * - * It only accepts a string as well formed if it consists as a - * single token. Multiple words will produce an error message - * - * It will produce an error for NAN and inf entries as well, - * in contrast to atof() or strtod(). - * The user needs to know that a serious numerical issue - * has occurred. - * - * It does not accept hexadecimal numbers. - * - * On any error, it will throw a CanteraError signal. + * Move real part of str to the front by copying the string + * - Comments are handled here, by terminating the copy at the + * first comment indicator, and inserting the null character at + * that point. */ - double atofCheck(const char *dptr) { - if (!dptr) { - throw CanteraError("atofCheck", "null pointer to string"); - } - char *eptr = (char *) malloc(strlen(dptr)+1); - strcpy(eptr, dptr); - int ll = stripLTWScstring(eptr); - if (ll == 0) { - throw CanteraError("atofCheck", "string has zero length"); - } - int numDot = 0; - int numExp = 0; - char ch; - int istart = 0; - ch = eptr[0]; - if (ch == '+' || ch == '-') { - istart = 1; - } - for (int i = istart; i < ll; i++) { - ch = eptr[i]; - if (isdigit(ch)) { - } else if (ch == '.') { - numDot++; - if (numDot > 1) { - free(eptr); - throw CanteraError("atofCheck", - "string has more than one ."); - } - } else if (ch == 'e' || ch == 'E' || ch == 'd' || ch == 'D') { - numExp++; - eptr[i] = 'E'; - if (numExp > 1) { - free(eptr); - throw CanteraError("atofCheck", - "string has more than one exp char"); - } - ch = eptr[i+1]; - if (ch == '+' || ch == '-') { - i++; - } - } else { - std::string hh(dptr); - free(eptr); - throw CanteraError("atofCheck", - "Trouble processing string, " + hh); - } - } - double rval = atof(eptr); - free(eptr); - return rval; + + while ( (ch = str[j+i]) != '\0' && + (ch != COM_CHAR)) { + str[j] = ch; + j++; } + str[j] = '\0'; + j--; + /* Remove trailing white space by inserting a null character */ + while( (j != -1 ) && isspace(str[j])) j--; + j++; + str[j] = '\0'; + return (j); + } + + + /* + * atofCheck is a wrapper around the C stdlib routine atof(). + * It does quite a bit more error checking than atof() or + * strtod(), and is quite a bit more restrictive. + * + * First it interprets both E, e, d, and D as exponents. + * atof() only interprets e or E as an exponent character. + * + * It only accepts a string as well formed if it consists as a + * single token. Multiple words will produce an error message + * + * It will produce an error for NAN and inf entries as well, + * in contrast to atof() or strtod(). + * The user needs to know that a serious numerical issue + * has occurred. + * + * It does not accept hexadecimal numbers. + * + * On any error, it will throw a CanteraError signal. + */ + double atofCheck(const char *dptr) { + if (!dptr) { + throw CanteraError("atofCheck", "null pointer to string"); + } + char *eptr = (char *) malloc(strlen(dptr)+1); + strcpy(eptr, dptr); + int ll = stripLTWScstring(eptr); + if (ll == 0) { + throw CanteraError("atofCheck", "string has zero length"); + } + int numDot = 0; + int numExp = 0; + char ch; + int istart = 0; + ch = eptr[0]; + if (ch == '+' || ch == '-') { + istart = 1; + } + for (int i = istart; i < ll; i++) { + ch = eptr[i]; + if (isdigit(ch)) { + } else if (ch == '.') { + numDot++; + if (numDot > 1) { + free(eptr); + throw CanteraError("atofCheck", + "string has more than one ."); + } + } else if (ch == 'e' || ch == 'E' || ch == 'd' || ch == 'D') { + numExp++; + eptr[i] = 'E'; + if (numExp > 1) { + free(eptr); + throw CanteraError("atofCheck", + "string has more than one exp char"); + } + ch = eptr[i+1]; + if (ch == '+' || ch == '-') { + i++; + } + } else { + std::string hh(dptr); + free(eptr); + throw CanteraError("atofCheck", + "Trouble processing string, " + hh); + } + } + double rval = atof(eptr); + free(eptr); + return rval; + } doublereal strSItoDbl(const std::string& strSI) { @@ -403,8 +408,8 @@ namespace Cantera { doublereal fp = 1.0; int n = v.size(); if (n > 2 || n < 1) { - throw CanteraError("strSItoDbl", - "number of tokens is too high"); + throw CanteraError("strSItoDbl", + "number of tokens is too high"); } else if (n == 2) { fp = toSI(v[1]); } diff --git a/Cantera/src/base/stringUtils.h b/Cantera/src/base/stringUtils.h index 73657202b..b502b9a5f 100755 --- a/Cantera/src/base/stringUtils.h +++ b/Cantera/src/base/stringUtils.h @@ -62,6 +62,16 @@ namespace Cantera { */ doublereal fpValue(std::string val); + //! Translate a string into one doublereal value + /*! + * Error checking is carried on the conversion. + * + * @param val String value of the double + * + * @return Returns a doublereal value + */ + doublereal fpValueCheck(std::string val); + std::string wrapString(const std::string& s, int len=70); int stripLTWScstring(char str[]); diff --git a/Cantera/src/base/xml.cpp b/Cantera/src/base/xml.cpp index d83c01eab..93969e668 100755 --- a/Cantera/src/base/xml.cpp +++ b/Cantera/src/base/xml.cpp @@ -994,7 +994,7 @@ namespace Cantera { } } - XML_Node& XML_Node::child(string loc) const { + XML_Node& XML_Node::child(std::string loc) const { string::size_type iloc; string cname; map::const_iterator i; @@ -1005,7 +1005,6 @@ namespace Cantera { cname = loc.substr(0,iloc); loc = loc.substr(iloc+1, loc.size()); i = m_childindex.find(cname); - //XML_Node* chld = m_childindex[cname]; if (i != m_childindex.end()) return i->second->child(loc); else { throw XML_NoChild(this, m_name, cname, lineNumber()); @@ -1014,8 +1013,6 @@ namespace Cantera { else { i = m_childindex.find(loc); if (i != m_childindex.end()) return *(i->second); - //XML_Node* chld = m_childindex[loc]; - //if (chld) return *chld; else { throw XML_NoChild(this, m_name, loc, lineNumber()); } diff --git a/Cantera/src/base/xml.h b/Cantera/src/base/xml.h index 7b9a45010..a1538dcaa 100755 --- a/Cantera/src/base/xml.h +++ b/Cantera/src/base/xml.h @@ -394,7 +394,7 @@ namespace Cantera { /*! * @param n Number of the child to return */ - XML_Node& child(const int n) const; + XML_Node& child(const int n) const ; //! Return an unchangeable reference to the vector of children of the current node /*! @@ -471,7 +471,14 @@ namespace Cantera { const XML_Node* findByName(const std::string& nm) const; XML_Node* findByName(const std::string& nm); void getChildren(std::string name, std::vector& children) const; + + //! Return a changeable reference to a child of the current node, + //! named by the argument + /*! + * @param loc Name of the child to return + */ XML_Node& child(std::string loc) const; + void writeHeader(std::ostream& s); void write(std::ostream& s, int level = 0) const; XML_Node& root() const { return *m_root; }