ReactOS 0.4.16-dev-974-g5022a45
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
26{
28}
29
30BOOLEAN IniOpenSection(PCSTR SectionName, ULONG_PTR* SectionId)
31{
33 PINI_SECTION Section;
34
35 TRACE("IniOpenSection() SectionName = %s\n", SectionName);
36
37 // Loop through each section and find the one we want
40 Entry = Entry->Flink)
41 {
42 Section = CONTAINING_RECORD(Entry, INI_SECTION, ListEntry);
43
44 // Compare against the section name
45 if (_stricmp(SectionName, Section->SectionName) == 0)
46 {
47 // We found it
48 if (SectionId)
49 *SectionId = (ULONG_PTR)Section;
50 TRACE("IniOpenSection() Found it! SectionId = 0x%x\n", SectionId);
51 return TRUE;
52 }
53 }
54
55 TRACE("IniOpenSection() Section not found.\n");
56
57 return FALSE;
58}
59
61{
62 PINI_SECTION Section = (PINI_SECTION)SectionId;
63
64 TRACE("IniGetNumSectionItems() SectionId = 0x%x\n", SectionId);
65 TRACE("IniGetNumSectionItems() Item count = %d\n", Section->SectionItemCount);
66
67 return Section->SectionItemCount;
68}
69
71{
72 PINI_SECTION Section = (PINI_SECTION)SectionId;
74 PINI_SECTION_ITEM SectionItem;
75
76 // Loop through each section item and find the one we want
77 for (Entry = Section->SectionItemList.Flink;
78 Entry != &Section->SectionItemList;
79 Entry = Entry->Flink)
80 {
81 SectionItem = CONTAINING_RECORD(Entry, INI_SECTION_ITEM, ListEntry);
82
83 // Check to see if this is the setting we want
84 if (SettingNumber == 0)
85 {
86 return SectionItem;
87 }
88
89 // Nope, keep going
90 SettingNumber--;
91 }
92 return NULL;
93}
94
96{
97 PINI_SECTION_ITEM SectionItem;
98
99 // Retrieve requested setting
100 SectionItem = IniGetSettingByNumber(SectionId, SettingIndex);
101 if (!SectionItem)
102 return 0;
103
104 // Return the size of the string plus 1 for the null-terminator
105 return (ULONG)(strlen(SectionItem->ItemName) + 1);
106}
107
109{
110 PINI_SECTION_ITEM SectionItem;
111
112 // Retrieve requested setting
113 SectionItem = IniGetSettingByNumber(SectionId, SettingIndex);
114 if (!SectionItem)
115 return 0;
116
117 // Return the size of the string plus 1 for the null-terminator
118 return (ULONG)(strlen(SectionItem->ItemValue) + 1);
119}
120
121BOOLEAN IniReadSettingByNumber(ULONG_PTR SectionId, ULONG SettingNumber, PCHAR SettingName, ULONG NameSize, PCHAR SettingValue, ULONG ValueSize)
122{
123 PINI_SECTION_ITEM SectionItem;
124 TRACE(".001 NameSize = %d ValueSize = %d\n", NameSize, ValueSize);
125
126 TRACE("IniReadSettingByNumber() SectionId = 0x%x\n", SectionId);
127
128 // Retrieve requested setting
129 SectionItem = IniGetSettingByNumber(SectionId, SettingNumber);
130 if (!SectionItem)
131 {
132 TRACE("IniReadSettingByNumber() Setting number %d not found.\n", SettingNumber);
133 return FALSE;
134 }
135
136 TRACE("IniReadSettingByNumber() Setting number %d found.\n", SettingNumber);
137 TRACE("IniReadSettingByNumber() Setting name = %s\n", SectionItem->ItemName);
138 TRACE("IniReadSettingByNumber() Setting value = %s\n", SectionItem->ItemValue);
139
140 TRACE("1 NameSize = %d ValueSize = %d\n", NameSize, ValueSize);
141 TRACE("2 NameSize = %d ValueSize = %d\n", NameSize, ValueSize);
142 strncpy(SettingName, SectionItem->ItemName, NameSize - 1);
143 SettingName[NameSize - 1] = '\0';
144 TRACE("3 NameSize = %d ValueSize = %d\n", NameSize, ValueSize);
145 strncpy(SettingValue, SectionItem->ItemValue, ValueSize - 1);
146 SettingValue[ValueSize - 1] = '\0';
147 TRACE("4 NameSize = %d ValueSize = %d\n", NameSize, ValueSize);
148 DbgDumpBuffer(DPRINT_INIFILE, SettingName, NameSize);
149 DbgDumpBuffer(DPRINT_INIFILE, SettingValue, ValueSize);
150
151 return TRUE;
152}
153
155{
156 PINI_SECTION Section = (PINI_SECTION)SectionId;
158 PINI_SECTION_ITEM SectionItem;
159
160 TRACE("IniReadSettingByName() SectionId = 0x%x\n", SectionId);
161
162 // Loop through each section item and find the one we want
163 for (Entry = Section->SectionItemList.Flink;
164 Entry != &Section->SectionItemList;
165 Entry = Entry->Flink)
166 {
167 SectionItem = CONTAINING_RECORD(Entry, INI_SECTION_ITEM, ListEntry);
168
169 // Check to see if this is the setting we want
170 if (_stricmp(SettingName, SectionItem->ItemName) == 0)
171 {
172 TRACE("IniReadSettingByName() Setting \'%s\' found.\n", SettingName);
173 TRACE("IniReadSettingByName() Setting value = %s\n", SectionItem->ItemValue);
174
175 strncpy(Buffer, SectionItem->ItemValue, BufferSize - 1);
176 Buffer[BufferSize - 1] = '\0';
177
178 return TRUE;
179 }
180 }
181
182 WARN("IniReadSettingByName() Setting \'%s\' not found.\n", SettingName);
183
184 return FALSE;
185}
186
187BOOLEAN IniAddSection(PCSTR SectionName, ULONG_PTR* SectionId)
188{
189 PINI_SECTION Section;
190
191 // Allocate a new section structure
192 Section = FrLdrTempAlloc(sizeof(INI_SECTION), TAG_INI_SECTION);
193 if (!Section)
194 {
195 return FALSE;
196 }
197
198 RtlZeroMemory(Section, sizeof(INI_SECTION));
199
200 // Allocate the section name buffer
201 Section->SectionName = FrLdrTempAlloc(strlen(SectionName) + sizeof(CHAR), TAG_INI_NAME);
202 if (!Section->SectionName)
203 {
205 return FALSE;
206 }
207
208 // Get the section name
209 strcpy(Section->SectionName, SectionName);
210 InitializeListHead(&Section->SectionItemList);
211
212 // Add it to the section list head
215
216 *SectionId = (ULONG_PTR)Section;
217
218 return TRUE;
219}
220
222{
223 PLIST_ENTRY ListEntry;
224 PINI_SECTION_ITEM SectionItem;
225
226 // Loop while there are section items
227 while (!IsListEmpty(&Section->SectionItemList))
228 {
229 // Remove the section item
230 ListEntry = RemoveHeadList(&Section->SectionItemList);
231 SectionItem = CONTAINING_RECORD(ListEntry, INI_SECTION_ITEM, ListEntry);
232
233 // Free it
234 FrLdrTempFree(SectionItem->ItemName, TAG_INI_NAME);
237 }
238
239 FrLdrTempFree(Section->SectionName, TAG_INI_NAME);
241}
242
244{
245 PLIST_ENTRY ListEntry;
246 PINI_SECTION Section;
247
248 // Loop while there are sections
250 {
251 // Remove the section
253 Section = CONTAINING_RECORD(ListEntry, INI_SECTION, ListEntry);
254
255 // Free it
256 IniFreeSection(Section);
257 }
258}
259
260BOOLEAN IniAddSettingValueToSection(ULONG_PTR SectionId, PCSTR SettingName, PCSTR SettingValue)
261{
262 PINI_SECTION Section = (PINI_SECTION)SectionId;
263 PINI_SECTION_ITEM SectionItem;
264
265 // Allocate a new item structure
267 if (!SectionItem)
268 {
269 return FALSE;
270 }
271
272 RtlZeroMemory(SectionItem, sizeof(INI_SECTION_ITEM));
273
274 // Allocate the setting name buffer
275 SectionItem->ItemName = FrLdrTempAlloc(strlen(SettingName) + 1, TAG_INI_NAME);
276 if (!SectionItem->ItemName)
277 {
279 return FALSE;
280 }
281
282 // Allocate the setting value buffer
283 SectionItem->ItemValue = FrLdrTempAlloc(strlen(SettingValue) + 1, TAG_INI_VALUE);
284 if (!SectionItem->ItemValue)
285 {
286 FrLdrTempFree(SectionItem->ItemName, TAG_INI_NAME);
288 return FALSE;
289 }
290
291 strcpy(SectionItem->ItemName, SettingName);
292 strcpy(SectionItem->ItemValue, SettingValue);
293
294 // Add it to the current section
295 Section->SectionItemCount++;
296 InsertTailList(&Section->SectionItemList, &SectionItem->ListEntry);
297
298 return TRUE;
299}
300
301BOOLEAN IniModifySettingValue(ULONG_PTR SectionId, PCSTR SettingName, PCSTR SettingValue)
302{
303 PINI_SECTION Section = (PINI_SECTION)SectionId;
305 PINI_SECTION_ITEM SectionItem;
306 PCHAR NewItemValue;
307
308 // Loop through each section item and find the one we want
309 for (Entry = Section->SectionItemList.Flink;
310 Entry != &Section->SectionItemList;
311 Entry = Entry->Flink)
312 {
313 SectionItem = CONTAINING_RECORD(Entry, INI_SECTION_ITEM, ListEntry);
314
315 // Check to see if this is the setting we want
316 if (_stricmp(SectionItem->ItemName, SettingName) == 0)
317 {
318 break;
319 }
320 // Nope, keep going
321 }
322 // If the section item does not exist, create it
323 if (Entry == &Section->SectionItemList)
324 {
325 return IniAddSettingValueToSection(SectionId, SettingName, SettingValue);
326 }
327
328 // Reallocate the new setting value buffer
329 NewItemValue = FrLdrTempAlloc(strlen(SettingValue) + 1, TAG_INI_VALUE);
330 if (!NewItemValue)
331 {
332 // We failed, bail out
333 return FALSE;
334 }
336 SectionItem->ItemValue = NewItemValue;
337
338 strcpy(SectionItem->ItemValue, SettingValue);
339
340 return TRUE;
341}
unsigned char BOOLEAN
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#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
VOID FrLdrTempFree(PVOID Allocation, ULONG Tag)
Definition: heap.c:553
PVOID FrLdrTempAlloc(_In_ SIZE_T Size, _In_ ULONG Tag)
Definition: heap.c:545
#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:187
ULONG IniGetSectionSettingValueSize(ULONG_PTR SectionId, ULONG SettingIndex)
Definition: inifile.c:108
ULONG IniGetNumSectionItems(ULONG_PTR SectionId)
Definition: inifile.c:60
BOOLEAN IniAddSettingValueToSection(ULONG_PTR SectionId, PCSTR SettingName, PCSTR SettingValue)
Definition: inifile.c:260
BOOLEAN IniModifySettingValue(ULONG_PTR SectionId, PCSTR SettingName, PCSTR SettingValue)
Definition: inifile.c:301
ULONG IniGetSectionSettingNameSize(ULONG_PTR SectionId, ULONG SettingIndex)
Definition: inifile.c:95
PINI_SECTION_ITEM IniGetSettingByNumber(ULONG_PTR SectionId, ULONG SettingNumber)
Definition: inifile.c:70
BOOLEAN IniReadSettingByNumber(ULONG_PTR SectionId, ULONG SettingNumber, PCHAR SettingName, ULONG NameSize, PCHAR SettingValue, ULONG ValueSize)
Definition: inifile.c:121
BOOLEAN IniReadSettingByName(ULONG_PTR SectionId, PCSTR SettingName, PCHAR Buffer, ULONG BufferSize)
Definition: inifile.c:154
BOOLEAN IniOpenSection(PCSTR SectionName, ULONG_PTR *SectionId)
Definition: inifile.c:30
PLIST_ENTRY IniGetFileSectionListHead(VOID)
Definition: inifile.c:25
VOID IniCleanup(VOID)
Definition: inifile.c:243
VOID IniFreeSection(PINI_SECTION Section)
Definition: inifile.c:221
#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
strncpy
Definition: string.h:335
strcpy
Definition: string.h:131
#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