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