-revert janderwalds change until because it breaks the gcc 4.x build
[reactos.git] / reactos / boot / freeldr / freeldr / inifile / inifile.c
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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <freeldr.h>
21
22 #define NDEBUG
23 #include <debug.h>
24
25 BOOLEAN IniOpenSection(PCSTR SectionName, ULONG* SectionId)
26 {
27 PINI_SECTION Section;
28
29 DbgPrint((DPRINT_INIFILE, "IniOpenSection() SectionName = %s\n", SectionName));
30
31 if (!IniFileSectionListHead)
32 return FALSE;
33
34 // Loop through each section and find the one they want
35 Section = (PINI_SECTION)RtlListGetHead((PLIST_ITEM)IniFileSectionListHead);
36 while (Section != NULL)
37 {
38 // Compare against the section name
39 if (_stricmp(SectionName, Section->SectionName) == 0)
40 {
41 // We found it
42 *SectionId = (ULONG)Section;
43 DbgPrint((DPRINT_INIFILE, "IniOpenSection() Found it! SectionId = 0x%x\n", SectionId));
44 return TRUE;
45 }
46
47 // Get the next section in the list
48 Section = (PINI_SECTION)RtlListGetNext((PLIST_ITEM)Section);
49 }
50
51 DbgPrint((DPRINT_INIFILE, "IniOpenSection() Section not found.\n"));
52
53 return FALSE;
54 }
55
56 ULONG IniGetNumSectionItems(ULONG SectionId)
57 {
58 PINI_SECTION Section = (PINI_SECTION)SectionId;
59
60 DbgPrint((DPRINT_INIFILE, "IniGetNumSectionItems() SectionId = 0x%x\n", SectionId));
61 DbgPrint((DPRINT_INIFILE, "IniGetNumSectionItems() Item count = %d\n", Section->SectionItemCount));
62
63 return Section->SectionItemCount;
64 }
65
66 ULONG IniGetSectionSettingNameSize(ULONG SectionId, ULONG SettingIndex)
67 {
68 PINI_SECTION Section = (PINI_SECTION)SectionId;
69
70 // Return the size of the string plus 1 for the null-terminator
71 return (strlen(Section->SectionItemList[SettingIndex].ItemName) + 1);
72 }
73
74 ULONG IniGetSectionSettingValueSize(ULONG SectionId, ULONG SettingIndex)
75 {
76 PINI_SECTION Section = (PINI_SECTION)SectionId;
77
78 // Return the size of the string plus 1 for the null-terminator
79 return (strlen(Section->SectionItemList[SettingIndex].ItemValue) + 1);
80 }
81
82 BOOLEAN IniReadSettingByNumber(ULONG SectionId, ULONG SettingNumber, PCHAR SettingName, ULONG NameSize, PCHAR SettingValue, ULONG ValueSize)
83 {
84 PINI_SECTION Section = (PINI_SECTION)SectionId;
85 PINI_SECTION_ITEM SectionItem;
86 #ifdef DEBUG
87 ULONG RealSettingNumber = SettingNumber;
88 #endif
89 DbgPrint((DPRINT_INIFILE, ".001 NameSize = %d ValueSize = %d\n", NameSize, ValueSize));
90
91 DbgPrint((DPRINT_INIFILE, "IniReadSettingByNumber() SectionId = 0x%x\n", SectionId));
92
93 // Loop through each section item and find the one they want
94 DbgPrint((DPRINT_INIFILE, ".01 NameSize = %d ValueSize = %d\n", NameSize, ValueSize));
95 SectionItem = (PINI_SECTION_ITEM)RtlListGetHead((PLIST_ITEM)Section->SectionItemList);
96 while (SectionItem != NULL)
97 {
98 DbgPrint((DPRINT_INIFILE, ".1 NameSize = %d ValueSize = %d\n", NameSize, ValueSize));
99 // Check to see if this is the setting they want
100 if (SettingNumber == 0)
101 {
102 DbgPrint((DPRINT_INIFILE, "IniReadSettingByNumber() Setting number %d found.\n", RealSettingNumber));
103 DbgPrint((DPRINT_INIFILE, "IniReadSettingByNumber() Setting name = %s\n", SectionItem->ItemName));
104 DbgPrint((DPRINT_INIFILE, "IniReadSettingByNumber() Setting value = %s\n", SectionItem->ItemValue));
105
106 DbgPrint((DPRINT_INIFILE, "1 NameSize = %d ValueSize = %d\n", NameSize, ValueSize));
107 RtlZeroMemory(SettingName, NameSize);
108 RtlZeroMemory(SettingValue, ValueSize);
109 DbgPrint((DPRINT_INIFILE, "2 NameSize = %d ValueSize = %d\n", NameSize, ValueSize));
110 strncpy(SettingName, SectionItem->ItemName, NameSize);
111 DbgPrint((DPRINT_INIFILE, "3 NameSize = %d ValueSize = %d\n", NameSize, ValueSize));
112 strncpy(SettingValue, SectionItem->ItemValue, ValueSize);
113 DbgPrint((DPRINT_INIFILE, "4 NameSize = %d ValueSize = %d\n", NameSize, ValueSize));
114 DbgDumpBuffer(DPRINT_INIFILE, SettingName, NameSize);
115 DbgDumpBuffer(DPRINT_INIFILE, SettingValue, ValueSize);
116
117 return TRUE;
118 }
119
120 // Nope, keep going
121 SettingNumber--;
122
123 // Get the next section item in the list
124 SectionItem = (PINI_SECTION_ITEM)RtlListGetNext((PLIST_ITEM)SectionItem);
125 }
126
127 DbgPrint((DPRINT_INIFILE, "IniReadSettingByNumber() Setting number %d not found.\n", RealSettingNumber));
128
129 return FALSE;
130 }
131
132 BOOLEAN IniReadSettingByName(ULONG SectionId, PCSTR SettingName, PCHAR Buffer, ULONG BufferSize)
133 {
134 PINI_SECTION Section = (PINI_SECTION)SectionId;
135 PINI_SECTION_ITEM SectionItem;
136
137 DbgPrint((DPRINT_INIFILE, "IniReadSettingByName() SectionId = 0x%x\n", SectionId));
138
139 // Loop through each section item and find the one they want
140 SectionItem = (PINI_SECTION_ITEM)RtlListGetHead((PLIST_ITEM)Section->SectionItemList);
141 while (SectionItem != NULL)
142 {
143 // Check to see if this is the setting they want
144 if (_stricmp(SettingName, SectionItem->ItemName) == 0)
145 {
146 DbgPrint((DPRINT_INIFILE, "IniReadSettingByName() Setting \'%s\' found.\n", SettingName));
147 DbgPrint((DPRINT_INIFILE, "IniReadSettingByName() Setting value = %s\n", SectionItem->ItemValue));
148
149 RtlZeroMemory(Buffer, BufferSize);
150 strncpy(Buffer, SectionItem->ItemValue, BufferSize);
151
152 return TRUE;
153 }
154
155 // Get the next section item in the list
156 SectionItem = (PINI_SECTION_ITEM)RtlListGetNext((PLIST_ITEM)SectionItem);
157 }
158
159 DbgPrint((DPRINT_INIFILE, "IniReadSettingByName() Setting \'%s\' not found.\n", SettingName));
160
161 return FALSE;
162 }
163
164 BOOLEAN IniAddSection(PCSTR SectionName, ULONG* SectionId)
165 {
166 PINI_SECTION Section;
167
168 // Allocate a new section structure
169 Section = MmAllocateMemory(sizeof(INI_SECTION));
170 if (!Section)
171 {
172 return FALSE;
173 }
174
175 RtlZeroMemory(Section, sizeof(INI_SECTION));
176
177 // Allocate the section name buffer
178 Section->SectionName = MmAllocateMemory(strlen(SectionName));
179 if (!Section->SectionName)
180 {
181 MmFreeMemory(Section);
182 return FALSE;
183 }
184
185 // Get the section name
186 strcpy(Section->SectionName, SectionName);
187
188 // Add it to the section list head
189 IniFileSectionCount++;
190 if (IniFileSectionListHead == NULL)
191 {
192 IniFileSectionListHead = Section;
193 }
194 else
195 {
196 RtlListInsertTail((PLIST_ITEM)IniFileSectionListHead, (PLIST_ITEM)Section);
197 }
198
199 *SectionId = (ULONG)Section;
200
201 return TRUE;
202 }
203
204 BOOLEAN IniAddSettingValueToSection(ULONG SectionId, PCSTR SettingName, PCSTR SettingValue)
205 {
206 PINI_SECTION Section = (PINI_SECTION)SectionId;
207 PINI_SECTION_ITEM SectionItem;
208
209 // Allocate a new item structure
210 SectionItem = MmAllocateMemory(sizeof(INI_SECTION_ITEM));
211 if (!SectionItem)
212 {
213 return FALSE;
214 }
215
216 RtlZeroMemory(SectionItem, sizeof(INI_SECTION_ITEM));
217
218 // Allocate the setting name buffer
219 SectionItem->ItemName = MmAllocateMemory(strlen(SettingName) + 1);
220 if (!SectionItem->ItemName)
221 {
222 MmFreeMemory(SectionItem);
223 return FALSE;
224 }
225
226 // Allocate the setting value buffer
227 SectionItem->ItemValue = MmAllocateMemory(strlen(SettingValue) + 1);
228 if (!SectionItem->ItemValue)
229 {
230 MmFreeMemory(SectionItem->ItemName);
231 MmFreeMemory(SectionItem);
232 return FALSE;
233 }
234
235 strcpy(SectionItem->ItemName, SettingName);
236 strcpy(SectionItem->ItemValue, SettingValue);
237
238 // Add it to the current section
239 Section->SectionItemCount++;
240 if (Section->SectionItemList == NULL)
241 {
242 Section->SectionItemList = SectionItem;
243 }
244 else
245 {
246 RtlListInsertTail((PLIST_ITEM)Section->SectionItemList, (PLIST_ITEM)SectionItem);
247 }
248
249 return TRUE;
250 }