Fixed a bug in the LBA extensions detection code.
[reactos.git] / freeldr / freeldr / inifile / parse.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 Brian Palmer <brianp@sginet.com>
4 *
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.
9 *
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.
14 *
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.
18 */
19
20 #include <freeldr.h>
21 #include "ini.h"
22 #include <ui.h>
23 #include <fs.h>
24 #include <rtl.h>
25 #include <mm.h>
26 #include <debug.h>
27
28
29 VOID IniParseFile(PUCHAR IniFileData, ULONG IniFileSize)
30 {
31 }
32
33 ULONG IniGetNextLineSize(PUCHAR IniFileData, ULONG IniFileSize, ULONG CurrentOffset)
34 {
35 ULONG Idx;
36 ULONG LineCharCount = 0;
37
38 // Loop through grabbing chars until we hit the end of the
39 // file or we encounter a new line char
40 for (Idx=0; (CurrentOffset < IniFileSize); CurrentOffset++)
41 {
42 // Increment the line character count
43 LineCharCount++;
44
45 // Check for new line char
46 if (IniFileData[CurrentOffset] == '\n')
47 {
48 CurrentOffset++;
49 break;
50 }
51 }
52
53 // Send back line character count
54 return LineCharCount;
55 }
56
57 ULONG IniGetNextLine(PUCHAR IniFileData, ULONG IniFileSize, PUCHAR Buffer, ULONG BufferSize, ULONG CurrentOffset)
58 {
59 ULONG Idx;
60
61 // Loop through grabbing chars until we hit the end of the
62 // file or we encounter a new line char
63 for (Idx=0; (CurrentOffset < IniFileSize); CurrentOffset++)
64 {
65 // If we haven't exceeded our buffer size yet
66 // then store another char
67 if (Idx < (BufferSize - 1))
68 {
69 Buffer[Idx++] = IniFileData[CurrentOffset];
70 }
71
72 // Check for new line char
73 if (IniFileData[CurrentOffset] == '\n')
74 {
75 CurrentOffset++;
76 break;
77 }
78 }
79
80 // Terminate the string
81 Buffer[Idx] = '\0';
82
83 // Get rid of newline & linefeed characters (if any)
84 if((Buffer[strlen(Buffer)-1] == '\n') || (Buffer[strlen(Buffer)-1] == '\r'))
85 Buffer[strlen(Buffer)-1] = '\0';
86 if((Buffer[strlen(Buffer)-1] == '\n') || (Buffer[strlen(Buffer)-1] == '\r'))
87 Buffer[strlen(Buffer)-1] = '\0';
88
89 // Send back new offset
90 return CurrentOffset;
91 }
92
93 BOOL IniIsLineEmpty(PUCHAR LineOfText, ULONG TextLength)
94 {
95 ULONG Idx;
96
97 // Check the first character (skipping whitespace)
98 // and make sure that it is an opening bracket
99 for (Idx=0; Idx<TextLength; Idx++)
100 {
101 if ((LineOfText[Idx] == ' ') ||
102 (LineOfText[Idx] == '\t') ||
103 (LineOfText[Idx] == '\n') ||
104 (LineOfText[Idx] == '\r'))
105 {
106 continue;
107 }
108 else
109 {
110 return FALSE;
111 }
112 }
113
114 return TRUE;
115 }
116
117 BOOL IniIsCommentLine(PUCHAR LineOfText, ULONG TextLength)
118 {
119 ULONG Idx;
120
121 // Check the first character (skipping whitespace)
122 // and make sure that it is an opening bracket
123 for (Idx=0; Idx<TextLength; Idx++)
124 {
125 if ((LineOfText[Idx] == ' ') ||
126 (LineOfText[Idx] == '\t'))
127 {
128 continue;
129 }
130 else if (LineOfText[Idx] == INI_FILE_COMMENT_CHAR)
131 {
132 return TRUE;
133 }
134 else
135 {
136 break;
137 }
138 }
139
140 return FALSE;
141 }
142
143 BOOL IniIsSectionName(PUCHAR LineOfText, ULONG TextLength)
144 {
145 ULONG Idx;
146
147 // Check the first character (skipping whitespace)
148 // and make sure that it is an opening bracket
149 for (Idx=0; Idx<TextLength; Idx++)
150 {
151 if ((LineOfText[Idx] == ' ') ||
152 (LineOfText[Idx] == '\t'))
153 {
154 continue;
155 }
156 else if (LineOfText[Idx] == '[')
157 {
158 return TRUE;
159 }
160 else
161 {
162 break;
163 }
164 }
165
166 return FALSE;
167 }
168
169 ULONG IniGetSectionNameSize(PUCHAR SectionNameLine, ULONG LineLength)
170 {
171 ULONG Idx;
172 ULONG NameSize;
173
174 // Find the opening bracket (skipping whitespace)
175 for (Idx=0; Idx<LineLength; Idx++)
176 {
177 if ((SectionNameLine[Idx] == ' ') ||
178 (SectionNameLine[Idx] == '\t'))
179 {
180 continue;
181 }
182 else //if (SectionNameLine[Idx] == '[')
183 {
184 break;
185 }
186 }
187
188 // Skip past the opening bracket
189 Idx++;
190
191 // Count the characters up until the closing bracket of EOL
192 for (NameSize=0; Idx<LineLength; Idx++)
193 {
194 if ((SectionNameLine[Idx] == ']') ||
195 (SectionNameLine[Idx] == '\0'))
196 {
197 break;
198 }
199
200 // Increment the count
201 NameSize++;
202 }
203
204 return NameSize;
205 }
206
207 VOID IniExtractSectionName(PUCHAR SectionName, PUCHAR SectionNameLine, ULONG LineLength)
208 {
209 ULONG Idx;
210 ULONG DestIdx;
211
212 // Find the opening bracket (skipping whitespace)
213 for (Idx=0; Idx<LineLength; Idx++)
214 {
215 if ((SectionNameLine[Idx] == ' ') ||
216 (SectionNameLine[Idx] == '\t'))
217 {
218 continue;
219 }
220 else //if (SectionNameLine[Idx] == '[')
221 {
222 break;
223 }
224 }
225
226 // Skip past the opening bracket
227 Idx++;
228
229 // Count the characters up until the closing bracket of EOL
230 for (DestIdx=0; Idx<LineLength; Idx++)
231 {
232 if ((SectionNameLine[Idx] == ']') ||
233 (SectionNameLine[Idx] == '\0'))
234 {
235 break;
236 }
237
238 // Grab a character and increment DestIdx
239 SectionName[DestIdx] = SectionNameLine[Idx];
240 DestIdx++;
241 }
242
243 }