]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/FilePaths.c
MdePkg/DxeRuntimeDebugLibSerialPort: Add new APIs
[mirror_edk2.git] / MdePkg / Library / BaseLib / FilePaths.c
CommitLineData
ab94587a 1/** @file\r
ae591c14 2 Defines file-path manipulation functions.\r
ab94587a 3\r
9fdf3178 4 Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>\r
7177be0b 5 Copyright (c) 2018, Dell Technologies. All rights reserved.<BR>\r
ab94587a 6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php.\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13**/\r
ae591c14
DM
14#include <Library/BaseMemoryLib.h>\r
15#include <Library/BaseLib.h>\r
ab94587a 16\r
17/**\r
6a623094
HW
18 Removes the last directory or file entry in a path. For a path which is\r
19 like L"fs0:startup.nsh", it's converted to L"fs0:".\r
ab94587a 20\r
ae591c14 21 @param[in,out] Path A pointer to the path to modify.\r
ab94587a 22\r
23 @retval FALSE Nothing was found to remove.\r
24 @retval TRUE A directory or file was removed.\r
25**/\r
26BOOLEAN\r
27EFIAPI\r
28PathRemoveLastItem(\r
29 IN OUT CHAR16 *Path\r
30 )\r
31{\r
32 CHAR16 *Walker;\r
33 CHAR16 *LastSlash;\r
34 //\r
35 // get directory name from path... ('chop' off extra)\r
36 //\r
37 for ( Walker = Path, LastSlash = NULL\r
38 ; Walker != NULL && *Walker != CHAR_NULL\r
39 ; Walker++\r
40 ){\r
6a623094
HW
41 if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {\r
42 LastSlash = Walker+1;\r
43 } else if (*Walker == L':' && *(Walker + 1) != L'\\' && *(Walker + 1) != CHAR_NULL) {\r
ab94587a 44 LastSlash = Walker+1;\r
45 }\r
46 }\r
47 if (LastSlash != NULL) {\r
48 *LastSlash = CHAR_NULL;\r
49 return (TRUE);\r
50 }\r
51 return (FALSE);\r
52}\r
53\r
54/**\r
ae591c14
DM
55 Function to clean up paths.\r
56\r
ab94587a 57 - Single periods in the path are removed.\r
58 - Double periods in the path are removed along with a single parent directory.\r
59 - Forward slashes L'/' are converted to backward slashes L'\'.\r
60\r
ae591c14 61 This will be done inline and the existing buffer may be larger than required\r
ab94587a 62 upon completion.\r
63\r
64 @param[in] Path The pointer to the string containing the path.\r
65\r
ae591c14 66 @return Returns Path, otherwise returns NULL to indicate that an error has occured.\r
ab94587a 67**/\r
68CHAR16*\r
69EFIAPI\r
70PathCleanUpDirectories(\r
71 IN CHAR16 *Path\r
bb99e328 72)\r
ab94587a 73{\r
74 CHAR16 *TempString;\r
ae591c14 75\r
bb99e328
RN
76 if (Path == NULL) {\r
77 return NULL;\r
ab94587a 78 }\r
bb99e328 79\r
ab94587a 80 //\r
bb99e328 81 // Replace the '/' with '\'\r
ab94587a 82 //\r
bb99e328 83 for (TempString = Path; *TempString != CHAR_NULL; TempString++) {\r
ab94587a 84 if (*TempString == L'/') {\r
85 *TempString = L'\\';\r
86 }\r
87 }\r
bb99e328 88\r
b08b045c
JD
89 //\r
90 // Replace the "\\" with "\"\r
91 //\r
92 while ((TempString = StrStr (Path, L"\\\\")) != NULL) {\r
93 CopyMem (TempString, TempString + 1, StrSize (TempString + 1));\r
94 }\r
95\r
ab94587a 96 //\r
bb99e328 97 // Remove all the "\.". E.g.: fs0:\abc\.\def\.\r
ab94587a 98 //\r
bb99e328
RN
99 while ((TempString = StrStr (Path, L"\\.\\")) != NULL) {\r
100 CopyMem (TempString, TempString + 2, StrSize (TempString + 2));\r
ab94587a 101 }\r
9fdf3178 102 if ((StrLen (Path) >= 2) && (StrCmp (Path + StrLen (Path) - 2, L"\\.") == 0)) {\r
bb99e328 103 Path[StrLen (Path) - 1] = CHAR_NULL;\r
ab94587a 104 }\r
bb99e328 105\r
ab94587a 106 //\r
bb99e328 107 // Remove all the "\..". E.g.: fs0:\abc\..\def\..\r
ab94587a 108 //\r
bb99e328
RN
109 while (((TempString = StrStr(Path, L"\\..")) != NULL) &&\r
110 ((*(TempString + 3) == L'\\') || (*(TempString + 3) == CHAR_NULL))\r
111 ) {\r
1fc3749d 112 *(TempString + 1) = CHAR_NULL;\r
bb99e328 113 PathRemoveLastItem(Path);\r
7177be0b
JD
114 if (*(TempString + 3) != CHAR_NULL) {\r
115 CopyMem (Path + StrLen (Path), TempString + 4, StrSize (TempString + 4));\r
116 }\r
ab94587a 117 }\r
118\r
bb99e328 119 return Path;\r
ab94587a 120}\r
121\r