]> code.delx.au - refind/blobdiff - refind/mystrings.c
Properly initialise variable to fix detection of non-Arch kernel versions
[refind] / refind / mystrings.c
index 75e2d899ca92c641d421af49897e1e806bb98a71..eeb656012acbf1343b3cf2680fad780c5d1b6bf5 100644 (file)
@@ -238,6 +238,8 @@ BOOLEAN LimitStringLength(CHAR16 *TheString, UINTN Limit) {
 // non-digit characters. For instance, if InString is "foo-3.3.4-7.img",
 // this function returns "3.3.4-7". If InString contains no digits,
 // the return value is NULL.
+// As a special case for Arch Linux the strings "linux" and "linux-lts"
+// are considered to be digits.
 CHAR16 *FindNumbers(IN CHAR16 *InString) {
     UINTN i, StartOfElement, EndOfElement = 0, CopyLength;
     CHAR16 *Found = NULL;
@@ -246,6 +248,21 @@ CHAR16 *FindNumbers(IN CHAR16 *InString) {
         return NULL;
 
     StartOfElement = StrLen(InString);
+
+    // Find "linux-lts" or "linux"
+    Found = MyStrStr(InString, L"linux-lts");
+    if (Found != NULL) {
+        StartOfElement = Found - InString;
+        EndOfElement = StartOfElement + StrLen(L"linux-lts") - 1;
+    } else {
+        Found = MyStrStr(InString, L"linux");
+        if (Found != NULL) {
+            StartOfElement = Found - InString;
+            EndOfElement = StartOfElement + StrLen(L"linux") - 1;
+        } // if
+    } // if/else
+    Found = NULL;
+
     // Find start & end of target element
     for (i = 0; InString[i] != L'\0'; i++) {
         if ((InString[i] >= L'0') && (InString[i] <= L'9')) {
@@ -255,6 +272,7 @@ CHAR16 *FindNumbers(IN CHAR16 *InString) {
                 EndOfElement = i;
         } // if
     } // for
+
     // Extract the target element
     if (EndOfElement > 0) {
         if (EndOfElement >= StartOfElement) {
@@ -348,6 +366,34 @@ BOOLEAN IsInSubstring(IN CHAR16 *BigString, IN CHAR16 *List) {
    return Found;
 } // BOOLEAN IsSubstringIn()
 
+// Replace *SearchString in **MainString with *ReplString -- but if *SearchString
+// is preceded by "%", instead remove that character.
+// Returns TRUE if replacement was done, FALSE otherwise.
+BOOLEAN ReplaceSubstring(IN OUT CHAR16 **MainString, IN CHAR16 *SearchString, IN CHAR16 *ReplString) {
+    BOOLEAN WasReplaced = FALSE;
+    CHAR16 *FoundSearchString, *NewString, *EndString;
+
+    FoundSearchString = MyStrStr(*MainString, SearchString);
+    if (FoundSearchString) {
+        NewString = AllocateZeroPool(sizeof(CHAR16) * StrLen(*MainString));
+        if (NewString) {
+            EndString = &(FoundSearchString[StrLen(SearchString)]);
+            FoundSearchString[0] = L'\0';
+            if ((FoundSearchString > *MainString) && (FoundSearchString[-1] == L'%')) {
+                FoundSearchString[-1] = L'\0';
+                ReplString = SearchString;
+            } // if
+            StrCpy(NewString, *MainString);
+            MergeStrings(&NewString, ReplString, L'\0');
+            MergeStrings(&NewString, EndString, L'\0');
+            MyFreePool(MainString);
+            *MainString = NewString;
+            WasReplaced = TRUE;
+        } // if
+    } // if
+    return WasReplaced;
+} // BOOLEAN ReplaceSubstring()
+
 // Returns TRUE if *Input contains nothing but valid hexadecimal characters,
 // FALSE otherwise. Note that a leading "0x" is NOT acceptable in the input!
 BOOLEAN IsValidHex(CHAR16 *Input) {