INI.H 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. ** Command & Conquer Generals(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:: /VSS_Sync/wwlib/ini.h $*
  25. * *
  26. * $Author:: Vss_sync $*
  27. * *
  28. * $Modtime:: 3/21/01 12:01p $*
  29. * *
  30. * $Revision:: 12 $*
  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 FileFactoryClass;
  53. struct INISection;
  54. struct INIEntry;
  55. template<class T> class TPoint3D;
  56. template<class T> class TPoint2D;
  57. template<class T> class TRect;
  58. template<class T> class List;
  59. template<class T, class U> class IndexClass;
  60. #ifndef NULL
  61. #define NULL 0L
  62. #endif
  63. /*
  64. ** This is an INI database handler class. It handles a database with a disk format identical
  65. ** to the INI files commonly used by Windows.
  66. */
  67. class INIClass {
  68. public:
  69. INIClass(void);
  70. INIClass(FileClass & file);
  71. INIClass(const char *filename);
  72. virtual ~INIClass(void);
  73. /*
  74. ** Fetch and store INI data.
  75. */
  76. int Load(FileClass & file);
  77. int Load(Straw & file);
  78. int Load(const char *filename);
  79. int Save(FileClass & file) const;
  80. int Save(Pipe & file) const;
  81. int Save(const char *filename) const;
  82. /*
  83. ** Erase all data within this INI file manager.
  84. */
  85. bool Clear(char const * section = NULL, char const * entry = NULL);
  86. // int Line_Count(char const * section) const;
  87. bool Is_Loaded(void) const;
  88. int Size(void) const;
  89. 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);}
  90. /*
  91. ** Fetch the number of sections in the INI file or verify if a specific
  92. ** section is present.
  93. */
  94. int Section_Count(void) const;
  95. bool Section_Present(char const * section) const {return(Find_Section(section) != NULL);}
  96. /*
  97. ** Fetch the number of entries in a section or get a particular entry in a section.
  98. */
  99. int Entry_Count(char const * section) const;
  100. char const * Get_Entry(char const * section, int index) const;
  101. /*
  102. ** Cound how many entries with the indicated prefix followed by a number exist in the section.
  103. */
  104. unsigned Enumerate_Entries(const char * section, const char * entry_prefix, unsigned startnumber = 0, unsigned endnumber = (unsigned) -1);
  105. /*
  106. ** Get the various data types from the section and entry specified.
  107. */
  108. PKey Get_PKey(bool fast) const;
  109. bool Get_Bool(char const * section, char const * entry, bool defvalue=false) const;
  110. float Get_Float(char const * section, char const * entry, float defvalue=0.0) const;
  111. int Get_Hex(char const * section, char const * entry, int defvalue=0) const;
  112. int Get_Int(char const * section, char const * entry, int defvalue=0) const;
  113. int Get_String(char const * section, char const * entry, char const * defvalue, char * buffer, int size) const;
  114. StringClass Get_String(char const * section, char const * entry, char const * defvalue="") const;
  115. int Get_List_Index(char const * section, char const * entry, int const defvalue, char *list[]);
  116. int * Get_Alloc_Int_Array(char const * section, char const * entry, int listend);
  117. int Get_Int_Bitfield(char const * section, char const * entry, int defvalue, char *list[]);
  118. char *Get_Alloc_String(char const * section, char const * entry, char const * defvalue) const;
  119. int Get_TextBlock(char const * section, char * buffer, int len) const;
  120. int Get_UUBlock(char const * section, void * buffer, int len) const;
  121. TRect<int> const Get_Rect(char const * section, char const * entry, TRect<int> const & defvalue) const;
  122. TPoint3D<int> const Get_Point(char const * section, char const * entry, TPoint3D<int> const & defvalue) const;
  123. TPoint2D<int> const Get_Point(char const * section, char const * entry, TPoint2D<int> const & defvalue) const;
  124. TPoint3D<float> const Get_Point(char const * section, char const * entry, TPoint3D<float> const & defvalue) const;
  125. TPoint2D<float> const Get_Point(char const * section, char const * entry, TPoint2D<float> const & defvalue) const;
  126. /*
  127. ** Put a data type to the section and entry specified.
  128. */
  129. bool Put_Bool(char const * section, char const * entry, bool value);
  130. bool Put_Float(char const * section, char const * entry, double number);
  131. bool Put_Hex(char const * section, char const * entry, int number);
  132. bool Put_Int(char const * section, char const * entry, int number, int format=0);
  133. bool Put_PKey(PKey const & key);
  134. bool Put_String(char const * section, char const * entry, char const * string);
  135. bool Put_TextBlock(char const * section, char const * text);
  136. bool Put_UUBlock(char const * section, void const * block, int len);
  137. bool Put_Rect(char const * section, char const * entry, TRect<int> const & value);
  138. bool Put_Point(char const * section, char const * entry, TPoint3D<int> const & value);
  139. bool Put_Point(char const * section, char const * entry, TPoint3D<float> const & value);
  140. bool Put_Point(char const * section, char const * entry, TPoint2D<int> const & value);
  141. // protected:
  142. enum {MAX_LINE_LENGTH=512};
  143. /*
  144. ** Access to the list of all sections within this INI file.
  145. */
  146. List<INISection *> & Get_Section_List() { return * SectionList; }
  147. IndexClass<int, INISection *> & Get_Section_Index() { return * SectionIndex; }
  148. /*
  149. ** Utility routines to help find the appropriate section and entry objects.
  150. */
  151. INISection * Find_Section(char const * section) const;
  152. INIEntry * Find_Entry(char const * section, char const * entry) const;
  153. static void Strip_Comments(char * buffer);
  154. // the CRC function is used to produce hopefully unique values for
  155. // each of the entries in a section. Debug messages will be produced
  156. // if redundant CRCs are generated for a particular INI file.
  157. static int CRC(const char *string);
  158. // the DuplicateCRCError function is used by Put_String and Load in the case
  159. // that an entry's CRC matches one already in the database.
  160. void DuplicateCRCError(const char *message, const char *entry);
  161. private:
  162. /*
  163. ** These functions are used to allocate and free the section list and section index
  164. */
  165. void Initialize(void);
  166. void Shutdown(void);
  167. /*
  168. ** This is the list of all sections within this INI file.
  169. */
  170. List<INISection *> * SectionList;
  171. IndexClass<int, INISection *> * SectionIndex;
  172. /*
  173. ** Ensure that the copy constructor and assignment operator never exist.
  174. */
  175. INIClass(INIClass const & rvalue);
  176. INIClass operator = (INIClass const & rvalue);
  177. };
  178. #endif