kutuzzzov

Урок 4 Синтаксис пространства имён

Mar 6th, 2023
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. // main.cpp
  2.  
  3. #include <cassert>
  4. #include <sstream>
  5.  
  6. // в этой задаче ситуация обратная:
  7. // меняйте только файлы ini.cpp, ini.h
  8. // а main.cpp трогать не нужно
  9. #include "ini.h"
  10.  
  11. using namespace std;
  12.  
  13. int main() {
  14.     std::istringstream input{
  15.         "[vegetables]\n"
  16.         "potatoes=10\n"
  17.         "onions=1 \n"
  18.         "\n"
  19.         "cucumbers=12\n"
  20.         "\n"
  21.         "[guests] \n"
  22.         "guest1_name = Ivan Durak\n"
  23.         "guest2_name =  Vasilisa Premudraya\n"
  24.         "[guest black list]"};
  25.     ini::Document doc = ini::Load(input);
  26.  
  27.     assert(doc.GetSectionCount() == 3);
  28.     assert((doc.GetSection("vegetables"s)
  29.             == ini::Section{
  30.                 {"potatoes"s, "10"s},
  31.                 {"onions"s, "1"s},
  32.                 {"cucumbers"s, "12"s},
  33.             }));
  34.     assert((doc.GetSection("guests"s)
  35.             == ini::Section{{"guest1_name"s, "Ivan Durak"s}, {"guest2_name"s, "Vasilisa Premudraya"s}}));
  36.     assert((doc.GetSection("dragons"s) == ini::Section{}));
  37.     assert((doc.GetSection("guest black list"s) == ini::Section{}));
  38.  
  39.     doc.AddSection("pets"s) = ini::Section{{"nasty"s, "rat"s}};
  40.     assert(doc.GetSectionCount() == 4);
  41. }
  42.  
  43. // ini.h
  44.  
  45. #include <iostream>
  46. #include <string>
  47. #include <unordered_map>
  48.  
  49. // поместите все имена библиотеки INI в пространство имён ini
  50. namespace ini {
  51. using Section = std::unordered_map<std::string, std::string>;
  52.  
  53. class Document {
  54. public:
  55.     // реализация методов должна быть в файле ini.cpp
  56.     Section& AddSection(std::string name);
  57.     const Section& GetSection(const std::string& name) const;
  58.     std::size_t GetSectionCount() const;
  59.  
  60. private:
  61.     std::unordered_map<std::string, Section> sections_;
  62. };
  63.  
  64. // определение этой функции должно быть в файле ini.cpp
  65. Document Load(std::istream& input);
  66. }
  67.  
  68. // ini.cpp
  69.  
  70. #include "ini.h"
  71.  
  72. using namespace std;
  73.  
  74. // место для реализаций методов и функций библиотеки ini
  75. // не забудьте, что они должны быть помещены в namespace ini
  76.  
  77. namespace ini {
  78.     Section& Document::AddSection(std::string name) {
  79.         return sections_[name];
  80.     }
  81.    
  82.     const Section& Document::GetSection(const std::string& name) const {
  83.         static Section static_empty;
  84.             if (sections_.find(name) == sections_.end()) {
  85.                 return static_empty;
  86.             }
  87.         return sections_.at(name);
  88.     }
  89.    
  90.     std::size_t Document::GetSectionCount() const {
  91.         return sections_.size();
  92.     }
  93.    
  94.     Document Load(std::istream& input) {
  95.     Document result;
  96.         Section* section = nullptr;
  97.         for (string line; getline(input, line);) {
  98.             auto pos = line.find_first_not_of(' ', 0);
  99.             if (pos == line.npos) {
  100.                 continue;
  101.             }
  102.             line = line.substr(pos);
  103.             if (line[0] == '[') {
  104.                 string section_name = line.substr(1, line.find(']') - 1);
  105.                 section = &result.AddSection(section_name);
  106.             }
  107.             else {
  108.                 pos = line.find('=');
  109.                 string unit_name = line.substr(0, line.find_last_not_of(' ', pos - 1) + 1);
  110.                 line = line.substr(line.find_first_not_of(' ', pos + 1));
  111.                 string unit_value = line.substr(0, line.find_last_not_of(' ') + 1);
  112.                 section->insert({ unit_name, unit_value });
  113.         }
  114.  
  115.         }
  116.         return result;
  117.         }
  118. }
  119.  
Add Comment
Please, Sign In to add comment