2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: .inf file parser
4 * FILE: lib/inflib/infrosget.c
5 * PURPOSE: Read .inf routines for use in ReactOS
6 * PROGRAMMER: Royce Mitchell III
11 /* INCLUDES *****************************************************************/
20 InfpFindFirstLine(HINF InfHandle
,
26 PINFCACHESECTION CacheSection
;
27 PINFCACHELINE CacheLine
;
29 if (InfHandle
== NULL
|| Section
== NULL
|| Context
== NULL
)
31 DPRINT("Invalid parameter\n");
32 return INF_STATUS_INVALID_PARAMETER
;
35 *Context
= MALLOC(sizeof(INFCONTEXT
));
38 DPRINT1("MALLOC() failed\n");
39 return INF_STATUS_NO_MEMORY
;
42 Cache
= (PINFCACHE
)InfHandle
;
44 /* Iterate through list of sections */
45 CacheSection
= Cache
->FirstSection
;
46 while (CacheSection
!= NULL
)
48 DPRINT("Comparing '%S' and '%S'\n", CacheSection
->Name
, Section
);
50 /* Are the section names the same? */
51 if (_tcsicmp(CacheSection
->Name
, Section
) == 0)
55 CacheLine
= InfpCacheFindKeyLine (CacheSection
, (PTCHAR
)Key
);
59 CacheLine
= CacheSection
->FirstLine
;
62 if (CacheLine
== NULL
)
63 return INF_STATUS_NOT_FOUND
;
65 (*Context
)->Inf
= (PVOID
)Cache
;
66 (*Context
)->Section
= (PVOID
)CacheSection
;
67 (*Context
)->Line
= (PVOID
)CacheLine
;
69 return INF_STATUS_SUCCESS
;
72 /* Get the next section */
73 CacheSection
= CacheSection
->Next
;
76 DPRINT("Section not found\n");
78 return INF_STATUS_NOT_FOUND
;
83 InfpFindNextLine(PINFCONTEXT ContextIn
,
84 PINFCONTEXT ContextOut
)
86 PINFCACHELINE CacheLine
;
88 if (ContextIn
== NULL
|| ContextOut
== NULL
)
89 return INF_STATUS_INVALID_PARAMETER
;
91 if (ContextIn
->Line
== NULL
)
92 return INF_STATUS_INVALID_PARAMETER
;
94 CacheLine
= (PINFCACHELINE
)ContextIn
->Line
;
95 if (CacheLine
->Next
== NULL
)
96 return INF_STATUS_NOT_FOUND
;
98 if (ContextIn
!= ContextOut
)
100 ContextOut
->Inf
= ContextIn
->Inf
;
101 ContextOut
->Section
= ContextIn
->Section
;
103 ContextOut
->Line
= (PVOID
)(CacheLine
->Next
);
105 return INF_STATUS_SUCCESS
;
110 InfpFindFirstMatchLine(PINFCONTEXT ContextIn
,
112 PINFCONTEXT ContextOut
)
114 PINFCACHELINE CacheLine
;
116 if (ContextIn
== NULL
|| ContextOut
== NULL
|| Key
== NULL
|| *Key
== 0)
117 return INF_STATUS_INVALID_PARAMETER
;
119 if (ContextIn
->Inf
== NULL
|| ContextIn
->Section
== NULL
)
120 return INF_STATUS_INVALID_PARAMETER
;
122 CacheLine
= ((PINFCACHESECTION
)(ContextIn
->Section
))->FirstLine
;
123 while (CacheLine
!= NULL
)
125 if (CacheLine
->Key
!= NULL
&& _tcsicmp (CacheLine
->Key
, Key
) == 0)
128 if (ContextIn
!= ContextOut
)
130 ContextOut
->Inf
= ContextIn
->Inf
;
131 ContextOut
->Section
= ContextIn
->Section
;
133 ContextOut
->Line
= (PVOID
)CacheLine
;
135 return INF_STATUS_SUCCESS
;
138 CacheLine
= CacheLine
->Next
;
141 return INF_STATUS_NOT_FOUND
;
146 InfpFindNextMatchLine(PINFCONTEXT ContextIn
,
148 PINFCONTEXT ContextOut
)
150 PINFCACHELINE CacheLine
;
152 if (ContextIn
== NULL
|| ContextOut
== NULL
|| Key
== NULL
|| *Key
== 0)
153 return INF_STATUS_INVALID_PARAMETER
;
155 if (ContextIn
->Inf
== NULL
|| ContextIn
->Section
== NULL
|| ContextIn
->Line
== NULL
)
156 return INF_STATUS_INVALID_PARAMETER
;
158 CacheLine
= (PINFCACHELINE
)ContextIn
->Line
;
159 while (CacheLine
!= NULL
)
161 if (CacheLine
->Key
!= NULL
&& _tcsicmp (CacheLine
->Key
, Key
) == 0)
164 if (ContextIn
!= ContextOut
)
166 ContextOut
->Inf
= ContextIn
->Inf
;
167 ContextOut
->Section
= ContextIn
->Section
;
169 ContextOut
->Line
= (PVOID
)CacheLine
;
171 return INF_STATUS_SUCCESS
;
174 CacheLine
= CacheLine
->Next
;
177 return INF_STATUS_NOT_FOUND
;
182 InfpGetLineCount(HINF InfHandle
,
186 PINFCACHESECTION CacheSection
;
188 if (InfHandle
== NULL
|| Section
== NULL
)
190 DPRINT("Invalid parameter\n");
194 Cache
= (PINFCACHE
)InfHandle
;
196 /* Iterate through list of sections */
197 CacheSection
= Cache
->FirstSection
;
198 while (CacheSection
!= NULL
)
200 /* Are the section names the same? */
201 if (_tcsicmp(CacheSection
->Name
, Section
) == 0)
203 return CacheSection
->LineCount
;
206 /* Get the next section */
207 CacheSection
= CacheSection
->Next
;
210 DPRINT("Section not found\n");
216 /* InfpGetLineText */
220 InfpGetFieldCount(PINFCONTEXT Context
)
222 if (Context
== NULL
|| Context
->Line
== NULL
)
225 return ((PINFCACHELINE
)Context
->Line
)->FieldCount
;
230 InfpGetBinaryField(PINFCONTEXT Context
,
233 ULONG ReturnBufferSize
,
236 PINFCACHELINE CacheLine
;
237 PINFCACHEFIELD CacheField
;
242 if (Context
== NULL
|| Context
->Line
== NULL
|| FieldIndex
== 0)
244 DPRINT("Invalid parameter\n");
245 return INF_STATUS_INVALID_PARAMETER
;
248 if (RequiredSize
!= NULL
)
251 CacheLine
= (PINFCACHELINE
)Context
->Line
;
253 if (FieldIndex
> (ULONG
)CacheLine
->FieldCount
)
254 return INF_STATUS_NOT_FOUND
;
256 CacheField
= CacheLine
->FirstField
;
257 for (Index
= 1; Index
< FieldIndex
; Index
++)
258 CacheField
= CacheField
->Next
;
260 Size
= CacheLine
->FieldCount
- FieldIndex
+ 1;
262 if (RequiredSize
!= NULL
)
263 *RequiredSize
= Size
;
265 if (ReturnBuffer
!= NULL
)
267 if (ReturnBufferSize
< Size
)
268 return INF_STATUS_BUFFER_OVERFLOW
;
270 /* Copy binary data */
272 while (CacheField
!= NULL
)
274 *Ptr
= (UCHAR
)_tcstoul (CacheField
->Data
, NULL
, 16);
277 CacheField
= CacheField
->Next
;
281 return INF_STATUS_SUCCESS
;
286 InfpGetIntField(PINFCONTEXT Context
,
290 PINFCACHELINE CacheLine
;
291 PINFCACHEFIELD CacheField
;
295 if (Context
== NULL
|| Context
->Line
== NULL
|| IntegerValue
== NULL
)
297 DPRINT("Invalid parameter\n");
298 return INF_STATUS_INVALID_PARAMETER
;
301 CacheLine
= (PINFCACHELINE
)Context
->Line
;
303 if (FieldIndex
> (ULONG
)CacheLine
->FieldCount
)
305 DPRINT("Invalid parameter\n");
306 return INF_STATUS_INVALID_PARAMETER
;
311 Ptr
= CacheLine
->Key
;
315 CacheField
= CacheLine
->FirstField
;
316 for (Index
= 1; Index
< FieldIndex
; Index
++)
317 CacheField
= CacheField
->Next
;
319 Ptr
= CacheField
->Data
;
322 *IntegerValue
= _tcstol(Ptr
, NULL
, 0);
324 return INF_STATUS_SUCCESS
;
329 InfpGetMultiSzField(PINFCONTEXT Context
,
332 ULONG ReturnBufferSize
,
335 PINFCACHELINE CacheLine
;
336 PINFCACHEFIELD CacheField
;
337 PINFCACHEFIELD FieldPtr
;
342 if (Context
== NULL
|| Context
->Line
== NULL
|| FieldIndex
== 0)
344 DPRINT("Invalid parameter\n");
345 return INF_STATUS_INVALID_PARAMETER
;
348 if (RequiredSize
!= NULL
)
351 CacheLine
= (PINFCACHELINE
)Context
->Line
;
353 if (FieldIndex
> (ULONG
)CacheLine
->FieldCount
)
354 return INF_STATUS_INVALID_PARAMETER
;
356 CacheField
= CacheLine
->FirstField
;
357 for (Index
= 1; Index
< FieldIndex
; Index
++)
358 CacheField
= CacheField
->Next
;
360 /* Calculate the required buffer size */
361 FieldPtr
= CacheField
;
363 while (FieldPtr
!= NULL
)
365 Size
+= (_tcslen (FieldPtr
->Data
) + 1);
366 FieldPtr
= FieldPtr
->Next
;
370 if (RequiredSize
!= NULL
)
371 *RequiredSize
= Size
;
373 if (ReturnBuffer
!= NULL
)
375 if (ReturnBufferSize
< Size
)
376 return INF_STATUS_BUFFER_OVERFLOW
;
378 /* Copy multi-sz string */
380 FieldPtr
= CacheField
;
381 while (FieldPtr
!= NULL
)
383 Size
= _tcslen (FieldPtr
->Data
) + 1;
385 _tcscpy (Ptr
, FieldPtr
->Data
);
388 FieldPtr
= FieldPtr
->Next
;
393 return INF_STATUS_SUCCESS
;
398 InfpGetStringField(PINFCONTEXT Context
,
401 ULONG ReturnBufferSize
,
404 PINFCACHELINE CacheLine
;
405 PINFCACHEFIELD CacheField
;
410 if (Context
== NULL
|| Context
->Line
== NULL
|| FieldIndex
== 0)
412 DPRINT("Invalid parameter\n");
413 return INF_STATUS_INVALID_PARAMETER
;
416 if (RequiredSize
!= NULL
)
419 CacheLine
= (PINFCACHELINE
)Context
->Line
;
421 if (FieldIndex
> (ULONG
)CacheLine
->FieldCount
)
422 return INF_STATUS_INVALID_PARAMETER
;
426 Ptr
= CacheLine
->Key
;
430 CacheField
= CacheLine
->FirstField
;
431 for (Index
= 1; Index
< FieldIndex
; Index
++)
432 CacheField
= CacheField
->Next
;
434 Ptr
= CacheField
->Data
;
437 Size
= _tcslen (Ptr
) + 1;
439 if (RequiredSize
!= NULL
)
440 *RequiredSize
= Size
;
442 if (ReturnBuffer
!= NULL
)
444 if (ReturnBufferSize
< Size
)
445 return INF_STATUS_BUFFER_OVERFLOW
;
447 _tcscpy (ReturnBuffer
, Ptr
);
450 return INF_STATUS_SUCCESS
;
455 InfpGetData(PINFCONTEXT Context
,
459 PINFCACHELINE CacheKey
;
461 if (Context
== NULL
|| Context
->Line
== NULL
|| Data
== NULL
)
463 DPRINT("Invalid parameter\n");
464 return INF_STATUS_INVALID_PARAMETER
;
467 CacheKey
= (PINFCACHELINE
)Context
->Line
;
469 *Key
= CacheKey
->Key
;
473 if (CacheKey
->FirstField
== NULL
)
479 *Data
= CacheKey
->FirstField
->Data
;
483 return INF_STATUS_SUCCESS
;
488 InfpGetDataField(PINFCONTEXT Context
,
492 PINFCACHELINE CacheLine
;
493 PINFCACHEFIELD CacheField
;
496 if (Context
== NULL
|| Context
->Line
== NULL
|| Data
== NULL
)
498 DPRINT("Invalid parameter\n");
499 return INF_STATUS_INVALID_PARAMETER
;
502 CacheLine
= (PINFCACHELINE
)Context
->Line
;
504 if (FieldIndex
> (ULONG
)CacheLine
->FieldCount
)
505 return INF_STATUS_INVALID_PARAMETER
;
509 *Data
= CacheLine
->Key
;
513 CacheField
= CacheLine
->FirstField
;
514 for (Index
= 1; Index
< FieldIndex
; Index
++)
515 CacheField
= CacheField
->Next
;
517 *Data
= CacheField
->Data
;
520 return INF_STATUS_SUCCESS
;
524 InfpFreeContext(PINFCONTEXT Context
)