/* * This file is part of VoidArchiveTool. * * Copyright (C) 2025 Yanczi * * Void Archive Toolis free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #pragma once #include #include #include #include #include #include #include class Txtpp { public: Txtpp(const std::string& path = "") { if (path != "") { Load(path); } } ~Txtpp() { if (file.is_open()) { file.close(); } } bool Load(const std::string& path) { if (!std::filesystem::exists(path)) { return false; } file.open(path); return file.is_open(); } void Close() { file.close(); } std::vector Get(const std::string& key) { std::vector tmp; Parse(key, tmp); return tmp; } template T getValue(const std::string& key, const std::string& val) { std::vector tmp; Parse(key, tmp); for (const auto& line : tmp) { std::string cleanLine = RemoveSpaces(line); std::string t; std::string v; bool tv = false; for (const char& c : cleanLine) { if (c != ":") {tv = true;} if (!tv) { t += c; } else { v += c; } } } return tmp; } private: const char sectionStart = '{'; const char sectionEnd = '}'; std::ifstream file; //----------------------------------------------------------------------------- // Wyszukiwanie danych po kluczu //----------------------------------------------------------------------------- void Parse(const std::string& key, std::vector& data) { std::string fullkey = sectionStart + key + sectionEnd; std::string line; bool wr = false; file.clear(); file.seekg(std::ios::beg); while (getline(file, line)) { std::string tmp = RemoveSpaces(line); if (tmp != "") { if (CheckKey(tmp)) { wr = UpperString(tmp) == fullkey ? true : false; } else { if (wr) { data.push_back(tmp); } } } } } //----------------------------------------------------------------------------- // Usuwa spacje //----------------------------------------------------------------------------- std::string RemoveSpaces(std::string _line) { std::stringstream ss(_line); char word; std::string tmp; std::string beforeWord = ""; while (ss >> word) { tmp += word; } return tmp; } //----------------------------------------------------------------------------- // Sprawdza czy dany ciąg jest kluczem //----------------------------------------------------------------------------- bool CheckKey(std::string key) { if (key[0] == sectionStart && key[key.length() - 1]) { return true; } return false; } //----------------------------------------------------------------------------- // Zamień cały ciąg na duże litery //----------------------------------------------------------------------------- std::string UpperString(std::string s) { std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return static_cast(std::toupper(c)); }); return s; } };