3 * Copyright (C) 1998-2002 Brian Palmer <brianp@sginet.com>
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.
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.
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.
27 BOOL
InitOperatingSystemList(PUCHAR
**SectionNamesPointer
, PUCHAR
**DisplayNamesPointer
, PULONG OperatingSystemCountPointer
)
30 ULONG CurrentOperatingSystemIndex
;
31 UCHAR SettingName
[80];
32 UCHAR SettingValue
[80];
33 ULONG OperatingSystemCount
;
35 ULONG OperatingSystemSectionId
;
36 ULONG SectionSettingCount
;
37 PUCHAR
*OperatingSystemSectionNames
;
38 PUCHAR
*OperatingSystemDisplayNames
;
41 // Open the [FreeLoader] section
43 if (!OpenSection("FreeLoader", &SectionId
))
45 MessageBox("Section [FreeLoader] not found in freeldr.ini.");
49 SectionSettingCount
= GetNumSectionItems(SectionId
);
50 OperatingSystemCount
= CountOperatingSystems(SectionId
);
53 // Allocate memory to hold operating system lists
55 if (!AllocateListMemory(&OperatingSystemSectionNames
, &OperatingSystemDisplayNames
, OperatingSystemCount
))
61 // Now loop through and read the operating system section names
63 CurrentOperatingSystemIndex
= 0;
64 for (Idx
=0; Idx
<SectionSettingCount
; Idx
++)
66 ReadSectionSettingByNumber(SectionId
, Idx
, SettingName
, 80, SettingValue
, 80);
68 if (stricmp(SettingName
, "OS") == 0)
70 strcpy(OperatingSystemSectionNames
[CurrentOperatingSystemIndex
], SettingValue
);
72 CurrentOperatingSystemIndex
++;
77 // Now loop through and read the operating system display names
79 for (Idx
=0; Idx
<OperatingSystemCount
; Idx
++)
81 if (OpenSection(OperatingSystemSectionNames
[Idx
], &OperatingSystemSectionId
))
83 if (ReadSectionSettingByName(OperatingSystemSectionId
, "Name", SettingValue
, 80))
86 // Remove any quotes around the string
88 RemoveQuotes(SettingValue
);
89 strcpy(OperatingSystemDisplayNames
[Idx
], SettingValue
);
93 sprintf(SettingName
, "Operating System '%s' has no Name= line in it's [section].", OperatingSystemSectionNames
[Idx
]);
94 MessageBox(SettingName
);
95 strcpy(OperatingSystemDisplayNames
[Idx
], "");
100 *OperatingSystemCountPointer
= OperatingSystemCount
;
101 *SectionNamesPointer
= OperatingSystemSectionNames
;
102 *DisplayNamesPointer
= OperatingSystemDisplayNames
;
107 ULONG
CountOperatingSystems(ULONG SectionId
)
110 UCHAR SettingName
[80];
111 UCHAR SettingValue
[80];
112 ULONG OperatingSystemCount
= 0;
113 ULONG SectionSettingCount
;
116 // Loop through and count the operating systems
118 SectionSettingCount
= GetNumSectionItems(SectionId
);
119 for (Idx
=0; Idx
<SectionSettingCount
; Idx
++)
121 ReadSectionSettingByNumber(SectionId
, Idx
, SettingName
, 80, SettingValue
, 80);
123 if (stricmp(SettingName
, "OS") == 0)
125 if (OpenSection(SettingValue
, NULL
))
127 OperatingSystemCount
++;
131 sprintf(SettingName
, "Operating System '%s' is listed in freeldr.ini but doesn't have a [section].", SettingValue
);
132 MessageBox(SettingName
);
137 return OperatingSystemCount
;
140 BOOL
AllocateListMemory(PUCHAR
**SectionNamesPointer
, PUCHAR
**DisplayNamesPointer
, ULONG OperatingSystemCount
)
143 PUCHAR
*OperatingSystemSectionNames
= NULL
;
144 PUCHAR
*OperatingSystemDisplayNames
= NULL
;
147 // Allocate memory to hold operating system list arrays
149 OperatingSystemSectionNames
= (PUCHAR
*) AllocateMemory( sizeof(PUCHAR
) * OperatingSystemCount
);
150 OperatingSystemDisplayNames
= (PUCHAR
*) AllocateMemory( sizeof(PUCHAR
) * OperatingSystemCount
);
153 // If either allocation failed then return FALSE
155 if ( (OperatingSystemSectionNames
== NULL
) || (OperatingSystemDisplayNames
== NULL
) )
157 if (OperatingSystemSectionNames
!= NULL
)
159 FreeMemory(OperatingSystemSectionNames
);
162 if (OperatingSystemDisplayNames
!= NULL
)
164 FreeMemory(OperatingSystemDisplayNames
);
171 // Clear our newly allocated memory
173 memset(OperatingSystemSectionNames
, 0, sizeof(PUCHAR
) * OperatingSystemCount
);
174 memset(OperatingSystemDisplayNames
, 0, sizeof(PUCHAR
) * OperatingSystemCount
);
177 // Loop through each array element and allocate it's string memory
179 for (Idx
=0; Idx
<OperatingSystemCount
; Idx
++)
181 OperatingSystemSectionNames
[Idx
] = (PUCHAR
) AllocateMemory(80);
182 OperatingSystemDisplayNames
[Idx
] = (PUCHAR
) AllocateMemory(80);
185 // If it failed then jump to the cleanup code
187 if ( (OperatingSystemSectionNames
[Idx
] == NULL
) || (OperatingSystemDisplayNames
[Idx
] == NULL
))
189 goto AllocateListMemoryFailed
;
193 *SectionNamesPointer
= OperatingSystemSectionNames
;
194 *DisplayNamesPointer
= OperatingSystemDisplayNames
;
198 AllocateListMemoryFailed
:
201 // Loop through each array element and free it's string memory
203 for (Idx
=0; Idx
<OperatingSystemCount
; Idx
++)
205 if (OperatingSystemSectionNames
[Idx
] != NULL
)
207 FreeMemory(OperatingSystemSectionNames
[Idx
]);
210 if (OperatingSystemDisplayNames
[Idx
] != NULL
)
212 FreeMemory(OperatingSystemDisplayNames
[Idx
]);
217 // Free operating system list arrays
219 FreeMemory(OperatingSystemSectionNames
);
220 FreeMemory(OperatingSystemDisplayNames
);
225 BOOL
RemoveQuotes(PUCHAR QuotedString
)
227 UCHAR TempString
[200];
230 // If this string is not quoted then return FALSE
232 if ((QuotedString
[0] != '\"') && (QuotedString
[strlen(QuotedString
)-1] != '\"'))
237 if (QuotedString
[0] == '\"')
239 strcpy(TempString
, (QuotedString
+ 1));
243 strcpy(TempString
, QuotedString
);
246 if (TempString
[strlen(TempString
)-1] == '\"')
248 TempString
[strlen(TempString
)-1] = '\0';
251 strcpy(QuotedString
, TempString
);