Cuberite
A lightweight, fast and extensible game server for Minecraft
IniFile.h
Go to the documentation of this file.
1 // IniFile.cpp: Implementation of the CIniFile class.
2 // Written by: Adam Clauss
3 // Email: cabadam@tamu.edu
4 // You may use this class / code as you wish in your programs. Feel free to distribute it, and
5 // email suggested changes to me.
6 //
7 // Rewritten by: Shane Hill
8 // Date: 2001-08-21
9 // Email: Shane.Hill@dsto.defence.gov.au
10 // Reason: Remove dependancy on MFC. Code should compile on any
11 // platform. Tested on Windows / Linux / Irix
13 
14 /*
15 !! MODIFIED BY FAKETRUTH and madmaxoft!!
16 */
17 
18 #pragma once
19 
20 
22 
23 #define MAX_KEYNAME 128
24 #define MAX_VALUENAME 128
25 #define MAX_VALUEDATA 2048
26 
27 
28 
29 
30 
31 // tolua_begin
32 
34 {
35 private:
37 
38 
40 
42 
43  struct key
44  {
45  std::vector<AString> m_Names;
46  std::vector<AString> m_Values;
47  std::vector<AString> m_Comments;
48  } ;
49 
50  std::vector<key> m_Keys;
51  std::vector<AString> m_Names;
52  std::vector<AString> m_Comments;
53 
55  AString CheckCase(const AString & s) const;
56 
58  void RemoveBom(AString & a_line) const;
59 
60 public:
61 
62  // NOTE: This has to be present for ToLua++'s parser to output the noID constant into the API
63  // We don't want to export the entire base class, so the constant needs to get pulled into this descendant
64  enum
65  {
67  };
68 
69 
71  cIniFile(void);
72 
73  // tolua_end
74  virtual ~cIniFile() override = default;
75 
76  virtual std::vector<std::pair<AString, AString>> GetValues(AString a_keyName) override;
77 
78  virtual bool KeyExists(const AString a_keyName) const override;
79 
80  // tolua_begin
81 
82  // Sets whether or not keynames and valuenames should be case sensitive.
83  // The default is case insensitive.
84  void CaseSensitive (void) { m_IsCaseInsensitive = false; }
85  void CaseInsensitive(void) { m_IsCaseInsensitive = true; }
86 
91  bool ReadFile(const AString & a_FileName, bool a_AllowExampleRedirect = true);
92 
95  bool WriteFile(const AString & a_FileName) const;
96 
97  virtual bool Flush() override { return WriteFile(m_Filename); }
98 
100  void Clear(void);
101 
103  bool HasValue(const AString & a_KeyName, const AString & a_ValueName) const override;
104 
106  int FindKey(const AString & keyname) const;
107 
109  int FindValue(const int keyID, const AString & valuename) const;
110 
112  int GetNumKeys(void) const { return static_cast<int>(m_Keys.size()); }
113 
115  int AddKeyName(const AString & keyname) override;
116 
117  // Returns key names by index.
118  AString GetKeyName(const int keyID) const;
119 
120  // Returns number of values stored for specified key.
121  int GetNumValues(const AString & keyname) const;
122  int GetNumValues(const int keyID) const;
123 
124  // Returns value name by index for a given keyname or keyID.
125  AString GetValueName(const AString & keyname, const int valueID) const;
126  AString GetValueName(const int keyID, const int valueID) const;
127 
128  // Gets value of [keyname] valuename =.
129  // Overloaded to return string, int, and double.
130  // Returns defValue if key / value not found.
131  AString GetValue (const AString & keyname, const AString & valuename, const AString & defValue = "") const override;
132  AString GetValue (const int keyID, const int valueID, const AString & defValue = "") const;
133  double GetValueF(const AString & keyname, const AString & valuename, const double defValue = 0) const;
134  int GetValueI(const AString & keyname, const AString & valuename, const int defValue = 0) const;
135  bool GetValueB(const AString & keyname, const AString & valuename, const bool defValue = false) const
136  {
137  return (GetValueI(keyname, valuename, defValue ? 1 : 0) != 0);
138  }
139 
140  // Gets the value; if not found, write the default to the INI file
141  AString GetValueSet (const AString & keyname, const AString & valuename, const AString & defValue = "") override;
142  double GetValueSetF(const AString & keyname, const AString & valuename, const double defValue = 0.0);
143  int GetValueSetI(const AString & keyname, const AString & valuename, const int defValue = 0) override;
144  Int64 GetValueSetI(const AString & keyname, const AString & valuename, const Int64 defValue = 0) override;
145  bool GetValueSetB(const AString & keyname, const AString & valuename, const bool defValue = false) override
146  {
147  return (GetValueSetI(keyname, valuename, defValue ? 1 : 0) != 0);
148  }
149 
150  // Adds a new value to the specified key.
151  // If a value of the same name already exists, creates another one (non-standard INI file)
152  void AddValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value) override;
153  void AddValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value);
154  void AddValueB(const AString & a_KeyName, const AString & a_ValueName, const bool a_Value)
155  {
156  return AddValueI(a_KeyName, a_ValueName, a_Value ? 1 : 0);
157  }
158  void AddValueF(const AString & a_KeyName, const AString & a_ValueName, const double a_Value);
159 
160  // Overwrites the value of [keyname].valuename
161  // Specify the optional parameter as false (0) if you do not want the value created if it doesn't exist.
162  // Returns true if value set, false otherwise.
163  // Overloaded to accept string, int, and double.
164  bool SetValue (const int keyID, const int valueID, const AString & value);
165  bool SetValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value, const bool a_CreateIfNotExists = true) override;
166  bool SetValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value, const bool a_CreateIfNotExists = true) override;
167  bool SetValueI(const AString & a_Keyname, const AString & a_ValueName, const Int64 a_Value, const bool a_CreateIfNotExists = true);
168  bool SetValueB(const AString & a_KeyName, const AString & a_ValueName, const bool a_Value, const bool a_CreateIfNotExists = true)
169  {
170  return SetValueI(a_KeyName, a_ValueName, int(a_Value), a_CreateIfNotExists);
171  }
172  bool SetValueF(const AString & a_KeyName, const AString & a_ValueName, const double a_Value, const bool a_CreateIfNotExists = true);
173 
174  // Deletes specified value.
175  // Returns true if value existed and deleted, false otherwise.
176  bool DeleteValueByID(const int keyID, const int valueID);
177  bool DeleteValue(const AString & keyname, const AString & valuename) override;
178 
179  // Deletes specified key and all values contained within.
180  // Returns true if key existed and deleted, false otherwise.
181  bool DeleteKey(const AString & keyname);
182 
183  // Header comment functions.
184  // Header comments are those comments before the first key.
185 
187  int GetNumHeaderComments(void) {return static_cast<int>(m_Comments.size());}
188 
190  void AddHeaderComment(const AString & comment);
191 
193  AString GetHeaderComment(const int commentID) const;
194 
196  bool DeleteHeaderComment(int commentID);
197 
199  void DeleteHeaderComments(void) {m_Comments.clear();}
200 
201 
202  // Key comment functions.
203  // Key comments are those comments within a key. Any comments
204  // defined within value names will be added to this list. Therefore,
205  // these comments will be moved to the top of the key definition when
206  // the CIniFile::WriteFile() is called.
207 
209  int GetNumKeyComments(const int keyID) const;
210 
212  int GetNumKeyComments(const AString & keyname) const;
213 
215  bool AddKeyComment(const int keyID, const AString & comment);
216 
218  bool AddKeyComment(const AString & keyname, const AString & comment) override;
219 
221  AString GetKeyComment(const int keyID, const int commentID) const;
222  AString GetKeyComment(const AString & keyname, const int commentID) const override;
223 
224  // Delete a key comment.
225  bool DeleteKeyComment(const int keyID, const int commentID);
226  bool DeleteKeyComment(const AString & keyname, const int commentID) override;
227 
228  // Delete all comments for a key.
229  bool DeleteKeyComments(const int keyID);
230  bool DeleteKeyComments(const AString & keyname);
231 };
232 
233 // tolua_end
234 
235 
236 
237 
238 
244  cSettingsRepositoryInterface & a_Settings,
245  const AString & a_KeyName,
246  const AString & a_PortsValueName,
247  const AString & a_OldIPv4ValueName,
248  const AString & a_OldIPv6ValueName,
249  const AString & a_DefaultValue
250 );
251 
252 
253 
bool DeleteKeyComments(const int keyID)
Definition: IniFile.cpp:824
double GetValueSetF(const AString &keyname, const AString &valuename, const double defValue=0.0)
Definition: IniFile.cpp:547
void AddValueI(const AString &a_KeyName, const AString &a_ValueName, const int a_Value)
Definition: IniFile.cpp:375
virtual std::vector< std::pair< AString, AString > > GetValues(AString a_keyName) override
returns a vector containing a name, value pair for each value under the key
Definition: IniFile.cpp:905
virtual ~cIniFile() override=default
int GetNumKeys(void) const
Returns number of keys currently in the ini.
Definition: IniFile.h:112
virtual bool Flush() override
Writes the changes to the backing store, if the repository has one.
Definition: IniFile.h:97
bool m_IsCaseInsensitive
Definition: IniFile.h:39
AStringVector ReadUpgradeIniPorts(cSettingsRepositoryInterface &a_Settings, const AString &a_KeyName, const AString &a_PortsValueName, const AString &a_OldIPv4ValueName, const AString &a_OldIPv6ValueName, const AString &a_DefaultValue)
Reads the list of ports from the INI file, possibly upgrading from IPv4 / IPv6-specific values into n...
Definition: IniFile.cpp:924
int GetValueI(const AString &keyname, const AString &valuename, const int defValue=0) const
Definition: IniFile.cpp:502
AString m_Filename
Definition: IniFile.h:41
int GetNumValues(const AString &keyname) const
Definition: IniFile.cpp:318
bool DeleteKey(const AString &keyname)
Definition: IniFile.cpp:626
void AddValueB(const AString &a_KeyName, const AString &a_ValueName, const bool a_Value)
Definition: IniFile.h:154
void AddValueF(const AString &a_KeyName, const AString &a_ValueName, const double a_Value)
Definition: IniFile.cpp:384
void CaseInsensitive(void)
Definition: IniFile.h:85
bool ReadFile(const AString &a_FileName, bool a_AllowExampleRedirect=true)
Reads the contents of the specified ini file If the file doesn&#39;t exist and a_AllowExampleRedirect is ...
Definition: IniFile.cpp:50
bool DeleteKeyComment(const int keyID, const int commentID)
Definition: IniFile.cpp:795
std::vector< AString > m_Comments
Definition: IniFile.h:47
virtual bool KeyExists(const AString a_keyName) const override
Returns true iff the specified key exists.
Definition: IniFile.cpp:896
std::vector< AString > m_Names
Definition: IniFile.h:51
std::vector< AString > AStringVector
Definition: StringUtils.h:14
int GetNumHeaderComments(void)
Returns the number of header comments.
Definition: IniFile.h:187
int FindKey(const AString &keyname) const
Returns index of specified key, or noID if not found.
Definition: IniFile.cpp:239
AString GetKeyComment(const int keyID, const int commentID) const
Return a key comment.
Definition: IniFile.cpp:768
bool WriteFile(const AString &a_FileName) const
Writes data stored in class to the specified ini file.
Definition: IniFile.cpp:189
bool SetValue(const int keyID, const int valueID, const AString &value)
Definition: IniFile.cpp:393
bool DeleteValue(const AString &keyname, const AString &valuename) override
Deletes the specified key, value pair.
Definition: IniFile.cpp:605
bool SetValueF(const AString &a_KeyName, const AString &a_ValueName, const double a_Value, const bool a_CreateIfNotExists=true)
Definition: IniFile.cpp:459
cSettingsRepositoryInterface Super
Definition: IniFile.h:36
void CaseSensitive(void)
Definition: IniFile.h:84
void AddHeaderComment(const AString &comment)
Adds a header comment.
Definition: IniFile.cpp:675
bool SetValueB(const AString &a_KeyName, const AString &a_ValueName, const bool a_Value, const bool a_CreateIfNotExists=true)
Definition: IniFile.h:168
void Clear(void)
Deletes all stored ini data (but doesn&#39;t touch the file)
Definition: IniFile.cpp:646
AString GetHeaderComment(const int commentID) const
Returns a header comment, or empty string if out of range.
Definition: IniFile.cpp:685
AString GetValueName(const AString &keyname, const int valueID) const
Definition: IniFile.cpp:345
std::vector< AString > m_Comments
Definition: IniFile.h:52
AString GetKeyName(const int keyID) const
Definition: IniFile.cpp:289
int GetNumKeyComments(const int keyID) const
Get number of key comments.
Definition: IniFile.cpp:713
int FindValue(const int keyID, const AString &valuename) const
Returns index of specified value, in the specified key, or noID if not found.
Definition: IniFile.cpp:256
AString CheckCase(const AString &s) const
If the object is case-insensitive, returns s as lowercase; otherwise returns s as-is.
Definition: IniFile.cpp:852
int AddKeyName(const AString &keyname) override
Add a key name.
Definition: IniFile.cpp:278
std::vector< AString > m_Values
Definition: IniFile.h:46
bool GetValueB(const AString &keyname, const AString &valuename, const bool defValue=false) const
Definition: IniFile.h:135
bool DeleteValueByID(const int keyID, const int valueID)
Definition: IniFile.cpp:587
std::string AString
Definition: StringUtils.h:13
double GetValueF(const AString &keyname, const AString &valuename, const double defValue=0) const
Definition: IniFile.cpp:513
std::vector< AString > m_Names
Definition: IniFile.h:45
bool SetValueI(const AString &a_KeyName, const AString &a_ValueName, const int a_Value, const bool a_CreateIfNotExists=true) override
Definition: IniFile.cpp:441
void AddValue(const AString &a_KeyName, const AString &a_ValueName, const AString &a_Value) override
Adds a new value to the specified key.
Definition: IniFile.cpp:359
int GetValueSetI(const AString &keyname, const AString &valuename, const int defValue=0) override
Definition: IniFile.cpp:558
bool DeleteHeaderComment(int commentID)
Deletes a header comment.
Definition: IniFile.cpp:698
void RemoveBom(AString &a_line) const
Removes the UTF-8 BOMs (Byte order makers), if present.
Definition: IniFile.cpp:871
std::vector< key > m_Keys
Definition: IniFile.h:50
bool GetValueSetB(const AString &keyname, const AString &valuename, const bool defValue=false) override
Definition: IniFile.h:145
bool HasValue(const AString &a_KeyName, const AString &a_ValueName) const override
Returns true iff the specified value exists.
Definition: IniFile.cpp:657
AString GetValue(const AString &keyname, const AString &valuename, const AString &defValue="") const override
Get the value at the specified key and value, returns defValue on failure.
Definition: IniFile.cpp:481
bool AddKeyComment(const int keyID, const AString &comment)
Add a key comment.
Definition: IniFile.cpp:740
cIniFile(void)
Creates a new instance with no data.
Definition: IniFile.cpp:41
signed long long Int64
Definition: Globals.h:107
void DeleteHeaderComments(void)
Deletes all header comments.
Definition: IniFile.h:199
AString GetValueSet(const AString &keyname, const AString &valuename, const AString &defValue="") override
Gets the value; if not found, write the default to the repository.
Definition: IniFile.cpp:524