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.
27 BOOL
InitOperatingSystemList(PCHAR
**SectionNamesPointer
, PCHAR
**DisplayNamesPointer
, ULONG
* OperatingSystemCountPointer
)
30 ULONG CurrentOperatingSystemIndex
;
31 CHAR SettingName
[260];
32 CHAR SettingValue
[260];
33 ULONG OperatingSystemCount
;
35 ULONG OperatingSystemSectionId
;
36 ULONG SectionSettingCount
;
37 PCHAR
*OperatingSystemSectionNames
;
38 PCHAR
*OperatingSystemDisplayNames
;
41 // Open the [FreeLoader] section
43 if (!IniOpenSection("Operating Systems", &SectionId
))
45 UiMessageBox("Section [Operating Systems] not found in freeldr.ini.");
49 SectionSettingCount
= IniGetNumSectionItems(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 and display names
63 CurrentOperatingSystemIndex
= 0;
64 for (Idx
=0; Idx
<SectionSettingCount
; Idx
++)
66 IniReadSettingByNumber(SectionId
, Idx
, SettingName
, 260, SettingValue
, 260);
68 if (IniOpenSection(SettingName
, &OperatingSystemSectionId
))
70 // Copy the section name
71 strcpy(OperatingSystemSectionNames
[CurrentOperatingSystemIndex
], SettingName
);
73 // Copy the display name
74 RemoveQuotes(SettingValue
);
75 strcpy(OperatingSystemDisplayNames
[CurrentOperatingSystemIndex
], SettingValue
);
77 CurrentOperatingSystemIndex
++;
81 *OperatingSystemCountPointer
= OperatingSystemCount
;
82 *SectionNamesPointer
= OperatingSystemSectionNames
;
83 *DisplayNamesPointer
= OperatingSystemDisplayNames
;
88 ULONG
CountOperatingSystems(ULONG SectionId
)
91 CHAR SettingName
[260];
92 CHAR SettingValue
[260];
93 ULONG OperatingSystemCount
= 0;
94 ULONG SectionSettingCount
;
97 // Loop through and count the operating systems
99 SectionSettingCount
= IniGetNumSectionItems(SectionId
);
100 for (Idx
=0; Idx
<SectionSettingCount
; Idx
++)
102 IniReadSettingByNumber(SectionId
, Idx
, SettingName
, 260, SettingValue
, 260);
104 if (IniOpenSection(SettingName
, NULL
))
106 OperatingSystemCount
++;
110 sprintf(SettingName
, "Operating System '%s' is listed in\nfreeldr.ini but doesn't have a [section].", SettingValue
);
111 UiMessageBox(SettingName
);
115 return OperatingSystemCount
;
118 BOOL
AllocateListMemory(PCHAR
**SectionNamesPointer
, PCHAR
**DisplayNamesPointer
, ULONG OperatingSystemCount
)
121 PCHAR
*OperatingSystemSectionNames
= NULL
;
122 PCHAR
*OperatingSystemDisplayNames
= NULL
;
125 // Allocate memory to hold operating system list arrays
127 OperatingSystemSectionNames
= MmAllocateMemory( sizeof(PCHAR
) * OperatingSystemCount
);
128 OperatingSystemDisplayNames
= MmAllocateMemory( sizeof(PCHAR
) * OperatingSystemCount
);
131 // If either allocation failed then return FALSE
133 if ( (OperatingSystemSectionNames
== NULL
) || (OperatingSystemDisplayNames
== NULL
) )
135 if (OperatingSystemSectionNames
!= NULL
)
137 MmFreeMemory(OperatingSystemSectionNames
);
140 if (OperatingSystemDisplayNames
!= NULL
)
142 MmFreeMemory(OperatingSystemDisplayNames
);
149 // Clear our newly allocated memory
151 memset(OperatingSystemSectionNames
, 0, sizeof(PCHAR
) * OperatingSystemCount
);
152 memset(OperatingSystemDisplayNames
, 0, sizeof(PCHAR
) * OperatingSystemCount
);
155 // Loop through each array element and allocate it's string memory
157 for (Idx
=0; Idx
<OperatingSystemCount
; Idx
++)
159 OperatingSystemSectionNames
[Idx
] = MmAllocateMemory(80);
160 OperatingSystemDisplayNames
[Idx
] = MmAllocateMemory(80);
163 // If it failed then jump to the cleanup code
165 if ( (OperatingSystemSectionNames
[Idx
] == NULL
) || (OperatingSystemDisplayNames
[Idx
] == NULL
))
167 goto AllocateListMemoryFailed
;
171 *SectionNamesPointer
= OperatingSystemSectionNames
;
172 *DisplayNamesPointer
= OperatingSystemDisplayNames
;
176 AllocateListMemoryFailed
:
179 // Loop through each array element and free it's string memory
181 for (Idx
=0; Idx
<OperatingSystemCount
; Idx
++)
183 if (OperatingSystemSectionNames
[Idx
] != NULL
)
185 MmFreeMemory(OperatingSystemSectionNames
[Idx
]);
188 if (OperatingSystemDisplayNames
[Idx
] != NULL
)
190 MmFreeMemory(OperatingSystemDisplayNames
[Idx
]);
195 // Free operating system list arrays
197 MmFreeMemory(OperatingSystemSectionNames
);
198 MmFreeMemory(OperatingSystemDisplayNames
);
203 BOOL
RemoveQuotes(PCHAR QuotedString
)
205 CHAR TempString
[200];
208 // If this string is not quoted then return FALSE
210 if ((QuotedString
[0] != '\"') && (QuotedString
[strlen(QuotedString
)-1] != '\"'))
215 if (QuotedString
[0] == '\"')
217 strcpy(TempString
, (QuotedString
+ 1));
221 strcpy(TempString
, QuotedString
);
224 if (TempString
[strlen(TempString
)-1] == '\"')
226 TempString
[strlen(TempString
)-1] = '\0';
229 strcpy(QuotedString
, TempString
);