|
| 1 | +#include "IniParser.h" |
| 2 | +#include "exceptions.h" |
| 3 | +#include <iostream> |
| 4 | +#include <fstream> |
| 5 | +#include <algorithm> |
| 6 | + |
| 7 | + |
| 8 | +IniParser::IniParser(std::string filename){ |
| 9 | + // конструктор, принимающий имя файла |
| 10 | + |
| 11 | + filename_ = filename; |
| 12 | + std::ifstream source_file = std::ifstream(this->filename_); |
| 13 | + std::string current_section; |
| 14 | + unsigned int line_number = 0; |
| 15 | + |
| 16 | + if (source_file) { |
| 17 | + while (source_file) { |
| 18 | + std::string line; |
| 19 | + std::getline(source_file, line); |
| 20 | + line_number++; |
| 21 | + |
| 22 | + // заполнение data |
| 23 | + if (this->get_line_type(line) == LineType::comment) { |
| 24 | + // если это комментарий, пропускаем строку |
| 25 | + |
| 26 | + } else if (this->get_line_type(line) == LineType::section) { |
| 27 | + // если это описание секции: |
| 28 | + this->process_section_line(line, current_section); |
| 29 | + |
| 30 | + } else if (this->get_line_type(line) == LineType::field) { |
| 31 | + // если это описание значения: |
| 32 | + this->process_field_line(line, current_section, line_number); |
| 33 | + |
| 34 | + } else if (this->get_line_type(line) == LineType::unknown) { |
| 35 | + std::cout << "Warning: bad syntax in line " << line_number << std::endl; |
| 36 | + } |
| 37 | + } |
| 38 | + } else { |
| 39 | + throw NoSuchFile(); |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +// деструктр |
| 44 | +IniParser::~IniParser() {}; |
| 45 | + |
| 46 | +std::string IniParser::_get_value(const std::string& value_path) { |
| 47 | + auto section_field = this->split_line(value_path, '.'); |
| 48 | + auto section = section_field[0]; |
| 49 | + auto field = section_field[1]; |
| 50 | + if (this->data.find(section) != this->data.end()) { |
| 51 | + if (this->data[section].find(field) != this->data[section].end()) { |
| 52 | + std::string value = this->data[section][field]; |
| 53 | + return value; |
| 54 | + } else { |
| 55 | + std::cout << "No field " << field << " in section " << section << std::endl; |
| 56 | + std::cout << "You probably meant: " << std::endl; |
| 57 | + for (auto el : data[section]) { |
| 58 | + std::cout << el.first << std::endl; |
| 59 | + }; |
| 60 | + throw NoSuchFieldInSection(); |
| 61 | + } |
| 62 | + } else { |
| 63 | + throw NoSuchSection(); |
| 64 | + } |
| 65 | +} |
| 66 | +void IniParser::process_section_line(std::string& line, std::string& current_section) { |
| 67 | + /* |
| 68 | + метод процессинга строки с секцией: |
| 69 | + - line - строка, |
| 70 | + - current_section - текущая секция в цикле обработки файла. |
| 71 | + */ |
| 72 | + |
| 73 | + line.erase(std::remove(line.begin(), line.end(), '['), line.end()); |
| 74 | + line.erase(std::remove(line.begin(), line.end(), ']'), line.end()); |
| 75 | + line.erase(std::remove(line.begin(), line.end(), ' '), line.end()); |
| 76 | + current_section = line; |
| 77 | + |
| 78 | + if (this->data.find(current_section) == this->data.end()) { |
| 79 | + // если такой секции ещё нет |
| 80 | + std::map<std::string, std::string> empty; |
| 81 | + this->data.insert({line, empty}); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +void IniParser::process_field_line(std::string& line, const std::string& current_section, const int& line_number) { |
| 86 | + /* |
| 87 | + метод процессинга строки с полем (парой ключ-значение): |
| 88 | + - line - строка, |
| 89 | + - current_section - текущая секция в цикле обработки файла, |
| 90 | + - line_number - номер строки в файле. |
| 91 | + */ |
| 92 | + |
| 93 | + auto key_value = this->split_line(line, '='); |
| 94 | + |
| 95 | + // подготовка ключа (удаление всех символов пробела) |
| 96 | + auto key = key_value[0]; |
| 97 | + key.erase(std::remove(key.begin(), key.end(), ' '), key.end()); |
| 98 | + // подготовка значения (удаление символов пробела, идущих до первого символа) |
| 99 | + if (key_value.size() > 1) { |
| 100 | + auto value = key_value[1]; |
| 101 | + auto value_start = value.find_first_not_of(' '); |
| 102 | + value = value.substr(value_start != std::string::npos ? value_start : 0); |
| 103 | + // удаление комментария из строки значения |
| 104 | + auto comment_start_pos = value.find(";"); |
| 105 | + value = value.substr(0, comment_start_pos); |
| 106 | + this->data[current_section][key] = value; |
| 107 | + } else { |
| 108 | + // если значения нет, ничего не добавляем |
| 109 | + } |
| 110 | + |
| 111 | +} |
| 112 | + |
| 113 | +// метод для получения значения конкретного поля файла |
| 114 | +template <typename VALUE_TYPE> |
| 115 | +VALUE_TYPE IniParser::get_value(std::string value_path) { |
| 116 | + static_assert(sizeof(VALUE_TYPE) == -1, "not implemented type for get_value"); |
| 117 | +}; |
| 118 | + |
| 119 | +template <> |
| 120 | +std::string IniParser::get_value(std::string value_path) { |
| 121 | + std::string value = this->_get_value(value_path); |
| 122 | + return value; |
| 123 | +}; |
| 124 | + |
| 125 | +template <> |
| 126 | +int IniParser::get_value(std::string value_path) { |
| 127 | + std::string value = this->_get_value(value_path); |
| 128 | + return std::stoi(value); |
| 129 | +}; |
| 130 | + |
| 131 | +template <> |
| 132 | +unsigned short IniParser::get_value(std::string value_path) { |
| 133 | + std::string value = this->_get_value(value_path); |
| 134 | + return std::stoul(value); |
| 135 | +}; |
| 136 | + |
| 137 | +template <> |
| 138 | +float IniParser::get_value(std::string value_path) { |
| 139 | + std::string value = this->_get_value(value_path); |
| 140 | + return std::stof(value); |
| 141 | +}; |
0 commit comments