INI.H 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. ** Command & Conquer Red Alert(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. /* $Header: /CounterStrike/INI.H 1 3/03/97 10:24a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** 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 ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : INI.H *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 05/15/96 *
  30. * *
  31. * Last Update : May 15, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #ifndef INI_H
  37. #define INI_H
  38. #include <stdlib.h>
  39. #include "listnode.h"
  40. #include "pipe.h"
  41. #include "wwfile.h"
  42. #include "pk.h"
  43. #include "fixed.h"
  44. #include "crc.h"
  45. #include "search.h"
  46. /*
  47. ** This is an INI database handler class. It handles a database with a disk format identical
  48. ** to the INI files commonly used by Windows.
  49. */
  50. class INIClass {
  51. public:
  52. INIClass(void) {}
  53. ~INIClass(void);
  54. /*
  55. ** Fetch and store INI data.
  56. */
  57. bool Load(FileClass & file);
  58. bool Load(Straw & file);
  59. int Save(FileClass & file) const;
  60. int Save(Pipe & file) const;
  61. /*
  62. ** Erase all data within this INI file manager.
  63. */
  64. bool Clear(char const * section = 0, char const * entry = 0);
  65. int Line_Count(char const * section) const;
  66. bool Is_Loaded(void) const {return(!SectionList.Is_Empty());}
  67. int Size(void) const;
  68. bool Is_Present(char const * section, char const * entry = 0) const {if (entry == 0) return(Find_Section(section) != 0);return(Find_Entry(section, entry) != 0);}
  69. /*
  70. ** Fetch the number of sections in the INI file or verify if a specific
  71. ** section is present.
  72. */
  73. int Section_Count(void) const;
  74. bool Section_Present(char const * section) const {return(Find_Section(section) != NULL);}
  75. /*
  76. ** Fetch the number of entries in a section or get a particular entry in a section.
  77. */
  78. int Entry_Count(char const * section) const;
  79. char const * Get_Entry(char const * section, int index) const;
  80. /*
  81. ** Get the various data types from the section and entry specified.
  82. */
  83. int Get_String(char const * section, char const * entry, char const * defvalue, char * buffer, int size) const;
  84. int Get_Int(char const * section, char const * entry, int defvalue=0) const;
  85. int Get_Hex(char const * section, char const * entry, int defvalue=0) const;
  86. bool Get_Bool(char const * section, char const * entry, bool defvalue=false) const;
  87. int Get_TextBlock(char const * section, char * buffer, int len) const;
  88. int Get_UUBlock(char const * section, void * buffer, int len) const;
  89. PKey Get_PKey(bool fast) const;
  90. fixed Get_Fixed(char const * section, char const * entry, fixed defvalue) const;
  91. /*
  92. ** Put a data type to the section and entry specified.
  93. */
  94. bool Put_Fixed(char const * section, char const * entry, fixed value);
  95. bool Put_String(char const * section, char const * entry, char const * string);
  96. bool Put_Hex(char const * section, char const * entry, int number);
  97. bool Put_Int(char const * section, char const * entry, int number, int format=0);
  98. bool Put_Bool(char const * section, char const * entry, bool value);
  99. bool Put_TextBlock(char const * section, char const * text);
  100. bool Put_UUBlock(char const * section, void const * block, int len);
  101. bool Put_PKey(PKey const & key);
  102. protected:
  103. enum {MAX_LINE_LENGTH=128};
  104. /*
  105. ** The value entries for the INI file are stored as objects of this type.
  106. ** The entry identifier and value string are combined into this object.
  107. */
  108. struct INIEntry : Node<INIEntry> {
  109. INIEntry(char * entry = 0, char * value = 0) : Entry(entry), Value(value) {}
  110. ~INIEntry(void) {free(Entry);Entry = 0;free(Value);Value = 0;}
  111. int Index_ID(void) const {return(CRCEngine()(Entry, strlen(Entry)));};
  112. char * Entry;
  113. char * Value;
  114. };
  115. /*
  116. ** Each section (bracketed) is represented by an object of this type. All entries
  117. ** subordinate to this section are attached.
  118. */
  119. struct INISection : Node<INISection> {
  120. INISection(char * section) : Section(section) {}
  121. ~INISection(void) {free(Section);Section = 0;EntryList.Delete();}
  122. INIEntry * Find_Entry(char const * entry) const;
  123. int Index_ID(void) const {return(CRCEngine()(Section, strlen(Section)));};
  124. char * Section;
  125. List<INIEntry> EntryList;
  126. IndexClass<INIEntry *>EntryIndex;
  127. };
  128. /*
  129. ** Utility routines to help find the appropriate section and entry objects.
  130. */
  131. INISection * Find_Section(char const * section) const;
  132. INIEntry * Find_Entry(char const * section, char const * entry) const;
  133. static void Strip_Comments(char * buffer);
  134. /*
  135. ** This is the list of all sections within this INI file.
  136. */
  137. List<INISection> SectionList;
  138. IndexClass<INISection *> SectionIndex;
  139. };
  140. #endif