diff --git a/include/cantera/base/stringUtils.h b/include/cantera/base/stringUtils.h index 8e8ca9ce3..1a1e41e4e 100644 --- a/include/cantera/base/stringUtils.h +++ b/include/cantera/base/stringUtils.h @@ -284,6 +284,9 @@ doublereal strSItoDbl(const std::string& strSI); void tokenizeString(const std::string& oval, std::vector& v); +//! Copy the contents of a std::string into a char array of a given length +void copyString(const std::string& source, char* dest, size_t length); + } #endif diff --git a/src/base/stringUtils.cpp b/src/base/stringUtils.cpp index e96168838..91cc7d069 100644 --- a/src/base/stringUtils.cpp +++ b/src/base/stringUtils.cpp @@ -658,4 +658,14 @@ void tokenizeString(const std::string& oval, } //================================================================================================ +void copyString(const std::string& source, char* dest, size_t length) +{ + const char* c_src = source.c_str(); + size_t N = std::min(length, source.length()+1); + std::copy(c_src, c_src + N, dest); + if (length != 0) { + dest[length-1] = '\0'; + } +} + } diff --git a/src/clib/ct.cpp b/src/clib/ct.cpp index 5c8a0f70e..42d6085c3 100644 --- a/src/clib/ct.cpp +++ b/src/clib/ct.cpp @@ -301,10 +301,7 @@ extern "C" { int phase_getName(int n, size_t lennm, char* nm) { try { - string name = ThermoCabinet::item(n).name(); - size_t lout = min(lennm, name.size()); - copy(name.c_str(), name.c_str() + lout, nm); - nm[lout] = '\0'; + copyString(ThermoCabinet::item(n).name(), nm, lennm); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -324,10 +321,7 @@ extern "C" { int phase_getSpeciesName(int n, size_t k, size_t lennm, char* nm) { try { - string spnm = ThermoCabinet::item(n).speciesName(k); - size_t lout = min(lennm, spnm.size()); - copy(spnm.c_str(), spnm.c_str() + lout, nm); - nm[lout] = '\0'; + copyString(ThermoCabinet::item(n).speciesName(k), nm, lennm); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -337,10 +331,7 @@ extern "C" { int phase_getElementName(int n, size_t m, size_t lennm, char* nm) { try { - string elnm = ThermoCabinet::item(n).elementName(m); - size_t lout = min(lennm, elnm.size()); - copy(elnm.c_str(), elnm.c_str() + lout, nm); - nm[lout] = '\0'; + copyString(ThermoCabinet::item(n).elementName(m), nm, lennm); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -1152,10 +1143,7 @@ extern "C" { try { Kinetics& k = KineticsCabinet::item(n); k.checkReactionIndex(i); - string r = k.reactionString(i); - int lout = min(len, (int)r.size()); - copy(r.c_str(), r.c_str() + lout, buf); - buf[lout] = '\0'; + copyString(k.reactionString(i), buf, len); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -1340,8 +1328,7 @@ extern "C" { if (int(s.size()) > ibuf - 1) { return -(static_cast(s.size()) + 1); } - copy(s.begin(), s.end(), buf); - buf[s.size() - 1] = '\0'; + copyString(s, buf, ibuf); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -1373,11 +1360,7 @@ extern "C" { { try { string e = lastErrorMessage(); - if (buflen > 0) { - int n = min(static_cast(e.size()), buflen-1); - copy(e.begin(), e.begin() + n, buf); - buf[min(n, buflen-1)] = '\0'; - } + copyString(e, buf, buflen); return int(e.size()); } catch (...) { return handleAllExceptions(-1, ERR); diff --git a/src/clib/ctfunc.cpp b/src/clib/ctfunc.cpp index ab297cc02..1ee64b6c6 100644 --- a/src/clib/ctfunc.cpp +++ b/src/clib/ctfunc.cpp @@ -155,12 +155,7 @@ extern "C" { int func_write(int i, size_t lennm, const char* arg, char* nm) { try { - std::string a = std::string(arg); - std::string w = FuncCabinet::item(i).write(a); - size_t ws = w.size(); - size_t lout = (lennm > ws ? ws : lennm); - std::copy(w.c_str(), w.c_str() + lout, nm); - nm[lout] = '\0'; + copyString(FuncCabinet::item(i).write(arg), nm, lennm); return 0; } catch (...) { return Cantera::handleAllExceptions(-1, ERR); diff --git a/src/clib/ctonedim.cpp b/src/clib/ctonedim.cpp index a3cb1c93f..4487b44d6 100644 --- a/src/clib/ctonedim.cpp +++ b/src/clib/ctonedim.cpp @@ -89,9 +89,7 @@ extern "C" { Domain1D& dom = DomainCabinet::item(i); dom.checkComponentIndex(n); string nm = dom.componentName(n); - size_t lout = std::min(sz, nm.size()); - copy(nm.c_str(), nm.c_str() + lout, buf); - buf[lout] = '\0'; + copyString(nm, buf, sz); return static_cast(nm.size()); } catch (...) { return handleAllExceptions(-1, ERR); diff --git a/test_problems/python/tut1/output_blessed.txt b/test_problems/python/tut1/output_blessed.txt index 21a9c8e10..5f0b2bdc7 100644 --- a/test_problems/python/tut1/output_blessed.txt +++ b/test_problems/python/tut1/output_blessed.txt @@ -75,6 +75,7 @@ C3H8 0 0 CH2CHO 0 0 CH3CHO 0 0 + gri30: @@ -147,6 +148,7 @@ C3H8 0 0 CH2CHO 0 0 CH3CHO 0 0 + gri30: @@ -219,6 +221,7 @@ C3H8 0 0 CH2CHO 0 0 CH3CHO 0 0 + gri30: @@ -291,6 +294,7 @@ C3H8 0.0188679 0.028814 -55.0607 CH2CHO 0.0188679 0.028127 -38.8293 CH3CHO 0.0188679 0.0287856 -57.467 + gri30: @@ -363,3 +367,4 @@ C3H8 0.00650712 0.0188679 -55.4841 CH2CHO 0.00666604 0.0188679 -39.2286 CH3CHO 0.00651352 0.0188679 -57.8894 + diff --git a/test_problems/python/tut4/output_blessed.txt b/test_problems/python/tut4/output_blessed.txt index 00dd307d0..a3dba59ba 100644 --- a/test_problems/python/tut4/output_blessed.txt +++ b/test_problems/python/tut4/output_blessed.txt @@ -75,6 +75,7 @@ C3H8 1.58019e-43 2.54277e-43 -154.981 CH2CHO 1.51542e-22 2.38041e-22 -91.6767 CH3CHO 2.467e-23 3.96589e-23 -103.501 + gri30: @@ -147,6 +148,7 @@ C3H8 0 0 CH2CHO 1.45246e-201 2.25803e-201 -484.655 CH3CHO 1.03051e-191 1.63957e-191 -537.674 + gri30: @@ -219,6 +221,7 @@ C3H8 0 0 CH2CHO 6.06557e-202 9.42964e-202 -485.516 CH3CHO 4.52646e-192 7.2017e-192 -538.627 + 0 2.4e-06 2.4e-06 1 4.58e-06 4.58e-06 2 0.141 0.141