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.
25 BOOLEAN
IniOpenSection(PCSTR SectionName
, ULONG
* SectionId
)
29 DbgPrint((DPRINT_INIFILE
, "IniOpenSection() SectionName = %s\n", SectionName
));
31 if (!IniFileSectionListHead
)
34 // Loop through each section and find the one they want
35 Section
= (PINI_SECTION
)RtlListGetHead((PLIST_ITEM
)IniFileSectionListHead
);
36 while (Section
!= NULL
)
38 // Compare against the section name
39 if (_stricmp(SectionName
, Section
->SectionName
) == 0)
42 *SectionId
= (ULONG
)Section
;
43 DbgPrint((DPRINT_INIFILE
, "IniOpenSection() Found it! SectionId = 0x%x\n", SectionId
));
47 // Get the next section in the list
48 Section
= (PINI_SECTION
)RtlListGetNext((PLIST_ITEM
)Section
);
51 DbgPrint((DPRINT_INIFILE
, "IniOpenSection() Section not found.\n"));
56 ULONG
IniGetNumSectionItems(ULONG SectionId
)
58 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
60 DbgPrint((DPRINT_INIFILE
, "IniGetNumSectionItems() SectionId = 0x%x\n", SectionId
));
61 DbgPrint((DPRINT_INIFILE
, "IniGetNumSectionItems() Item count = %d\n", Section
->SectionItemCount
));
63 return Section
->SectionItemCount
;
66 ULONG
IniGetSectionSettingNameSize(ULONG SectionId
, ULONG SettingIndex
)
68 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
70 // Return the size of the string plus 1 for the null-terminator
71 return (strlen(Section
->SectionItemList
[SettingIndex
].ItemName
) + 1);
74 ULONG
IniGetSectionSettingValueSize(ULONG SectionId
, ULONG SettingIndex
)
76 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
78 // Return the size of the string plus 1 for the null-terminator
79 return (strlen(Section
->SectionItemList
[SettingIndex
].ItemValue
) + 1);
82 BOOLEAN
IniReadSettingByNumber(ULONG SectionId
, ULONG SettingNumber
, PCHAR SettingName
, ULONG NameSize
, PCHAR SettingValue
, ULONG ValueSize
)
84 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
85 PINI_SECTION_ITEM SectionItem
;
87 ULONG RealSettingNumber
= SettingNumber
;
89 DbgPrint((DPRINT_INIFILE
, ".001 NameSize = %d ValueSize = %d\n", NameSize
, ValueSize
));
91 DbgPrint((DPRINT_INIFILE
, "IniReadSettingByNumber() SectionId = 0x%x\n", SectionId
));
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
)
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)
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
));
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
);
123 // Get the next section item in the list
124 SectionItem
= (PINI_SECTION_ITEM
)RtlListGetNext((PLIST_ITEM
)SectionItem
);
127 DbgPrint((DPRINT_INIFILE
, "IniReadSettingByNumber() Setting number %d not found.\n", RealSettingNumber
));
132 BOOLEAN
IniReadSettingByName(ULONG SectionId
, PCSTR SettingName
, PCHAR Buffer
, ULONG BufferSize
)
134 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
135 PINI_SECTION_ITEM SectionItem
;
137 DbgPrint((DPRINT_INIFILE
, "IniReadSettingByName() SectionId = 0x%x\n", SectionId
));
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
)
143 // Check to see if this is the setting they want
144 if (_stricmp(SettingName
, SectionItem
->ItemName
) == 0)
146 DbgPrint((DPRINT_INIFILE
, "IniReadSettingByName() Setting \'%s\' found.\n", SettingName
));
147 DbgPrint((DPRINT_INIFILE
, "IniReadSettingByName() Setting value = %s\n", SectionItem
->ItemValue
));
149 RtlZeroMemory(Buffer
, BufferSize
);
150 strncpy(Buffer
, SectionItem
->ItemValue
, BufferSize
);
155 // Get the next section item in the list
156 SectionItem
= (PINI_SECTION_ITEM
)RtlListGetNext((PLIST_ITEM
)SectionItem
);
159 DbgPrint((DPRINT_INIFILE
, "IniReadSettingByName() Setting \'%s\' not found.\n", SettingName
));
164 BOOLEAN
IniAddSection(PCSTR SectionName
, ULONG
* SectionId
)
166 PINI_SECTION Section
;
168 // Allocate a new section structure
169 Section
= MmAllocateMemory(sizeof(INI_SECTION
));
175 RtlZeroMemory(Section
, sizeof(INI_SECTION
));
177 // Allocate the section name buffer
178 Section
->SectionName
= MmAllocateMemory(strlen(SectionName
));
179 if (!Section
->SectionName
)
181 MmFreeMemory(Section
);
185 // Get the section name
186 strcpy(Section
->SectionName
, SectionName
);
188 // Add it to the section list head
189 IniFileSectionCount
++;
190 if (IniFileSectionListHead
== NULL
)
192 IniFileSectionListHead
= Section
;
196 RtlListInsertTail((PLIST_ITEM
)IniFileSectionListHead
, (PLIST_ITEM
)Section
);
199 *SectionId
= (ULONG
)Section
;
204 BOOLEAN
IniAddSettingValueToSection(ULONG SectionId
, PCSTR SettingName
, PCSTR SettingValue
)
206 PINI_SECTION Section
= (PINI_SECTION
)SectionId
;
207 PINI_SECTION_ITEM SectionItem
;
209 // Allocate a new item structure
210 SectionItem
= MmAllocateMemory(sizeof(INI_SECTION_ITEM
));
216 RtlZeroMemory(SectionItem
, sizeof(INI_SECTION_ITEM
));
218 // Allocate the setting name buffer
219 SectionItem
->ItemName
= MmAllocateMemory(strlen(SettingName
) + 1);
220 if (!SectionItem
->ItemName
)
222 MmFreeMemory(SectionItem
);
226 // Allocate the setting value buffer
227 SectionItem
->ItemValue
= MmAllocateMemory(strlen(SettingValue
) + 1);
228 if (!SectionItem
->ItemValue
)
230 MmFreeMemory(SectionItem
->ItemName
);
231 MmFreeMemory(SectionItem
);
235 strcpy(SectionItem
->ItemName
, SettingName
);
236 strcpy(SectionItem
->ItemValue
, SettingValue
);
238 // Add it to the current section
239 Section
->SectionItemCount
++;
240 if (Section
->SectionItemList
== NULL
)
242 Section
->SectionItemList
= SectionItem
;
246 RtlListInsertTail((PLIST_ITEM
)Section
->SectionItemList
, (PLIST_ITEM
)SectionItem
);