sanek2005_15

ini reader

Apr 20th, 2021
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. class IniFile
  7. {
  8. private:
  9.     class Section
  10.     {
  11.     public:
  12.         class Parameter
  13.         {
  14.         private:
  15.             string parameterName;
  16.             string value;
  17.  
  18.         public:
  19.  
  20.             string GetName()
  21.             {
  22.                 return parameterName;
  23.             }
  24.  
  25.             string GetValue()
  26.             {
  27.                 return value;
  28.             }
  29.  
  30.             Parameter(string name, string value)
  31.             {
  32.                 this->parameterName = name;
  33.                 this->value = value;
  34.             }
  35.         };
  36.  
  37.         Parameter GetParameterByName(string name)
  38.         {
  39.             for (int i = 0; i < parameters.size(); i++)
  40.             {
  41.                 if (parameters[i].GetName() == name)
  42.                     return parameters[i];
  43.             }
  44.             throw runtime_error("parameter not found!");
  45.         }
  46.  
  47.         void AddParameter(string name, string value)
  48.         {
  49.             parameters.push_back(Parameter(name, value));
  50.         }
  51.  
  52.         string GetName()
  53.         {
  54.             return name;
  55.         }
  56.  
  57.         Section(string name)
  58.         {
  59.             this->name = name;
  60.         }
  61.  
  62.     private:
  63.         vector<Parameter> parameters;
  64.         string name;
  65.     };
  66.  
  67.     void AddSection(string name)
  68.     {
  69.         sections.push_back(Section(name));
  70.     }
  71.  
  72.     vector<Section> sections;
  73. public:
  74.     Section GetSectionByName(string name)
  75.     {
  76.         for (int i = 0; i < sections.size(); i++)
  77.         {
  78.             if (sections[i].GetName() == name)
  79.                 return sections[i];
  80.         }
  81.         throw runtime_error("section not found!");
  82.     }
  83.  
  84.     void LoadFile(string path)
  85.     {
  86.         ifstream file;
  87.         file.open(path);
  88.  
  89.         if (!file.is_open())
  90.         {
  91.             throw runtime_error("not doest open file!");
  92.         }
  93.         else
  94.         {
  95.             while (true)
  96.             {
  97.                 string temp = "";
  98.                 if (file.get() == '[')
  99.                 {                
  100.                     while (file.get() != ']')
  101.                     {
  102.                         temp += file.get();
  103.                     }
  104.                 }
  105.                 AddSection(temp);
  106.                 break;
  107.             }
  108.         }
  109.         file.close();
  110.     }
  111.  
  112. #ifdef _DEBUG
  113.     void ShowAllSections()
  114.     {
  115.         for (int i = 0; i < sections.size(); i++)
  116.         {
  117.             cout << sections[i].GetName() << endl;
  118.         }
  119.     }
  120. #endif // DEBUG
  121.  
  122.     IniFile()
  123.     {
  124.  
  125.     }
  126. };
  127.  
  128. int main()
  129. {
  130.     IniFile ini;
  131.     ini.LoadFile("test.ini");
  132.     ini.ShowAllSections();
  133.     return 0;
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment