]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Common/String.py
BaseTools: skip updating temporary variable.
[mirror_edk2.git] / BaseTools / Source / Python / Common / String.py
index 068a63d1c124cda1e67dbc8270244c3b0cb60bac..5dc5b85dc5a459fb03358fd9ff07571cdc1de3b1 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 # This file is used to define common string related functions used in parsing process\r
 #\r
-# Copyright (c) 2007 - 2008, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2007 - 2015, 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
 #\r
 import re\r
 import DataType\r
-import os.path\r
+import Common.LongFilePathOs as os\r
 import string\r
 import EdkLogger as EdkLogger\r
 \r
 import GlobalData\r
 from BuildToolError import *\r
 from CommonDataClass.Exceptions import *\r
+from Common.LongFilePathSupport import OpenLongFilePath as open\r
+from Common.MultipleWorkspace import MultipleWorkspace as mws\r
 \r
 gHexVerPatt = re.compile('0x[a-f0-9]{4}[a-f0-9]{4}$', re.IGNORECASE)\r
 gHumanReadableVerPatt = re.compile(r'([1-9][0-9]*|0)\.[0-9]{1,2}$')\r
@@ -43,25 +45,36 @@ def GetSplitValueList(String, SplitTag=DataType.TAB_VALUE_SPLIT, MaxSplit= -1):
     ValueList = []\r
     Last = 0\r
     Escaped = False\r
-    InString = False\r
+    InSingleQuoteString = False\r
+    InDoubleQuoteString = False\r
+    InParenthesis = 0\r
     for Index in range(0, len(String)):\r
         Char = String[Index]\r
 \r
         if not Escaped:\r
             # Found a splitter not in a string, split it\r
-            if not InString and Char == SplitTag:\r
+            if (not InSingleQuoteString or not InDoubleQuoteString) and InParenthesis == 0 and Char == SplitTag:\r
                 ValueList.append(String[Last:Index].strip())\r
                 Last = Index + 1\r
                 if MaxSplit > 0 and len(ValueList) >= MaxSplit:\r
                     break\r
 \r
-            if Char == '\\' and InString:\r
+            if Char == '\\' and (InSingleQuoteString or InDoubleQuoteString):\r
                 Escaped = True\r
-            elif Char == '"':\r
-                if not InString:\r
-                    InString = True\r
+            elif Char == '"' and not InSingleQuoteString:\r
+                if not InDoubleQuoteString:\r
+                    InDoubleQuoteString = True\r
                 else:\r
-                    InString = False\r
+                    InDoubleQuoteString = False\r
+            elif Char == "'" and not InDoubleQuoteString:\r
+                if not InSingleQuoteString:\r
+                    InSingleQuoteString = True\r
+                else:\r
+                    InSingleQuoteString = False\r
+            elif Char == '(':\r
+                InParenthesis = InParenthesis + 1\r
+            elif Char == ')':\r
+                InParenthesis = InParenthesis - 1\r
         else:\r
             Escaped = False\r
 \r
