2 * PROJECT: .inf file parser
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PROGRAMMER: Royce Mitchell III
6 * Ge van Geldorp <gvg@reactos.org>
9 /* INCLUDES *****************************************************************/
18 InfpFindFirstLine(PINFCACHE Cache
,
23 PINFCACHESECTION CacheSection
;
24 PINFCACHELINE CacheLine
;
26 if (Cache
== NULL
|| Section
== NULL
|| Context
== NULL
)
28 DPRINT1("Invalid parameter\n");
29 return INF_STATUS_INVALID_PARAMETER
;
32 CacheSection
= InfpFindSection(Cache
, Section
);
33 if (NULL
== CacheSection
)
35 DPRINT("Section not found\n");
36 return INF_STATUS_NOT_FOUND
;
41 CacheLine
= InfpFindKeyLine(CacheSection
, Key
);
45 CacheLine
= CacheSection
->FirstLine
;
48 if (NULL
== CacheLine
)
50 DPRINT("Key not found\n");
51 return INF_STATUS_NOT_FOUND
;
54 *Context
= MALLOC(sizeof(INFCONTEXT
));
57 DPRINT1("MALLOC() failed\n");
58 return INF_STATUS_NO_MEMORY
;
60 (*Context
)->Inf
= (PVOID
)Cache
;
61 (*Context
)->Section
= (PVOID
)CacheSection
;
62 (*Context
)->Line
= (PVOID
)CacheLine
;
64 return INF_STATUS_SUCCESS
;
69 InfpFindNextLine(PINFCONTEXT ContextIn
,
70 PINFCONTEXT ContextOut
)
72 PINFCACHELINE CacheLine
;
74 if (ContextIn
== NULL
|| ContextOut
== NULL
)
75 return INF_STATUS_INVALID_PARAMETER
;
77 if (ContextIn
->Line
== NULL
)
78 return INF_STATUS_INVALID_PARAMETER
;
80 CacheLine
= (PINFCACHELINE
)ContextIn
->Line
;
81 if (CacheLine
->Next
== NULL
)
82 return INF_STATUS_NOT_FOUND
;
84 if (ContextIn
!= ContextOut
)
86 ContextOut
->Inf
= ContextIn
->Inf
;
87 ContextOut
->Section
= ContextIn
->Section
;
89 ContextOut
->Line
= (PVOID
)(CacheLine
->Next
);
91 return INF_STATUS_SUCCESS
;
96 InfpFindFirstMatchLine(PINFCONTEXT ContextIn
,
98 PINFCONTEXT ContextOut
)
100 PINFCACHELINE CacheLine
;
102 if (ContextIn
== NULL
|| ContextOut
== NULL
|| Key
== NULL
|| *Key
== 0)
103 return INF_STATUS_INVALID_PARAMETER
;
105 if (ContextIn
->Inf
== NULL
|| ContextIn
->Section
== NULL
)
106 return INF_STATUS_INVALID_PARAMETER
;
108 CacheLine
= ((PINFCACHESECTION
)(ContextIn
->Section
))->FirstLine
;
109 while (CacheLine
!= NULL
)
111 if (CacheLine
->Key
!= NULL
&& _tcsicmp (CacheLine
->Key
, Key
) == 0)
114 if (ContextIn
!= ContextOut
)
116 ContextOut
->Inf
= ContextIn
->Inf
;
117 ContextOut
->Section
= ContextIn
->Section
;
119 ContextOut
->Line
= (PVOID
)CacheLine
;
121 return INF_STATUS_SUCCESS
;
124 CacheLine
= CacheLine
->Next
;
127 return INF_STATUS_NOT_FOUND
;
132 InfpFindNextMatchLine(PINFCONTEXT ContextIn
,
134 PINFCONTEXT ContextOut
)
136 PINFCACHELINE CacheLine
;
138 if (ContextIn
== NULL
|| ContextOut
== NULL
|| Key
== NULL
|| *Key
== 0)
139 return INF_STATUS_INVALID_PARAMETER
;
141 if (ContextIn
->Inf
== NULL
|| ContextIn
->Section
== NULL
|| ContextIn
->Line
== NULL
)
142 return INF_STATUS_INVALID_PARAMETER
;
144 CacheLine
= (PINFCACHELINE
)ContextIn
->Line
;
145 while (CacheLine
!= NULL
)
147 if (CacheLine
->Key
!= NULL
&& _tcsicmp (CacheLine
->Key
, Key
) == 0)
150 if (ContextIn
!= ContextOut
)
152 ContextOut
->Inf
= ContextIn
->Inf
;
153 ContextOut
->Section
= ContextIn
->Section
;
155 ContextOut
->Line
= (PVOID
)CacheLine
;
157 return INF_STATUS_SUCCESS
;
160 CacheLine
= CacheLine
->Next
;
163 return INF_STATUS_NOT_FOUND
;
168 InfpGetLineCount(HINF InfHandle
,
172 PINFCACHESECTION CacheSection
;
174 if (InfHandle
== NULL
|| Section
== NULL
)
176 DPRINT("Invalid parameter\n");
180 Cache
= (PINFCACHE
)InfHandle
;
182 /* Iterate through list of sections */
183 CacheSection
= Cache
->FirstSection
;
184 while (CacheSection
!= NULL
)
186 /* Are the section names the same? */
187 if (_tcsicmp(CacheSection
->Name
, Section
) == 0)
189 return CacheSection
->LineCount
;
192 /* Get the next section */
193 CacheSection
= CacheSection
->Next
;
196 DPRINT("Section not found\n");
202 /* InfpGetLineText */
206 InfpGetFieldCount(PINFCONTEXT Context
)
208 if (Context
== NULL
|| Context
->Line
== NULL
)
211 return ((PINFCACHELINE
)Context
->Line
)->FieldCount
;
216 InfpGetBinaryField(PINFCONTEXT Context
,
219 ULONG ReturnBufferSize
,
222 PINFCACHELINE CacheLine
;
223 PINFCACHEFIELD CacheField
;
228 if (Context
== NULL
|| Context
->Line
== NULL
|| FieldIndex
== 0)
230 DPRINT("Invalid parameter\n");
231 return INF_STATUS_INVALID_PARAMETER
;
234 if (RequiredSize
!= NULL
)
237 CacheLine
= (PINFCACHELINE
)Context
->Line
;
239 if (FieldIndex
> (ULONG
)CacheLine
->FieldCount
)
240 return INF_STATUS_NOT_FOUND
;
242 CacheField
= CacheLine
->FirstField
;
243 for (Index
= 1; Index
< FieldIndex
; Index
++)
244 CacheField
= CacheField
->Next
;
246 Size
= CacheLine
->FieldCount
- FieldIndex
+ 1;
248 if (RequiredSize
!= NULL
)
249 *RequiredSize
= Size
;
251 if (ReturnBuffer
!= NULL
)
253 if (ReturnBufferSize
< Size
)
254 return INF_STATUS_BUFFER_OVERFLOW
;
256 /* Copy binary data */
258 while (CacheField
!= NULL
)
260 *Ptr
= (UCHAR
)_tcstoul (CacheField
->Data
, NULL
, 16);
263 CacheField
= CacheField
->Next
;
267 return INF_STATUS_SUCCESS
;
272 InfpGetIntField(PINFCONTEXT Context
,
276 PINFCACHELINE CacheLine
;
277 PINFCACHEFIELD CacheField
;
281 if (Context
== NULL
|| Context
->Line
== NULL
|| IntegerValue
== NULL
)
283 DPRINT("Invalid parameter\n");
284 return INF_STATUS_INVALID_PARAMETER
;
287 CacheLine
= (PINFCACHELINE
)Context
->Line
;
289 if (FieldIndex
> (ULONG
)CacheLine
->FieldCount
)
291 DPRINT("Invalid parameter\n");
292 return INF_STATUS_INVALID_PARAMETER
;
297 Ptr
= CacheLine
->Key
;
301 CacheField
= CacheLine
->FirstField
;
302 for (Index
= 1; Index
< FieldIndex
; Index
++)
303 CacheField
= CacheField
->Next
;
305 Ptr
= CacheField
->Data
;
308 *IntegerValue
= _tcstol(Ptr
, NULL
, 0);
310 return INF_STATUS_SUCCESS
;
315 InfpGetMultiSzField(PINFCONTEXT Context
,
318 ULONG ReturnBufferSize
,
321 PINFCACHELINE CacheLine
;
322 PINFCACHEFIELD CacheField
;
323 PINFCACHEFIELD FieldPtr
;
328 if (Context
== NULL
|| Context
->Line
== NULL
|| FieldIndex
== 0)
330 DPRINT("Invalid parameter\n");
331 return INF_STATUS_INVALID_PARAMETER
;
334 if (RequiredSize
!= NULL
)
337 CacheLine
= (PINFCACHELINE
)Context
->Line
;
339 if (FieldIndex
> (ULONG
)CacheLine
->FieldCount
)
340 return INF_STATUS_INVALID_PARAMETER
;
342 CacheField
= CacheLine
->FirstField
;
343 for (Index
= 1; Index
< FieldIndex
; Index
++)
344 CacheField
= CacheField
->Next
;
346 /* Calculate the required buffer size */
347 FieldPtr
= CacheField
;
349 while (FieldPtr
!= NULL
)
351 Size
+= (_tcslen (FieldPtr
->Data
) + 1);
352 FieldPtr
= FieldPtr
->Next
;
356 if (RequiredSize
!= NULL
)
357 *RequiredSize
= Size
;
359 if (ReturnBuffer
!= NULL
)
361 if (ReturnBufferSize
< Size
)
362 return INF_STATUS_BUFFER_OVERFLOW
;
364 /* Copy multi-sz string */
366 FieldPtr
= CacheField
;
367 while (FieldPtr
!= NULL
)
369 Size
= _tcslen (FieldPtr
->Data
) + 1;
371 _tcscpy (Ptr
, FieldPtr
->Data
);
374 FieldPtr
= FieldPtr
->Next
;
379 return INF_STATUS_SUCCESS
;
384 InfpGetStringField(PINFCONTEXT Context
,
387 ULONG ReturnBufferSize
,
390 PINFCACHELINE CacheLine
;
391 PINFCACHEFIELD CacheField
;
396 if (Context
== NULL
|| Context
->Line
== NULL
|| FieldIndex
== 0)
398 DPRINT("Invalid parameter\n");
399 return INF_STATUS_INVALID_PARAMETER
;
402 if (RequiredSize
!= NULL
)
405 CacheLine
= (PINFCACHELINE
)Context
->Line
;
407 if (FieldIndex
> (ULONG
)CacheLine
->FieldCount
)
408 return INF_STATUS_INVALID_PARAMETER
;
412 Ptr
= CacheLine
->Key
;
416 CacheField
= CacheLine
->FirstField
;
417 for (Index
= 1; Index
< FieldIndex
; Index
++)
418 CacheField
= CacheField
->Next
;
420 Ptr
= CacheField
->Data
;
423 Size
= _tcslen (Ptr
) + 1;
425 if (RequiredSize
!= NULL
)
426 *RequiredSize
= Size
;
428 if (ReturnBuffer
!= NULL
)
430 if (ReturnBufferSize
< Size
)
431 return INF_STATUS_BUFFER_OVERFLOW
;
433 _tcscpy (ReturnBuffer
, Ptr
);
436 return INF_STATUS_SUCCESS
;
441 InfpGetData(PINFCONTEXT Context
,
445 PINFCACHELINE CacheKey
;
447 if (Context
== NULL
|| Context
->Line
== NULL
|| Data
== NULL
)
449 DPRINT("Invalid parameter\n");
450 return INF_STATUS_INVALID_PARAMETER
;
453 CacheKey
= (PINFCACHELINE
)Context
->Line
;
455 *Key
= CacheKey
->Key
;
459 if (CacheKey
->FirstField
== NULL
)
465 *Data
= CacheKey
->FirstField
->Data
;
469 return INF_STATUS_SUCCESS
;
474 InfpGetDataField(PINFCONTEXT Context
,
478 PINFCACHELINE CacheLine
;
479 PINFCACHEFIELD CacheField
;
482 if (Context
== NULL
|| Context
->Line
== NULL
|| Data
== NULL
)
484 DPRINT("Invalid parameter\n");
485 return INF_STATUS_INVALID_PARAMETER
;
488 CacheLine
= (PINFCACHELINE
)Context
->Line
;
490 if (FieldIndex
> (ULONG
)CacheLine
->FieldCount
)
491 return INF_STATUS_INVALID_PARAMETER
;
495 *Data
= CacheLine
->Key
;
499 CacheField
= CacheLine
->FirstField
;
500 for (Index
= 1; Index
< FieldIndex
; Index
++)
501 CacheField
= CacheField
->Next
;
503 *Data
= CacheField
->Data
;
506 return INF_STATUS_SUCCESS
;
510 InfpFreeContext(PINFCONTEXT Context
)