ReactOS 0.4.16-dev-122-g325d74c
inifile.c
Go to the documentation of this file.
1/*
2 * FreeLoader
3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
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 2 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include <freeldr.h>
21
22#include <debug.h>
24
25BOOLEAN IniOpenSection(PCSTR SectionName, ULONG_PTR* SectionId)
26{
28 PINI_SECTION Section;
29
30 TRACE("IniOpenSection() SectionName = %s\n", SectionName);
31
32 // Loop through each section and find the one we want
35 Entry = Entry->Flink)
36 {
37 Section = CONTAINING_RECORD(Entry, INI_SECTION, ListEntry);
38
39 // Compare against the section name
40 if (_stricmp(SectionName, Section->SectionName) == 0)
41 {
42 // We found it
43 if (SectionId)
44 *SectionId = (ULONG_PTR)Section;
45 TRACE("IniOpenSection() Found it! SectionId = 0x%x\n", SectionId);
46 return TRUE;
47 }
48 }
49
50 TRACE("IniOpenSection() Section not found.\n");
51
52 return FALSE;
53}
54
56{
57 PINI_SECTION Section = (PINI_SECTION)SectionId;
58
59 TRACE("IniGetNumSectionItems() SectionId = 0x%x\n", SectionId);
60 TRACE("IniGetNumSectionItems() Item count = %d\n", Section->SectionItemCount);
61
62 return Section->SectionItemCount;
63}
64
66{
67 PINI_SECTION Section = (PINI_SECTION)SectionId;
69 PINI_SECTION_ITEM SectionItem;
70
71 // Loop through each section item and find the one we want
72 for (Entry = Section->SectionItemList.Flink;
73 Entry != &Section->SectionItemList;
74 Entry = Entry->Flink)
75 {
76 SectionItem = CONTAINING_RECORD(Entry, INI_SECTION_ITEM, ListEntry);
77
78 // Check to see if this is the setting we want
79 if (SettingNumber == 0)
80 {
81 return SectionItem;
82 }
83
84 // Nope, keep going
85 SettingNumber--;
86 }
87 return NULL;
88}
89
91{
92 PINI_SECTION_ITEM SectionItem;
93
94 // Retrieve requested setting
95 SectionItem = IniGetSettingByNumber(SectionId, SettingIndex);
96 if (!SectionItem)
97 return 0;
98
99 // Return the size of the string plus 1 for the null-terminator
100 return (ULONG)(strlen(SectionItem->ItemName) + 1);
101}
102
104{
105 PINI_SECTION_ITEM SectionItem;
106
107 // Retrieve requested setting
108 SectionItem = IniGetSettingByNumber(SectionId, SettingIndex);
109 if (!SectionItem)
110 return 0;
111
112 // Return the size of the string plus 1 for the null-terminator
113 return (ULONG)(strlen(SectionItem->ItemValue) + 1);
114}
115
116BOOLEAN IniReadSettingByNumber(ULONG_PTR SectionId, ULONG SettingNumber, PCHAR SettingName, ULONG NameSize, PCHAR SettingValue, ULONG ValueSize)
117{
118 PINI_SECTION_ITEM SectionItem;
119 TRACE(".001 NameSize = %d ValueSize = %d\n", NameSize, ValueSize);
120
121 TRACE("IniReadSettingByNumber() SectionId = 0x%x\n", SectionId);
122
123 // Retrieve requested setting
124 SectionItem = IniGetSettingByNumber(SectionId, SettingNumber);
125 if (!SectionItem)
126 {
127 TRACE("IniReadSettingByNumber() Setting number %d not found.\n", SettingNumber);
128 return FALSE;
129 }
130
131 TRACE("IniReadSettingByNumber() Setting number %d found.\n", SettingNumber);
132 TRACE("IniReadSettingByNumber() Setting name = %s\n", SectionItem->ItemName);
133 TRACE("IniReadSettingByNumber() Setting value = %s\n", SectionItem->ItemValue);
134
135 TRACE("1 NameSize = %d ValueSize = %d\n", NameSize, ValueSize);
136 TRACE("2 NameSize = %d ValueSize = %d\n", NameSize, ValueSize);
137 strncpy(SettingName, SectionItem->ItemName, NameSize - 1);
138 SettingName[NameSize - 1] = '\0';
139 TRACE("3 NameSize = %d ValueSize = %d\n", NameSize, ValueSize);
140 strncpy(SettingValue, SectionItem->ItemValue, ValueSize - 1);
141 SettingValue[ValueSize - 1] = '\0';
142 TRACE("4 NameSize = %d ValueSize = %d\n", NameSize, ValueSize);
143 DbgDumpBuffer(DPRINT_INIFILE, SettingName, NameSize);
144 DbgDumpBuffer(DPRINT_INIFILE, SettingValue, ValueSize);
145
146 return TRUE;
147}
148
150{
151 PINI_SECTION Section = (PINI_SECTION)SectionId;
153 PINI_SECTION_ITEM SectionItem;
154
155 TRACE("IniReadSettingByName() SectionId = 0x%x\n", SectionId);
156
157 // Loop through each section item and find the one we want
158 for (Entry = Section->SectionItemList.Flink;
159 Entry != &Section->SectionItemList;
160 Entry = Entry->Flink)
161 {
162 SectionItem = CONTAINING_RECORD(Entry, INI_SECTION_ITEM, ListEntry);
163
164 // Check to see if this is the setting we want
165 if (_stricmp(SettingName, SectionItem->ItemName) == 0)
166 {
167 TRACE("IniReadSettingByName() Setting \'%s\' found.\n", SettingName);
168 TRACE("IniReadSettingByName() Setting value = %s\n", SectionItem->ItemValue);
169
170 strncpy(Buffer, SectionItem->ItemValue, BufferSize - 1);
171 Buffer[BufferSize - 1] = '\0';
172
173 return TRUE;
174 }
175 }
176
177 WARN("IniReadSettingByName() Setting \'%s\' not found.\n", SettingName);
178
179 return FALSE;
180}
181
182BOOLEAN IniAddSection(PCSTR SectionName, ULONG_PTR* SectionId)
183{
184 PINI_SECTION Section;
185
186 // Allocate a new section structure
187 Section = FrLdrTempAlloc(sizeof(INI_SECTION), TAG_INI_SECTION);
188 if (!Section)
189 {
190 return FALSE;
191 }
192
193 RtlZeroMemory(Section, sizeof(INI_SECTION));
194
195 // Allocate the section name buffer
196 Section->SectionName = FrLdrTempAlloc(strlen(SectionName) + sizeof(CHAR), TAG_INI_NAME);
197 if (!Section->SectionName)
198 {
200 return FALSE;
201 }
202
203 // Get the section name
204 strcpy(Section->SectionName, SectionName);
205 InitializeListHead(&Section->SectionItemList);
206
207 // Add it to the section list head
210
211 *SectionId = (ULONG_PTR)Section;
212
213 return TRUE;
214}
215
217{
218 PLIST_ENTRY ListEntry;
219 PINI_SECTION_ITEM SectionItem;
220
221 // Loop while there are section items
222 while (!IsListEmpty(&Section->SectionItemList))
223 {
224 // Remove the section item
225 ListEntry = RemoveHeadList(&Section->SectionItemList);
226 SectionItem = CONTAINING_RECORD(ListEntry, INI_SECTION_ITEM, ListEntry);
227
228 // Free it
229 FrLdrTempFree(SectionItem->ItemName, TAG_INI_NAME);
232 }
233
234 FrLdrTempFree(Section->SectionName, TAG_INI_NAME);
236}
237
239{
240 PLIST_ENTRY ListEntry;
241 PINI_SECTION Section;
242
243 // Loop while there are sections
245 {
246 // Remove the section
248 Section = CONTAINING_RECORD(ListEntry, INI_SECTION, ListEntry);
249
250 // Free it
251 IniFreeSection(Section);
252 }
253}
254
255BOOLEAN IniAddSettingValueToSection(ULONG_PTR SectionId, PCSTR SettingName, PCSTR SettingValue)
256{
257 PINI_SECTION Section = (PINI_SECTION)SectionId;
258 PINI_SECTION_ITEM SectionItem;
259
260 // Allocate a new item structure
262 if (!SectionItem)
263 {
264 return FALSE;
265 }
266
267 RtlZeroMemory(SectionItem, sizeof(INI_SECTION_ITEM));
268
269 // Allocate the setting name buffer
270 SectionItem->ItemName = FrLdrTempAlloc(strlen(SettingName) + 1, TAG_INI_NAME);
271 if (!SectionItem->ItemName)
272 {
274 return FALSE;
275 }
276
277 // Allocate the setting value buffer
278 SectionItem->ItemValue = FrLdrTempAlloc(strlen(SettingValue) + 1, TAG_INI_VALUE);
279 if (!SectionItem->ItemValue)
280 {
281 FrLdrTempFree(SectionItem->ItemName, TAG_INI_NAME);
283 return FALSE;
284 }
285
286 strcpy(SectionItem->ItemName, SettingName);
287 strcpy(SectionItem->ItemValue, SettingValue);
288
289 // Add it to the current section
290 Section->SectionItemCount++;
291 InsertTailList(&Section->SectionItemList, &SectionItem->ListEntry);
292
293 return TRUE;
294}
295
296BOOLEAN IniModifySettingValue(ULONG_PTR SectionId, PCSTR SettingName, PCSTR SettingValue)
297{
298 PINI_SECTION Section = (PINI_SECTION)SectionId;
300 PINI_SECTION_ITEM SectionItem;
301 PCHAR NewItemValue;
302
303 // Loop through each section item and find the one we want
304 for (Entry = Section->SectionItemList.Flink;
305 Entry != &Section->SectionItemList;
306 Entry = Entry->Flink)
307 {
308 SectionItem = CONTAINING_RECORD(Entry, INI_SECTION_ITEM, ListEntry);
309
310 // Check to see if this is the setting we want
311 if (_stricmp(SectionItem->ItemName, SettingName) == 0)
312 {
313 break;
314 }
315 // Nope, keep going
316 }
317 // If the section item does not exist, create it
318 if (Entry == &Section->SectionItemList)
319 {
320 return IniAddSettingValueToSection(SectionId, SettingName, SettingValue);
321 }
322
323 // Reallocate the new setting value buffer
324 NewItemValue = FrLdrTempAlloc(strlen(SettingValue) + 1, TAG_INI_VALUE);
325 if (!NewItemValue)
326 {
327 // We failed, bail out
328 return FALSE;
329 }
331 SectionItem->ItemValue = NewItemValue;
332
333 strcpy(SectionItem->ItemValue, SettingValue);
334
335 return TRUE;
336}
unsigned char BOOLEAN
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
char * strncpy(char *DstString, const char *SrcString, ACPI_SIZE Count)
Definition: utclib.c:427
#define WARN(fmt,...)
Definition: precomp.h:61
#define DbgDumpBuffer(mask, buf, len)
Definition: debug.h:122
#define DPRINT_INIFILE
Definition: debug.h:27
#define DBG_DEFAULT_CHANNEL(ch)
Definition: debug.h:106
FORCEINLINE PVOID FrLdrTempAlloc(_In_ SIZE_T Size, _In_ ULONG Tag)
Definition: mm.h:188
FORCEINLINE VOID FrLdrTempFree(PVOID Allocation, ULONG Tag)
Definition: mm.h:197
#define _stricmp
Definition: cat.c:22
Definition: bufpool.h:45
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define ULONG_PTR
Definition: config.h:101
#define InsertTailList(ListHead, Entry)
#define InsertHeadList(ListHead, Entry)
#define IsListEmpty(ListHead)
Definition: env_spec_w32.h:954
#define RemoveHeadList(ListHead)
Definition: env_spec_w32.h:964
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
struct _INI_SECTION * PINI_SECTION
BOOLEAN IniAddSection(PCSTR SectionName, ULONG_PTR *SectionId)
Definition: inifile.c:182
ULONG IniGetSectionSettingValueSize(ULONG_PTR SectionId, ULONG SettingIndex)
Definition: inifile.c:103
ULONG IniGetNumSectionItems(ULONG_PTR SectionId)
Definition: inifile.c:55
BOOLEAN IniAddSettingValueToSection(ULONG_PTR SectionId, PCSTR SettingName, PCSTR SettingValue)
Definition: inifile.c:255
BOOLEAN IniModifySettingValue(ULONG_PTR SectionId, PCSTR SettingName, PCSTR SettingValue)
Definition: inifile.c:296
ULONG IniGetSectionSettingNameSize(ULONG_PTR SectionId, ULONG SettingIndex)
Definition: inifile.c:90
PINI_SECTION_ITEM IniGetSettingByNumber(ULONG_PTR SectionId, ULONG SettingNumber)
Definition: inifile.c:65
BOOLEAN IniReadSettingByNumber(ULONG_PTR SectionId, ULONG SettingNumber, PCHAR SettingName, ULONG NameSize, PCHAR SettingValue, ULONG ValueSize)
Definition: inifile.c:116
BOOLEAN IniReadSettingByName(ULONG_PTR SectionId, PCSTR SettingName, PCHAR Buffer, ULONG BufferSize)
Definition: inifile.c:149
BOOLEAN IniOpenSection(PCSTR SectionName, ULONG_PTR *SectionId)
Definition: inifile.c:25
VOID IniCleanup(VOID)
Definition: inifile.c:238
VOID IniFreeSection(PINI_SECTION Section)
Definition: inifile.c:216
#define TAG_INI_VALUE
Definition: inifile.h:28
ULONG IniFileSectionCount
Definition: parse.c:27
#define TAG_INI_SECTION_ITEM
Definition: inifile.h:26
#define TAG_INI_SECTION
Definition: inifile.h:25
#define TAG_INI_NAME
Definition: inifile.h:27
LIST_ENTRY IniFileSectionListHead
Definition: parse.c:25
#define TRACE(s)
Definition: solgame.cpp:4
base of all file and directory entries
Definition: entries.h:83
LIST_ENTRY ListEntry
Definition: inifile.h:35
PCHAR ItemName
Definition: inifile.h:36
PCHAR ItemValue
Definition: inifile.h:37
LIST_ENTRY ListEntry
Definition: inicache.h:21
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
@ INIFILE
Definition: tnconfig.cpp:129
const char * PCSTR
Definition: typedefs.h:52
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
uint32_t ULONG
Definition: typedefs.h:59
char * PCHAR
Definition: typedefs.h:51
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:254
_In_ WDFUSBINTERFACE _In_ UCHAR SettingIndex
Definition: wdfusb.h:2303
char CHAR
Definition: xmlstorage.h:175