3 * Copyright (C) 1998-2003 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.
22 BOOL
InitOperatingSystemList(PCSTR
**SectionNamesPointer
, PCSTR
**DisplayNamesPointer
, ULONG
* OperatingSystemCountPointer
)
25 ULONG CurrentOperatingSystemIndex
;
26 CHAR SettingName
[260];
27 CHAR SettingValue
[260];
28 ULONG OperatingSystemCount
;
30 ULONG OperatingSystemSectionId
;
31 ULONG SectionSettingCount
;
32 PCHAR
*OperatingSystemSectionNames
;
33 PCHAR
*OperatingSystemDisplayNames
;
36 // Open the [FreeLoader] section
38 if (!IniOpenSection("Operating Systems", &SectionId
))
40 UiMessageBox("Section [Operating Systems] not found in freeldr.ini.");
44 SectionSettingCount
= IniGetNumSectionItems(SectionId
);
45 OperatingSystemCount
= CountOperatingSystems(SectionId
);
48 // Allocate memory to hold operating system lists
50 if (!AllocateListMemory(&OperatingSystemSectionNames
, &OperatingSystemDisplayNames
, OperatingSystemCount
))
56 // Now loop through and read the operating system section and display names
58 CurrentOperatingSystemIndex
= 0;
59 for (Idx
=0; Idx
<SectionSettingCount
; Idx
++)
61 IniReadSettingByNumber(SectionId
, Idx
, SettingName
, 260, SettingValue
, 260);
63 if (IniOpenSection(SettingName
, &OperatingSystemSectionId
))
65 // Copy the section name
66 strcpy(OperatingSystemSectionNames
[CurrentOperatingSystemIndex
], SettingName
);
68 // Copy the display name
69 RemoveQuotes(SettingValue
);
70 strcpy(OperatingSystemDisplayNames
[CurrentOperatingSystemIndex
], SettingValue
);
72 CurrentOperatingSystemIndex
++;
76 *OperatingSystemCountPointer
= OperatingSystemCount
;
77 *SectionNamesPointer
= (PCSTR
*)OperatingSystemSectionNames
;
78 *DisplayNamesPointer
= (PCSTR
*)OperatingSystemDisplayNames
;
83 ULONG
CountOperatingSystems(ULONG SectionId
)
86 CHAR SettingName
[260];
87 CHAR SettingValue
[260];
88 ULONG OperatingSystemCount
= 0;
89 ULONG SectionSettingCount
;
92 // Loop through and count the operating systems
94 SectionSettingCount
= IniGetNumSectionItems(SectionId
);
95 for (Idx
=0; Idx
<SectionSettingCount
; Idx
++)
97 IniReadSettingByNumber(SectionId
, Idx
, SettingName
, 260, SettingValue
, 260);
99 if (IniOpenSection(SettingName
, NULL
))
101 OperatingSystemCount
++;
105 sprintf(SettingName
, "Operating System '%s' is listed in\nfreeldr.ini but doesn't have a [section].", SettingValue
);
106 UiMessageBox(SettingName
);
110 return OperatingSystemCount
;
113 BOOL
AllocateListMemory(PCHAR
**SectionNamesPointer
, PCHAR
**DisplayNamesPointer
, ULONG OperatingSystemCount
)
116 PCHAR
*OperatingSystemSectionNames
= NULL
;
117 PCHAR
*OperatingSystemDisplayNames
= NULL
;
120 // Allocate memory to hold operating system list arrays
122 OperatingSystemSectionNames
= MmAllocateMemory( sizeof(PCHAR
) * OperatingSystemCount
);
123 OperatingSystemDisplayNames
= MmAllocateMemory( sizeof(PCHAR
) * OperatingSystemCount
);
126 // If either allocation failed then return FALSE
128 if ( (OperatingSystemSectionNames
== NULL
) || (OperatingSystemDisplayNames
== NULL
) )
130 if (OperatingSystemSectionNames
!= NULL
)
132 MmFreeMemory(OperatingSystemSectionNames
);
135 if (OperatingSystemDisplayNames
!= NULL
)
137 MmFreeMemory(OperatingSystemDisplayNames
);
144 // Clear our newly allocated memory
146 memset(OperatingSystemSectionNames
, 0, sizeof(PCHAR
) * OperatingSystemCount
);
147 memset(OperatingSystemDisplayNames
, 0, sizeof(PCHAR
) * OperatingSystemCount
);
150 // Loop through each array element and allocate it's string memory
152 for (Idx
=0; Idx
<OperatingSystemCount
; Idx
++)
154 OperatingSystemSectionNames
[Idx
] = MmAllocateMemory(80);
155 OperatingSystemDisplayNames
[Idx
] = MmAllocateMemory(80);
158 // If it failed then jump to the cleanup code
160 if ( (OperatingSystemSectionNames
[Idx
] == NULL
) || (OperatingSystemDisplayNames
[Idx
] == NULL
))
162 goto AllocateListMemoryFailed
;
166 *SectionNamesPointer
= OperatingSystemSectionNames
;
167 *DisplayNamesPointer
= OperatingSystemDisplayNames
;
171 AllocateListMemoryFailed
:
174 // Loop through each array element and free it's string memory
176 for (Idx
=0; Idx
<OperatingSystemCount
; Idx
++)
178 if (OperatingSystemSectionNames
[Idx
] != NULL
)
180 MmFreeMemory(OperatingSystemSectionNames
[Idx
]);
183 if (OperatingSystemDisplayNames
[Idx
] != NULL
)
185 MmFreeMemory(OperatingSystemDisplayNames
[Idx
]);
190 // Free operating system list arrays
192 MmFreeMemory(OperatingSystemSectionNames
);
193 MmFreeMemory(OperatingSystemDisplayNames
);
198 BOOL
RemoveQuotes(PCHAR QuotedString
)
200 CHAR TempString
[200];
203 // If this string is not quoted then return FALSE
205 if ((QuotedString
[0] != '\"') && (QuotedString
[strlen(QuotedString
)-1] != '\"'))
210 if (QuotedString
[0] == '\"')
212 strcpy(TempString
, (QuotedString
+ 1));
216 strcpy(TempString
, QuotedString
);
219 if (TempString
[strlen(TempString
)-1] == '\"')
221 TempString
[strlen(TempString
)-1] = '\0';
224 strcpy(QuotedString
, TempString
);