ini.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/ini.h $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 11/14/01 1:32a $*
  29. * *
  30. * $Revision:: 16 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if _MSC_VER >= 1000
  36. #pragma once
  37. #endif // _MSC_VER >= 1000
  38. #ifndef INI_H
  39. #define INI_H
  40. //#include "listnode.h"
  41. //#include "trect.h"
  42. //#include "index.h"
  43. //#include "pipe.h"
  44. //#include "pk.h"
  45. //#include "straw.h"
  46. //#include "wwfile.h"
  47. class PKey;
  48. class FileClass;
  49. class Straw;
  50. class Pipe;
  51. class StringClass;
  52. class WideStringClass;
  53. class FileFactoryClass;
  54. struct INISection;
  55. struct INIEntry;
  56. template<class T> class TPoint3D;
  57. template<class T> class TPoint2D;
  58. template<class T> class TRect;
  59. template<class T> class List;
  60. template<class T, class U> class IndexClass;
  61. #ifndef NULL
  62. #define NULL 0L
  63. #endif
  64. /*
  65. ** This is an INI database handler class. It handles a database with a disk format identical
  66. ** to the INI files commonly used by Windows.
  67. */
  68. class INIClass {
  69. public:
  70. INIClass(void);
  71. INIClass(FileClass & file);
  72. INIClass(const char *filename);
  73. virtual ~INIClass(void);
  74. /*
  75. ** This setting allows you to control the behavior of loading blank entries.
  76. ** If you set this to true, a blank entry (ie. "foo=") will be loaded with
  77. ** a value of " ". If set to false, blank entries will be ignored.
  78. ** The default behavior is to ignore blank entries.
  79. ** This is a static method, because in general this is an application-level
  80. ** decision as opposed to a per-ini file decision.
  81. */
  82. static void Keep_Blank_Entries(bool keep_blanks);
  83. /*
  84. ** Fetch and store INI data.
  85. */
  86. int Load(FileClass & file);
  87. int Load(Straw & file);
  88. int Load(const char *filename);
  89. int Save(FileClass & file) const;
  90. int Save(Pipe & file) const;
  91. int Save(const char *filename) const;
  92. /*
  93. ** Fetch the name of the INI file (if it wasn't created from a Straw).
  94. */
  95. const char * Get_Filename(void) const;
  96. /*
  97. ** Erase all data within this INI file manager.
  98. */
  99. bool Clear(char const * section = NULL, char const * entry = NULL);
  100. // int Line_Count(char const * section) const;
  101. bool Is_Loaded(void) const;
  102. int Size(void) const;
  103. bool Is_Present(char const * section, char const * entry = NULL) const {if (entry == 0) return(Find_Section(section) != 0);return(Find_Entry(section, entry) != 0);}
  104. /*
  105. ** Fetch the number of sections in the INI file or verify if a specific
  106. ** section is present.
  107. */
  108. int Section_Count(void) const;
  109. bool Section_Present(char const * section) const {return(Find_Section(section) != NULL);}
  110. /*
  111. ** Fetch the number of entries in a section or get a particular entry in a section.
  112. */
  113. int Entry_Count(char const * section) const;
  114. char const * Get_Entry(char const * section, int index) const;
  115. /*
  116. ** Cound how many entries with the indicated prefix followed by a number exist in the section.
  117. */
  118. unsigned Enumerate_Entries(const char * section, const char * entry_prefix, unsigned startnumber = 0, unsigned endnumber = (unsigned) -1);
  119. /*
  120. ** Get the various data types from the section and entry specified.
  121. */
  122. PKey Get_PKey(bool fast) const;
  123. bool Get_Bool(char const * section, char const * entry, bool defvalue=false) const;
  124. float Get_Float(char const * section, char const * entry, float defvalue=0.0f) const;
  125. double Get_Double(char const * section, char const * entry, double defvalue=0.0) const;
  126. int Get_Hex(char const * section, char const * entry, int defvalue=0) const;
  127. int Get_Int(char const * section, char const * entry, int defvalue=0) const;
  128. int Get_String(char const * section, char const * entry, char const * defvalue, char * buffer, int size) const;
  129. const StringClass& Get_String(StringClass& new_string, char const * section, char const * entry, char const * defvalue="") const;
  130. const WideStringClass& Get_Wide_String(WideStringClass& new_string, char const * section, char const * entry, unsigned short const * defvalue=L"") const;
  131. int Get_List_Index(char const * section, char const * entry, int const defvalue, char *list[]);
  132. int * Get_Alloc_Int_Array(char const * section, char const * entry, int listend);
  133. int Get_Int_Bitfield(char const * section, char const * entry, int defvalue, char *list[]);
  134. char *Get_Alloc_String(char const * section, char const * entry, char const * defvalue) const;
  135. int Get_TextBlock(char const * section, char * buffer, int len) const;
  136. int Get_UUBlock(char const * section, void * buffer, int len) const;
  137. int Get_UUBlock(char const * section, char const *entry, void * block, int len) const;
  138. TRect<int> const Get_Rect(char const * section, char const * entry, TRect<int> const & defvalue) const;
  139. TPoint3D<int> const Get_Point(char const * section, char const * entry, TPoint3D<int> const & defvalue) const;
  140. TPoint2D<int> const Get_Point(char const * section, char const * entry, TPoint2D<int> const & defvalue) const;
  141. TPoint3D<float> const Get_Point(char const * section, char const * entry, TPoint3D<float> const & defvalue) const;
  142. TPoint2D<float> const Get_Point(char const * section, char const * entry, TPoint2D<float> const & defvalue) const;
  143. /*
  144. ** Put a data type to the section and entry specified.
  145. */
  146. bool Put_Bool(char const * section, char const * entry, bool value);
  147. bool Put_Float(char const * section, char const * entry, float number);
  148. bool Put_Double(char const * section, char const * entry, double number);
  149. bool Put_Hex(char const * section, char const * entry, int number);
  150. bool Put_Int(char const * section, char const * entry, int number, int format=0);
  151. bool Put_PKey(PKey const & key);
  152. bool Put_String(char const * section, char const * entry, char const * string);
  153. bool Put_TextBlock(char const * section, char const * text);
  154. bool Put_UUBlock(char const * section, void const * block, int len);
  155. bool Put_UUBlock(char const * section, char const *entry, void const * block, int len);
  156. bool Put_Rect(char const * section, char const * entry, TRect<int> const & value);
  157. bool Put_Point(char const * section, char const * entry, TPoint3D<int> const & value);
  158. bool Put_Point(char const * section, char const * entry, TPoint3D<float> const & value);
  159. bool Put_Point(char const * section, char const * entry, TPoint2D<int> const & value);
  160. bool Put_Wide_String(char const * section, char const * entry, const unsigned short * string);
  161. // protected:
  162. enum {MAX_LINE_LENGTH=512};
  163. /*
  164. ** Access to the list of all sections within this INI file.
  165. */
  166. List<INISection *> & Get_Section_List() { return * SectionList; }
  167. IndexClass<int, INISection *> & Get_Section_Index() { return * SectionIndex; }
  168. /*
  169. ** Utility routines to help find the appropriate section and entry objects.
  170. */
  171. INISection * Find_Section(char const * section) const;
  172. INIEntry * Find_Entry(char const * section, char const * entry) const;
  173. static void Strip_Comments(char * buffer);
  174. // the CRC function is used to produce hopefully unique values for
  175. // each of the entries in a section. Debug messages will be produced
  176. // if redundant CRCs are generated for a particular INI file.
  177. static int CRC(const char *string);
  178. // the DuplicateCRCError function is used by Put_String and Load in the case
  179. // that an entry's CRC matches one already in the database.
  180. void DuplicateCRCError(const char *message, const char *section, const char *entry);
  181. private:
  182. /*
  183. ** These functions are used to allocate and free the section list and section index
  184. */
  185. void Initialize(void);
  186. void Shutdown(void);
  187. /*
  188. ** This is the list of all sections within this INI file.
  189. */
  190. List<INISection *> * SectionList;
  191. IndexClass<int, INISection *> * SectionIndex;
  192. /*
  193. ** Ensure that the copy constructor and assignment operator never exist.
  194. */
  195. INIClass(INIClass const & rvalue);
  196. INIClass operator = (INIClass const & rvalue);
  197. /*
  198. ** The name of the file we were loaded from (if applicable).
  199. */
  200. mutable char * Filename;
  201. /*
  202. ** The flag used to control the blank entry loading behavior.
  203. */
  204. static bool KeepBlankEntries;
  205. };
  206. #endif