2013-04-18 03:09:55 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 04:46:09 +00:00
|
|
|
|
|
|
|
|
2009-03-28 08:57:34 +00:00
|
|
|
#ifndef _INIFILE_H_
|
|
|
|
#define _INIFILE_H_
|
2008-12-08 04:46:09 +00:00
|
|
|
|
|
|
|
#include <string>
|
2010-06-03 18:05:08 +00:00
|
|
|
#include <vector>
|
2008-12-08 04:46:09 +00:00
|
|
|
|
|
|
|
#include "StringUtil.h"
|
|
|
|
|
2010-06-04 19:56:34 +00:00
|
|
|
class IniFile
|
2008-12-08 04:46:09 +00:00
|
|
|
{
|
2010-06-03 18:05:08 +00:00
|
|
|
public:
|
2010-06-04 19:56:34 +00:00
|
|
|
class Section
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2010-06-05 05:30:23 +00:00
|
|
|
friend class IniFile;
|
2010-06-04 19:56:34 +00:00
|
|
|
|
2010-06-05 05:30:23 +00:00
|
|
|
public:
|
|
|
|
Section() {}
|
|
|
|
Section(const std::string& _name) : name(_name) {}
|
2010-06-04 19:56:34 +00:00
|
|
|
|
|
|
|
bool Exists(const char *key) const;
|
2010-06-05 05:30:23 +00:00
|
|
|
bool Delete(const char *key);
|
2010-06-04 19:56:34 +00:00
|
|
|
|
|
|
|
std::string* GetLine(const char* key, std::string* valueOut, std::string* commentOut);
|
|
|
|
void Set(const char* key, const char* newValue);
|
2010-06-05 05:30:23 +00:00
|
|
|
void Set(const char* key, const std::string& newValue, const std::string& defaultValue);
|
|
|
|
|
2010-06-04 19:56:34 +00:00
|
|
|
void Set(const std::string &key, const std::string &value) {
|
|
|
|
Set(key.c_str(), value.c_str());
|
|
|
|
}
|
|
|
|
bool Get(const char* key, std::string* value, const char* defaultValue);
|
|
|
|
|
|
|
|
void Set(const char* key, u32 newValue) {
|
|
|
|
Set(key, StringFromFormat("0x%08x", newValue).c_str());
|
|
|
|
}
|
|
|
|
void Set(const char* key, float newValue) {
|
|
|
|
Set(key, StringFromFormat("%f", newValue).c_str());
|
|
|
|
}
|
2010-06-05 05:30:23 +00:00
|
|
|
void Set(const char* key, const float newValue, const float defaultValue);
|
2010-06-04 19:56:34 +00:00
|
|
|
void Set(const char* key, double newValue) {
|
|
|
|
Set(key, StringFromFormat("%f", newValue).c_str());
|
|
|
|
}
|
2010-07-19 10:36:30 +00:00
|
|
|
|
|
|
|
void Set(const char* key, int newValue, int defaultValue);
|
2010-06-04 19:56:34 +00:00
|
|
|
void Set(const char* key, int newValue) {
|
|
|
|
Set(key, StringFromInt(newValue).c_str());
|
|
|
|
}
|
2010-07-19 10:36:30 +00:00
|
|
|
|
|
|
|
void Set(const char* key, bool newValue, bool defaultValue);
|
2010-06-04 19:56:34 +00:00
|
|
|
void Set(const char* key, bool newValue) {
|
|
|
|
Set(key, StringFromBool(newValue).c_str());
|
|
|
|
}
|
|
|
|
void Set(const char* key, const std::vector<std::string>& newValues);
|
|
|
|
|
|
|
|
bool Get(const char* key, int* value, int defaultValue = 0);
|
|
|
|
bool Get(const char* key, u32* value, u32 defaultValue = 0);
|
|
|
|
bool Get(const char* key, bool* value, bool defaultValue = false);
|
|
|
|
bool Get(const char* key, float* value, float defaultValue = false);
|
|
|
|
bool Get(const char* key, double* value, double defaultValue = false);
|
|
|
|
bool Get(const char* key, std::vector<std::string>& values);
|
|
|
|
|
|
|
|
bool operator < (const Section& other) const {
|
|
|
|
return name < other.name;
|
|
|
|
}
|
2010-06-03 04:55:39 +00:00
|
|
|
|
2010-06-05 05:30:23 +00:00
|
|
|
protected:
|
|
|
|
std::vector<std::string> lines;
|
|
|
|
std::string name;
|
|
|
|
std::string comment;
|
|
|
|
};
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 18:05:08 +00:00
|
|
|
bool Load(const char* filename);
|
2010-06-04 19:56:34 +00:00
|
|
|
bool Load(const std::string &filename) { return Load(filename.c_str()); }
|
2010-06-03 18:05:08 +00:00
|
|
|
bool Save(const char* filename);
|
2010-06-04 19:56:34 +00:00
|
|
|
bool Save(const std::string &filename) { return Save(filename.c_str()); }
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-04 19:56:34 +00:00
|
|
|
// Returns true if key exists in section
|
2010-06-03 18:05:08 +00:00
|
|
|
bool Exists(const char* sectionName, const char* key) const;
|
2009-03-20 11:51:22 +00:00
|
|
|
|
2010-06-04 19:56:34 +00:00
|
|
|
// TODO: Get rid of these, in favor of the Section ones.
|
|
|
|
void Set(const char* sectionName, const char* key, const char* newValue) {
|
|
|
|
GetOrCreateSection(sectionName)->Set(key, newValue);
|
|
|
|
}
|
|
|
|
void Set(const char* sectionName, const char* key, const std::string& newValue) {
|
|
|
|
GetOrCreateSection(sectionName)->Set(key, newValue.c_str());
|
|
|
|
}
|
|
|
|
void Set(const char* sectionName, const char* key, int newValue) {
|
|
|
|
GetOrCreateSection(sectionName)->Set(key, newValue);
|
|
|
|
}
|
|
|
|
void Set(const char* sectionName, const char* key, u32 newValue) {
|
|
|
|
GetOrCreateSection(sectionName)->Set(key, newValue);
|
|
|
|
}
|
|
|
|
void Set(const char* sectionName, const char* key, bool newValue) {
|
|
|
|
GetOrCreateSection(sectionName)->Set(key, newValue);
|
|
|
|
}
|
|
|
|
void Set(const char* sectionName, const char* key, const std::vector<std::string>& newValues) {
|
|
|
|
GetOrCreateSection(sectionName)->Set(key, newValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Get rid of these, in favor of the Section ones.
|
2010-06-03 18:05:08 +00:00
|
|
|
bool Get(const char* sectionName, const char* key, std::string* value, const char* defaultValue = "");
|
|
|
|
bool Get(const char* sectionName, const char* key, int* value, int defaultValue = 0);
|
|
|
|
bool Get(const char* sectionName, const char* key, u32* value, u32 defaultValue = 0);
|
|
|
|
bool Get(const char* sectionName, const char* key, bool* value, bool defaultValue = false);
|
|
|
|
bool Get(const char* sectionName, const char* key, std::vector<std::string>& values);
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2011-02-13 22:36:12 +00:00
|
|
|
template<typename T> bool GetIfExists(const char* sectionName, const char* key, T value)
|
|
|
|
{
|
|
|
|
if (Exists(sectionName, key))
|
|
|
|
return Get(sectionName, key, value);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-03 18:05:08 +00:00
|
|
|
bool GetKeys(const char* sectionName, std::vector<std::string>& keys) const;
|
2010-06-04 19:56:34 +00:00
|
|
|
|
|
|
|
void SetLines(const char* sectionName, const std::vector<std::string> &lines);
|
2010-07-23 05:22:12 +00:00
|
|
|
bool GetLines(const char* sectionName, std::vector<std::string>& lines, const bool remove_comments = true) const;
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 18:05:08 +00:00
|
|
|
bool DeleteKey(const char* sectionName, const char* key);
|
|
|
|
bool DeleteSection(const char* sectionName);
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 18:05:08 +00:00
|
|
|
void SortSections();
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-04 19:56:34 +00:00
|
|
|
Section* GetOrCreateSection(const char* section);
|
2008-12-08 04:46:09 +00:00
|
|
|
|
|
|
|
private:
|
2010-06-04 19:56:34 +00:00
|
|
|
std::vector<Section> sections;
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 18:05:08 +00:00
|
|
|
const Section* GetSection(const char* section) const;
|
|
|
|
Section* GetSection(const char* section);
|
|
|
|
std::string* GetLine(const char* section, const char* key);
|
|
|
|
void CreateSection(const char* section);
|
2008-12-08 04:46:09 +00:00
|
|
|
};
|
|
|
|
|
2009-03-28 08:57:34 +00:00
|
|
|
#endif // _INIFILE_H_
|