Added copyString as a consistent interface for copying std::string to char*

This commit is contained in:
Ray Speth 2012-08-02 15:48:27 +00:00
parent f2a1aa219b
commit 6a78bea039
7 changed files with 29 additions and 32 deletions

View file

@ -284,6 +284,9 @@ doublereal strSItoDbl(const std::string& strSI);
void tokenizeString(const std::string& oval,
std::vector<std::string>& 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

View file

@ -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';
}
}
}

View file

@ -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<int>(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<int>(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);

View file

@ -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);

View file

@ -89,9 +89,7 @@ extern "C" {
Domain1D& dom = DomainCabinet::item(i);
dom.checkComponentIndex(n);
string nm = dom.componentName(n);
size_t lout = std::min<size_t>(sz, nm.size());
copy(nm.c_str(), nm.c_str() + lout, buf);
buf[lout] = '\0';
copyString(nm, buf, sz);
return static_cast<int>(nm.size());
} catch (...) {
return handleAllExceptions(-1, ERR);

View file

@ -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

View file

@ -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