From 4c360b54e3c3f8dcecf11adff9ffd5c2eaef0024 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 10 Jan 2014 16:23:22 +0100 Subject: [PATCH 1/4] Fixed cIniFile's SetValue(). How did we not see this earlier? Each call to SetValue would actually ADD a value! --- lib/inifile/iniFile.cpp | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib/inifile/iniFile.cpp b/lib/inifile/iniFile.cpp index da523e783..5ca6f6618 100644 --- a/lib/inifile/iniFile.cpp +++ b/lib/inifile/iniFile.cpp @@ -384,15 +384,7 @@ bool cIniFile::SetValue(const AString & keyname, const AString & valuename, cons } else { - if (!create) - { - keys[keyID].values[valueID] = value; - } - else - { - keys[keyID].names.resize(keys[keyID].names.size() + 1, valuename); - keys[keyID].values.resize(keys[keyID].values.size() + 1, value); - } + keys[keyID].values[valueID] = value; } return true; From a332a5dc730289cca0357c7387731fd25e0b74cd Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 11 Jan 2014 16:44:28 +0100 Subject: [PATCH 2/4] IniFile: Split SetValue() into AddValue() and SetValue(). Each function does what one would assume - AddValue adds a new value, SetValue overwrites existing value (creates a new one if not exists, if instructed to do so). --- lib/inifile/iniFile.cpp | 85 +++++++++++++++++++++++++++-------------- lib/inifile/iniFile.h | 34 +++++++++++------ 2 files changed, 78 insertions(+), 41 deletions(-) diff --git a/lib/inifile/iniFile.cpp b/lib/inifile/iniFile.cpp index 5ca6f6618..c1fd9c929 100644 --- a/lib/inifile/iniFile.cpp +++ b/lib/inifile/iniFile.cpp @@ -344,47 +344,79 @@ AString cIniFile::GetValueName(const AString & keyname, const int valueID) const -bool cIniFile::SetValue(const int keyID, const int valueID, const AString & value) +void cIniFile::AddValue(const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value) { - if ((keyID < (int)keys.size()) && (valueID < (int)keys[keyID].names.size())) + int keyID = FindKey(a_KeyName); + if (keyID == noID) { - keys[keyID].values[valueID] = value; + keyID = int(AddKeyName(a_KeyName)); } - return false; + + keys[keyID].names.push_back(a_ValueName); + keys[keyID].values.push_back(a_Value); } -bool cIniFile::SetValue(const AString & keyname, const AString & valuename, const AString & value, bool const create) +void cIniFile::AddValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value) { - int keyID = FindKey(keyname); + AddValue(a_KeyName, a_ValueName, Printf("%d", a_Value)); +} + + + + + +void cIniFile::AddValueF(const AString & a_KeyName, const AString & a_ValueName, const double a_Value) +{ + AddValue(a_KeyName, a_ValueName, Printf("%f", a_Value)); +} + + + + + +bool cIniFile::SetValue(const int keyID, const int valueID, const AString & value) +{ + if (((size_t)keyID >= keys.size()) || ((size_t)valueID >= keys[keyID].names.size())) + { + return false; + } + keys[keyID].values[valueID] = value; + return true; +} + + + + + +bool cIniFile::SetValue(const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value, const bool a_CreateIfNotExists) +{ + int keyID = FindKey(a_KeyName); if (keyID == noID) { - if (create) - { - keyID = int(AddKeyName(keyname)); - } - else + if (!a_CreateIfNotExists) { return false; } + keyID = AddKeyName(a_KeyName); } - int valueID = FindValue(int(keyID), valuename); + int valueID = FindValue(keyID, a_ValueName); if (valueID == noID) { - if (!create) + if (!a_CreateIfNotExists) { return false; } - keys[keyID].names.resize(keys[keyID].names.size() + 1, valuename); - keys[keyID].values.resize(keys[keyID].values.size() + 1, value); + keys[keyID].names.push_back(a_ValueName); + keys[keyID].values.push_back(a_Value); } else { - keys[keyID].values[valueID] = value; + keys[keyID].values[valueID] = a_Value; } return true; @@ -394,37 +426,32 @@ bool cIniFile::SetValue(const AString & keyname, const AString & valuename, cons -bool cIniFile::SetValueI(const AString & keyname, const AString & valuename, const int value, bool const create) +bool cIniFile::SetValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value, const bool a_CreateIfNotExists) { - AString Data; - Printf(Data, "%d", value); - return SetValue(keyname, valuename, Data, create); + return SetValue(a_KeyName, a_ValueName, Printf("%d", a_Value), a_CreateIfNotExists); } -bool cIniFile::SetValueF(const AString & keyname, const AString & valuename, double const value, bool const create) +bool cIniFile::SetValueF(const AString & a_KeyName, const AString & a_ValueName, double const a_Value, const bool a_CreateIfNotExists) { - AString Data; - Printf(Data, "%f", value); - return SetValue(keyname, valuename, Data, create); + return SetValue(a_KeyName, a_ValueName, Printf("%f", a_Value), a_CreateIfNotExists); } -bool cIniFile::SetValueV(const AString & keyname, const AString & valuename, char * format, ...) +bool cIniFile::SetValueV(const AString & a_KeyName, const AString & a_ValueName, const char * a_Format, ...) { va_list args; - va_start(args, format); - + va_start(args, a_Format); AString Data; - AppendVPrintf(Data, format, args); + AppendVPrintf(Data, a_Format, args); va_end(args); - return SetValue(keyname, valuename, Data); + return SetValue(a_KeyName, a_ValueName, Data); } diff --git a/lib/inifile/iniFile.h b/lib/inifile/iniFile.h index 83d961fc6..40af618dc 100644 --- a/lib/inifile/iniFile.h +++ b/lib/inifile/iniFile.h @@ -35,7 +35,7 @@ class cIniFile { private: - bool m_IsCaseInsensitive; + bool m_IsCaseInsensitive; struct key { @@ -122,22 +122,32 @@ public: return (GetValueSetI(keyname, valuename, defValue ? 1 : 0) != 0); } - // Sets value of [keyname] valuename =. - // Specify the optional paramter as false (0) if you do not want it to create - // the key if it doesn't exist. Returns true if data entered, false otherwise. - // Overloaded to accept string, int, and double. - bool SetValue( const int keyID, const int valueID, const AString & value); - bool SetValue( const AString & keyname, const AString & valuename, const AString & value, const bool create = true); - bool SetValueI( const AString & keyname, const AString & valuename, const int value, const bool create = true); - bool SetValueB( const AString & keyname, const AString & valuename, const bool value, const bool create = true) + // Adds a new value to the specified key. + // If a value of the same name already exists, creates another one (non-standard INI file) + void AddValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value); + void AddValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value); + void AddValueB(const AString & a_KeyName, const AString & a_ValueName, const bool a_Value) { - return SetValueI( keyname, valuename, int(value), create); + return AddValueI(a_KeyName, a_ValueName, a_Value ? 1 : 0); } - bool SetValueF( const AString & keyname, const AString & valuename, const double value, const bool create = true); + void AddValueF(const AString & a_KeyName, const AString & a_ValueName, const double a_Value); + + // Overwrites the value of [keyname].valuename + // Specify the optional parameter as false (0) if you do not want the value created if it doesn't exist. + // Returns true if value set, false otherwise. + // Overloaded to accept string, int, and double. + bool SetValue (const int keyID, const int valueID, const AString & value); + bool SetValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value, const bool a_CreateIfNotExists = true); + bool SetValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value, const bool a_CreateIfNotExists = true); + bool SetValueB(const AString & a_KeyName, const AString & a_ValueName, const bool a_Value, const bool a_CreateIfNotExists = true) + { + return SetValueI(a_KeyName, a_ValueName, int(a_Value), a_CreateIfNotExists); + } + bool SetValueF(const AString & a_KeyName, const AString & a_ValueName, const double a_Value, const bool a_CreateIfNotExists = true); // tolua_end - bool SetValueV( const AString & keyname, const AString & valuename, char *format, ...); + bool SetValueV( const AString & a_KeyName, const AString & a_ValueName, const char * a_Format, ...); // tolua_begin From 2e0fcbdcb7a773509f8d08f50c8cbeb07d612d51 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 11 Jan 2014 16:50:52 +0100 Subject: [PATCH 3/4] Documented the cIniFile:AddValue* functions. Now the documentation really matches the implementation. --- MCServer/Plugins/APIDump/APIDesc.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/MCServer/Plugins/APIDump/APIDesc.lua b/MCServer/Plugins/APIDump/APIDesc.lua index 26537918e..9b117b0fa 100644 --- a/MCServer/Plugins/APIDump/APIDesc.lua +++ b/MCServer/Plugins/APIDump/APIDesc.lua @@ -867,6 +867,10 @@ ValueName0=SomeOtherValue { Params = "KeyName, Comment", Return = "", Notes = "Adds a comment to be stored in the file under the specified key" }, }, AddKeyName = { Params = "KeyName", Returns = "number", Notes = "Adds a new key of the specified name. Returns the KeyID of the new key." }, + AddValue = { Params = "KeyName, ValueName, Value", Return = "", Notes = "Adds a new value of the specified name to the specified key. If another value of the same name exists in the key, both are kept (nonstandard INI file)" }, + AddValueB = { Params = "KeyName, ValueName, Value", Return = "", Notes = "Adds a new bool value of the specified name to the specified key. If another value of the same name exists in the key, both are kept (nonstandard INI file)" }, + AddValueF = { Params = "KeyName, ValueName, Value", Return = "", Notes = "Adds a new float value of the specified name to the specified key. If another value of the same name exists in the key, both are kept (nonstandard INI file)" }, + AddValueI = { Params = "KeyName, ValueName, Value", Return = "", Notes = "Adds a new integer value of the specified name to the specified key. If another value of the same name exists in the key, both are kept (nonstandard INI file)" }, CaseInsensitive = { Params = "", Return = "", Notes = "Sets key names' and value names' comparisons to case insensitive (default)." }, CaseSensitive = { Params = "", Return = "", Notes = "Sets key names and value names comparisons to case sensitive." }, Clear = { Params = "", Return = "", Notes = "Removes all the in-memory data. Note that , like all the other operations, this doesn't affect any file data." }, From 7739238d3d5d7778b3f9239f5b12fa101c8ac13d Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 11 Jan 2014 20:10:50 +0100 Subject: [PATCH 4/4] Fixed reading the files. Duplicate values were ignored. --- lib/inifile/iniFile.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/inifile/iniFile.cpp b/lib/inifile/iniFile.cpp index c1fd9c929..afa1c110d 100644 --- a/lib/inifile/iniFile.cpp +++ b/lib/inifile/iniFile.cpp @@ -137,7 +137,7 @@ bool cIniFile::ReadFile(const AString & a_FileName, bool a_AllowExampleRedirect) { valuename = line.substr(0, pLeft); value = line.substr(pLeft + 1); - SetValue(keyname, valuename, value); + AddValue(keyname, valuename, value); break; }