Guest User

Untitled

a guest
Nov 20th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <fstream>
  4. #include <map>
  5.  
  6. class ConfigFile
  7. {
  8.     public:
  9.         ConfigFile(const std::string& filename)
  10.         {
  11.             std::ifstream file(filename.c_str());
  12.             std::string line;
  13.             while( std::getline(file, line) )
  14.             {
  15.                 std::istringstream iss(line);
  16.                 std::string key, value;
  17.                 iss >> key;
  18.                 iss >> value;
  19.                 m_conf.insert( std::pair<std::string, std::string>(key,value) );
  20.             }
  21.             file.close();
  22.         }
  23.  
  24.         template<typename T>
  25.         const T& operator[](const std::string& key)
  26.         {
  27.             std::ostringstream oss(m_conf[key]);
  28.             T result;
  29.             result << oss;
  30.             return result;
  31.         }
  32.  
  33.     private:
  34.         std::map<std::string, std::string> m_conf;
  35. };
  36.  
  37.  
  38. int main()
  39. {
  40.     ConfigFile cf("config.in");
  41.     double altitude = cf["altitude"];
  42.  
  43.     std::cout << altitude << std::endl;
  44.  
  45.     return 0;
  46. }
Add Comment
Please, Sign In to add comment