IniFile: Support extending the list of loaded keys and sections with data from other ini files.

Changes a lot of parsing code which previously was not aware of the notion of
key/value, and operated only with raw lines. Now key/value is the default and
lines are handled as raw only if they do not contain =, or they start with $ or
+ (for Gecko/AR compatibility).
This commit is contained in:
Pierre Bourdon
2013-09-06 19:56:19 +02:00
parent d1e96c7282
commit cf4c39d2be
2 changed files with 97 additions and 99 deletions

View File

@ -6,11 +6,21 @@
#ifndef _INIFILE_H_
#define _INIFILE_H_
#include <map>
#include <string>
#include <set>
#include <vector>
#include "StringUtil.h"
struct CaseInsensitiveStringCompare
{
bool operator() (const std::string& a, const std::string& b) const
{
return strcasecmp(a.c_str(), b.c_str()) < 0;
}
};
class IniFile
{
public:
@ -25,7 +35,6 @@ public:
bool Exists(const char *key) const;
bool Delete(const char *key);
std::string* GetLine(const char* key, std::string* valueOut);
void Set(const char* key, const char* newValue);
void Set(const char* key, const std::string& newValue, const std::string& defaultValue);
@ -68,12 +77,24 @@ public:
}
protected:
std::vector<std::string> lines;
std::string name;
std::vector<std::string> keys_order;
std::map<std::string, std::string, CaseInsensitiveStringCompare> values;
std::vector<std::string> lines;
};
bool Load(const char* filename);
bool Load(const std::string &filename) { return Load(filename.c_str()); }
/**
* Loads sections and keys.
* @param filename filename of the ini file which should be loaded
* @param keep_current_data If true, "extends" the currently loaded list of sections and keys with the loaded data (and replaces existing entries). If false, existing data will be erased.
* @warning Using any other operations than "Get*" and "Exists" is untested and will behave unexpectedly
* @todo This really is just a hack to support having two levels of gameinis (defaults and user-specified) and should eventually be replaced with a less stupid system.
*/
bool Load(const char* filename, bool keep_current_data = false);
bool Load(const std::string &filename, bool keep_current_data = false) { return Load(filename.c_str(), keep_current_data); }
bool Save(const char* filename);
bool Save(const std::string &filename) { return Save(filename.c_str()); }