Initial commit
This commit is contained in:
64
src/ini_handler.h
Normal file
64
src/ini_handler.h
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
SWR INI Tool - Switchroot INI Configuration Editor
|
||||
Copyright (C) 2026 Switchroot
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
class IniHandler
|
||||
{
|
||||
public:
|
||||
IniHandler();
|
||||
|
||||
bool load(const std::string& path);
|
||||
bool save();
|
||||
bool save(const std::string& path);
|
||||
|
||||
std::string get(const std::string& section, const std::string& key, const std::string& defaultValue = "") const;
|
||||
int getInt(const std::string& section, const std::string& key, int defaultValue = 0) const;
|
||||
bool getBool(const std::string& section, const std::string& key, bool defaultValue = false) const;
|
||||
|
||||
void set(const std::string& section, const std::string& key, const std::string& value);
|
||||
void setInt(const std::string& section, const std::string& key, int value);
|
||||
void setBool(const std::string& section, const std::string& key, bool value);
|
||||
|
||||
bool hasSection(const std::string& section) const;
|
||||
bool hasKey(const std::string& section, const std::string& key) const;
|
||||
|
||||
std::vector<std::string> getSections() const;
|
||||
std::vector<std::pair<std::string, std::string>> getKeys(const std::string& section) const;
|
||||
|
||||
// Find the first section that is not "config" (the OS-specific section)
|
||||
std::string findOsSection() const;
|
||||
|
||||
const std::string& getFilePath() const { return filePath; }
|
||||
bool isLoaded() const { return loaded; }
|
||||
|
||||
private:
|
||||
struct Line
|
||||
{
|
||||
enum Type { BLANK, COMMENT, SECTION, KEY_VALUE };
|
||||
Type type;
|
||||
std::string raw;
|
||||
std::string section;
|
||||
std::string key;
|
||||
std::string value;
|
||||
};
|
||||
|
||||
std::vector<Line> lines;
|
||||
std::string filePath;
|
||||
bool loaded;
|
||||
|
||||
std::string rebuildLine(const Line& line) const;
|
||||
int findLine(const std::string& section, const std::string& key) const;
|
||||
int findSectionEnd(const std::string& section) const;
|
||||
};
|
||||
Reference in New Issue
Block a user