14 #define SNPRINTF _snprintf 16 #define SNPRINTF snprintf 25 #include <boost/algorithm/string.hpp> 29 namespace ba = boost::algorithm;
34 std::string
fp2str(
const double x,
const std::string& fmt)
37 "Use fmt::format instead.");
39 int n = SNPRINTF(buf, 63, fmt.c_str(), x);
42 return std::string(buf);
44 return std::string(
" ");
47 std::string
int2str(
const int n,
const std::string& fmt)
50 "Use fmt::format instead.");
52 int m = SNPRINTF(buf, 30, fmt.c_str(), n);
55 return std::string(buf);
57 return std::string(
" ");
63 "Use fmt::format instead.");
70 const std::string& sep)
74 for (
size_t i = 0; i < v.size(); i++) {
75 SNPRINTF(buf, 63, fmt.c_str(), v[i]);
77 if (i != v.size() - 1) {
87 warn_deprecated(
"lowercase",
"Use boost::algorithm::to_lower_copy instead. " 88 "To be removed after Cantera 2.3.");
90 for (
size_t i = 0; i < s.size(); i++) {
91 lc[i] = (char) tolower(s[i]);
99 "To be removed after Cantera 2.3.");
100 return ba::trim_copy(s);
106 for (
size_t i = 0; i < s.size(); i++) {
115 const std::vector<std::string>& names)
118 for (
size_t k = 0; k < names.size(); k++) {
125 while (stop < ss.size()) {
126 size_t colon = ss.find(
':', left);
130 size_t valstart = ss.find_first_not_of(
" \t\n", colon+1);
131 stop = ss.find_first_of(
", ;\n\t", valstart);
132 std::string name = ba::trim_copy(ss.substr(start, colon-start));
133 if (!names.empty() && x.find(name) == x.end()) {
135 "unknown species '" + name +
"'");
137 if (
getValue(x, name, 0.0) != 0.0) {
139 "Duplicate key: '" + name +
"'.");
144 value =
fpValueCheck(ss.substr(valstart, stop-colon-1));
150 std::string testname = ss.substr(start, stop-colon-1);
151 if (testname.find_first_of(
" \n\t") !=
npos) {
154 }
else if (ss.substr(valstart, stop-colon-1).find(
':') !=
npos) {
163 start = ss.find_first_not_of(
", ;\n\t", stop+1);
167 throw CanteraError(
"parseCompString",
"Unable to parse key-value pair:" 168 "\n'{}'", ss.substr(start, stop));
170 if (stop !=
npos && !ba::trim_copy(ss.substr(stop)).empty()) {
171 throw CanteraError(
"parseCompString",
"Found non-key:value data " 172 "in composition string: '" + ss.substr(stop) +
"'");
179 return std::atoi(ba::trim_copy(val).c_str());
185 std::stringstream ss(val);
186 ss.imbue(std::locale(
"C"));
193 std::string str = ba::trim_copy(val);
195 throw CanteraError(
"fpValueCheck",
"string has zero length");
202 if (ch ==
'+' || ch ==
'-') {
205 for (
size_t i = istart; i < str.size(); i++) {
208 }
else if (ch ==
'.') {
212 "string has more than one .");
216 "string has decimal point in exponent");
218 }
else if (ch ==
'e' || ch ==
'E' || ch ==
'd' || ch ==
'D') {
223 "string has more than one exp char");
226 if (ch ==
'+' || ch ==
'-') {
231 "Trouble processing string, " + str);
239 warn_deprecated(
"wrapString",
"Unused. To be removed after Cantera 2.3.");
242 for (
size_t n = 0; n < s.size(); n++) {
248 if (count > len && s[n] ==
' ') {
259 std::string s = ba::trim_copy(nameStr);
261 size_t ibegin = s.find_first_not_of(
" ;\n\t");
263 s = s.substr(ibegin,s.size());
264 size_t icolon = s.find(
':');
265 size_t iend = s.find_first_of(
" ;\n\t");
267 phaseName = s.substr(0, icolon);
268 s = s.substr(icolon+1, s.size());
269 icolon = s.find(
':');
271 throw CanteraError(
"parseSpeciesName()",
"two colons in name: " + nameStr);
276 "Species name has \" ;/\n/\t\" in the middle of it: " + nameStr);
284 std::vector<std::string> v;
288 if (n > 2 || n < 1) {
290 "number of tokens is too high");
300 std::string val = ba::trim_copy(in_val);
307 ba::split(v, val, ba::is_space(), ba::token_compress_on);
310 size_t copyString(
const std::string& source,
char* dest,
size_t length)
312 const char* c_src = source.c_str();
313 size_t N = std::min(length, source.length()+1);
314 size_t ret = (length >= source.length() + 1) ? 0 : source.length() + 1;
315 std::copy(c_src, c_src + N, dest);
317 dest[length-1] =
'\0';
std::map< std::string, doublereal > compositionMap
Map connecting a string name with a double.
doublereal fpValue(const std::string &val)
Translate a string into one doublereal value.
std::string int2str(const int n, const std::string &fmt)
Convert an int to a string using a format converter.
CTML ("Cantera Markup Language") is the variant of XML that Cantera uses to store data...
std::string vec2str(const vector_fp &v, const std::string &fmt, const std::string &sep)
Convert a vector to a string (separated by commas)
Various templated functions that carry out common vector operations (see Templated Utility Functions)...
doublereal toSI(const std::string &unit)
Return the conversion factor to convert unit std::string 'unit' to SI units.
const size_t npos
index returned by functions to indicate "no position"
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
std::string lowercase(const std::string &s)
Cast a copy of a string to lower case.
std::string stripws(const std::string &s)
Strip the leading and trailing white space from a string.
std::string fp2str(const double x, const std::string &fmt)
Convert a double into a c++ string.
void tokenizeString(const std::string &in_val, std::vector< std::string > &v)
This function separates a string up into tokens according to the location of white space...
Base class for exceptions thrown by Cantera classes.
const U & getValue(const std::map< T, U > &m, const T &key)
Const accessor for a value in a std::map.
std::string parseSpeciesName(const std::string &nameStr, std::string &phaseName)
Parse a name string, separating out the phase name from the species name.
size_t 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.
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.
Contains declarations for string manipulation functions within Cantera.
Namespace for the Cantera kernel.
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.
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...