Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*==========================================
- scfgpars.h
- ===========================================*/
- #ifndef SCFGPARS_H_INCLUDED
- #define SCFGPARS_H_INCLUDED
- #include <iostream>
- #include <fstream>
- #include <sys/stat.h>
- #include <map>
- #include <sstream>
- class scfgPars
- {
- public:
- scfgPars();
- void load(const std::string);
- void setEntry(std::string, std::string);
- int getInt(const std::string, int);
- bool getBool(const std::string, bool);
- std::string getString(const std::string, std::string);
- private:
- void runTest();
- bool fileExists(const std::string&);
- bool isInt(const std::string&);
- bool isBool(const std::string&);
- std::string intToString(int);
- std::string boolToString(bool);
- int stringToInt(std::string);
- bool stringToBool(std::string);
- unsigned int findIdLine(const std::string);
- void getFileData();
- private:
- std::ifstream iFile;
- std::ofstream oFile;
- std::map <std::string, std::pair<std::string, int> > cfgEntries;
- std::map <std::string, std::pair<std::string, int> >::iterator entriesIt;
- std::map <int, std::string> cfgFile;
- std::map <int, std::string>::iterator fileIt;
- std::string line;
- const char * charFileName;
- };
- #endif // SCFGPARS_H_INCLUDED
- /*==========================================
- scfgpars.cpp
- ===========================================*/
- #include "scfgPars.h"
- //========================= main functions =========================
- scfgPars::scfgPars()
- {
- }
- void scfgPars::load(const std::string filename)
- {
- charFileName = filename.c_str();
- if(!fileExists(filename))
- {
- oFile.open(charFileName);
- oFile.close();
- }
- iFile.open(charFileName);
- getFileData();
- std::cout << "Configuration file loaded" << std::endl;
- runTest();
- }
- //========================= utility functions =========================
- void scfgPars::runTest()
- {
- setEntry("dub", "dub5");
- setEntry("settest2", "test");
- std::string strtest = getString("strtest1", "strtest");
- int inttest = getInt("inttest1", 42);
- bool booltest = getBool("booltest1", "true");
- for(entriesIt=cfgEntries.begin(); entriesIt!=cfgEntries.end(); ++entriesIt)
- {
- std::cout << "line: " << entriesIt->second.second << "\t\t" << entriesIt->first << " = " << entriesIt->second.first << std::endl;
- }
- std::cout << "====================================================================" << std::endl;
- for(fileIt=cfgFile.begin(); fileIt!=cfgFile.end(); ++fileIt)
- {
- std::cout << "line: " << fileIt->first << "\t\t" << fileIt->second << std::endl;
- }
- }
- bool scfgPars::fileExists(const std::string& filename)
- {
- struct stat buf;
- if (stat(filename.c_str(), &buf) != -1)
- {
- return true;
- }
- return false;
- }
- bool scfgPars::isInt(const std::string& s)
- {
- std::string::const_iterator it = s.begin();
- while (it != s.end() && std::isdigit(*it)) ++it;
- return !s.empty() && it == s.end();
- }
- bool scfgPars::isBool(const std::string& s)
- {
- return s == "true" || s == "false";
- }
- std::string scfgPars::intToString(int i)
- {
- std::ostringstream temp;
- temp << i;
- return temp.str();
- }
- std::string scfgPars::boolToString(bool b)
- {
- if(b == true)
- return "true";
- else
- return "false";
- }
- int scfgPars::stringToInt(std::string str)
- {
- std::stringstream temp( str );
- int i;
- temp >> i;
- if (temp.fail())
- {
- std::cout << "fatal error: could not convert string to int";
- return 0;
- }
- else
- {
- return i;
- }
- }
- bool scfgPars::stringToBool(std::string str)
- {
- if(str == "true")
- return true;
- else
- return false;
- }
- //========================= file parser =========================
- void scfgPars::getFileData()
- {
- std::string* buffer;
- std::string id, value;
- int lineNumber = 1;
- while(std::getline(iFile, line))
- {
- cfgFile [lineNumber] = line;
- if(line.find("=") != std::string::npos && line.at(0) != '=')
- {
- buffer = &id;
- int Ecounter = 0; // equals counter
- id.clear();
- value.clear();
- for (unsigned i=0; i<line.length(); ++i)
- {
- char lineChar = line.at(i);
- if(lineChar == '=')
- {
- Ecounter++;
- buffer = &value;
- }
- if(Ecounter > 1) break;
- if(lineChar != ' ' && lineChar != '=')
- *buffer += lineChar;
- }
- if(Ecounter == 1)
- cfgEntries [id] = std::make_pair(value, lineNumber);
- }
- lineNumber++;
- }
- }
- //========================= get and set functions =========================
- void scfgPars::setEntry(std::string id, std::string value)
- {
- int lineNumber = 0;
- if (cfgEntries.count(id) == 1)
- lineNumber = cfgEntries[id].second;
- else
- lineNumber = cfgFile.end()->first +1;
- cfgEntries [id] = std::make_pair(value, lineNumber);
- cfgFile [lineNumber] = id + " = " + value;
- oFile.open(charFileName);
- for(fileIt=cfgFile.begin(); fileIt!=cfgFile.end(); ++fileIt)
- {
- oFile << fileIt->second << std::endl;
- }
- oFile.close();
- }
- int scfgPars::getInt(const std::string id, int dint)
- {
- if(cfgEntries.count(id) == 1)
- {
- const std::string output = cfgEntries.find(id)->second.first;
- if(!isInt(output))
- {
- setEntry(id, intToString(dint));
- return dint;
- }
- else
- {
- return stringToInt(output);
- }
- }
- else
- {
- setEntry(id, intToString(dint));
- return dint;
- }
- }
- bool scfgPars::getBool(const std::string id, bool dbool)
- {
- if(cfgEntries.count(id) == 1)
- {
- const std::string output = cfgEntries.find(id)->second.first;
- if(!isBool(output))
- {
- setEntry(id, boolToString(dbool));
- return dbool;
- }
- else
- {
- return stringToBool(output);
- }
- }
- else
- {
- setEntry(id, boolToString(dbool));
- return dbool;
- }
- }
- std::string scfgPars::getString(const std::string id, std::string dstring)
- {
- if(cfgEntries.count(id) == 1)
- {
- return cfgEntries.find(id)->second.first;
- }
- else
- {
- setEntry(id, dstring);
- return dstring;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement