Rally X
ELEN3009 Project by Justin Wernick and David Schneider
source/data/Config.cpp
Go to the documentation of this file.
00001 #include "Config.h"
00002 
00003 const string Config::SCREEN_WIDTH_KEY("screen_width");
00004 const string Config::SCREEN_HEIGHT_KEY("screen_height");
00005 const string Config::FULLSCREEN_KEY("fullscreen");
00006 
00007 const string Config::SCREEN_WIDTH_DEFAULT("800");
00008 const string Config::SCREEN_HEIGHT_DEFAULT("600");
00009 const string Config::FULLSCREEN_DEFAULT("false");
00010 
00011 
00012 Config::Config(const string& filename)
00013     :_screenWidth(0),
00014     _screenHeight(0)
00015 {
00016     ifstream inStream(filename.c_str(), fstream::in);
00017 
00018     map<string, string> readValues;
00019     map<string, string> unfoundValues;
00020     readFile(inStream, readValues);
00021     fillValues(readValues, unfoundValues);
00022 
00023     inStream.close();
00024 
00025     ofstream outStream(filename.c_str(), fstream::app);
00026 
00027     writeUnfoundValues(outStream, unfoundValues);
00028 
00029     outStream.close();
00030 }
00031 
00032 unsigned int Config::screenWidth() const
00033 {
00034     return _screenWidth;
00035 }
00036 unsigned int Config::screenHeight() const
00037 {
00038     return _screenHeight;
00039 }
00040 bool Config::fullscreen() const
00041 {
00042     return _fullscreen;
00043 }
00044 
00045 void Config::readFile(ifstream& file, map<string,string>& map)
00046 {
00047     if (!file.is_open()) return;
00048 
00049     string nextEntry;
00050     while(!file.eof())
00051     {
00052         file >> nextEntry;
00053 
00054         string::size_type equalsIndex = nextEntry.find("=",0);
00055         if (equalsIndex!=string::npos)
00056         {
00057             string key = nextEntry.substr(0,equalsIndex);
00058             string value = nextEntry.substr(equalsIndex+1);
00059 
00060             map[key] = value;
00061         }
00062     }
00063 }
00064 
00065 void Config::fillValues(const map<string, string>& readValues, map<string, string>& unfoundValues)
00066 {
00067     setScreenWidth(extractValue(readValues, unfoundValues, SCREEN_WIDTH_KEY, SCREEN_WIDTH_DEFAULT));
00068     setScreenHeight(extractValue(readValues, unfoundValues, SCREEN_HEIGHT_KEY, SCREEN_HEIGHT_DEFAULT));
00069     setFullscreen(extractValue(readValues, unfoundValues, FULLSCREEN_KEY, FULLSCREEN_DEFAULT));
00070 }
00071 
00072 string Config::extractValue(const map<string, string>& readValues, map<string, string>& unfoundValues, const string& key, const string& defaultValue)
00073 {
00074     map<string, string>::const_iterator findIter = readValues.find(key);
00075     if (findIter != readValues.end())
00076     {
00077         return findIter->second;
00078     }
00079     else
00080     {
00081         unfoundValues[key] = defaultValue;
00082         return defaultValue;
00083     }
00084 }
00085 
00086 void Config::writeUnfoundValues(ofstream& file, const map<string, string>& unfoundValues)
00087 {
00088     for (map<string, string>::const_iterator iter = unfoundValues.begin(); iter!=unfoundValues.end(); ++iter)
00089     {
00090         file << iter->first << "=" << iter->second << endl;
00091     }
00092 }
00093 
00094 void Config::setScreenWidth(const string& screenWidthStr)
00095 {
00096     _screenWidth = atoi(screenWidthStr.c_str());
00097 }
00098 void Config::setScreenHeight(const string& screenHeightStr)
00099 {
00100     _screenHeight = atoi(screenHeightStr.c_str());
00101 }
00102 void Config::setFullscreen(const string& fullscreenStr)
00103 {
00104     _fullscreen = fullscreenStr=="true";
00105 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator