2012-06-14 09:06:06 -04:00
|
|
|
// IniFile.cpp: Implementation of the CIniFile class.
|
|
|
|
// Written by: Adam Clauss
|
|
|
|
// Email: cabadam@tamu.edu
|
2015-05-09 03:25:09 -04:00
|
|
|
// You may use this class / code as you wish in your programs. Feel free to distribute it, and
|
2012-06-14 09:06:06 -04:00
|
|
|
// email suggested changes to me.
|
|
|
|
//
|
|
|
|
// Rewritten by: Shane Hill
|
2015-05-09 03:25:09 -04:00
|
|
|
// Date: 2001-08-21
|
2012-06-14 09:06:06 -04:00
|
|
|
// Email: Shane.Hill@dsto.defence.gov.au
|
|
|
|
// Reason: Remove dependancy on MFC. Code should compile on any
|
2015-05-09 03:25:09 -04:00
|
|
|
// platform. Tested on Windows / Linux / Irix
|
2014-10-27 17:34:02 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
!! MODIFIED BY FAKETRUTH and madmaxoft!!
|
|
|
|
*/
|
|
|
|
|
2015-01-24 03:45:07 -05:00
|
|
|
#pragma once
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
2015-05-14 10:47:51 -04:00
|
|
|
#include "SettingsRepositoryInterface.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#define MAX_KEYNAME 128
|
|
|
|
#define MAX_VALUENAME 128
|
|
|
|
#define MAX_VALUEDATA 2048
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
// tolua_begin
|
|
|
|
|
2015-05-14 10:47:51 -04:00
|
|
|
class cIniFile : public cSettingsRepositoryInterface
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2012-06-14 09:06:06 -04:00
|
|
|
private:
|
2014-01-11 10:44:28 -05:00
|
|
|
bool m_IsCaseInsensitive;
|
2015-05-14 10:47:51 -04:00
|
|
|
|
|
|
|
AString m_Filename;
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
struct key
|
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
std::vector<AString> names;
|
|
|
|
std::vector<AString> values;
|
|
|
|
std::vector<AString> comments;
|
2013-08-05 09:24:23 -04:00
|
|
|
} ;
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
std::vector<key> keys;
|
|
|
|
std::vector<AString> names;
|
|
|
|
std::vector<AString> comments;
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
/// If the object is case-insensitive, returns s as lowercase; otherwise returns s as-is
|
2013-10-25 05:38:14 -04:00
|
|
|
AString CheckCase(const AString & s) const;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-02-15 19:56:36 -05:00
|
|
|
/// Removes the UTF-8 BOMs (Byte order makers), if present.
|
|
|
|
void RemoveBom(AString & a_line) const;
|
2014-09-03 17:00:16 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
public:
|
2013-08-05 09:24:23 -04:00
|
|
|
|
2013-10-25 05:15:44 -04:00
|
|
|
/// Creates a new instance with no data
|
|
|
|
cIniFile(void);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2015-05-14 10:47:51 -04:00
|
|
|
// tolua_end
|
|
|
|
virtual ~cIniFile() = default;
|
|
|
|
|
|
|
|
virtual std::vector<std::pair<AString, AString>> GetValues(AString a_keyName) override;
|
|
|
|
|
|
|
|
virtual bool KeyExists(const AString a_keyName) const override;
|
|
|
|
|
|
|
|
// tolua_begin
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Sets whether or not keynames and valuenames should be case sensitive.
|
|
|
|
// The default is case insensitive.
|
2013-08-05 09:24:23 -04:00
|
|
|
void CaseSensitive (void) { m_IsCaseInsensitive = false; }
|
|
|
|
void CaseInsensitive(void) { m_IsCaseInsensitive = true; }
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-10-25 05:15:44 -04:00
|
|
|
/** Reads the contents of the specified ini file
|
2013-08-05 09:24:23 -04:00
|
|
|
If the file doesn't exist and a_AllowExampleRedirect is true, tries to read <basename>.example.ini, and
|
|
|
|
writes its contents as <basename>.ini, if successful.
|
|
|
|
Returns true if successful, false otherwise.
|
|
|
|
*/
|
2013-10-25 05:15:44 -04:00
|
|
|
bool ReadFile(const AString & a_FileName, bool a_AllowExampleRedirect = true);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-10-25 05:15:44 -04:00
|
|
|
/// Writes data stored in class to the specified ini file
|
|
|
|
bool WriteFile(const AString & a_FileName) const;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2015-05-14 10:47:51 -04:00
|
|
|
virtual bool Flush() override { return WriteFile(m_Filename); }
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
/// Deletes all stored ini data (but doesn't touch the file)
|
|
|
|
void Clear(void);
|
2014-09-03 17:00:16 -04:00
|
|
|
|
|
|
|
/** Returns true iff the specified value exists. */
|
2015-05-23 07:56:08 -04:00
|
|
|
bool HasValue(const AString & a_KeyName, const AString & a_ValueName) const override;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
/// Returns index of specified key, or noID if not found
|
2013-10-25 05:38:14 -04:00
|
|
|
int FindKey(const AString & keyname) const;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
/// Returns index of specified value, in the specified key, or noID if not found
|
2013-10-25 05:38:14 -04:00
|
|
|
int FindValue(const int keyID, const AString & valuename) const;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
/// Returns number of keys currently in the ini
|
2015-05-24 07:56:56 -04:00
|
|
|
int GetNumKeys(void) const { return static_cast<int>(keys.size()); }
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
/// Add a key name
|
2015-05-23 07:56:08 -04:00
|
|
|
int AddKeyName(const AString & keyname) override;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Returns key names by index.
|
2013-10-25 05:38:14 -04:00
|
|
|
AString GetKeyName(const int keyID) const;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Returns number of values stored for specified key.
|
2013-10-25 05:38:14 -04:00
|
|
|
int GetNumValues(const AString & keyname) const;
|
|
|
|
int GetNumValues(const int keyID) const;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Returns value name by index for a given keyname or keyID.
|
2013-10-25 05:38:14 -04:00
|
|
|
AString GetValueName(const AString & keyname, const int valueID) const;
|
|
|
|
AString GetValueName(const int keyID, const int valueID) const;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Gets value of [keyname] valuename =.
|
|
|
|
// Overloaded to return string, int, and double.
|
2015-05-09 03:25:09 -04:00
|
|
|
// Returns defValue if key / value not found.
|
2015-05-23 07:56:08 -04:00
|
|
|
AString GetValue (const AString & keyname, const AString & valuename, const AString & defValue = "") const override;
|
2013-10-25 05:38:14 -04:00
|
|
|
AString GetValue (const int keyID, const int valueID, const AString & defValue = "") const;
|
2013-08-05 09:24:23 -04:00
|
|
|
double GetValueF(const AString & keyname, const AString & valuename, const double defValue = 0) const;
|
|
|
|
int GetValueI(const AString & keyname, const AString & valuename, const int defValue = 0) const;
|
|
|
|
bool GetValueB(const AString & keyname, const AString & valuename, const bool defValue = false) const
|
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
return (GetValueI(keyname, valuename, defValue ? 1 : 0) != 0);
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Gets the value; if not found, write the default to the INI file
|
2015-05-23 07:56:08 -04:00
|
|
|
AString GetValueSet (const AString & keyname, const AString & valuename, const AString & defValue = "") override;
|
2013-08-05 09:24:23 -04:00
|
|
|
double GetValueSetF(const AString & keyname, const AString & valuename, const double defValue = 0.0);
|
2015-05-23 07:56:08 -04:00
|
|
|
int GetValueSetI(const AString & keyname, const AString & valuename, const int defValue = 0) override;
|
|
|
|
Int64 GetValueSetI(const AString & keyname, const AString & valuename, const Int64 defValue = 0) override;
|
|
|
|
bool GetValueSetB(const AString & keyname, const AString & valuename, const bool defValue = false) override
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
return (GetValueSetI(keyname, valuename, defValue ? 1 : 0) != 0);
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-01-11 10:44:28 -05:00
|
|
|
// Adds a new value to the specified key.
|
|
|
|
// If a value of the same name already exists, creates another one (non-standard INI file)
|
2015-05-23 07:56:08 -04:00
|
|
|
void AddValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value) override;
|
2014-01-11 10:44:28 -05:00
|
|
|
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 AddValueI(a_KeyName, a_ValueName, a_Value ? 1 : 0);
|
|
|
|
}
|
|
|
|
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.
|
2012-06-14 09:06:06 -04:00
|
|
|
// Overloaded to accept string, int, and double.
|
2014-01-11 10:44:28 -05:00
|
|
|
bool SetValue (const int keyID, const int valueID, const AString & value);
|
2015-05-23 07:56:08 -04:00
|
|
|
bool SetValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value, const bool a_CreateIfNotExists = true) override;
|
|
|
|
bool SetValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value, const bool a_CreateIfNotExists = true) override;
|
2014-06-05 12:58:29 -04:00
|
|
|
bool SetValueI(const AString & a_Keyname, const AString & a_ValueName, const Int64 a_Value, const bool a_CreateIfNotExists = true);
|
2014-01-11 10:44:28 -05:00
|
|
|
bool SetValueB(const AString & a_KeyName, const AString & a_ValueName, const bool a_Value, const bool a_CreateIfNotExists = true)
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2014-01-11 10:44:28 -05:00
|
|
|
return SetValueI(a_KeyName, a_ValueName, int(a_Value), a_CreateIfNotExists);
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
2014-01-11 10:44:28 -05:00
|
|
|
bool SetValueF(const AString & a_KeyName, const AString & a_ValueName, const double a_Value, const bool a_CreateIfNotExists = true);
|
2013-08-07 08:32:08 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Deletes specified value.
|
|
|
|
// Returns true if value existed and deleted, false otherwise.
|
2013-10-25 05:38:14 -04:00
|
|
|
bool DeleteValueByID(const int keyID, const int valueID);
|
2015-05-23 07:56:08 -04:00
|
|
|
bool DeleteValue(const AString & keyname, const AString & valuename) override;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Deletes specified key and all values contained within.
|
|
|
|
// Returns true if key existed and deleted, false otherwise.
|
2013-10-25 05:38:14 -04:00
|
|
|
bool DeleteKey(const AString & keyname);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Header comment functions.
|
|
|
|
// Header comments are those comments before the first key.
|
2013-10-25 05:38:14 -04:00
|
|
|
|
2013-10-26 04:33:28 -04:00
|
|
|
/// Returns the number of header comments
|
2015-05-24 07:56:56 -04:00
|
|
|
int GetNumHeaderComments(void) {return static_cast<int>(comments.size());}
|
2013-10-25 05:38:14 -04:00
|
|
|
|
2013-10-26 04:33:28 -04:00
|
|
|
/// Adds a header comment
|
2013-10-25 05:38:14 -04:00
|
|
|
void AddHeaderComment(const AString & comment);
|
|
|
|
|
2013-10-26 04:33:28 -04:00
|
|
|
/// Returns a header comment, or empty string if out of range
|
2013-10-25 05:38:14 -04:00
|
|
|
AString GetHeaderComment(const int commentID) const;
|
|
|
|
|
2013-10-26 04:33:28 -04:00
|
|
|
/// Deletes a header comment. Returns true if successful
|
2013-10-25 05:38:14 -04:00
|
|
|
bool DeleteHeaderComment(int commentID);
|
|
|
|
|
2013-10-26 04:33:28 -04:00
|
|
|
/// Deletes all header comments
|
2013-10-25 05:38:14 -04:00
|
|
|
void DeleteHeaderComments(void) {comments.clear();}
|
2013-08-05 09:24:23 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Key comment functions.
|
|
|
|
// Key comments are those comments within a key. Any comments
|
|
|
|
// defined within value names will be added to this list. Therefore,
|
|
|
|
// these comments will be moved to the top of the key definition when
|
|
|
|
// the CIniFile::WriteFile() is called.
|
2013-10-25 05:38:14 -04:00
|
|
|
|
|
|
|
/// Get number of key comments
|
|
|
|
int GetNumKeyComments(const int keyID) const;
|
|
|
|
|
|
|
|
/// Get number of key comments
|
|
|
|
int GetNumKeyComments(const AString & keyname) const;
|
2013-08-05 09:24:23 -04:00
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
/// Add a key comment
|
|
|
|
bool AddKeyComment(const int keyID, const AString & comment);
|
|
|
|
|
|
|
|
/// Add a key comment
|
2015-05-23 07:56:08 -04:00
|
|
|
bool AddKeyComment(const AString & keyname, const AString & comment) override;
|
2013-08-05 09:24:23 -04:00
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
/// Return a key comment
|
|
|
|
AString GetKeyComment(const int keyID, const int commentID) const;
|
2015-05-23 07:56:08 -04:00
|
|
|
AString GetKeyComment(const AString & keyname, const int commentID) const override;
|
2013-08-05 09:24:23 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Delete a key comment.
|
2013-10-25 05:38:14 -04:00
|
|
|
bool DeleteKeyComment(const int keyID, const int commentID);
|
2015-05-23 07:56:08 -04:00
|
|
|
bool DeleteKeyComment(const AString & keyname, const int commentID) override;
|
2013-08-05 09:24:23 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Delete all comments for a key.
|
2013-10-25 05:38:14 -04:00
|
|
|
bool DeleteKeyComments(const int keyID);
|
|
|
|
bool DeleteKeyComments(const AString & keyname);
|
2013-08-05 09:24:23 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// tolua_end
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2015-01-24 03:45:07 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-09 03:25:09 -04:00
|
|
|
/** Reads the list of ports from the INI file, possibly upgrading from IPv4 / IPv6-specific values into new version-agnostic value.
|
2015-01-24 03:45:07 -05:00
|
|
|
Reads the list of ports from a_PortsValueName. If that value doesn't exist or is empty, the list is combined from values
|
|
|
|
in a_OldIPv4ValueName and a_OldIPv6ValueName; in this case the old values are removed from the INI file.
|
|
|
|
If there is none of the three values or they are all empty, the default is used and stored in the Ports value. */
|
|
|
|
AStringVector ReadUpgradeIniPorts(
|
2015-05-14 10:47:51 -04:00
|
|
|
cSettingsRepositoryInterface & a_Settings,
|
2015-01-24 03:45:07 -05:00
|
|
|
const AString & a_KeyName,
|
|
|
|
const AString & a_PortsValueName,
|
|
|
|
const AString & a_OldIPv4ValueName,
|
|
|
|
const AString & a_OldIPv6ValueName,
|
|
|
|
const AString & a_DefaultValue
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|