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