INI.H 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/INI.H 1 3/03/97 10:24a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** 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 ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : INI.H *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 05/15/96 *
  26. * *
  27. * Last Update : May 15, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  32. #ifndef INI_H
  33. #define INI_H
  34. #include <stdlib.h>
  35. #include "listnode.h"
  36. #include "pipe.h"
  37. #include "wwfile.h"
  38. #include "pk.h"
  39. #include "fixed.h"
  40. #include "crc.h"
  41. #include "search.h"
  42. /*
  43. ** This is an INI database handler class. It handles a database with a disk format identical
  44. ** to the INI files commonly used by Windows.
  45. */
  46. class INIClass {
  47. public:
  48. INIClass(void) {}
  49. ~INIClass(void);
  50. /*
  51. ** Fetch and store INI data.
  52. */
  53. bool Load(FileClass & file);
  54. bool Load(Straw & file);
  55. int Save(FileClass & file) const;
  56. int Save(Pipe & file) const;
  57. /*
  58. ** Erase all data within this INI file manager.
  59. */
  60. bool Clear(char const * section = 0, char const * entry = 0);
  61. int Line_Count(char const * section) const;
  62. bool Is_Loaded(void) const {return(!SectionList.Is_Empty());}
  63. int Size(void) const;
  64. 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);}
  65. /*
  66. ** Fetch the number of sections in the INI file or verify if a specific
  67. ** section is present.
  68. */
  69. int Section_Count(void) const;
  70. bool Section_Present(char const * section) const {return(Find_Section(section) != NULL);}
  71. /*
  72. ** Fetch the number of entries in a section or get a particular entry in a section.
  73. */
  74. int Entry_Count(char const * section) const;
  75. char const * Get_Entry(char const * section, int index) const;
  76. /*
  77. ** Get the various data types from the section and entry specified.
  78. */
  79. int Get_String(char const * section, char const * entry, char const * defvalue, char * buffer, int size) const;
  80. int Get_Int(char const * section, char const * entry, int defvalue=0) const;
  81. int Get_Hex(char const * section, char const * entry, int defvalue=0) const;
  82. bool Get_Bool(char const * section, char const * entry, bool defvalue=false) const;
  83. int Get_TextBlock(char const * section, char * buffer, int len) const;
  84. int Get_UUBlock(char const * section, void * buffer, int len) const;
  85. PKey Get_PKey(bool fast) const;
  86. fixed Get_Fixed(char const * section, char const * entry, fixed defvalue) const;
  87. /*
  88. ** Put a data type to the section and entry specified.
  89. */
  90. bool Put_Fixed(char const * section, char const * entry, fixed value);
  91. bool Put_String(char const * section, char const * entry, char const * string);
  92. bool Put_Hex(char const * section, char const * entry, int number);
  93. bool Put_Int(char const * section, char const * entry, int number, int format=0);
  94. bool Put_Bool(char const * section, char const * entry, bool value);
  95. bool Put_TextBlock(char const * section, char const * text);
  96. bool Put_UUBlock(char const * section, void const * block, int len);
  97. bool Put_PKey(PKey const & key);
  98. protected:
  99. enum {MAX_LINE_LENGTH=128};
  100. /*
  101. ** The value entries for the INI file are stored as objects of this type.
  102. ** The entry identifier and value string are combined into this object.
  103. */
  104. struct INIEntry : Node<INIEntry> {
  105. INIEntry(char * entry = 0, char * value = 0) : Entry(entry), Value(value) {}
  106. ~INIEntry(void) {free(Entry);Entry = 0;free(Value);Value = 0;}
  107. int Index_ID(void) const {return(CRCEngine()(Entry, strlen(Entry)));};
  108. char * Entry;
  109. char * Value;
  110. };
  111. /*
  112. ** Each section (bracketed) is represented by an object of this type. All entries
  113. ** subordinate to this section are attached.
  114. */
  115. struct INISection : Node<INISection> {
  116. INISection(char * section) : Section(section) {}
  117. ~INISection(void) {free(Section);Section = 0;EntryList.Delete();}
  118. INIEntry * Find_Entry(char const * entry) const;
  119. int Index_ID(void) const {return(CRCEngine()(Section, strlen(Section)));};
  120. char * Section;
  121. List<INIEntry> EntryList;
  122. IndexClass<INIEntry *>EntryIndex;
  123. };
  124. /*
  125. ** Utility routines to help find the appropriate section and entry objects.
  126. */
  127. INISection * Find_Section(char const * section) const;
  128. INIEntry * Find_Entry(char const * section, char const * entry) const;
  129. static void Strip_Comments(char * buffer);
  130. /*
  131. ** This is the list of all sections within this INI file.
  132. */
  133. List<INISection> SectionList;
  134. IndexClass<INISection *> SectionIndex;
  135. };
  136. #endif