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 
33 class cIniFile:
35 {
36 private:
37 
38  // tolua_end
39 
41 
42 
44 
46 
47  struct key
48  {
49  std::vector<AString> m_Names;
50  std::vector<AString> m_Values;
51  std::vector<AString> m_Comments;
52  } ;
53 
54  std::vector<key> m_Keys;
55  std::vector<AString> m_Names;
56  std::vector<AString> m_Comments;
57 
59  AString CheckCase(const AString & s) const;
60 
62  void RemoveBom(AString & a_line) const;
63 
64  // tolua_begin
65 
66 public:
67 
68  // NOTE: This has to be present for ToLua++'s parser to output the noID constant into the API
69  // We don't want to export the entire base class, so the constant needs to get pulled into this descendant
70  enum
71  {
73  };
74 
75 
77  cIniFile(void);
78 
79  // tolua_end
80 
81  virtual std::vector<std::pair<AString, AString>> GetValues(AString a_keyName) override;
82 
83  virtual bool KeyExists(const AString a_keyName) const override;
84 
85  // tolua_begin
86 
87  // Sets whether or not keynames and valuenames should be case sensitive.
88  // The default is case insensitive.
89  void CaseSensitive (void) { m_IsCaseInsensitive = false; }
90  void CaseInsensitive(void) { m_IsCaseInsensitive = true; }
91 
96  bool ReadFile(const AString & a_FileName, bool a_AllowExampleRedirect = true);
97 
100  bool WriteFile(const AString & a_FileName) const;
101 
102  virtual bool Flush() override { return WriteFile(m_Filename); }
103 
105  void Clear(void);
106 
108  bool HasValue(const AString & a_KeyName, const AString & a_ValueName) const override;
109 
111  int FindKey(const AString & keyname) const;
112 
114  int FindValue(const int keyID, const AString & valuename) const;
115 
117  int GetNumKeys(void) const { return static_cast<int>(m_Keys.size()); }
118 
120  int AddKeyName(const AString & keyname) override;
121 
122  // Returns key names by index.
123  AString GetKeyName(const int keyID) const;
124 
125  // Returns number of values stored for specified key.
126  int GetNumValues(const AString & keyname) const;
127  int GetNumValues(const int keyID) const;
128 
129  // Returns value name by index for a given keyname or keyID.
130  AString GetValueName(const AString & keyname, const int valueID) const;
131  AString GetValueName(const int keyID, const int valueID) const;
132 
133  // Gets value of [keyname] valuename =.
134  // Overloaded to return string, int, and double.
135  // Returns defValue if key / value not found.
136  AString GetValue (const AString & keyname, const AString & valuename, const AString & defValue = "") const override;
137  AString GetValue (const int keyID, const int valueID, const AString & defValue = "") const;
138  double GetValueF(const AString & keyname, const AString & valuename, const double defValue = 0) const;
139  int GetValueI(const AString & keyname, const AString & valuename, const int defValue = 0) const;
140  bool GetValueB(const AString & keyname, const AString & valuename, const bool defValue = false) const
141  {
142  return (GetValueI(keyname, valuename, defValue ? 1 : 0) != 0);
143  }
144 
145  // Gets the value; if not found, write the default to the INI file
146  AString GetValueSet (const AString & keyname, const AString & valuename, const AString & defValue = "") override;
147  double GetValueSetF(const AString & keyname, const AString & valuename, const double defValue = 0.0);
148  int GetValueSetI(const AString & keyname, const AString & valuename, const int defValue = 0) override;
149  Int64 GetValueSetI(const AString & keyname, const AString & valuename, const Int64 defValue = 0) override;
150  bool GetValueSetB(const AString & keyname, const AString & valuename, const bool defValue = false) override
151  {
152  return (GetValueSetI(keyname, valuename, defValue ? 1 : 0) != 0);
153  }
154 
155  // Adds a new value to the specified key.
156  // If a value of the same name already exists, creates another one (non-standard INI file)
157  void AddValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value) override;
158  void AddValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value);
159  void AddValueB(const AString & a_KeyName, const AString & a_ValueName, const bool a_Value)
160  {
161  return AddValueI(a_KeyName, a_ValueName, a_Value ? 1 : 0);
162  }
163  void AddValueF(const AString & a_KeyName, const AString & a_ValueName, const double a_Value);
164 
165  // Overwrites the value of [keyname].valuename
166  // Specify the optional parameter as false (0) if you do not want the value created if it doesn't exist.
167  // Returns true if value set, false otherwise.
168  // Overloaded to accept string, int, and double.
169  bool SetValue (const int keyID, const int valueID, const AString & value);
170  bool SetValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value, const bool a_CreateIfNotExists = true) override;
171  bool SetValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value, const bool a_CreateIfNotExists = true) override;
172  bool SetValueI(const AString & a_Keyname, const AString & a_ValueName, const Int64 a_Value, const bool a_CreateIfNotExists = true);
173  bool SetValueB(const AString & a_KeyName, const AString & a_ValueName, const bool a_Value, const bool a_CreateIfNotExists = true)
174  {
175  return SetValueI(a_KeyName, a_ValueName, int(a_Value), a_CreateIfNotExists);
176  }
177  bool SetValueF(const AString & a_KeyName, const AString & a_ValueName, const double a_Value, const bool a_CreateIfNotExists = true);
178 
179  // Deletes specified value.
180  // Returns true if value existed and deleted, false otherwise.
181  bool DeleteValueByID(const int keyID, const int valueID);
182  bool DeleteValue(const AString & keyname, const AString & valuename) override;
183 
184  // Deletes specified key and all values contained within.
185  // Returns true if key existed and deleted, false otherwise.
186  bool DeleteKey(const AString & keyname);
187 
188  // Header comment functions.
189  // Header comments are those comments before the first key.
190 
192  int GetNumHeaderComments(void) {return static_cast<int>(m_Comments.size());}
193 
195  void AddHeaderComment(const AString & comment);
196 
198  AString GetHeaderComment(const int commentID) const;
199 
201  bool DeleteHeaderComment(int commentID);
202 
204  void DeleteHeaderComments(void) {m_Comments.clear();}
205 
206 
207  // Key comment functions.
208  // Key comments are those comments within a key. Any comments
209  // defined within value names will be added to this list. Therefore,
210  // these comments will be moved to the top of the key definition when
211  // the CIniFile::WriteFile() is called.
212 
214  int GetNumKeyComments(const int keyID) const;
215 
217  int GetNumKeyComments(const AString & keyname) const;
218 
220  bool AddKeyComment(const int keyID, const AString & comment);
221 
223  bool AddKeyComment(const AString & keyname, const AString & comment) override;
224 
226  AString GetKeyComment(const int keyID, const int commentID) const;
227  AString GetKeyComment(const AString & keyname, const int commentID) const override;
228 
229  // Delete a key comment.
230  bool DeleteKeyComment(const int keyID, const int commentID);
231  bool DeleteKeyComment(const AString & keyname, const int commentID) override;
232 
233  // Delete all comments for a key.
234  bool DeleteKeyComments(const int keyID);
235  bool DeleteKeyComments(const AString & keyname);
236 };
237 
238 // tolua_end
239 
240 
241 
242 
243 
249  cSettingsRepositoryInterface & a_Settings,
250  const AString & a_KeyName,
251  const AString & a_PortsValueName,
252  const AString & a_OldIPv4ValueName,
253  const AString & a_OldIPv6ValueName,
254  const AString & a_DefaultValue
255 );
256 
257 
258 
signed long long Int64
Definition: Globals.h:151
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:923
std::vector< AString > AStringVector
Definition: StringUtils.h:12
std::string AString
Definition: StringUtils.h:11
AString GetHeaderComment(const int commentID) const
Returns a header comment, or empty string if out of range.
Definition: IniFile.cpp:684
void RemoveBom(AString &a_line) const
Removes the UTF-8 BOMs (Byte order makers), if present.
Definition: IniFile.cpp:870
bool DeleteValueByID(const int keyID, const int valueID)
Definition: IniFile.cpp:586
void AddValueB(const AString &a_KeyName, const AString &a_ValueName, const bool a_Value)
Definition: IniFile.h:159
void DeleteHeaderComments(void)
Deletes all header comments.
Definition: IniFile.h:204
AString m_Filename
Definition: IniFile.h:45
bool HasValue(const AString &a_KeyName, const AString &a_ValueName) const override
Returns true iff the specified value exists.
Definition: IniFile.cpp:656
int AddKeyName(const AString &keyname) override
Add a key name.
Definition: IniFile.cpp:282
AString GetKeyName(const int keyID) const
Definition: IniFile.cpp:293
bool DeleteKeyComments(const int keyID)
Definition: IniFile.cpp:823
int FindKey(const AString &keyname) const
Returns index of specified key, or noID if not found.
Definition: IniFile.cpp:243
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:485
bool WriteFile(const AString &a_FileName) const
Writes data stored in class to the specified ini file.
Definition: IniFile.cpp:190
bool m_IsCaseInsensitive
Definition: IniFile.h:43
bool AddKeyComment(const int keyID, const AString &comment)
Add a key comment.
Definition: IniFile.cpp:739
std::vector< AString > m_Comments
Definition: IniFile.h:56
int GetNumValues(const AString &keyname) const
Definition: IniFile.cpp:322
bool DeleteValue(const AString &keyname, const AString &valuename) override
Deletes the specified key, value pair.
Definition: IniFile.cpp:604
bool ReadFile(const AString &a_FileName, bool a_AllowExampleRedirect=true)
Reads the contents of the specified ini file If the file doesn't exist and a_AllowExampleRedirect is ...
Definition: IniFile.cpp:50
bool SetValueF(const AString &a_KeyName, const AString &a_ValueName, const double a_Value, const bool a_CreateIfNotExists=true)
Definition: IniFile.cpp:463
void CaseInsensitive(void)
Definition: IniFile.h:90
void CaseSensitive(void)
Definition: IniFile.h:89
virtual bool KeyExists(const AString a_keyName) const override
Returns true iff the specified key exists.
Definition: IniFile.cpp:895
void Clear(void)
Deletes all stored ini data (but doesn't touch the file)
Definition: IniFile.cpp:645
bool SetValueB(const AString &a_KeyName, const AString &a_ValueName, const bool a_Value, const bool a_CreateIfNotExists=true)
Definition: IniFile.h:173
@ noID
Definition: IniFile.h:72
int GetNumHeaderComments(void)
Returns the number of header comments.
Definition: IniFile.h:192
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:904
AString GetValueName(const AString &keyname, const int valueID) const
Definition: IniFile.cpp:349
virtual bool Flush() override
Writes the changes to the backing store, if the repository has one.
Definition: IniFile.h:102
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:363
int GetValueI(const AString &keyname, const AString &valuename, const int defValue=0) const
Definition: IniFile.cpp:506
bool DeleteKeyComment(const int keyID, const int commentID)
Definition: IniFile.cpp:794
void AddHeaderComment(const AString &comment)
Adds a header comment.
Definition: IniFile.cpp:674
int GetValueSetI(const AString &keyname, const AString &valuename, const int defValue=0) override
Definition: IniFile.cpp:559
bool DeleteKey(const AString &keyname)
Definition: IniFile.cpp:625
cIniFile(void)
Creates a new instance with no data.
Definition: IniFile.cpp:41
bool GetValueB(const AString &keyname, const AString &valuename, const bool defValue=false) const
Definition: IniFile.h:140
void AddValueF(const AString &a_KeyName, const AString &a_ValueName, const double a_Value)
Definition: IniFile.cpp:388
bool SetValueI(const AString &a_KeyName, const AString &a_ValueName, const int a_Value, const bool a_CreateIfNotExists=true) override
Definition: IniFile.cpp:445
bool SetValue(const int keyID, const int valueID, const AString &value)
Definition: IniFile.cpp:397
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:526
std::vector< AString > m_Names
Definition: IniFile.h:55
bool DeleteHeaderComment(int commentID)
Deletes a header comment.
Definition: IniFile.cpp:697
void AddValueI(const AString &a_KeyName, const AString &a_ValueName, const int a_Value)
Definition: IniFile.cpp:379
int GetNumKeys(void) const
Returns number of keys currently in the ini.
Definition: IniFile.h:117
int GetNumKeyComments(const int keyID) const
Get number of key comments.
Definition: IniFile.cpp:712
bool GetValueSetB(const AString &keyname, const AString &valuename, const bool defValue=false) override
Definition: IniFile.h:150
double GetValueSetF(const AString &keyname, const AString &valuename, const double defValue=0.0)
Definition: IniFile.cpp:549
double GetValueF(const AString &keyname, const AString &valuename, const double defValue=0) const
Definition: IniFile.cpp:516
std::vector< key > m_Keys
Definition: IniFile.h:54
AString CheckCase(const AString &s) const
If the object is case-insensitive, returns s as lowercase; otherwise returns s as-is.
Definition: IniFile.cpp:851
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:260
AString GetKeyComment(const int keyID, const int commentID) const
Return a key comment.
Definition: IniFile.cpp:767
std::vector< AString > m_Names
Definition: IniFile.h:49
std::vector< AString > m_Comments
Definition: IniFile.h:51
std::vector< AString > m_Values
Definition: IniFile.h:50