3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
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.
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.
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.
23 DBG_DEFAULT_CHANNEL(INIFILE
);
25 BOOLEAN
IniOpenSection(PCSTR SectionName
, ULONG_PTR
* SectionId
)
29 TRACE("IniOpenSection() SectionName = %s\n", SectionName
);
31 // Loop through each section and find the one they want
32 Section
= CONTAINING_RECORD(IniFileSectionListHead
.Flink
, INI_SECTION
, ListEntry
);
33 while (&Section
->ListEntry
!= &IniFileSectionListHead
)
35 // Compare against the section name
36 if (_stricmp(SectionName
, Section
->SectionName
) == 0)
40 *SectionId
= (ULONG_PTR
)Section
;
41 TRACE("IniOpenSection() Found it! SectionId = 0x%x\n", SectionId
);
45 // Get the next section in the list
46 Section
= CONTAINING_RECORD(Section
->ListEntry
.Flink
, INI_SECTION
, ListEntry
);
49 TRACE("IniOpenSection() Section not found.\n");
54 ULONG
IniGetNumSectionItems(ULONG_PTR SectionId
)
56 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
58 TRACE("IniGetNumSectionItems() SectionId = 0x%x\n", SectionId
);
59 TRACE("IniGetNumSectionItems() Item count = %d\n", Section
->SectionItemCount
);
61 return Section
->SectionItemCount
;
64 PINI_SECTION_ITEM
IniGetSettingByNumber(ULONG_PTR SectionId
, ULONG SettingNumber
)
66 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
67 PINI_SECTION_ITEM SectionItem
;
69 // Loop through each section item and find the one they want
70 SectionItem
= CONTAINING_RECORD(Section
->SectionItemList
.Flink
, INI_SECTION_ITEM
, ListEntry
);
71 while (&SectionItem
->ListEntry
!= &Section
->SectionItemList
)
73 // Check to see if this is the setting they want
74 if (SettingNumber
== 0)
82 // Get the next section item in the list
83 SectionItem
= CONTAINING_RECORD(SectionItem
->ListEntry
.Flink
, INI_SECTION_ITEM
, ListEntry
);
88 ULONG
IniGetSectionSettingNameSize(ULONG_PTR SectionId
, ULONG SettingIndex
)
90 PINI_SECTION_ITEM SectionItem
;
92 // Retrieve requested setting
93 SectionItem
= IniGetSettingByNumber(SectionId
, SettingIndex
);
97 // Return the size of the string plus 1 for the null-terminator
98 return (ULONG
)(strlen(SectionItem
->ItemName
) + 1);
101 ULONG
IniGetSectionSettingValueSize(ULONG_PTR SectionId
, ULONG SettingIndex
)
103 PINI_SECTION_ITEM SectionItem
;
105 // Retrieve requested setting
106 SectionItem
= IniGetSettingByNumber(SectionId
, SettingIndex
);
110 // Return the size of the string plus 1 for the null-terminator
111 return (ULONG
)(strlen(SectionItem
->ItemValue
) + 1);
114 BOOLEAN
IniReadSettingByNumber(ULONG_PTR SectionId
, ULONG SettingNumber
, PCHAR SettingName
, ULONG NameSize
, PCHAR SettingValue
, ULONG ValueSize
)
116 PINI_SECTION_ITEM SectionItem
;
117 TRACE(".001 NameSize = %d ValueSize = %d\n", NameSize
, ValueSize
);
119 TRACE("IniReadSettingByNumber() SectionId = 0x%x\n", SectionId
);
121 // Retrieve requested setting
122 SectionItem
= IniGetSettingByNumber(SectionId
, SettingNumber
);
125 TRACE("IniReadSettingByNumber() Setting number %d not found.\n", SettingNumber
);
129 TRACE("IniReadSettingByNumber() Setting number %d found.\n", SettingNumber
);
130 TRACE("IniReadSettingByNumber() Setting name = %s\n", SectionItem
->ItemName
);
131 TRACE("IniReadSettingByNumber() Setting value = %s\n", SectionItem
->ItemValue
);
133 TRACE("1 NameSize = %d ValueSize = %d\n", NameSize
, ValueSize
);
134 TRACE("2 NameSize = %d ValueSize = %d\n", NameSize
, ValueSize
);
135 strncpy(SettingName
, SectionItem
->ItemName
, NameSize
- 1);
136 SettingName
[NameSize
- 1] = '\0';
137 TRACE("3 NameSize = %d ValueSize = %d\n", NameSize
, ValueSize
);
138 strncpy(SettingValue
, SectionItem
->ItemValue
, ValueSize
- 1);
139 SettingValue
[ValueSize
- 1] = '\0';
140 TRACE("4 NameSize = %d ValueSize = %d\n", NameSize
, ValueSize
);
141 DbgDumpBuffer(DPRINT_INIFILE
, SettingName
, NameSize
);
142 DbgDumpBuffer(DPRINT_INIFILE
, SettingValue
, ValueSize
);
147 BOOLEAN
IniReadSettingByName(ULONG_PTR SectionId
, PCSTR SettingName
, PCHAR Buffer
, ULONG BufferSize
)
149 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
150 PINI_SECTION_ITEM SectionItem
;
152 TRACE("IniReadSettingByName() SectionId = 0x%x\n", SectionId
);
154 // Loop through each section item and find the one they want
155 SectionItem
= CONTAINING_RECORD(Section
->SectionItemList
.Flink
, INI_SECTION_ITEM
, ListEntry
);
156 while (&SectionItem
->ListEntry
!= &Section
->SectionItemList
)
158 // Check to see if this is the setting they want
159 if (_stricmp(SettingName
, SectionItem
->ItemName
) == 0)
161 TRACE("IniReadSettingByName() Setting \'%s\' found.\n", SettingName
);
162 TRACE("IniReadSettingByName() Setting value = %s\n", SectionItem
->ItemValue
);
164 strncpy(Buffer
, SectionItem
->ItemValue
, BufferSize
- 1);
165 Buffer
[BufferSize
- 1] = '\0';
170 // Get the next section item in the list
171 SectionItem
= CONTAINING_RECORD(SectionItem
->ListEntry
.Flink
, INI_SECTION_ITEM
, ListEntry
);
174 WARN("IniReadSettingByName() Setting \'%s\' not found.\n", SettingName
);
179 BOOLEAN
IniAddSection(PCSTR SectionName
, ULONG_PTR
* SectionId
)
181 PINI_SECTION Section
;
183 // Allocate a new section structure
184 Section
= FrLdrTempAlloc(sizeof(INI_SECTION
), TAG_INI_SECTION
);
190 RtlZeroMemory(Section
, sizeof(INI_SECTION
));
192 // Allocate the section name buffer
193 Section
->SectionName
= FrLdrTempAlloc(strlen(SectionName
) + sizeof(CHAR
), TAG_INI_NAME
);
194 if (!Section
->SectionName
)
196 FrLdrTempFree(Section
, TAG_INI_SECTION
);
200 // Get the section name
201 strcpy(Section
->SectionName
, SectionName
);
203 // Add it to the section list head
204 IniFileSectionCount
++;
205 InsertHeadList(&IniFileSectionListHead
, &Section
->ListEntry
);
207 *SectionId
= (ULONG_PTR
)Section
;
212 VOID
IniFreeSection(PINI_SECTION Section
)
214 PLIST_ENTRY ListEntry
;
215 PINI_SECTION_ITEM SectionItem
;
217 // Loop while there are section items
218 while (!IsListEmpty(&Section
->SectionItemList
))
220 // Remove the section item
221 ListEntry
= RemoveHeadList(&Section
->SectionItemList
);
222 SectionItem
= CONTAINING_RECORD(ListEntry
, INI_SECTION_ITEM
, ListEntry
);
225 FrLdrTempFree(SectionItem
->ItemName
, TAG_INI_NAME
);
226 FrLdrTempFree(SectionItem
->ItemValue
, TAG_INI_VALUE
);
227 FrLdrTempFree(SectionItem
, TAG_INI_SECTION_ITEM
);
230 FrLdrTempFree(Section
->SectionName
, TAG_INI_NAME
);
231 FrLdrTempFree(Section
, TAG_INI_SECTION
);
234 VOID
IniCleanup(VOID
)
236 PLIST_ENTRY ListEntry
;
237 PINI_SECTION Section
;
239 // Loop while there are sections
240 while (!IsListEmpty(&IniFileSectionListHead
))
242 // Remove the section
243 ListEntry
= RemoveHeadList(&IniFileSectionListHead
);
244 Section
= CONTAINING_RECORD(ListEntry
, INI_SECTION
, ListEntry
);
247 IniFreeSection(Section
);
251 BOOLEAN
IniAddSettingValueToSection(ULONG_PTR SectionId
, PCSTR SettingName
, PCSTR SettingValue
)
253 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
254 PINI_SECTION_ITEM SectionItem
;
256 // Allocate a new item structure
257 SectionItem
= FrLdrTempAlloc(sizeof(INI_SECTION_ITEM
), TAG_INI_SECTION_ITEM
);
263 RtlZeroMemory(SectionItem
, sizeof(INI_SECTION_ITEM
));
265 // Allocate the setting name buffer
266 SectionItem
->ItemName
= FrLdrTempAlloc(strlen(SettingName
) + 1, TAG_INI_NAME
);
267 if (!SectionItem
->ItemName
)
269 FrLdrTempFree(SectionItem
, TAG_INI_SECTION_ITEM
);
273 // Allocate the setting value buffer
274 SectionItem
->ItemValue
= FrLdrTempAlloc(strlen(SettingValue
) + 1, TAG_INI_VALUE
);
275 if (!SectionItem
->ItemValue
)
277 FrLdrTempFree(SectionItem
->ItemName
, TAG_INI_NAME
);
278 FrLdrTempFree(SectionItem
, TAG_INI_SECTION_ITEM
);
282 strcpy(SectionItem
->ItemName
, SettingName
);
283 strcpy(SectionItem
->ItemValue
, SettingValue
);
285 // Add it to the current section
286 Section
->SectionItemCount
++;
287 InsertTailList(&Section
->SectionItemList
, &SectionItem
->ListEntry
);