3 * Copyright (C) 1999, 2000, 2001 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.
24 BOOL
InitOperatingSystemList(PUCHAR
**SectionNamesPointer
, PUCHAR
**DisplayNamesPointer
, PULONG OperatingSystemCountPointer
)
27 ULONG CurrentOperatingSystemIndex
;
28 UCHAR SettingName
[80];
29 UCHAR SettingValue
[80];
30 ULONG OperatingSystemCount
;
32 ULONG OperatingSystemSectionId
;
33 ULONG SectionSettingCount
;
34 PUCHAR
*OperatingSystemSectionNames
;
35 PUCHAR
*OperatingSystemDisplayNames
;
38 // Open the [FreeLoader] section
40 if (!OpenSection("FreeLoader", &SectionId
))
42 MessageBox("Section [FreeLoader] not found in freeldr.ini.");
46 SectionSettingCount
= GetNumSectionItems(SectionId
);
47 OperatingSystemCount
= CountOperatingSystems(SectionId
);
50 // Allocate memory to hold operating system lists
52 if (!AllocateListMemory(&OperatingSystemSectionNames
, &OperatingSystemDisplayNames
, OperatingSystemCount
))
58 // Now loop through and read the operating system section names
60 CurrentOperatingSystemIndex
= 0;
61 for (Idx
=0; Idx
<SectionSettingCount
; Idx
++)
63 ReadSectionSettingByNumber(SectionId
, Idx
, SettingName
, 80, SettingValue
, 80);
65 if (stricmp(SettingName
, "OS") == 0)
67 strcpy(OperatingSystemSectionNames
[CurrentOperatingSystemIndex
], SettingValue
);
69 CurrentOperatingSystemIndex
++;
74 // Now loop through and read the operating system display names
76 for (Idx
=0; Idx
<OperatingSystemCount
; Idx
++)
78 if (OpenSection(OperatingSystemSectionNames
[Idx
], &OperatingSystemSectionId
))
80 if (ReadSectionSettingByName(OperatingSystemSectionId
, "Name", SettingValue
, 80))
83 // Remove any quotes around the string
85 RemoveQuotes(SettingValue
);
86 strcpy(OperatingSystemDisplayNames
[Idx
], SettingValue
);
90 sprintf(SettingName
, "Operating System '%s' has no Name= line in it's [section].", OperatingSystemSectionNames
[Idx
]);
91 MessageBox(SettingName
);
92 strcpy(OperatingSystemDisplayNames
[Idx
], "");
97 *OperatingSystemCountPointer
= OperatingSystemCount
;
98 *SectionNamesPointer
= OperatingSystemSectionNames
;
99 *DisplayNamesPointer
= OperatingSystemDisplayNames
;
104 ULONG
CountOperatingSystems(ULONG SectionId
)
107 UCHAR SettingName
[80];
108 UCHAR SettingValue
[80];
109 ULONG OperatingSystemCount
= 0;
110 ULONG SectionSettingCount
;
113 // Loop through and count the operating systems
115 SectionSettingCount
= GetNumSectionItems(SectionId
);
116 for (Idx
=0; Idx
<SectionSettingCount
; Idx
++)
118 ReadSectionSettingByNumber(SectionId
, Idx
, SettingName
, 80, SettingValue
, 80);
120 if (stricmp(SettingName
, "OS") == 0)
122 if (OpenSection(SettingValue
, NULL
))
124 OperatingSystemCount
++;
128 sprintf(SettingName
, "Operating System '%s' is listed in freeldr.ini but doesn't have a [section].", SettingValue
);
129 MessageBox(SettingName
);
134 return OperatingSystemCount
;
137 BOOL
AllocateListMemory(PUCHAR
**SectionNamesPointer
, PUCHAR
**DisplayNamesPointer
, ULONG OperatingSystemCount
)
140 PUCHAR
*OperatingSystemSectionNames
= NULL
;
141 PUCHAR
*OperatingSystemDisplayNames
= NULL
;
144 // Allocate memory to hold operating system list arrays
146 OperatingSystemSectionNames
= (PUCHAR
*) AllocateMemory( sizeof(PUCHAR
) * OperatingSystemCount
);
147 OperatingSystemDisplayNames
= (PUCHAR
*) AllocateMemory( sizeof(PUCHAR
) * OperatingSystemCount
);
150 // If either allocation failed then return FALSE
152 if ( (OperatingSystemSectionNames
== NULL
) || (OperatingSystemDisplayNames
== NULL
) )
154 if (OperatingSystemSectionNames
!= NULL
)
156 FreeMemory(OperatingSystemSectionNames
);
159 if (OperatingSystemDisplayNames
!= NULL
)
161 FreeMemory(OperatingSystemDisplayNames
);
168 // Clear our newly allocated memory
170 memset(OperatingSystemSectionNames
, 0, sizeof(PUCHAR
) * OperatingSystemCount
);
171 memset(OperatingSystemDisplayNames
, 0, sizeof(PUCHAR
) * OperatingSystemCount
);
174 // Loop through each array element and allocate it's string memory
176 for (Idx
=0; Idx
<OperatingSystemCount
; Idx
++)
178 OperatingSystemSectionNames
[Idx
] = (PUCHAR
) AllocateMemory(80);
179 OperatingSystemDisplayNames
[Idx
] = (PUCHAR
) AllocateMemory(80);
182 // If it failed then jump to the cleanup code
184 if ( (OperatingSystemSectionNames
[Idx
] == NULL
) || (OperatingSystemDisplayNames
[Idx
] == NULL
))
186 goto AllocateListMemoryFailed
;
190 *SectionNamesPointer
= OperatingSystemSectionNames
;
191 *DisplayNamesPointer
= OperatingSystemDisplayNames
;
195 AllocateListMemoryFailed
:
198 // Loop through each array element and free it's string memory
200 for (Idx
=0; Idx
<OperatingSystemCount
; Idx
++)
202 if (OperatingSystemSectionNames
[Idx
] != NULL
)
204 FreeMemory(OperatingSystemSectionNames
[Idx
]);
207 if (OperatingSystemDisplayNames
[Idx
] != NULL
)
209 FreeMemory(OperatingSystemDisplayNames
[Idx
]);
214 // Free operating system list arrays
216 FreeMemory(OperatingSystemSectionNames
);
217 FreeMemory(OperatingSystemDisplayNames
);
222 BOOL
RemoveQuotes(PUCHAR QuotedString
)
224 UCHAR TempString
[200];
227 // If this string is not quoted then return FALSE
229 if ((QuotedString
[0] != '\"') && (QuotedString
[strlen(QuotedString
)-1] != '\"'))
234 if (QuotedString
[0] == '\"')
236 strcpy(TempString
, (QuotedString
+ 1));
240 strcpy(TempString
, QuotedString
);
243 if (TempString
[strlen(TempString
)-1] == '\"')
245 TempString
[strlen(TempString
)-1] = '\0';
248 strcpy(QuotedString
, TempString
);