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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 BOOL
IniOpenSection(PCSTR SectionName
, ULONG
* SectionId
)
31 DbgPrint((DPRINT_INIFILE
, "IniOpenSection() SectionName = %s\n", SectionName
));
33 if (!IniFileSectionListHead
)
36 // Loop through each section and find the one they want
37 Section
= (PINI_SECTION
)RtlListGetHead((PLIST_ITEM
)IniFileSectionListHead
);
38 while (Section
!= NULL
)
40 // Compare against the section name
41 if (stricmp(SectionName
, Section
->SectionName
) == 0)
44 *SectionId
= (ULONG
)Section
;
45 DbgPrint((DPRINT_INIFILE
, "IniOpenSection() Found it! SectionId = 0x%x\n", SectionId
));
49 // Get the next section in the list
50 Section
= (PINI_SECTION
)RtlListGetNext((PLIST_ITEM
)Section
);
53 DbgPrint((DPRINT_INIFILE
, "IniOpenSection() Section not found.\n"));
58 ULONG
IniGetNumSectionItems(ULONG SectionId
)
60 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
62 DbgPrint((DPRINT_INIFILE
, "IniGetNumSectionItems() SectionId = 0x%x\n", SectionId
));
63 DbgPrint((DPRINT_INIFILE
, "IniGetNumSectionItems() Item count = %d\n", Section
->SectionItemCount
));
65 return Section
->SectionItemCount
;
68 ULONG
IniGetSectionSettingNameSize(ULONG SectionId
, ULONG SettingIndex
)
70 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
72 // Return the size of the string plus 1 for the null-terminator
73 return (strlen(Section
->SectionItemList
[SettingIndex
].ItemName
) + 1);
76 ULONG
IniGetSectionSettingValueSize(ULONG SectionId
, ULONG SettingIndex
)
78 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
80 // Return the size of the string plus 1 for the null-terminator
81 return (strlen(Section
->SectionItemList
[SettingIndex
].ItemValue
) + 1);
84 BOOL
IniReadSettingByNumber(ULONG SectionId
, ULONG SettingNumber
, PCHAR SettingName
, ULONG NameSize
, PCHAR SettingValue
, ULONG ValueSize
)
86 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
87 PINI_SECTION_ITEM SectionItem
;
89 ULONG RealSettingNumber
= SettingNumber
;
91 DbgPrint((DPRINT_INIFILE
, ".001 NameSize = %d ValueSize = %d\n", NameSize
, ValueSize
));
93 DbgPrint((DPRINT_INIFILE
, "IniReadSettingByNumber() SectionId = 0x%x\n", SectionId
));
95 // Loop through each section item and find the one they want
96 DbgPrint((DPRINT_INIFILE
, ".01 NameSize = %d ValueSize = %d\n", NameSize
, ValueSize
));
97 SectionItem
= (PINI_SECTION_ITEM
)RtlListGetHead((PLIST_ITEM
)Section
->SectionItemList
);
98 while (SectionItem
!= NULL
)
100 DbgPrint((DPRINT_INIFILE
, ".1 NameSize = %d ValueSize = %d\n", NameSize
, ValueSize
));
101 // Check to see if this is the setting they want
102 if (SettingNumber
== 0)
104 DbgPrint((DPRINT_INIFILE
, "IniReadSettingByNumber() Setting number %d found.\n", RealSettingNumber
));
105 DbgPrint((DPRINT_INIFILE
, "IniReadSettingByNumber() Setting name = %s\n", SectionItem
->ItemName
));
106 DbgPrint((DPRINT_INIFILE
, "IniReadSettingByNumber() Setting value = %s\n", SectionItem
->ItemValue
));
108 DbgPrint((DPRINT_INIFILE
, "1 NameSize = %d ValueSize = %d\n", NameSize
, ValueSize
));
109 RtlZeroMemory(SettingName
, NameSize
);
110 RtlZeroMemory(SettingValue
, ValueSize
);
111 DbgPrint((DPRINT_INIFILE
, "2 NameSize = %d ValueSize = %d\n", NameSize
, ValueSize
));
112 strncpy(SettingName
, SectionItem
->ItemName
, NameSize
);
113 DbgPrint((DPRINT_INIFILE
, "3 NameSize = %d ValueSize = %d\n", NameSize
, ValueSize
));
114 strncpy(SettingValue
, SectionItem
->ItemValue
, ValueSize
);
115 DbgPrint((DPRINT_INIFILE
, "4 NameSize = %d ValueSize = %d\n", NameSize
, ValueSize
));
116 DbgDumpBuffer(DPRINT_INIFILE
, SettingName
, NameSize
);
117 DbgDumpBuffer(DPRINT_INIFILE
, SettingValue
, ValueSize
);
125 // Get the next section item in the list
126 SectionItem
= (PINI_SECTION_ITEM
)RtlListGetNext((PLIST_ITEM
)SectionItem
);
129 DbgPrint((DPRINT_INIFILE
, "IniReadSettingByNumber() Setting number %d not found.\n", RealSettingNumber
));
134 BOOL
IniReadSettingByName(ULONG SectionId
, PCSTR SettingName
, PCHAR Buffer
, ULONG BufferSize
)
136 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
137 PINI_SECTION_ITEM SectionItem
;
139 DbgPrint((DPRINT_INIFILE
, "IniReadSettingByName() SectionId = 0x%x\n", SectionId
));
141 // Loop through each section item and find the one they want
142 SectionItem
= (PINI_SECTION_ITEM
)RtlListGetHead((PLIST_ITEM
)Section
->SectionItemList
);
143 while (SectionItem
!= NULL
)
145 // Check to see if this is the setting they want
146 if (stricmp(SettingName
, SectionItem
->ItemName
) == 0)
148 DbgPrint((DPRINT_INIFILE
, "IniReadSettingByName() Setting \'%s\' found.\n", SettingName
));
149 DbgPrint((DPRINT_INIFILE
, "IniReadSettingByName() Setting value = %s\n", SectionItem
->ItemValue
));
151 RtlZeroMemory(Buffer
, BufferSize
);
152 strncpy(Buffer
, SectionItem
->ItemValue
, BufferSize
);
157 // Get the next section item in the list
158 SectionItem
= (PINI_SECTION_ITEM
)RtlListGetNext((PLIST_ITEM
)SectionItem
);
161 DbgPrint((DPRINT_INIFILE
, "IniReadSettingByName() Setting \'%s\' not found.\n", SettingName
));
166 BOOL
IniAddSection(PCSTR SectionName
, ULONG
* SectionId
)
168 PINI_SECTION Section
;
170 // Allocate a new section structure
171 Section
= MmAllocateMemory(sizeof(INI_SECTION
));
177 RtlZeroMemory(Section
, sizeof(INI_SECTION
));
179 // Allocate the section name buffer
180 Section
->SectionName
= MmAllocateMemory(strlen(SectionName
));
181 if (!Section
->SectionName
)
183 MmFreeMemory(Section
);
187 // Get the section name
188 strcpy(Section
->SectionName
, SectionName
);
190 // Add it to the section list head
191 IniFileSectionCount
++;
192 if (IniFileSectionListHead
== NULL
)
194 IniFileSectionListHead
= Section
;
198 RtlListInsertTail((PLIST_ITEM
)IniFileSectionListHead
, (PLIST_ITEM
)Section
);
201 *SectionId
= (ULONG
)Section
;
206 BOOL
IniAddSettingValueToSection(ULONG SectionId
, PCSTR SettingName
, PCSTR SettingValue
)
208 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
209 PINI_SECTION_ITEM SectionItem
;
211 // Allocate a new item structure
212 SectionItem
= MmAllocateMemory(sizeof(INI_SECTION_ITEM
));
218 RtlZeroMemory(SectionItem
, sizeof(INI_SECTION_ITEM
));
220 // Allocate the setting name buffer
221 SectionItem
->ItemName
= MmAllocateMemory(strlen(SettingName
) + 1);
222 if (!SectionItem
->ItemName
)
224 MmFreeMemory(SectionItem
);
228 // Allocate the setting value buffer
229 SectionItem
->ItemValue
= MmAllocateMemory(strlen(SettingValue
) + 1);
230 if (!SectionItem
->ItemValue
)
232 MmFreeMemory(SectionItem
->ItemName
);
233 MmFreeMemory(SectionItem
);
237 strcpy(SectionItem
->ItemName
, SettingName
);
238 strcpy(SectionItem
->ItemValue
, SettingValue
);
240 // Add it to the current section
241 Section
->SectionItemCount
++;
242 if (Section
->SectionItemList
== NULL
)
244 Section
->SectionItemList
= SectionItem
;
248 RtlListInsertTail((PLIST_ITEM
)Section
->SectionItemList
, (PLIST_ITEM
)SectionItem
);