Revert 45697:
[reactos.git] / lib / inflib / infrosput.c
1 /*
2 * PROJECT: .inf file parser
3 * LICENSE: GPL - See COPYING in the top level directory
4 * COPYRIGHT: Copyright 2005 Ge van Geldorp <gvg@reactos.org>
5 */
6
7 /* INCLUDES *****************************************************************/
8
9 #include "inflib.h"
10 #include "infros.h"
11
12 #define NDEBUG
13 #include <debug.h>
14
15 NTSTATUS
16 InfWriteFile(HINF InfHandle,
17 PUNICODE_STRING FileName,
18 PUNICODE_STRING HeaderComment)
19 {
20 OBJECT_ATTRIBUTES ObjectAttributes;
21 IO_STATUS_BLOCK IoStatusBlock;
22 HANDLE FileHandle;
23 NTSTATUS Status;
24 INFSTATUS InfStatus;
25 PCHAR Buffer;
26 ULONG BufferSize;
27 PCHAR HeaderBuffer;
28 ULONG HeaderBufferSize;
29 UINT Index;
30
31 InfStatus = InfpBuildFileBuffer((PINFCACHE) InfHandle, &Buffer, &BufferSize);
32 if (! INF_SUCCESS(InfStatus))
33 {
34 DPRINT("Failed to create buffer (Status 0x%lx)\n", InfStatus);
35 return InfStatus;
36 }
37
38 /* Open the inf file */
39 InitializeObjectAttributes(&ObjectAttributes,
40 FileName,
41 0,
42 NULL,
43 NULL);
44
45 Status = NtOpenFile(&FileHandle,
46 GENERIC_WRITE | SYNCHRONIZE,
47 &ObjectAttributes,
48 &IoStatusBlock,
49 0,
50 FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
51 if (!INF_SUCCESS(Status))
52 {
53 DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
54 FREE(Buffer);
55 return Status;
56 }
57
58 DPRINT("NtOpenFile() successful\n");
59
60 if (NULL != HeaderComment && 0 != HeaderComment->Length)
61 {
62 /* This is just a comment header, don't abort on errors here */
63 HeaderBufferSize = HeaderComment->Length / sizeof(WCHAR) + 7;
64 HeaderBuffer = MALLOC(HeaderBufferSize);
65 if (NULL != HeaderBuffer)
66 {
67 strcpy(HeaderBuffer, "; ");
68 for (Index = 0; Index < HeaderComment->Length / sizeof(WCHAR); Index++)
69 {
70 HeaderBuffer[2 + Index] = (CHAR) HeaderComment->Buffer[Index];
71 }
72 strcpy(HeaderBuffer + (2 + HeaderComment->Length / sizeof(WCHAR)),
73 "\r\n\r\n");
74 NtWriteFile(FileHandle,
75 NULL,
76 NULL,
77 NULL,
78 &IoStatusBlock,
79 HeaderBuffer,
80 HeaderBufferSize,
81 NULL,
82 NULL);
83 FREE(HeaderBuffer);
84 }
85 }
86
87 /* Write main contents */
88 Status = NtWriteFile(FileHandle,
89 NULL,
90 NULL,
91 NULL,
92 &IoStatusBlock,
93 Buffer,
94 BufferSize,
95 NULL,
96 NULL);
97
98 NtClose(FileHandle);
99 FREE(Buffer);
100
101 if (!INF_SUCCESS(Status))
102 {
103 DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
104 FREE(Buffer);
105 return(Status);
106 }
107
108 return STATUS_SUCCESS;
109 }
110
111 BOOLEAN
112 InfFindOrAddSection(HINF InfHandle,
113 PCWSTR Section,
114 PINFCONTEXT *Context)
115 {
116 return INF_SUCCESS(InfpFindOrAddSection((PINFCACHE) InfHandle,
117 Section, Context));
118 }
119
120 BOOLEAN
121 InfHostAddLine(PINFCONTEXT Context, PCWSTR Key)
122 {
123 return INF_SUCCESS(InfpAddLineWithKey(Context, Key));
124 }
125
126 BOOLEAN
127 InfHostAddField(PINFCONTEXT Context, PCWSTR Data)
128 {
129 return INF_SUCCESS(InfpAddField(Context, Data));
130 }
131
132 /* EOF */