extended CParser to be able to read/save any string

git-svn-id: https://clonekeenplus.svn.sourceforge.net/svnroot/clonekeenplus/cgenius/trunk@154 4df4b0f3-56ce-47cb-b001-ed939b7d65a6
This commit is contained in:
albertzeyer
2009-07-24 17:24:56 +00:00
parent fd1d4e1b32
commit 6c0db530e2
2 changed files with 24 additions and 11 deletions

View File

@@ -79,7 +79,7 @@ bool CParser::saveParseFile() // open, write on the file and close
// read the value of the to be seeked keyword and returns it as an int.
// If no value was detected, it returns -1;
// If something was detected, the file is also rewinded!
int CParser::getIntValue(const std::string& keyword, const std::string& category)
std::string CParser::getValue(const std::string& keyword, const std::string& category, const std::string& def)
{
// The getter will search for category and than for keyword. After that, read the value and return it!
for(std::list<std::string>::iterator line = m_filebuffer.begin() ; line != m_filebuffer.end() ; ++line )
@@ -95,29 +95,26 @@ int CParser::getIntValue(const std::string& keyword, const std::string& category
if((*line)[0] == '[') break;
if(subStrCaseEqual(*line, keyword + " =", keyword.size() + 2))
{
int value = from_string<int>(line->substr(keyword.size() + 2));
return value;
}
return line->substr(keyword.size() + 2);
}
}
break;
}
}
return -1;
return def;
}
// This function saves the value of a keyword. If the value already exists in the file
// it will be overwritten.
// If something was written the file is also rewinded!
void CParser::saveIntValue(const std::string& keyword, const std::string& category,int value)
void CParser::saveValue(const std::string& keyword, const std::string& category, const std::string& value)
{
// Three cases:
// 1.- category doesn't exist
// 2.- category exists, but keyword not
// 3.- category and keyword exist, only the value must be changed
std::string newline = keyword + " = " + itoa(value);
std::string newline = keyword + " = " + value;
for(std::list<std::string>::iterator line = m_filebuffer.begin() ; line != m_filebuffer.end() ; ++line )
{
@@ -159,3 +156,17 @@ void CParser::saveIntValue(const std::string& keyword, const std::string& catego
}
int CParser::getIntValue(const std::string& keyword, const std::string& category, int def) {
std::string str = getValue(keyword, category);
if(str == "") return def;
bool fail = false;
int v = from_string<int>(str, fail);
if(fail) return def;
return v;
}
void CParser::saveIntValue(const std::string& keyword, const std::string& category,int value) {
saveValue(keyword, category, itoa(value));
}

View File

@@ -25,15 +25,17 @@ public:
bool saveParseFile();
// read the value of the to be seeked keyword and returns it as an int.
// If no value was detected, it returns -1;
// If something was detected, the file is also rewinded!
int getIntValue(const std::string& keyword, const std::string& category);
// If no value was detected, it returns def;
int getIntValue(const std::string& keyword, const std::string& category, int def = -1);
// This function saves the value of a keyword. If the value already exists in the file
// it will be overwritten.
// If something was written the file is also rewinded!
void saveIntValue(const std::string& keyword, const std::string& category, int value);
std::string getValue(const std::string& keyword, const std::string& category, const std::string& def = "");
void saveValue(const std::string& keyword, const std::string& category, const std::string& value);
bool isOpen() {return m_isOpen;}
private: