- Merge from trunk up to r45543
[reactos.git] / boot / freeldr / freeldr / oslist.c
index d10debd..24313b1 100644 (file)
 
 #include <freeldr.h>
 
-BOOLEAN InitOperatingSystemList(PCSTR **SectionNamesPointer, PCSTR **DisplayNamesPointer, ULONG* OperatingSystemCountPointer)
+static PCSTR CopyString(PCSTR Source)
 {
-       ULONG           Idx;
-       ULONG           CurrentOperatingSystemIndex;
-       CHAR    SettingName[260];
-       CHAR    SettingValue[260];
-       ULONG           OperatingSystemCount;
-       ULONG_PTR       SectionId;
-       ULONG           SectionSettingCount;
-       PCHAR   *OperatingSystemSectionNames;
-       PCHAR   *OperatingSystemDisplayNames;
+       PSTR Dest;
 
-       //
-       // Open the [FreeLoader] section
-       //
-       if (!IniOpenSection("Operating Systems", &SectionId))
+       if (!Source)
+               return NULL;
+       Dest = MmHeapAlloc(strlen(Source) + 1);
+       if (Dest)
        {
-               UiMessageBox("Section [Operating Systems] not found in freeldr.ini.");
-               return FALSE;
+               strcpy(Dest, Source);
        }
 
-       SectionSettingCount = IniGetNumSectionItems(SectionId);
-       OperatingSystemCount = CountOperatingSystems(SectionId);
+       return Dest;
+}
 
