Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.cpp
- #include <cassert>
- #include <sstream>
- // в этой задаче ситуация обратная:
- // меняйте только файлы ini.cpp, ini.h
- // а main.cpp трогать не нужно
- #include "ini.h"
- using namespace std;
- int main() {
- std::istringstream input{
- "[vegetables]\n"
- "potatoes=10\n"
- "onions=1 \n"
- "\n"
- "cucumbers=12\n"
- "\n"
- "[guests] \n"
- "guest1_name = Ivan Durak\n"
- "guest2_name = Vasilisa Premudraya\n"
- "[guest black list]"};
- ini::Document doc = ini::Load(input);
- assert(doc.GetSectionCount() == 3);
- assert((doc.GetSection("vegetables"s)
- == ini::Section{
- {"potatoes"s, "10"s},
- {"onions"s, "1"s},
- {"cucumbers"s, "12"s},
- }));
- assert((doc.GetSection("guests"s)
- == ini::Section{{"guest1_name"s, "Ivan Durak"s}, {"guest2_name"s, "Vasilisa Premudraya"s}}));
- assert((doc.GetSection("dragons"s) == ini::Section{}));
- assert((doc.GetSection("guest black list"s) == ini::Section{}));
- doc.AddSection("pets"s) = ini::Section{{"nasty"s, "rat"s}};
- assert(doc.GetSectionCount() == 4);
- }
- // ini.h
- #include <iostream>
- #include <string>
- #include <unordered_map>
- // поместите все имена библиотеки INI в пространство имён ini
- namespace ini {
- using Section = std::unordered_map<std::string, std::string>;
- class Document {
- public:
- // реализация методов должна быть в файле ini.cpp
- Section& AddSection(std::string name);
- const Section& GetSection(const std::string& name) const;
- std::size_t GetSectionCount() const;
- private:
- std::unordered_map<std::string, Section> sections_;
- };
- // определение этой функции должно быть в файле ini.cpp
- Document Load(std::istream& input);
- }
- // ini.cpp
- #include "ini.h"
- using namespace std;
- // место для реализаций методов и функций библиотеки ini
- // не забудьте, что они должны быть помещены в namespace ini
- namespace ini {
- Section& Document::AddSection(std::string name) {
- return sections_[name];
- }
- const Section& Document::GetSection(const std::string& name) const {
- static Section static_empty;
- if (sections_.find(name) == sections_.end()) {
- return static_empty;
- }
- return sections_.at(name);
- }
- std::size_t Document::GetSectionCount() const {
- return sections_.size();
- }
- Document Load(std::istream& input) {
- Document result;
- Section* section = nullptr;
- for (string line; getline(input, line);) {
- auto pos = line.find_first_not_of(' ', 0);
- if (pos == line.npos) {
- continue;
- }
- line = line.substr(pos);
- if (line[0] == '[') {
- string section_name = line.substr(1, line.find(']') - 1);
- section = &result.AddSection(section_name);
- }
- else {
- pos = line.find('=');
- string unit_name = line.substr(0, line.find_last_not_of(' ', pos - 1) + 1);
- line = line.substr(line.find_first_not_of(' ', pos + 1));
- string unit_value = line.substr(0, line.find_last_not_of(' ') + 1);
- section->insert({ unit_name, unit_value });
- }
- }
- return result;
- }
- }
Add Comment
Please, Sign In to add comment