@@ -271,7 +284,8 @@ def ReplaceMacro(String, MacroDefinitions={}, SelfReplacement=False, RaiseError=
                 if SelfReplacement:\r
                     String = String.replace("$(%s)" % Macro, '')\r
                 continue\r
-            String = String.replace("$(%s)" % Macro, MacroDefinitions[Macro])\r
+            if "$(%s)" % Macro not in MacroDefinitions[Macro]:\r
+                String = String.replace("$(%s)" % Macro, MacroDefinitions[Macro])\r
         # in case there's macro not defined\r
         if String == LastString:\r
             break\r
@@ -303,6 +317,11 @@ def NormPath(Path, Defines={}):
         # To local path format\r
         #\r
         Path = os.path.normpath(Path)\r
+        if Path.startswith(GlobalData.gWorkspace) and not Path.startswith(GlobalData.gBuildDirectory) and not os.path.exists(Path):\r
+            Path = Path[len (GlobalData.gWorkspace):]\r
+            if Path[0] == os.path.sep:\r
+                Path = Path[1:]\r
+            Path = mws.join(GlobalData.gWorkspace, Path)\r
 \r
     if IsRelativePath and Path[0] != '.':\r
         Path = os.path.join('.', Path)\r
@@ -332,14 +351,17 @@ def CleanString(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyle
     #\r
     # remove comments, but we should escape comment character in string\r
     #\r
-    InString = False\r
+    InDoubleQuoteString = False\r
+    InSingleQuoteString = False\r
     CommentInString = False\r
     for Index in range(0, len(Line)):\r
-        if Line[Index] == '"':\r
-            InString = not InString\r
-        elif Line[Index] == CommentCharacter and InString :\r
+        if Line[Index] == '"' and not InSingleQuoteString:\r
+            InDoubleQuoteString = not InDoubleQuoteString\r
+        elif Line[Index] == "'" and not InDoubleQuoteString:\r
+            InSingleQuoteString = not InSingleQuoteString\r
+        elif Line[Index] == CommentCharacter and (InSingleQuoteString or InDoubleQuoteString):\r
             CommentInString = True\r
-        elif Line[Index] == CommentCharacter and not InString :\r
+        elif Line[Index] == CommentCharacter and not (InSingleQuoteString or InDoubleQuoteString):\r
             Line = Line[0: Index]\r
             break\r
 \r
@@ -368,7 +390,7 @@ def CleanString(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyle
 \r
 ## CleanString2\r
 #\r
-# Split comments in a string\r
+# Split statement with comments in a string\r
 # Remove spaces\r
 #\r
 # @param Line:              The string to be cleaned\r
@@ -387,26 +409,23 @@ def CleanString2(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyl
     if AllowCppStyleComment:\r
         Line = Line.replace(DataType.TAB_COMMENT_EDK_SPLIT, CommentCharacter)\r
     #\r
-    # separate comments and statements\r
-    #\r
-    LineParts = Line.split(CommentCharacter, 1);\r
-    #\r
-    # remove whitespace again\r
+    # separate comments and statements, but we should escape comment character in string\r
     #\r
-    Line = LineParts[0].strip();\r
-    if len(LineParts) > 1:\r
-        Comment = LineParts[1].strip()\r
-        # Remove prefixed and trailing comment characters\r
-        Start = 0\r
-        End = len(Comment)\r
-        while Start < End and Comment.startswith(CommentCharacter, Start, End):\r
-            Start += 1\r
-        while End >= 0 and Comment.endswith(CommentCharacter, Start, End):\r
-            End -= 1\r
-        Comment = Comment[Start:End]\r
-        Comment = Comment.strip()\r
-    else:\r
-        Comment = ''\r
+    InDoubleQuoteString = False\r
+    InSingleQuoteString = False\r
+    CommentInString = False\r
+    Comment = ''\r
+    for Index in range(0, len(Line)):\r
+        if Line[Index] == '"' and not InSingleQuoteString:\r
+            InDoubleQuoteString = not InDoubleQuoteString\r
+        elif Line[Index] == "'" and not InDoubleQuoteString:\r
+            InSingleQuoteString = not InSingleQuoteString\r
+        elif Line[Index] == CommentCharacter and (InDoubleQuoteString or InSingleQuoteString):\r
+            CommentInString = True\r
+        elif Line[Index] == CommentCharacter and not (InDoubleQuoteString or InSingleQuoteString):\r
+            Comment = Line[Index:].strip()\r
+            Line = Line[0:Index].strip()\r
+            break\r
 \r
     return Line, Comment\r
 \r
@@ -615,7 +634,7 @@ def PreCheck(FileName, FileContent, SupSectionTag):
 # @retval True The file type is correct\r
 #\r
 def CheckFileType(CheckFilename, ExtName, ContainerFilename, SectionName, Line, LineNo= -1):\r
-    if CheckFilename != '' and CheckFilename != None:\r
+    if CheckFilename != '' and CheckFilename is not None:\r
         (Root, Ext) = os.path.splitext(CheckFilename)\r
         if Ext.upper() != ExtName.upper():\r
             ContainerFile = open(ContainerFilename, 'r').read()\r
@@ -643,7 +662,7 @@ def CheckFileType(CheckFilename, ExtName, ContainerFilename, SectionName, Line,
 #\r
 def CheckFileExist(WorkspaceDir, CheckFilename, ContainerFilename, SectionName, Line, LineNo= -1):\r
     CheckFile = ''\r
-    if CheckFilename != '' and CheckFilename != None:\r
+    if CheckFilename != '' and CheckFilename is not None:\r
         CheckFile = WorkspaceFile(WorkspaceDir, CheckFilename)\r
         if not os.path.isfile(CheckFile):\r
             ContainerFile = open(ContainerFilename, 'r').read()\r
@@ -706,7 +725,7 @@ def RaiseParserError(Line, Section, File, Format='', LineNo= -1):
 # @retval string A full path\r
 #\r
 def WorkspaceFile(WorkspaceDir, Filename):\r
-    return os.path.join(NormPath(WorkspaceDir), NormPath(Filename))\r
+    return mws.join(NormPath(WorkspaceDir), NormPath(Filename))\r
 \r
 ## Split string\r
 #\r
@@ -798,20 +817,34 @@ def GetHelpTextList(HelpTextClassList):
 def StringToArray(String):\r
     if isinstance(String, unicode):\r
         if len(unicode) == 0:\r
-            return "{0x00, 0x00}"\r
-        return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C) for C in String])\r
+            return "{0x00,0x00}"\r
+        return "{%s,0x00,0x00}" % ",".join(["0x%02x,0x00" % ord(C) for C in String])\r
     elif String.startswith('L"'):\r
         if String == "L\"\"":\r
-            return "{0x00, 0x00}"\r
+            return "{0x00,0x00}"\r
         else:\r
-            return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C) for C in String[2:-1]])\r
+            return "{%s,0x00,0x00}" % ",".join(["0x%02x,0x00" % ord(C) for C in String[2:-1]])\r
     elif String.startswith('"'):\r
         if String == "\"\"":\r
-            return "{0x00}";\r
+            return "{0x00,0x00}"\r
         else:\r
-            return "{%s, 0x00}" % ", ".join(["0x%02x" % ord(C) for C in String[1:-1]])\r
+            StringLen = len(String[1:-1])\r
+            if StringLen % 2:\r
+                return "{%s,0x00}" % ",".join(["0x%02x" % ord(C) for C in String[1:-1]])\r
+            else:\r
+                return "{%s,0x00,0x00}" % ",".join(["0x%02x" % ord(C) for C in String[1:-1]])\r
+    elif String.startswith('{'):\r
+        StringLen = len(String.split(","))\r
+        if StringLen % 2:\r
+            return "{%s,0x00}" % ",".join([ C.strip() for C in String[1:-1].split(',')])\r
+        else:\r
+            return "{%s}" % ",".join([ C.strip() for C in String[1:-1].split(',')])\r
+        \r
     else:\r
-        return '{%s, 0}' % ', '.join(String.split())\r
+        if len(String.split()) % 2:\r
+            return '{%s,0}' % ','.join(String.split())\r
+        else:\r
+            return '{%s,0,0}' % ','.join(String.split())\r
 \r
 def StringArrayLength(String):\r
     if isinstance(String, unicode):\r