-       //
-       // Allocate memory to hold operating system lists
-       //
-       if (!AllocateListMemory(&OperatingSystemSectionNames, &OperatingSystemDisplayNames, OperatingSystemCount))
-       {
-               return FALSE;
-       }
+OperatingSystemItem* InitOperatingSystemList(ULONG* OperatingSystemCountPointer)
+{
+       ULONG Idx;
+       CHAR SettingName[260];
+       CHAR SettingValue[260];
+       ULONG_PTR SectionId;
+       PCHAR TitleStart, TitleEnd;
+       PCSTR OsLoadOptions;
+       ULONG Count;
+       OperatingSystemItem* Items;
 
        //
-       // Now loop through and read the operating system section and display names
+       // Open the [FreeLoader] section
        //
-       CurrentOperatingSystemIndex = 0;
-       for (Idx=0; Idx<SectionSettingCount; Idx++)
+       if (!IniOpenSection("Operating Systems", &SectionId))
        {
-               IniReadSettingByNumber(SectionId, Idx, SettingName, sizeof(SettingName), SettingValue, sizeof(SettingValue));
-
-               // Copy the section name
-               strcpy(OperatingSystemSectionNames[CurrentOperatingSystemIndex], SettingName);
-
-               // Copy the display name
-               RemoveQuotes(SettingValue);
-               strcpy(OperatingSystemDisplayNames[CurrentOperatingSystemIndex], SettingValue);
-
-               CurrentOperatingSystemIndex++;
+               return NULL;
        }
 
-       *OperatingSystemCountPointer = OperatingSystemCount;
-       *SectionNamesPointer = (PCSTR*)OperatingSystemSectionNames;
-       *DisplayNamesPointer = (PCSTR*)OperatingSystemDisplayNames;
-
-       return TRUE;
-}
-
-ULONG CountOperatingSystems(ULONG SectionId)
-{
-       return IniGetNumSectionItems(SectionId);
-}
-
-BOOLEAN AllocateListMemory(PCHAR **SectionNamesPointer, PCHAR **DisplayNamesPointer, ULONG OperatingSystemCount)
-{
-       ULONG           Idx;
-       PCHAR   *OperatingSystemSectionNames = NULL;
-       PCHAR   *OperatingSystemDisplayNames = NULL;
-
        //
-       // Allocate memory to hold operating system list arrays
+       // Count number of operating systems in the section
        //
-       OperatingSystemSectionNames = MmHeapAlloc( sizeof(PCHAR) * OperatingSystemCount);
-       OperatingSystemDisplayNames = MmHeapAlloc( sizeof(PCHAR) * OperatingSystemCount);
+       Count = IniGetNumSectionItems(SectionId);
 
        //
-       // If either allocation failed then return FALSE
+       // Allocate memory to hold operating system lists
        //
-       if ( (OperatingSystemSectionNames == NULL) || (OperatingSystemDisplayNames == NULL) )
+       Items = MmHeapAlloc(Count * sizeof(OperatingSystemItem));
+       if (!Items)
        {
-               if (OperatingSystemSectionNames != NULL)
-               {
-                       MmHeapFree(OperatingSystemSectionNames);
-               }
-
-               if (OperatingSystemDisplayNames != NULL)
-               {
-                       MmHeapFree(OperatingSystemDisplayNames);
-               }
-
-               return FALSE;
+               return NULL;
        }
 
        //
-       // Clear our newly allocated memory
-       //
-       memset(OperatingSystemSectionNames, 0, sizeof(PCHAR) * OperatingSystemCount);
-       memset(OperatingSystemDisplayNames, 0, sizeof(PCHAR) * OperatingSystemCount);
-
-       //
-       // Loop through each array element and allocate it's string memory
+       // Now loop through and read the operating system section and display names
        //
-       for (Idx=0; Idx<OperatingSystemCount; Idx++)
+       for (Idx = 0; Idx < Count; Idx++)
        {
-               OperatingSystemSectionNames[Idx] = MmHeapAlloc(80);
-               OperatingSystemDisplayNames[Idx] = MmHeapAlloc(80);
+               IniReadSettingByNumber(SectionId, Idx, SettingName, sizeof(SettingName), SettingValue, sizeof(SettingValue));
 
                //
-               // If it failed then jump to the cleanup code
+               // Search start and end of the title
                //
-               if ( (OperatingSystemSectionNames[Idx] == NULL) || (OperatingSystemDisplayNames[Idx] == NULL))
+               OsLoadOptions = NULL;
+               TitleStart = SettingValue;
+               while (*TitleStart == ' ' || *TitleStart == '"')
+                       TitleStart++;
+               TitleEnd = TitleStart;
+               if (*TitleEnd != ANSI_NULL)
+                       TitleEnd++;
+               while (*TitleEnd != ANSI_NULL && *TitleEnd != '"')
+                       TitleEnd++;
+               if (*TitleEnd != ANSI_NULL)
                {
-                       goto AllocateListMemoryFailed;
+                       *TitleEnd = ANSI_NULL;
+                       OsLoadOptions = TitleEnd + 1;
                }
-       }
-
-       *SectionNamesPointer = OperatingSystemSectionNames;
-       *DisplayNamesPointer = OperatingSystemDisplayNames;
-
-       return TRUE;
-
-AllocateListMemoryFailed:
 
-       //
-       // Loop through each array element and free it's string memory
-       //
-       for (Idx=0; Idx<OperatingSystemCount; Idx++)
-       {
-               if (OperatingSystemSectionNames[Idx] != NULL)
-               {
-                       MmHeapFree(OperatingSystemSectionNames[Idx]);
-               }
-
-               if (OperatingSystemDisplayNames[Idx] != NULL)
-               {
-                       MmHeapFree(OperatingSystemDisplayNames[Idx]);
-               }
+               //
+               // Copy the system partition, identifier and options
+               //
+               Items[Idx].SystemPartition = CopyString(SettingName);
+               Items[Idx].LoadIdentifier = CopyString(TitleStart);
+               Items[Idx].OsLoadOptions = CopyString(OsLoadOptions);
        }
 
        //
-       // Free operating system list arrays
-       //
-       MmHeapFree(OperatingSystemSectionNames);
-       MmHeapFree(OperatingSystemDisplayNames);
-
-       return FALSE;
-}
-
-BOOLEAN RemoveQuotes(PCHAR QuotedString)
-{
-       CHAR    TempString[200];
-       PCHAR p;
-       PSTR Start;
-
-       //
-       // Skip spaces up to "
-       //
-       p = QuotedString;
-       while (*p == ' ' || *p == '"')
-               p++;
-       Start = p;
-
-       //
-       // Go up to next "
+       // Return success
        //
-       while (*p != '"' && *p != ANSI_NULL)
-               p++;
-       *p = ANSI_NULL;
-
-       //
-       // Copy result
-       //
-       strcpy(TempString, Start);
-       strcpy(QuotedString, TempString);
-
-       return TRUE;
+       *OperatingSystemCountPointer = Count;
+       return Items;
 }