Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Sep 9th, 2010 | Syntax: C++ | Size: 3.18 KB | Hits: 37 | Expires: Never
Copy text to clipboard
  1. #include "Config.h"
  2.  
  3. Config::Config(std::string ConfigFileName) {
  4.         this->configFileName = ConfigFileName;
  5.  
  6.         this->fileStream.open(this->configFileName);
  7.         if (this->fileStream.is_open()) {
  8.                 this->searchPattern = boost::regex("^([ \\t]+)?([^#][^\\s]+)\\s*=\\s*(?:(\\d+)|(?:\"([^\"]*)\")).*$");
  9.                 this->ReloadConfig();
  10.         }
  11. }
  12.  
  13. void Config::lineParser()
  14. {
  15.         //--keep thread alive until file is fully pushed into lineBuffer and lineBuffer is empty
  16.         while(this->isFileFinish == false || this->lineBuffer.size() != 0)
  17.         {
  18.                 //--wait until lines for parsing are avaible
  19.                 if (this->lineBuffer.size() == 0)
  20.                         boost::this_thread::sleep(boost::posix_time::millisec(1));
  21.                 //--
  22.                 else
  23.                 {
  24.                         boost::match_results<std::string::const_iterator> matches;
  25.                         //--lock lineBuffer object
  26.                         this->lineBufferMutex.lock();
  27.                         //copy first line into temp var
  28.                         std::string line = *this->lineBuffer.begin();
  29.                        
  30.                         //remove first line from lineBuffer
  31.                         this->lineBuffer.pop_front();
  32.                         this->lineBufferMutex.unlock();
  33.                         //--
  34.  
  35.                         if (boost::regex_match(line, matches, this->searchPattern))
  36.                         {
  37.                                 //--insert match into our configMap (we have to lock our configMap object first)
  38.                                 this->configMapMutex.lock();
  39.                                 if (matches[3].matched)
  40.                                         //insert number (without qoutes)
  41.                                         this->configMap.insert(std::make_pair(matches[2], matches[3]));
  42.                                 else
  43.                                         //insert string (in quoutes)
  44.                                         this->configMap.insert(std::make_pair(matches[2], matches[4]));
  45.                                 this->configMapMutex.unlock();
  46.                                 //--
  47.                         }
  48.                 }
  49.                 //--
  50.         }
  51. }
  52.  
  53. void Config::ReloadConfig() {
  54.         //--reset
  55.         this->configMap.clear();
  56.         this->fileStream.seekg(0, ios::beg);
  57.         this->isFileFinish = false;
  58.         //--
  59.  
  60.         //--define variables
  61.         std::string line;
  62.         boost::thread_group threadPool;
  63.         //--
  64.  
  65.         //--create parser-threads for each logical cpu
  66.         for(uint32 i = 0; i <  boost::thread::hardware_concurrency(); i++)
  67.         {
  68.                 threadPool.create_thread(boost::bind(&Config::lineParser, this));
  69.         }
  70.         //--
  71.  
  72.         //--push lines from file in our lineBuffer
  73.         while(std::getline(this->fileStream, line))
  74.         {
  75.                 this->lineBuffer.push_back(line);
  76.         }
  77.         //--
  78.         this->isFileFinish = true;
  79.         //wait until all lines are parsed
  80.         threadPool.join_all();
  81. }
  82.  
  83. std::string Config::GetStringDefault(std::string Name, std::string DefaultValue)
  84. {
  85.         stringMapType::const_iterator configNode = this->configMap.find(Name);
  86.         if(configNode != this->configMap.end())
  87.                 return configNode->second;
  88.         else
  89.                 return DefaultValue;
  90. }
  91.  
  92. bool Config::GetBoolDefault(std::string Name, bool DefaultValue)
  93. {
  94.         std::string baseValue = this->GetStringDefault(Name, "");
  95.         if (baseValue == "")
  96.                 return DefaultValue;
  97.         else
  98.                 return boost::lexical_cast<bool>(baseValue);
  99. }
  100. int Config::GetIntDefault(std::string Name, int DefaultValue)
  101. {
  102.         std::string baseValue = this->GetStringDefault(Name, "");
  103.         if (baseValue == "")
  104.                 return DefaultValue;
  105.         else
  106.                 return boost::lexical_cast<int>(baseValue);
  107. }
  108.  
  109. float Config::GetFloatDefault(std::string Name, float DefaultValue)
  110. {
  111.         std::string baseValue = this->GetStringDefault(Name, "");
  112.         if (baseValue == "")
  113.                 return DefaultValue;
  114.         else
  115.                 return boost::lexical_cast<float>(baseValue);
  116. }
  117.  
  118. Config::~Config() {
  119.         this->fileStream.close();
  120. }