2008-01-16 11:45:17 +00:00
|
|
|
#include "CfgParser.h"
|
|
|
|
#include "StringUtilities.h"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
namespace BlueCore
|
2008-01-16 11:45:17 +00:00
|
|
|
{
|
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
void CfgParser::parseFile(const std::string& filename)
|
|
|
|
{
|
|
|
|
ifstream file(filename.c_str(), ios::in);
|
|
|
|
std::string line;
|
2008-01-16 11:45:17 +00:00
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
while (file.good())
|
|
|
|
{
|
|
|
|
getline(file, line);
|
|
|
|
parseLine(line);
|
|
|
|
}
|
|
|
|
}
|
2008-01-16 11:45:17 +00:00
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
void CfgParser::parseLine(const std::string& line)
|
2008-01-16 11:45:17 +00:00
|
|
|
{
|
2008-01-20 10:16:37 +00:00
|
|
|
std::string::size_type i(line.find_first_of('='));
|
2008-01-16 11:45:17 +00:00
|
|
|
|
|
|
|
if (i == std::string::npos)
|
|
|
|
return;
|
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
std::string key = trim(string(line, 0, i-1));
|
|
|
|
std::string value = trim(string(line, i+1));
|
2008-01-16 11:45:17 +00:00
|
|
|
|
|
|
|
_Pairs[key] = value;
|
|
|
|
}
|
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
void CfgParser::parse(const char* buffer, unsigned int length)
|
2008-01-16 11:45:17 +00:00
|
|
|
{
|
2008-01-20 10:16:37 +00:00
|
|
|
const char* ptr = buffer;
|
|
|
|
const char* end = buffer + length;
|
|
|
|
unsigned int l = 0;
|
2008-01-16 11:45:17 +00:00
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
while (ptr < end)
|
|
|
|
{
|
|
|
|
// find end of line
|
|
|
|
while (ptr[l] != '\n')
|
|
|
|
{
|
|
|
|
l++;
|
|
|
|
if (ptr + l >= end)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
parseLine(string(ptr, l));
|
|
|
|
ptr += l+1;
|
|
|
|
l = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-01-16 11:45:17 +00:00
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
double CfgParser::get(const std::string& key, double defaultValue)
|
2008-01-16 11:45:17 +00:00
|
|
|
{
|
2008-01-20 10:16:37 +00:00
|
|
|
double value;
|
2008-01-16 11:45:17 +00:00
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
if (getDouble(key, value) == false)
|
|
|
|
return defaultValue;
|
|
|
|
else
|
|
|
|
return value;
|
2008-01-16 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
bool CfgParser::getDouble(const std::string& key, double& value)
|
2008-01-16 11:45:17 +00:00
|
|
|
{
|
2008-01-20 10:16:37 +00:00
|
|
|
std::map<std::string, std::string>::const_iterator result;
|
|
|
|
result = _Pairs.find(key);
|
|
|
|
|
|
|
|
if (result != _Pairs.end())
|
|
|
|
{
|
|
|
|
value = atof(result->second.c_str());
|
|
|
|
return true;
|
2008-01-16 11:45:17 +00:00
|
|
|
}
|
2008-01-20 10:16:37 +00:00
|
|
|
else
|
|
|
|
return false;
|
2008-01-16 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
int CfgParser::get(const std::string& key, int defaultValue)
|
2008-01-16 11:45:17 +00:00
|
|
|
{
|
2008-01-20 10:16:37 +00:00
|
|
|
int value;
|
2008-01-16 11:45:17 +00:00
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
if (getInteger(key, value) == false)
|
|
|
|
return defaultValue;
|
|
|
|
else
|
|
|
|
return value;
|
2008-01-16 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
bool CfgParser::getInteger(const std::string& key, int& value)
|
2008-01-16 11:45:17 +00:00
|
|
|
{
|
2008-01-20 10:16:37 +00:00
|
|
|
std::map<std::string, std::string>::const_iterator result;
|
|
|
|
result = _Pairs.find(key);
|
|
|
|
|
|
|
|
if (result != _Pairs.end())
|
|
|
|
{
|
|
|
|
value = atoi(result->second.c_str());
|
|
|
|
return true;
|
2008-01-16 11:45:17 +00:00
|
|
|
}
|
2008-01-20 10:16:37 +00:00
|
|
|
else
|
|
|
|
return false;
|
2008-01-16 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
bool CfgParser::get(const std::string& key, bool defaultValue)
|
2008-01-16 11:45:17 +00:00
|
|
|
{
|
2008-01-20 10:16:37 +00:00
|
|
|
bool value;
|
2008-01-16 11:45:17 +00:00
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
if (getBoolean(key, value) == false)
|
|
|
|
return defaultValue;
|
|
|
|
else
|
|
|
|
return value;
|
2008-01-16 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
bool CfgParser::getBoolean(const std::string& key, bool& value)
|
2008-01-16 11:45:17 +00:00
|
|
|
{
|
2008-01-20 10:16:37 +00:00
|
|
|
std::map<std::string, std::string>::const_iterator result;
|
|
|
|
result = _Pairs.find(key);
|
|
|
|
|
|
|
|
if (result != _Pairs.end())
|
2008-01-16 11:45:17 +00:00
|
|
|
{
|
2008-01-20 10:16:37 +00:00
|
|
|
value = false;
|
2008-01-16 11:45:17 +00:00
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
if (result->second == "true")
|
|
|
|
value = true;
|
|
|
|
else if (result->second == "1")
|
|
|
|
value = true;
|
|
|
|
else if (result->second == "yes")
|
|
|
|
value = true;
|
2008-01-16 11:45:17 +00:00
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2008-01-16 11:45:17 +00:00
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
std::string CfgParser::get(const std::string& key, std::string defaultValue)
|
2008-01-16 11:45:17 +00:00
|
|
|
{
|
2008-01-20 10:16:37 +00:00
|
|
|
std::string value;
|
2008-01-16 11:45:17 +00:00
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
if (getString(key, value) == false)
|
|
|
|
return defaultValue;
|
|
|
|
else
|
|
|
|
return value;
|
2008-01-16 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
bool CfgParser::getString(const std::string& key, std::string& value)
|
2008-01-16 11:45:17 +00:00
|
|
|
{
|
2008-01-20 10:16:37 +00:00
|
|
|
std::map<std::string, std::string>::const_iterator result;
|
|
|
|
result = _Pairs.find(key);
|
|
|
|
|
|
|
|
if (result != _Pairs.end())
|
|
|
|
{
|
2008-01-16 11:45:17 +00:00
|
|
|
value = result->second;
|
2008-01-20 10:16:37 +00:00
|
|
|
return true;
|
2008-01-16 11:45:17 +00:00
|
|
|
}
|
2008-01-20 10:16:37 +00:00
|
|
|
else
|
|
|
|
return false;
|
2008-01-16 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
bool CfgParser::getStrings(const std::string& key, std::vector<string>& strings)
|
|
|
|
{
|
|
|
|
std::map<std::string, std::string>::const_iterator result;
|
|
|
|
result = _Pairs.find(key);
|
2008-01-16 11:45:17 +00:00
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
if (result != _Pairs.end())
|
|
|
|
{
|
|
|
|
explode(result->second, strings, true, ",");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2008-01-16 11:45:17 +00:00
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
bool CfgParser::getDoubles(const std::string& key, std::vector<double>& doubles)
|
2008-01-16 11:45:17 +00:00
|
|
|
{
|
2008-01-20 10:16:37 +00:00
|
|
|
std::map<std::string, std::string>::const_iterator result;
|
|
|
|
result = _Pairs.find(key);
|
2008-01-16 11:45:17 +00:00
|
|
|
|
2008-01-20 10:16:37 +00:00
|
|
|
if (result != _Pairs.end())
|
|
|
|
{
|
|
|
|
std::vector<std::string> strings;
|
|
|
|
explode(result->second, strings, true, ",");
|
|
|
|
std::vector<std::string>::iterator i;
|
|
|
|
for (i = strings.begin(); i != strings.end(); i++)
|
|
|
|
doubles.push_back(atof((*i).c_str()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2008-01-16 11:45:17 +00:00
|
|
|
|
|
|
|
} // namespace BlueCore
|
|
|
|
|