Fixed warnings.
[reactos.git] / freeldr / freeldr / oslist.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1999, 2000, 2001 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 "parseini.h"
22 #include "oslist.h"
23 #include "stdlib.h"
24 #include "memory.h"
25 #include "tui.h"
26
27 BOOL InitOperatingSystemList(PUCHAR **SectionNamesPointer, PUCHAR **DisplayNamesPointer, PULONG OperatingSystemCountPointer)
28 {
29 ULONG Idx;
30 ULONG CurrentOperatingSystemIndex;
31 UCHAR SettingName[80];
32 UCHAR SettingValue[80];
33 ULONG OperatingSystemCount;
34 ULONG SectionId;
35 ULONG OperatingSystemSectionId;
36 ULONG SectionSettingCount;
37 PUCHAR *OperatingSystemSectionNames;
38 PUCHAR *OperatingSystemDisplayNames;
39
40 //
41 // Open the [FreeLoader] section
42 //
43 if (!OpenSection("FreeLoader", &SectionId))
44 {
45 MessageBox("Section [FreeLoader] not found in freeldr.ini.");
46 return FALSE;
47 }
48
49 SectionSettingCount = GetNumSectionItems(SectionId);
50 OperatingSystemCount = CountOperatingSystems(SectionId);
51
52 //
53 // Allocate memory to hold operating system lists
54 //
55 if (!AllocateListMemory(&OperatingSystemSectionNames, &OperatingSystemDisplayNames, OperatingSystemCount))
56 {
57 return FALSE;
58 }
59
60 //
61 // Now loop through and read the operating system section names
62 //
63 CurrentOperatingSystemIndex = 0;
64 for (Idx=0; Idx<SectionSettingCount; Idx++)
65 {
66 ReadSectionSettingByNumber(SectionId, Idx, SettingName, 80, SettingValue, 80);
67
68 if (stricmp(SettingName, "OS") == 0)
69 {
70 strcpy(OperatingSystemSectionNames[CurrentOperatingSystemIndex], SettingValue);
71
72 CurrentOperatingSystemIndex++;
73 }
74 }
75
76 //
77 // Now loop through and read the operating system display names
78 //
79 for (Idx=0; Idx<OperatingSystemCount; Idx++)
80 {
81 if (OpenSection(OperatingSystemSectionNames[Idx], &OperatingSystemSectionId))
82 {
83 if (ReadSectionSettingByName(OperatingSystemSectionId, "Name", SettingValue, 80))
84 {
85 //
86 // Remove any quotes around the string
87 //
88 RemoveQuotes(SettingValue);
89 strcpy(OperatingSystemDisplayNames[Idx], SettingValue);
90 }
91 else
92 {
93 sprintf(SettingName, "Operating System '%s' has no Name= line in it's [section].", OperatingSystemSectionNames[Idx]);
94 MessageBox(SettingName);
95 strcpy(OperatingSystemDisplayNames[Idx], "");
96 }
97 }
98 }
99
100 *OperatingSystemCountPointer = OperatingSystemCount;
101 *SectionNamesPointer = OperatingSystemSectionNames;
102 *DisplayNamesPointer = OperatingSystemDisplayNames;
103
104 return TRUE;
105 }
106
107 ULONG CountOperatingSystems(ULONG SectionId)
108 {
109 ULONG Idx;
110 UCHAR SettingName[80];
111 UCHAR SettingValue[80];
112 ULONG OperatingSystemCount = 0;
113 ULONG SectionSettingCount;
114
115 //
116 // Loop through and count the operating systems
117 //
118 SectionSettingCount = GetNumSectionItems(SectionId);
119 for (Idx=0; Idx<SectionSettingCount; Idx++)
120 {
121 ReadSectionSettingByNumber(SectionId, Idx, SettingName, 80, SettingValue, 80);
122
123 if (stricmp(SettingName, "OS") == 0)
124 {
125 if (OpenSection(SettingValue, NULL))
126 {
127 OperatingSystemCount++;
128 }
129 else
130 {
131 sprintf(SettingName, "Operating System '%s' is listed in freeldr.ini but doesn't have a [section].", SettingValue);
132 MessageBox(SettingName);
133 }
134 }
135 }
136
137 return OperatingSystemCount;
138 }
139
140 BOOL AllocateListMemory(PUCHAR **SectionNamesPointer, PUCHAR **DisplayNamesPointer, ULONG OperatingSystemCount)
141 {
142 ULONG Idx;
143 PUCHAR *OperatingSystemSectionNames = NULL;
144 PUCHAR *OperatingSystemDisplayNames = NULL;
145
146 //
147 // Allocate memory to hold operating system list arrays
148 //
149 OperatingSystemSectionNames = (PUCHAR*) AllocateMemory( sizeof(PUCHAR) * OperatingSystemCount);
150 OperatingSystemDisplayNames = (PUCHAR*) AllocateMemory( sizeof(PUCHAR) * OperatingSystemCount);
151
152 //
153 // If either allocation failed then return FALSE
154 //
155 if ( (OperatingSystemSectionNames == NULL) || (OperatingSystemDisplayNames == NULL) )
156 {
157 if (OperatingSystemSectionNames != NULL)
158 {
159 FreeMemory(OperatingSystemSectionNames);
160 }
161
162 if (OperatingSystemDisplayNames != NULL)
163 {
164 FreeMemory(OperatingSystemDisplayNames);
165 }
166
167 return FALSE;
168 }
169
170 //
171 // Clear our newly allocated memory
172 //
173 memset(OperatingSystemSectionNames, 0, sizeof(PUCHAR) * OperatingSystemCount);
174 memset(OperatingSystemDisplayNames, 0, sizeof(PUCHAR) * OperatingSystemCount);
175
176 //
177 // Loop through each array element and allocate it's string memory
178 //
179 for (Idx=0; Idx<OperatingSystemCount; Idx++)
180 {
181 OperatingSystemSectionNames[Idx] = (PUCHAR) AllocateMemory(80);
182 OperatingSystemDisplayNames[Idx] = (PUCHAR) AllocateMemory(80);
183
184 //
185 // If it failed then jump to the cleanup code
186 //
187 if ( (OperatingSystemSectionNames[Idx] == NULL) || (OperatingSystemDisplayNames[Idx] == NULL))
188 {
189 goto AllocateListMemoryFailed;
190 }
191 }
192
193 *SectionNamesPointer = OperatingSystemSectionNames;
194 *DisplayNamesPointer = OperatingSystemDisplayNames;
195
196 return TRUE;
197
198 AllocateListMemoryFailed:
199
200 //
201 // Loop through each array element and free it's string memory
202 //
203 for (Idx=0; Idx<OperatingSystemCount; Idx++)
204 {
205 if (OperatingSystemSectionNames[Idx] != NULL)
206 {
207 FreeMemory(OperatingSystemSectionNames[Idx]);
208 }
209
210 if (OperatingSystemDisplayNames[Idx] != NULL)
211 {
212 FreeMemory(OperatingSystemDisplayNames[Idx]);
213 }
214 }
215
216 //
217 // Free operating system list arrays
218 //
219 FreeMemory(OperatingSystemSectionNames);
220 FreeMemory(OperatingSystemDisplayNames);
221
222 return FALSE;
223 }
224
225 BOOL RemoveQuotes(PUCHAR QuotedString)
226 {
227 UCHAR TempString[200];
228
229 //
230 // If this string is not quoted then return FALSE
231 //
232 if ((QuotedString[0] != '\"') && (QuotedString[strlen(QuotedString)-1] != '\"'))
233 {
234 return FALSE;
235 }
236
237 if (QuotedString[0] == '\"')
238 {
239 strcpy(TempString, (QuotedString + 1));
240 }
241 else
242 {
243 strcpy(TempString, QuotedString);
244 }
245
246 if (TempString[strlen(TempString)-1] == '\"')
247 {
248 TempString[strlen(TempString)-1] = '\0';
249 }
250
251 strcpy(QuotedString, TempString);
252
253 return TRUE;
254 }