Created new subtree for groups of related test programs.
[reactos.git] / freeldr / freeldr / oslist.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 <inifile.h>
22 #include <oslist.h>
23 #include <rtl.h>
24 #include <mm.h>
25 #include <ui.h>
26
27 BOOL InitOperatingSystemList(PUCHAR **SectionNamesPointer, PUCHAR **DisplayNamesPointer, U32* OperatingSystemCountPointer)
28 {
29 U32 Idx;
30 U32 CurrentOperatingSystemIndex;
31 UCHAR SettingName[260];
32 UCHAR SettingValue[260];
33 U32 OperatingSystemCount;
34 U32 SectionId;
35 U32 OperatingSystemSectionId;
36 U32 SectionSettingCount;
37 PUCHAR *OperatingSystemSectionNames;
38 PUCHAR *OperatingSystemDisplayNames;
39
40 //
41 // Open the [FreeLoader] section
42 //
43 if (!IniOpenSection("FreeLoader", &SectionId))
44 {
45 UiMessageBox("Section [FreeLoader] not found in freeldr.ini.");
46 return FALSE;
47 }
48
49 SectionSettingCount = IniGetNumSectionItems(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 IniReadSettingByNumber(SectionId, Idx, SettingName, 260, SettingValue, 260);
67
68 if (stricmp(SettingName, "OS") == 0 && IniOpenSection(SettingValue, &OperatingSystemSectionId))
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 (IniOpenSection(OperatingSystemSectionNames[Idx], &OperatingSystemSectionId))
82 {
83 if (IniReadSettingByName(OperatingSystemSectionId, "Name", SettingValue, 260))
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\nName= line in it's [section].", OperatingSystemSectionNames[Idx]);
94 UiMessageBox(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 U32 CountOperatingSystems(U32 SectionId)
108 {
109 U32 Idx;
110 UCHAR SettingName[260];
111 UCHAR SettingValue[260];
112 U32 OperatingSystemCount = 0;
113 U32 SectionSettingCount;
114
115 //
116 // Loop through and count the operating systems
117 //
118 SectionSettingCount = IniGetNumSectionItems(SectionId);
119 for (Idx=0; Idx<SectionSettingCount; Idx++)
120 {
121 IniReadSettingByNumber(SectionId, Idx, SettingName, 260, SettingValue, 260);
122
123 if (stricmp(SettingName, "OS") == 0)
124 {
125 if (IniOpenSection(SettingValue, NULL))
126 {
127 OperatingSystemCount++;
128 }
129 else
130 {
131 sprintf(SettingName, "Operating System '%s' is listed in\nfreeldr.ini but doesn't have a [section].", SettingValue);
132 UiMessageBox(SettingName);
133 }
134 }
135 }
136
137 return OperatingSystemCount;
138 }
139
140 BOOL AllocateListMemory(PUCHAR **SectionNamesPointer, PUCHAR **DisplayNamesPointer, U32 OperatingSystemCount)
141 {
142 U32 Idx;
143 PUCHAR *OperatingSystemSectionNames = NULL;
144 PUCHAR *OperatingSystemDisplayNames = NULL;
145
146 //
147 // Allocate memory to hold operating system list arrays
148 //
149 OperatingSystemSectionNames = (PUCHAR*) MmAllocateMemory( sizeof(PUCHAR) * OperatingSystemCount);
150 OperatingSystemDisplayNames = (PUCHAR*) MmAllocateMemory( 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 MmFreeMemory(OperatingSystemSectionNames);
160 }
161
162 if (OperatingSystemDisplayNames != NULL)
163 {
164 MmFreeMemory(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) MmAllocateMemory(80);
182 OperatingSystemDisplayNames[Idx] = (PUCHAR) MmAllocateMemory(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 MmFreeMemory(OperatingSystemSectionNames[Idx]);
208 }
209
210 if (OperatingSystemDisplayNames[Idx] != NULL)
211 {
212 MmFreeMemory(OperatingSystemDisplayNames[Idx]);
213 }
214 }
215
216 //
217 // Free operating system list arrays
218 //
219 MmFreeMemory(OperatingSystemSectionNames);
220 MmFreeMemory(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 }