CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Data/Format/IniFile.h
Views: 1401
// IniFile1// Taken from Dolphin but relicensed by me, Henrik Rydgard, under the MIT2// license as I wrote the whole thing originally and it has barely changed.34#pragma once56#include <istream>7#include <memory>8#include <map>9#include <string>10#include <string_view>11#include <vector>12#include <cstdint>1314#include "Common/File/Path.h"1516class VFSInterface;1718class ParsedIniLine {19public:20ParsedIniLine() {}21ParsedIniLine(std::string_view key, std::string_view value) {22this->key = key;23this->value = value;24}25ParsedIniLine(std::string_view key, std::string_view value, std::string_view comment) {26this->key = key;27this->value = value;28this->comment = comment;29}30static ParsedIniLine CommentOnly(std::string_view comment) {31return ParsedIniLine(std::string_view(), std::string_view(), comment);32}3334// Comments only come from "ParseFrom".35void ParseFrom(std::string_view line);36void Reconstruct(std::string *output) const;3738// Having these as views allows a more efficient internal representation, like one joint string.39std::string_view Key() const { return key; }40std::string_view Value() const { return value; }41std::string_view Comment() const { return comment; }4243void SetValue(std::string_view newValue) { value = newValue; }4445private:46std::string key;47std::string value;48std::string comment;49};5051class Section {52friend class IniFile;5354public:55Section() {}56Section(std::string_view name) : name_(name) {}5758bool Exists(std::string_view key) const;59bool Delete(std::string_view key);6061void Clear();6263std::map<std::string, std::string> ToMap() const;6465ParsedIniLine *GetLine(std::string_view key);66const ParsedIniLine *GetLine(std::string_view key) const;6768void Set(std::string_view key, const char* newValue);69void Set(std::string_view key, const std::string& newValue, const std::string& defaultValue);7071void Set(std::string_view key, const std::string &value) {72Set(key, value.c_str());73}74bool Get(std::string_view key, std::string* value, const char* defaultValue) const;7576void Set(std::string_view key, uint32_t newValue);77void Set(std::string_view key, uint64_t newValue);78void Set(std::string_view key, float newValue);79void Set(std::string_view key, const float newValue, const float defaultValue);80void Set(std::string_view key, double newValue);8182void Set(std::string_view key, int newValue, int defaultValue);83void Set(std::string_view key, int newValue);8485void Set(std::string_view key, bool newValue, bool defaultValue);86void Set(std::string_view key, bool newValue) {87Set(key, newValue ? "True" : "False");88}89void Set(std::string_view key, const std::vector<std::string>& newValues);9091// Declare without a body to make it fail to compile. This is to prevent accidentally92// setting a pointer as a bool. The failure is in the linker unfortunately, but that's better93// than accidentally succeeding in a bad way.94template<class T>95void Set(std::string_view key, T *ptr);9697void AddComment(const std::string &comment);9899bool Get(std::string_view key, int* value, int defaultValue = 0) const;100bool Get(std::string_view key, uint32_t* value, uint32_t defaultValue = 0) const;101bool Get(std::string_view key, uint64_t* value, uint64_t defaultValue = 0) const;102bool Get(std::string_view key, bool* value, bool defaultValue = false) const;103bool Get(std::string_view key, float* value, float defaultValue = false) const;104bool Get(std::string_view key, double* value, double defaultValue = false) const;105bool Get(std::string_view key, std::vector<std::string>& values) const;106107// Return a list of all keys in this section108bool GetKeys(std::vector<std::string> &keys) const;109110bool operator < (const Section& other) const {111return name_ < other.name_;112}113114const std::string &name() const {115return name_;116}117118protected:119std::vector<ParsedIniLine> lines_;120std::string name_;121std::string comment;122};123124class IniFile {125public:126bool Load(const Path &path);127bool Load(std::istream &istream);128bool LoadFromVFS(VFSInterface &vfs, const std::string &filename);129130bool Save(const Path &path);131132// Returns true if key exists in section133bool Exists(const char* sectionName, const char* key) const;134135// These will not create the section if it doesn't exist.136bool Get(const char* sectionName, const char* key, std::string* value, const char* defaultValue = "");137bool Get(const char* sectionName, const char* key, int* value, int defaultValue = 0);138bool Get(const char* sectionName, const char* key, uint32_t* value, uint32_t defaultValue = 0);139bool Get(const char* sectionName, const char* key, uint64_t* value, uint64_t defaultValue = 0);140bool Get(const char* sectionName, const char* key, bool* value, bool defaultValue = false);141bool Get(const char* sectionName, const char* key, std::vector<std::string>& values);142143bool GetKeys(const char* sectionName, std::vector<std::string>& keys) const;144145bool DeleteKey(const char* sectionName, const char* key);146bool DeleteSection(const char* sectionName);147148void SortSections();149150std::vector<std::unique_ptr<Section>> &Sections() { return sections; }151152bool HasSection(const char *section) { return GetSection(section) != nullptr; }153const Section* GetSection(const char* section) const;154Section* GetSection(const char* section);155156Section* GetOrCreateSection(const char* section);157158private:159std::vector<std::unique_ptr<Section>> sections;160};161162163