Improve use of local variables in src/base

This commit is contained in:
Ray Speth 2016-01-29 18:00:00 -05:00
parent 484da253da
commit 4c58f11dd8
4 changed files with 50 additions and 81 deletions

View file

@ -192,8 +192,7 @@ void Application::thread_complete()
XML_Node* Application::get_XML_File(const std::string& file, int debug) XML_Node* Application::get_XML_File(const std::string& file, int debug)
{ {
std::unique_lock<std::mutex> xmlLock(xml_mutex); std::unique_lock<std::mutex> xmlLock(xml_mutex);
std::string path = ""; std::string path = findInputFile(file);
path = findInputFile(file);
int mtime = get_modified_time(path); int mtime = get_modified_time(path);
if (xmlfiles.find(path) != xmlfiles.end()) { if (xmlfiles.find(path) != xmlfiles.end()) {
@ -327,10 +326,8 @@ void Application::Messages::logErrors()
void Application::setDefaultDirectories() void Application::setDefaultDirectories()
{ {
std::vector<string>& dirs = inputDirs;
// always look in the local directory first // always look in the local directory first
dirs.push_back("."); inputDirs.push_back(".");
#ifdef _WIN32 #ifdef _WIN32
// Under Windows, the Cantera setup utility records the installation // Under Windows, the Cantera setup utility records the installation
@ -340,7 +337,7 @@ void Application::setDefaultDirectories()
readStringRegistryKey("SOFTWARE\\Cantera\\Cantera " CANTERA_SHORT_VERSION, readStringRegistryKey("SOFTWARE\\Cantera\\Cantera " CANTERA_SHORT_VERSION,
"InstallDir", installDir, ""); "InstallDir", installDir, "");
if (installDir != "") { if (installDir != "") {
dirs.push_back(installDir + "data"); inputDirs.push_back(installDir + "data");
// Scripts for converting mechanisms to CTI and CMTL are installed in // Scripts for converting mechanisms to CTI and CMTL are installed in
// the 'bin' subdirectory. Add that directory to the PYTHONPATH. // the 'bin' subdirectory. Add that directory to the PYTHONPATH.
@ -357,7 +354,7 @@ void Application::setDefaultDirectories()
#ifdef DARWIN #ifdef DARWIN
// add a default data location for Mac OS X // add a default data location for Mac OS X
dirs.push_back("/Applications/Cantera/data"); inputDirs.push_back("/Applications/Cantera/data");
#endif #endif
// if environment variable CANTERA_DATA is defined, then add it to the // if environment variable CANTERA_DATA is defined, then add it to the
@ -375,11 +372,11 @@ void Application::setDefaultDirectories()
size_t start = 0; size_t start = 0;
size_t end = s.find(pathsep); size_t end = s.find(pathsep);
while(end != npos) { while(end != npos) {
dirs.push_back(s.substr(start, end-start)); inputDirs.push_back(s.substr(start, end-start));
start = end + 1; start = end + 1;
end = s.find(pathsep, start); end = s.find(pathsep, start);
} }
dirs.push_back(s.substr(start,end)); inputDirs.push_back(s.substr(start,end));
} }
// CANTERA_DATA is defined in file config.h. This file is written during the // CANTERA_DATA is defined in file config.h. This file is written during the
@ -387,7 +384,7 @@ void Application::setDefaultDirectories()
// 'prefix' option to 'configure', or else to /usr/local/cantera. // 'prefix' option to 'configure', or else to /usr/local/cantera.
#ifdef CANTERA_DATA #ifdef CANTERA_DATA
string datadir = string(CANTERA_DATA); string datadir = string(CANTERA_DATA);
dirs.push_back(datadir); inputDirs.push_back(datadir);
#endif #endif
} }
@ -414,7 +411,6 @@ std::string Application::findInputFile(const std::string& name)
std::unique_lock<std::mutex> dirLock(dir_mutex); std::unique_lock<std::mutex> dirLock(dir_mutex);
string::size_type islash = name.find('/'); string::size_type islash = name.find('/');
string::size_type ibslash = name.find('\\'); string::size_type ibslash = name.find('\\');
string inname;
std::vector<string>& dirs = inputDirs; std::vector<string>& dirs = inputDirs;
// Expand "~/" to user's home directory, if possible // Expand "~/" to user's home directory, if possible
@ -430,17 +426,14 @@ std::string Application::findInputFile(const std::string& name)
if (islash == string::npos && ibslash == string::npos) { if (islash == string::npos && ibslash == string::npos) {
size_t nd = dirs.size(); size_t nd = dirs.size();
inname = "";
for (size_t i = 0; i < nd; i++) { for (size_t i = 0; i < nd; i++) {
inname = dirs[i] + "/" + name; string inname = dirs[i] + "/" + name;
std::ifstream fin(inname); std::ifstream fin(inname);
if (fin) { if (fin) {
return inname; return inname;
} }
} }
string msg; string msg = "\nInput file " + name + " not found in director";
msg = "\nInput file " + name
+ " not found in director";
msg += (nd == 1 ? "y " : "ies "); msg += (nd == 1 ? "y " : "ies ");
for (size_t i = 0; i < nd; i++) { for (size_t i = 0; i < nd; i++) {
msg += "\n'" + dirs[i] + "'"; msg += "\n'" + dirs[i] + "'";

View file

@ -77,8 +77,7 @@ void thread_complete()
XML_Node* get_XML_File(const std::string& file, int debug) XML_Node* get_XML_File(const std::string& file, int debug)
{ {
XML_Node* xtmp = app()->get_XML_File(file, debug); return app()->get_XML_File(file, debug);
return xtmp;
} }
XML_Node* get_XML_from_string(const std::string& text) XML_Node* get_XML_from_string(const std::string& text)
@ -157,8 +156,7 @@ doublereal actEnergyToSI(const std::string& unit)
string canteraRoot() string canteraRoot()
{ {
char* ctroot = 0; char* ctroot = getenv("CANTERA_ROOT");
ctroot = getenv("CANTERA_ROOT");
if (ctroot != 0) { if (ctroot != 0) {
return string(ctroot); return string(ctroot);
} }
@ -193,7 +191,7 @@ static void split_at_pound(const std::string& src, std::string& file, std::strin
XML_Node* get_XML_Node(const std::string& file_ID, XML_Node* root) XML_Node* get_XML_Node(const std::string& file_ID, XML_Node* root)
{ {
std::string fname, idstr; std::string fname, idstr;
XML_Node* db, *doc; XML_Node* db;
split_at_pound(file_ID, fname, idstr); split_at_pound(file_ID, fname, idstr);
if (fname == "") { if (fname == "") {
if (!root) throw CanteraError("get_XML_Node", if (!root) throw CanteraError("get_XML_Node",
@ -216,7 +214,7 @@ XML_Node* get_XML_Node(const std::string& file_ID, XML_Node* root)
throw err; throw err;
} }
} }
doc = get_XML_File(fname); XML_Node* doc = get_XML_File(fname);
if (!doc) throw CanteraError("get_XML_Node", if (!doc) throw CanteraError("get_XML_Node",
"get_XML_File failed trying to open "+fname); "get_XML_File failed trying to open "+fname);
db = doc->findID(idstr, 3); db = doc->findID(idstr, 3);
@ -233,7 +231,7 @@ XML_Node* get_XML_NameID(const std::string& nameTarget,
XML_Node* root) XML_Node* root)
{ {
string fname, idTarget; string fname, idTarget;
XML_Node* db, *doc; XML_Node* db;
split_at_pound(file_ID, fname, idTarget); split_at_pound(file_ID, fname, idTarget);
if (fname == "") { if (fname == "") {
if (!root) { if (!root) {
@ -241,7 +239,7 @@ XML_Node* get_XML_NameID(const std::string& nameTarget,
} }
db = root->findNameID(nameTarget, idTarget); db = root->findNameID(nameTarget, idTarget);
} else { } else {
doc = get_XML_File(fname); XML_Node* doc = get_XML_File(fname);
if (!doc) { if (!doc) {
return 0; return 0;
} }

View file

@ -252,13 +252,12 @@ std::string wrapString(const std::string& s, const int len)
std::string parseSpeciesName(const std::string& nameStr, std::string& phaseName) std::string parseSpeciesName(const std::string& nameStr, std::string& phaseName)
{ {
std::string s = stripws(nameStr); std::string s = stripws(nameStr);
std::string::size_type ibegin, iend, icolon;
phaseName = ""; phaseName = "";
ibegin = s.find_first_not_of(" ;\n\t"); size_t ibegin = s.find_first_not_of(" ;\n\t");
if (ibegin != std::string::npos) { if (ibegin != std::string::npos) {
s = s.substr(ibegin,s.size()); s = s.substr(ibegin,s.size());
icolon = s.find(':'); size_t icolon = s.find(':');
iend = s.find_first_of(" ;\n\t"); size_t iend = s.find_first_of(" ;\n\t");
if (icolon != std::string::npos) { if (icolon != std::string::npos) {
phaseName = s.substr(0, icolon); phaseName = s.substr(0, icolon);
s = s.substr(icolon+1, s.size()); s = s.substr(icolon+1, s.size());

View file

@ -139,17 +139,15 @@ void XML_Reader::getchr(char& ch)
static string::size_type findUnbackslashed(const std::string& s, const char q, static string::size_type findUnbackslashed(const std::string& s, const char q,
std::string::size_type istart = 0) std::string::size_type istart = 0)
{ {
string::size_type iloc, icurrent, len; size_t icurrent = istart;
icurrent = istart;
len = s.size();
while (true) { while (true) {
iloc = s.find(q, icurrent); size_t iloc = s.find(q, icurrent);
if (iloc == string::npos || iloc == 0) { if (iloc == string::npos || iloc == 0) {
return iloc; return iloc;
} }
char cm1 = s[iloc-1]; char cm1 = s[iloc-1];
if (cm1 == '\\') { if (cm1 == '\\') {
if (iloc >= (len -1)) { if (iloc >= (s.size() -1)) {
return string::npos; return string::npos;
} }
icurrent = iloc + 1; icurrent = iloc + 1;
@ -197,10 +195,8 @@ int XML_Reader::findQuotedString(const std::string& s, std::string& rstring) con
void XML_Reader::parseTag(const std::string& tag, std::string& name, void XML_Reader::parseTag(const std::string& tag, std::string& name,
std::map<std::string, std::string>& attribs) const std::map<std::string, std::string>& attribs) const
{ {
string::size_type iloc;
string attr, val;
string s = stripws(tag); string s = stripws(tag);
iloc = s.find(' '); size_t iloc = s.find(' ');
if (iloc != string::npos) { if (iloc != string::npos) {
name = s.substr(0, iloc); name = s.substr(0, iloc);
s = stripws(s.substr(iloc+1,s.size())); s = stripws(s.substr(iloc+1,s.size()));
@ -214,11 +210,12 @@ void XML_Reader::parseTag(const std::string& tag, std::string& name,
if (iloc == string::npos) { if (iloc == string::npos) {
break; break;
} }
attr = stripws(s.substr(0,iloc)); string attr = stripws(s.substr(0,iloc));
if (attr == "") { if (attr == "") {
break; break;
} }
s = stripws(s.substr(iloc+1,s.size())); s = stripws(s.substr(iloc+1,s.size()));
string val;
iloc = findQuotedString(s, val); iloc = findQuotedString(s, val);
attribs[attr] = val; attribs[attr] = val;
if (iloc != string::npos) { if (iloc != string::npos) {
@ -236,7 +233,7 @@ void XML_Reader::parseTag(const std::string& tag, std::string& name,
std::string XML_Reader::readTag(std::map<std::string, std::string>& attribs) std::string XML_Reader::readTag(std::map<std::string, std::string>& attribs)
{ {
string name, tag = ""; string tag = "";
bool incomment = false; bool incomment = false;
char ch = '-'; char ch = '-';
while (true) { while (true) {
@ -275,6 +272,7 @@ std::string XML_Reader::readTag(std::map<std::string, std::string>& attribs)
attribs.clear(); attribs.clear();
return tag; return tag;
} else { } else {
string name;
parseTag(tag, name, attribs); parseTag(tag, name, attribs);
return name; return name;
} }
@ -283,14 +281,13 @@ std::string XML_Reader::readTag(std::map<std::string, std::string>& attribs)
std::string XML_Reader::readValue() std::string XML_Reader::readValue()
{ {
string tag = ""; string tag = "";
char ch, lastch; char ch = '\n';
ch = '\n';
bool front = true; bool front = true;
while (true) { while (true) {
if (m_s.eof()) { if (m_s.eof()) {
break; break;
} }
lastch = ch; char lastch = ch;
getchr(ch); getchr(ch);
if (ch == '\n') { if (ch == '\n') {
front = true; front = true;
@ -593,13 +590,12 @@ XML_Node* XML_Node::findNameID(const std::string& nameTarget,
const std::string& idTarget) const const std::string& idTarget) const
{ {
XML_Node* scResult = 0; XML_Node* scResult = 0;
XML_Node* sc;
std::string idattrib = id(); std::string idattrib = id();
if (name() == nameTarget && (idTarget == "" || idTarget == idattrib)) { if (name() == nameTarget && (idTarget == "" || idTarget == idattrib)) {
return const_cast<XML_Node*>(this); return const_cast<XML_Node*>(this);
} }
for (size_t n = 0; n < m_children.size(); n++) { for (size_t n = 0; n < m_children.size(); n++) {
sc = m_children[n]; XML_Node* sc = m_children[n];
if (sc->name() == nameTarget) { if (sc->name() == nameTarget) {
if (idTarget == "") { if (idTarget == "") {
return sc; return sc;
@ -611,7 +607,7 @@ XML_Node* XML_Node::findNameID(const std::string& nameTarget,
} }
} }
for (size_t n = 0; n < m_children.size(); n++) { for (size_t n = 0; n < m_children.size(); n++) {
sc = m_children[n]; XML_Node* sc = m_children[n];
scResult = sc->findNameID(nameTarget, idTarget); scResult = sc->findNameID(nameTarget, idTarget);
if (scResult) { if (scResult) {
return scResult; return scResult;
@ -624,7 +620,6 @@ XML_Node* XML_Node::findNameIDIndex(const std::string& nameTarget,
const std::string& idTarget, const int index_i) const const std::string& idTarget, const int index_i) const
{ {
XML_Node* scResult = 0; XML_Node* scResult = 0;
XML_Node* sc;
std::string idattrib = id(); std::string idattrib = id();
std::string ii = attrib("index"); std::string ii = attrib("index");
std::string index_s = int2str(index_i); std::string index_s = int2str(index_i);
@ -633,7 +628,7 @@ XML_Node* XML_Node::findNameIDIndex(const std::string& nameTarget,
return const_cast<XML_Node*>(this); return const_cast<XML_Node*>(this);
} }
for (size_t n = 0; n < m_children.size(); n++) { for (size_t n = 0; n < m_children.size(); n++) {
sc = m_children[n]; XML_Node* sc = m_children[n];
if (sc->name() == nameTarget) { if (sc->name() == nameTarget) {
ii = sc->attrib("index"); ii = sc->attrib("index");
int indexR = atoi(ii.c_str()); int indexR = atoi(ii.c_str());
@ -656,9 +651,8 @@ XML_Node* XML_Node::findID(const std::string& id_, const int depth) const
return const_cast<XML_Node*>(this); return const_cast<XML_Node*>(this);
} }
if (depth > 0) { if (depth > 0) {
XML_Node* r = 0;
for (size_t i = 0; i < nChildren(); i++) { for (size_t i = 0; i < nChildren(); i++) {
r = m_children[i]->findID(id_, depth-1); XML_Node* r = m_children[i]->findID(id_, depth-1);
if (r != 0) { if (r != 0) {
return r; return r;
} }
@ -674,10 +668,9 @@ XML_Node* XML_Node::findByAttr(const std::string& attr,
return const_cast<XML_Node*>(this); return const_cast<XML_Node*>(this);
} }
if (depth > 0) { if (depth > 0) {
XML_Node* r = 0;
size_t n = nChildren(); size_t n = nChildren();
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
r = m_children[i]->findByAttr(attr, val, depth - 1); XML_Node* r = m_children[i]->findByAttr(attr, val, depth - 1);
if (r != 0) { if (r != 0) {
return r; return r;
} }
@ -692,9 +685,8 @@ XML_Node* XML_Node::findByName(const std::string& nm, int depth)
return this; return this;
} }
if (depth > 0) { if (depth > 0) {
XML_Node* r = 0;
for (size_t i = 0; i < nChildren(); i++) { for (size_t i = 0; i < nChildren(); i++) {
r = m_children[i]->findByName(nm); XML_Node* r = m_children[i]->findByName(nm);
if (r != 0) { if (r != 0) {
return r; return r;
} }
@ -709,9 +701,8 @@ const XML_Node* XML_Node::findByName(const std::string& nm, int depth) const
return const_cast<XML_Node*>(this); return const_cast<XML_Node*>(this);
} }
if (depth > 0) { if (depth > 0) {
const XML_Node* r = 0;
for (size_t i = 0; i < nChildren(); i++) { for (size_t i = 0; i < nChildren(); i++) {
r = m_children[i]->findByName(nm); XML_Node* r = m_children[i]->findByName(nm);
if (r != 0) { if (r != 0) {
return r; return r;
} }
@ -728,13 +719,11 @@ void XML_Node::writeHeader(std::ostream& s)
void XML_Node::build(std::istream& f) void XML_Node::build(std::istream& f)
{ {
XML_Reader r(f); XML_Reader r(f);
string nm, nm2, val;
XML_Node* node = this; XML_Node* node = this;
map<string, string> node_attribs;
bool first = true; bool first = true;
while (!f.eof()) { while (!f.eof()) {
node_attribs.clear(); map<string, string> node_attribs;
nm = r.readTag(node_attribs); string nm = r.readTag(node_attribs);
if (nm == "EOF") { if (nm == "EOF") {
break; break;
@ -744,7 +733,7 @@ void XML_Node::build(std::istream& f)
} }
int lnum = r.m_line; int lnum = r.m_line;
if (nm[nm.size() - 1] == '/') { if (nm[nm.size() - 1] == '/') {
nm2 = nm.substr(0,nm.size()-1); string nm2 = nm.substr(0,nm.size()-1);
if (first) { if (first) {
node->setName(nm2); node->setName(nm2);
first = false; first = false;
@ -763,8 +752,7 @@ void XML_Node::build(std::istream& f)
} else { } else {
node = &node->addChild(nm); node = &node->addChild(nm);
} }
val = r.readValue(); node->addValue(r.readValue());
node->addValue(val);
node->attribs() = node_attribs; node->attribs() = node_attribs;
node->setLineNumber(lnum); node->setLineNumber(lnum);
} else if (nm.substr(0,2) == "--") { } else if (nm.substr(0,2) == "--") {
@ -783,7 +771,6 @@ void XML_Node::build(std::istream& f)
void XML_Node::copyUnion(XML_Node* const node_dest) const void XML_Node::copyUnion(XML_Node* const node_dest) const
{ {
XML_Node* sc, *dc;
node_dest->addValue(m_value); node_dest->addValue(m_value);
if (m_name == "") { if (m_name == "") {
return; return;
@ -795,9 +782,9 @@ void XML_Node::copyUnion(XML_Node* const node_dest) const
} }
const vector<XML_Node*> &vsc = node_dest->children(); const vector<XML_Node*> &vsc = node_dest->children();
for (size_t n = 0; n < m_children.size(); n++) { for (size_t n = 0; n < m_children.size(); n++) {
sc = m_children[n]; XML_Node* sc = m_children[n];
size_t ndc = node_dest->nChildren(); size_t ndc = node_dest->nChildren();
dc = 0; XML_Node* dc = 0;
if (! sc->m_iscomment) { if (! sc->m_iscomment) {
for (size_t idc = 0; idc < ndc; idc++) { for (size_t idc = 0; idc < ndc; idc++) {
XML_Node* dcc = vsc[idc]; XML_Node* dcc = vsc[idc];
@ -828,7 +815,6 @@ void XML_Node::copyUnion(XML_Node* const node_dest) const
void XML_Node::copy(XML_Node* const node_dest) const void XML_Node::copy(XML_Node* const node_dest) const
{ {
XML_Node* sc, *dc;
node_dest->addValue(m_value); node_dest->addValue(m_value);
node_dest->setName(m_name); node_dest->setName(m_name);
node_dest->setLineNumber(m_linenum); node_dest->setLineNumber(m_linenum);
@ -841,11 +827,11 @@ void XML_Node::copy(XML_Node* const node_dest) const
const vector<XML_Node*> &vsc = node_dest->children(); const vector<XML_Node*> &vsc = node_dest->children();
for (size_t n = 0; n < m_children.size(); n++) { for (size_t n = 0; n < m_children.size(); n++) {
sc = m_children[n]; XML_Node* sc = m_children[n];
size_t ndc = node_dest->nChildren(); size_t ndc = node_dest->nChildren();
// Here is where we do a malloc of the child node. // Here is where we do a malloc of the child node.
node_dest->addChild(sc->name()); node_dest->addChild(sc->name());
dc = vsc[ndc]; XML_Node* dc = vsc[ndc];
sc->copy(dc); sc->copy(dc);
} }
} }
@ -879,13 +865,11 @@ std::vector<XML_Node*> XML_Node::getChildren(const std::string& nm) const
XML_Node& XML_Node::child(const std::string& aloc) const XML_Node& XML_Node::child(const std::string& aloc) const
{ {
string::size_type iloc;
string cname;
string loc = aloc; string loc = aloc;
while (true) { while (true) {
iloc = loc.find('/'); size_t iloc = loc.find('/');
if (iloc != string::npos) { if (iloc != string::npos) {
cname = loc.substr(0,iloc); string cname = loc.substr(0,iloc);
loc = loc.substr(iloc+1, loc.size()); loc = loc.substr(iloc+1, loc.size());
auto i = m_childindex.find(cname); auto i = m_childindex.find(cname);
if (i != m_childindex.end()) { if (i != m_childindex.end()) {
@ -1044,37 +1028,32 @@ XML_Node* findXMLPhase(XML_Node* root,
const std::string& idtarget) const std::string& idtarget)
{ {
XML_Node* scResult = 0; XML_Node* scResult = 0;
XML_Node* sc;
if (!root) { if (!root) {
return 0; return 0;
} }
string idattrib; if (root->name() == "phase") {
string rname = root->name();
if (rname == "phase") {
if (idtarget == "") { if (idtarget == "") {
return root; return root;
} }
idattrib = root->id(); if (idtarget == root->id()) {
if (idtarget == idattrib) {
return root; return root;
} }
} }
const vector<XML_Node*> &vsc = root->children(); const vector<XML_Node*> &vsc = root->children();
for (size_t n = 0; n < root->nChildren(); n++) { for (size_t n = 0; n < root->nChildren(); n++) {
sc = vsc[n]; XML_Node* sc = vsc[n];
if (sc->name() == "phase") { if (sc->name() == "phase") {
if (idtarget == "") { if (idtarget == "") {
return sc; return sc;
} }
idattrib = sc->id(); if (idtarget == sc->id()) {
if (idtarget == idattrib) {
return sc; return sc;
} }
} }
} }
for (size_t n = 0; n < root->nChildren(); n++) { for (size_t n = 0; n < root->nChildren(); n++) {
sc = vsc[n]; XML_Node* sc = vsc[n];
scResult = findXMLPhase(sc, idtarget); scResult = findXMLPhase(sc, idtarget);
if (scResult) { if (scResult) {
return scResult; return scResult;