common/config: code formatting cleanup

NOTE:  please use hardtabs (at least in drivers/sdl and drivers/common/config*)
  devs: we should set a coding standard for future code/patches that we should be trying to conform to to prevent a mishmash of tabs/spaces and  un-elegant code
This commit is contained in:
punkrockguy318 2012-03-06 06:10:49 +00:00
parent 878c379242
commit 06c8230c88
1 changed files with 96 additions and 88 deletions

View File

@ -576,6 +576,7 @@ char* Config::getConfigDirectory()
return strdup(_dir.c_str());
}
// load and parse the default configuration file
int
Config::_load()
{
@ -585,14 +586,15 @@ Config::_load()
return success;
}
// load and parse a given configuration file
int
Config::_loadFile(const char* fname)
{
signed int pos, eqPos;
std::fstream config;
std::map<std::string, int>::iterator int_i;
std::map<std::string, double>::iterator dbl_i;
std::map<std::string, std::string>::iterator str_i;
signed int pos, eqPos;
std::fstream config;
std::map<std::string, int>::iterator int_i;
std::map<std::string, double>::iterator dbl_i;
std::map<std::string, std::string>::iterator str_i;
std::string configFile;
// if no filename argument was passed, parse the default configuration file
if(fname == NULL)
@ -602,60 +604,61 @@ Config::_loadFile(const char* fname)
{
configFile = fname;
}
std::string line, name, value;
char buf[1024];
std::string line, name, value;
char buf[1024];
// set the exception handling to catch i/o errors
config.exceptions(std::fstream::badbit);
// set the exception handling to catch i/o errors
config.exceptions(std::fstream::badbit);
try {
// open the file for reading (create if it doesn't exist)
config.open(configFile.c_str(), std::ios::in | std::ios::out);
if(!config.is_open()) {
// XXX file couldn't be opened?
return 0;
}
try {
// open the file for reading (create if it doesn't exist)
config.open(configFile.c_str(), std::ios::in | std::ios::out);
if(!config.is_open()) {
// XXX file couldn't be opened?
return 0;
}
while(!config.eof()) {
// read a line
config.getline(buf, 1024);
line = buf;
while(!config.eof()) {
// read a line
config.getline(buf, 1024);
line = buf;
// check line validity
eqPos = line.find("=");
if(line[0] == '#') {
// skip this line
continue;
}
// check line validity
eqPos = line.find("=");
if(line[0] == '#') {
// skip this line
continue;
}
// get the name and value for the option
pos = line.find(" ");
name = line.substr(0, (pos > eqPos) ? eqPos : pos);
pos = line.find_first_not_of(" ", eqPos + 1);
if (pos == std::string::npos) value = "";
else value = line.substr(pos);
// get the name and value for the option
pos = line.find(" ");
name = line.substr(0, (pos > eqPos) ? eqPos : pos);
pos = line.find_first_not_of(" ", eqPos + 1);
if (pos == std::string::npos)
value = "";
else value = line.substr(pos);
// check if the option exists, and if so, set it appropriately
str_i = _strOptMap.find(name);
dbl_i = _dblOptMap.find(name);
int_i = _intOptMap.find(name);
if(str_i != _strOptMap.end()) {
str_i->second = value;
} else if(int_i != _intOptMap.end()) {
int_i->second = atol(value.c_str());
} else if(dbl_i != _dblOptMap.end()) {
dbl_i->second = atof(value.c_str());
}
}
// check if the option exists, and if so, set it appropriately
str_i = _strOptMap.find(name);
dbl_i = _dblOptMap.find(name);
int_i = _intOptMap.find(name);
if(str_i != _strOptMap.end()) {
str_i->second = value;
} else if(int_i != _intOptMap.end()) {
int_i->second = atol(value.c_str());
} else if(dbl_i != _dblOptMap.end()) {
dbl_i->second = atof(value.c_str());
}
}
// close the file
config.close();
} catch(std::fstream::failure e) {
std::cerr << e.what() << std::endl;
return -1;
}
// close the file
config.close();
} catch(std::fstream::failure e) {
std::cerr << e.what() << std::endl;
return -1;
}
return 0;
return 0;
}
/**
@ -664,48 +667,53 @@ Config::_loadFile(const char* fname)
int
Config::save()
{
std::fstream config;
std::map<std::string, int>::iterator int_i;
std::map<std::string, double>::iterator dbl_i;
std::map<std::string, std::string>::iterator str_i;
std::string configFile = _dir + "/" + cfgFile;
char buf[1024];
std::fstream config;
std::map<std::string, int>::iterator int_i;
std::map<std::string, double>::iterator dbl_i;
std::map<std::string, std::string>::iterator str_i;
std::string configFile = _dir + "/" + cfgFile;
char buf[1024];
// set the exception handling to catch i/o errors
config.exceptions(std::ios::failbit | std::ios::badbit);
// set the exception handling to catch i/o errors
config.exceptions(std::ios::failbit | std::ios::badbit);
try {
// open the file, truncate and for write
config.open(configFile.c_str(), std::ios::out | std::ios::trunc);
try
{
// open the file, truncate and for write
config.open(configFile.c_str(), std::ios::out | std::ios::trunc);
// write a warning
strcpy(buf, "# Auto-generated\n# SDL keysyms defined in /usr/include/SDL/SDL_keysym.h\n# getSDLKey can be found \
// write a warning
strcpy(buf, "# Auto-generated\n# SDL keysyms defined in /usr/include/SDL/SDL_keysym.h\n# getSDLKey can be found \
in the source directory and can assist in remapping hotkeys\n#\n");
config.write(buf, strlen(buf));
config.write(buf, strlen(buf));
// write each configuration setting
for(int_i = _intOptMap.begin(); int_i != _intOptMap.end(); int_i++) {
snprintf(buf, 1024, "%s = %d\n",
int_i->first.c_str(), int_i->second);
config.write(buf, strlen(buf));
}
for(dbl_i = _dblOptMap.begin(); dbl_i != _dblOptMap.end(); dbl_i++) {
snprintf(buf, 1024, "%s = %f\n",
dbl_i->first.c_str(), dbl_i->second);
config.write(buf, strlen(buf));
}
for(str_i = _strOptMap.begin(); str_i != _strOptMap.end(); str_i++) {
snprintf(buf, 1024, "%s = %s\n",
str_i->first.c_str(), str_i->second.c_str());
config.write(buf, strlen(buf));
}
// write each configuration setting
for(int_i = _intOptMap.begin(); int_i != _intOptMap.end(); int_i++)
{
snprintf(buf, 1024, "%s = %d\n",
int_i->first.c_str(), int_i->second);
config.write(buf, strlen(buf));
}
for(dbl_i = _dblOptMap.begin(); dbl_i != _dblOptMap.end(); dbl_i++)
{
snprintf(buf, 1024, "%s = %f\n",
dbl_i->first.c_str(), dbl_i->second);
config.write(buf, strlen(buf));
}
for(str_i = _strOptMap.begin(); str_i != _strOptMap.end(); str_i++)
{
snprintf(buf, 1024, "%s = %s\n",
str_i->first.c_str(), str_i->second.c_str());
config.write(buf, strlen(buf));
}
// close the file
config.close();
} catch(std::fstream::failure e) {
std::cerr << e.what() << std::endl;
return -1;
}
// close the file
config.close();
} catch(std::fstream::failure e)
{
std::cerr << e.what() << std::endl;
return -1;
}
return 0;
return 0;
}