]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/BasePathLib/BasePathLib.c
This refactors 3 functions out of ShellCommandLib and puts them into a new library...
[mirror_edk2.git] / ShellPkg / Library / BasePathLib / BasePathLib.c
diff --git a/ShellPkg/Library/BasePathLib/BasePathLib.c b/ShellPkg/Library/BasePathLib/BasePathLib.c
new file mode 100644 (file)
index 0000000..53a2a09
--- /dev/null
@@ -0,0 +1,122 @@
+/** @file\r
+  Provides interface to path manipulation functions.\r
+\r
+  Copyright (c) 2011, 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
+  http://opensource.org/licenses/bsd-license.php.\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+**/\r
+\r
+#include <Base.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/PathLib.h>\r
+#include <Library/BaseLib.h>\r
+\r
+/**\r
+  Removes the last directory or file entry in a path by changing the last\r
+  L'\' to a CHAR_NULL.\r
+\r
+  @param[in,out] Path    The pointer to the path to modify.\r
+\r
+  @retval FALSE     Nothing was found to remove.\r
+  @retval TRUE      A directory or file was removed.\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+PathRemoveLastItem(\r
+  IN OUT CHAR16 *Path\r
+  )\r
+{\r
+  CHAR16        *Walker;\r
+  CHAR16        *LastSlash;\r
+  //\r
+  // get directory name from path... ('chop' off extra)\r
+  //\r
+  for ( Walker = Path, LastSlash = NULL\r
+      ; Walker != NULL && *Walker != CHAR_NULL\r
+      ; Walker++\r
+     ){\r
+    if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {\r
+      LastSlash = Walker+1;\r
+    }\r
+  }\r
+  if (LastSlash != NULL) {\r
+    *LastSlash = CHAR_NULL;\r
+    return (TRUE);\r
+  }\r
+  return (FALSE);\r
+}\r
+\r
+/**\r
+  Function to clean up paths.  \r
+  \r
+  - Single periods in the path are removed.\r
+  - Double periods in the path are removed along with a single parent directory.\r
+  - Forward slashes L'/' are converted to backward slashes L'\'.\r
+\r
+  This will be done inline and the existing buffer may be larger than required \r
+  upon completion.\r
+\r
+  @param[in] Path       The pointer to the string containing the path.\r
+\r
+  @retval NULL          An error occured.\r
+  @return Path in all other instances.\r
+**/\r
+CHAR16*\r
+EFIAPI\r
+PathCleanUpDirectories(\r
+  IN CHAR16 *Path\r
+  )\r
+{\r
+  CHAR16  *TempString;\r
+  UINTN   TempSize;\r
+  if (Path==NULL) {\r
+    return(NULL);\r
+  }\r
+\r
+  //\r
+  // Fix up the / vs \\r
+  //\r
+  for (TempString = Path ; TempString != NULL && *TempString != CHAR_NULL ; TempString++) {\r
+    if (*TempString == L'/') {\r
+      *TempString = L'\\';\r
+    }\r
+  }\r
+\r
+  //\r
+  // Fix up the ..\r
+  //\r
+  while ((TempString = StrStr(Path, L"\\..\\")) != NULL) {\r
+    *TempString = CHAR_NULL;\r
+    TempString  += 4;\r
+    PathRemoveLastItem(Path);\r
+    TempSize = StrSize(TempString);\r
+    CopyMem(Path+StrLen(Path), TempString, TempSize);\r
+  }\r
+  if ((TempString = StrStr(Path, L"\\..")) != NULL && *(TempString + 3) == CHAR_NULL) {\r
+    *TempString = CHAR_NULL;\r
+    PathRemoveLastItem(Path);\r
+  }\r
+\r
+  //\r
+  // Fix up the .\r
+  //\r
+  while ((TempString = StrStr(Path, L"\\.\\")) != NULL) {\r
+    *TempString = CHAR_NULL;\r
+    TempString  += 2;\r
+    TempSize = StrSize(TempString);\r
+    CopyMem(Path+StrLen(Path), TempString, TempSize);\r
+  }\r
+  if ((TempString = StrStr(Path, L"\\.")) != NULL && *(TempString + 2) == CHAR_NULL) {\r
+    *TempString = CHAR_NULL;\r
+  }\r
+\r
+\r
+\r
+  return (Path);\r
+}\r
+\r