148 lines
3.2 KiB
C++
148 lines
3.2 KiB
C++
/*
|
||
* 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 <https://www.gnu.org/licenses/>.
|
||
*/
|
||
|
||
|
||
|
||
#pragma once
|
||
|
||
#include <fstream>
|
||
#include <string>
|
||
#include <vector>
|
||
#include <filesystem>
|
||
#include <sstream>
|
||
#include <algorithm>
|
||
#include <cctype>
|
||
|
||
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<std::string> Get(const std::string& key)
|
||
{
|
||
std::vector<std::string> tmp;
|
||
Parse(key, tmp);
|
||
return tmp;
|
||
}
|
||
|
||
private:
|
||
const char sectionStart = '{';
|
||
const char sectionEnd = '}';
|
||
|
||
std::ifstream file;
|
||
|
||
//-----------------------------------------------------------------------------
|
||
// Wyszukiwanie danych po kluczu
|
||
//-----------------------------------------------------------------------------
|
||
void Parse(const std::string& key, std::vector<std::string>& 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<63>g jest kluczem
|
||
//-----------------------------------------------------------------------------
|
||
bool CheckKey(std::string key)
|
||
{
|
||
if (key[0] == sectionStart && key[key.length() - 1])
|
||
{
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
//-----------------------------------------------------------------------------
|
||
// Zamie<69> ca<63>y ci<63>g na du<64>e litery
|
||
//-----------------------------------------------------------------------------
|
||
std::string UpperString(std::string s) {
|
||
std::transform(s.begin(), s.end(), s.begin(),
|
||
[](unsigned char c) { return static_cast<char>(std::toupper(c)); });
|
||
return s;
|
||
}
|
||
};
|
||
|