]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkCompatibilityPkg/Sample/Tools/Source/Common/ParseInf.c
Sync all bug fixes between EDK1.04 and EDK1.06 into EdkCompatibilityPkg.
[mirror_edk2.git] / EdkCompatibilityPkg / Sample / Tools / Source / Common / ParseInf.c
index 3d9fe11ab9d55fddb9e0a349a0a9e19649c26cf5..5abea631069f4fcc2c2418e137bff59a4df09579 100644 (file)
@@ -1,6 +1,6 @@
 /*++\r
 \r
-Copyright (c) 2004, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials                          \r
 are licensed and made available under the terms and conditions of the BSD License         \r
 which accompanies this distribution.  The full text of the license may be found at        \r
@@ -343,6 +343,152 @@ Returns:
   return EFI_NOT_FOUND;\r
 }\r
 \r
+EFI_STATUS\r
+FindTokenInstanceInSection (\r
+  IN MEMORY_FILE    *InputFile,\r
+  IN CHAR8          *Section,\r
+  IN UINTN          Instance,\r
+  OUT CHAR8         *Token,\r
+  OUT CHAR8         *Value\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Finds the Instance-th token in a section.\r
+\r
+Arguments:\r
+\r
+  InputFile Memory file image.\r
+  Section   The section to search for, a string within [].\r
+  Instance  Specify the Instance-th token to search for, starting from zero\r
+  Token     The token name to return. Caller should allocate the buffer.\r
+            Must be _MAX_PATH in size.\r
+  Value     The token value to return. Caller should allocate the buffer.\r
+            Must be _MAX_PATH in size.\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS             Token and Value found.\r
+  EFI_ABORTED             Format error detected in INF file.\r
+  EFI_INVALID_PARAMETER   Input argument was null.\r
+  EFI_LOAD_ERROR          Error reading from the file.\r
+  EFI_NOT_FOUND           Section/Token/Value not found.\r
+\r
+--*/\r
+{\r
+  CHAR8   InputBuffer[_MAX_PATH];\r
+  CHAR8   *CurrentToken;\r
+  CHAR8   *CurrentValue;\r
+  BOOLEAN ParseError;\r
+  BOOLEAN ReadError;\r
+  UINTN   InstanceIndex;\r
+\r
+  //\r
+  // Check input parameters\r
+  //\r
+  if (InputFile->FileImage == NULL ||\r
+      InputFile->Eof == NULL ||\r
+      InputFile->CurrentFilePointer == NULL ||\r
+      Section == NULL ||\r
+      strlen (Section) == 0 ||\r
+      Value == NULL\r
+      ) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  //\r
+  // Initialize error codes\r
+  //\r
+  ParseError  = FALSE;\r
+  ReadError   = FALSE;\r
+\r
+  //\r
+  // Initialize our instance counter for the search token\r
+  //\r
+  InstanceIndex = 0;\r
+\r
+  if (FindSection (InputFile, Section)) {\r
+    //\r
+    // Found the desired section, find and read the desired token\r
+    //\r
+    do {\r
+      //\r
+      // Read a line from the file\r
+      //\r
+      if (ReadLine (InputFile, InputBuffer, _MAX_PATH) == NULL) {\r
+        //\r
+        // Error reading from input file\r
+        //\r
+        ReadError = TRUE;\r
+        break;\r
+      }\r
+      //\r
+      // Get the first non-whitespace string\r
+      //\r
+      CurrentToken = strtok (InputBuffer, " \t\n");\r
+      if (CurrentToken == NULL) {\r
+        //\r
+        // Whitespace line found (or comment) so continue\r
+        //\r
+        CurrentToken = InputBuffer;\r
+        continue;\r
+      }\r
+      //\r
+      // Make sure we have not reached the end of the current section\r
+      //\r
+      if (CurrentToken[0] == '[') {\r
+        break;\r
+      }\r
+      //\r
+      // Check if it is the correct instance\r
+      //\r
+      if (Instance == InstanceIndex) {\r
+        //\r
+        // Copy the contents following the =\r
+        //\r
+        CurrentValue = strtok (NULL, "= \t\n");\r
+        if (CurrentValue == NULL) {\r
+          //\r
+          // Nothing found, parsing error\r
+          //\r
+          ParseError = TRUE;\r
+        } else {\r
+          //\r
+          // Copy the current token to the output value\r
+          //\r
+          strcpy (Token, CurrentToken);\r
+          strcpy (Value, CurrentValue);\r
+          return EFI_SUCCESS;\r
+        }\r
+      } else {\r
+        //\r
+        // Increment the occurrance found\r
+        //\r
+        InstanceIndex++;\r
+      }\r
+    } while (\r
+      !ParseError &&\r
+      !ReadError &&\r
+      InputFile->CurrentFilePointer < InputFile->Eof &&\r
+      CurrentToken[0] != '[' &&\r
+      InstanceIndex <= Instance\r
+    );\r
+  }\r
+  //\r
+  // Distinguish between read errors and INF file format errors.\r
+  //\r
+  if (ReadError) {\r
+    return EFI_LOAD_ERROR;\r
+  }\r
+\r
+  if (ParseError) {\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  return EFI_NOT_FOUND;\r
+}\r
+\r
+\r
 EFI_STATUS\r
 StringToGuid (\r
   IN CHAR8      *AsciiGuidBuffer,\r