Core: INI_ fixed reloading of config

git-svn-id: svn://ultimatepp.org/upp/trunk@5185 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2012-07-13 11:34:27 +00:00
parent c1ff9e2fcc
commit ebe090e53d
2 changed files with 60 additions and 40 deletions

View file

@ -50,11 +50,12 @@ VectorMap<String, String> LoadIniFile(const char *filename) {
static StaticMutex sMtx;
static char sIniFile[256];
static bool s_ini_loaded;
static int64 s_ini_version = 1;
void ReloadIniFile()
{
s_ini_loaded = false;
Mutex::Lock __(sMtx);
s_ini_version++;
}
void SetIniFile(const char *name) {
@ -63,11 +64,21 @@ void SetIniFile(const char *name) {
ReloadIniFile();
}
bool sIniChanged(int64& version)
{
if(version != s_ini_version) {
version = s_ini_version;
return true;
}
return false;
}
String GetIniKey(const char *id, const String& def) {
Mutex::Lock __(sMtx);
static VectorMap<String, String> key;
if(!s_ini_loaded) {
s_ini_loaded = true;
static int64 version;
if(sIniChanged(version)) {
key = LoadIniFile(*sIniFile ? sIniFile : ~ConfigFile("q.ini"));
#ifdef PLATFORM_WIN32
if(key.GetCount() == 0)
@ -90,21 +101,24 @@ String GetIniKey(const char *id)
IniString::operator String()
{
ONCELOCK_(loaded) {
static Array<String> ss;
String& x = ss.Add();
x = TrimBoth(GetIniKey(id));
if(IsNull(x))
x = (*def)();
value = &x;
String x;
{
Mutex::Lock __(sMtx);
String& s = (*ref_fn)();
if(sIniChanged(version)) {
s = TrimBoth(GetIniKey(id));
if(IsNull(s))
s = (*def)();
}
x = s;
}
return *value;
return x;
}
String IniString::operator=(const String& s)
{
operator String();
*value = s;
Mutex::Lock __(sMtx);
(*ref_fn)() = s;
return s;
}
@ -146,7 +160,8 @@ int64 ReadIniInt(const char *id)
}
IniInt::operator int() {
ONCELOCK_(loaded) {
Mutex::Lock __(sMtx);
if(sIniChanged(version)) {
value = (int)ReadIniInt(id);
if(IsNull(value))
value = (*def)();
@ -155,7 +170,7 @@ IniInt::operator int() {
}
int IniInt::operator=(int b) {
ONCELOCK_(loaded) {}
Mutex::Lock __(sMtx);
return value = b;
}
@ -166,7 +181,8 @@ String IniInt::ToString() const
IniInt64::operator int64()
{
ONCELOCK_(loaded) {
Mutex::Lock __(sMtx);
if(sIniChanged(version)) {
value = ReadIniInt(id);
if(IsNull(value))
value = (*def)();
@ -176,7 +192,7 @@ IniInt64::operator int64()
int64 IniInt64::operator=(int64 b)
{
ONCELOCK_(loaded) {}
Mutex::Lock __(sMtx);
return value = b;
}
@ -187,7 +203,8 @@ String IniInt64::ToString() const
IniDouble::operator double()
{
ONCELOCK_(loaded) {
Mutex::Lock __(sMtx);
if(sIniChanged(version)) {
value = ScanDouble(TrimBoth(ToLower(GetIniKey(id))));
if(IsNull(value))
value = (*def)();
@ -197,7 +214,7 @@ IniDouble::operator double()
double IniDouble::operator=(double b)
{
ONCELOCK_(loaded) {}
Mutex::Lock __(sMtx);
return value = b;
}
@ -207,7 +224,8 @@ String IniDouble::ToString() const
}
IniBool::operator bool() {
ONCELOCK_(loaded) {
Mutex::Lock __(sMtx);
if(sIniChanged(version)) {
String h = TrimBoth(ToLower(GetIniKey(id)));
if(h.GetCount())
value = h == "1" || h == "yes" || h == "true" || h == "y";
@ -218,7 +236,7 @@ IniBool::operator bool() {
}
bool IniBool::operator=(bool b) {
ONCELOCK_(loaded) {}
Mutex::Lock __(sMtx);
return value = b;
}

View file

@ -32,9 +32,9 @@ String GetIniKey(const char *id);
struct IniString {
// "private":
const char *id;
String (*def)();
bool loaded;
String *value;
String (*def)();
String& (*ref_fn)();
int64 version;
// "public:"
operator String();
String operator=(const String& s);
@ -45,48 +45,48 @@ struct IniInt {
// "private":
const char *id;
int (*def)();
bool loaded;
int64 version;
int value;
// "public:"
operator int();
int operator=(int b);
String ToString() const;
String ToString() const;
};
struct IniInt64 {
// "private":
const char *id;
int64 (*def)();
bool loaded;
int64 version;
int64 value;
// "public:"
operator int64();
int64 operator=(int64 b);
String ToString() const;
String ToString() const;
};
struct IniDouble {
// "private":
const char *id;
double (*def)();
bool loaded;
int64 version;
double value;
// "public:"
operator double();
double operator=(double b);
String ToString() const;
String ToString() const;
};
struct IniBool {
// "private":
const char *id;
bool (*def)();
bool loaded;
int64 version;
bool value;
// "public:"
operator bool();
bool operator=(bool b);
String ToString() const;
String ToString() const;
};
void AddIniInfo(const char *id, String (*current)(), String (*def)(), const char *info);
@ -101,18 +101,20 @@ struct IniInfo {
const Array<IniInfo> GetIniInfo();
String GetIniInfoFormatted();
#define INI_TYPE(var, def, info, type, decl)\
#define INI_TYPE(var, def, info, type, decl, ref)\
type DefIni_##var() { return def; }\
decl var = { #var, DefIni_##var };\
decl var = { #var, DefIni_##var, ref };\
String AsStringIniCurrent_##var() { return AsString(var); } \
String AsStringIniDefault_##var() { return AsString(DefIni_##var()); } \
INITBLOCK { AddIniInfo(#var, AsStringIniCurrent_##var, AsStringIniDefault_##var, info); }
#define INI_BOOL(var, def, info) INI_TYPE(var, def, info, bool, IniBool);
#define INI_STRING(var, def, info) INI_TYPE(var, def, info, String, IniString);
#define INI_INT(var, def, info) INI_TYPE(var, def, info, int, IniInt);
#define INI_INT64(var, def, info) INI_TYPE(var, def, info, int64, IniInt64);
#define INI_DOUBLE(var, def, info) INI_TYPE(var, def, info, double, IniDouble);
#define INI_BOOL(var, def, info) INI_TYPE(var, def, info, bool, IniBool, 0);
#define INI_INT(var, def, info) INI_TYPE(var, def, info, int, IniInt, 0);
#define INI_INT64(var, def, info) INI_TYPE(var, def, info, int64, IniInt64, 0);
#define INI_DOUBLE(var, def, info) INI_TYPE(var, def, info, double, IniDouble, 0);
#define INI_STRING(var, def, info) String& DefRef##_var() { static String x; return x; }\
INI_TYPE(var, def, info, String, IniString, DefRef##_var);
/*
#define INI_BOOL(var, def, info)\