]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/FilePaths.c
MdePkg: Enhance PathRemoveLastItem() to support "FS0:File.txt"
[mirror_edk2.git] / MdePkg / Library / BaseLib / FilePaths.c
CommitLineData
ab94587a 1/** @file\r
ae591c14 2 Defines file-path manipulation functions.\r
ab94587a 3\r
85df6124 4 Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>\r
ab94587a 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12**/\r
ae591c14
DM
13#include <Uefi/UefiBaseType.h>\r
14#include <Library/BaseMemoryLib.h>\r
15#include <Library/BaseLib.h>\r
16#include <Protocol/SimpleTextIn.h>\r
ab94587a 17\r
18/**\r
19 Removes the last directory or file entry in a path by changing the last\r
54b1e0ec
RN
20 L'\' to a CHAR_NULL. For a path which is like L"fs0:startup.nsh",\r
21 it's converted to L"fs0:".\r
ab94587a 22\r
ae591c14 23 @param[in,out] Path A pointer to the path to modify.\r
ab94587a 24\r
25 @retval FALSE Nothing was found to remove.\r
26 @retval TRUE A directory or file was removed.\r
27**/\r
28BOOLEAN\r
29EFIAPI\r
30PathRemoveLastItem(\r
31 IN OUT CHAR16 *Path\r
32 )\r
33{\r
34 CHAR16 *Walker;\r
35 CHAR16 *LastSlash;\r
36 //\r
37 // get directory name from path... ('chop' off extra)\r
38 //\r
39 for ( Walker = Path, LastSlash = NULL\r
40 ; Walker != NULL && *Walker != CHAR_NULL\r
41 ; Walker++\r
42 ){\r
54b1e0ec 43 if ((*Walker == L'\\' || *Walker == 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
72 )\r
73{\r
74 CHAR16 *TempString;\r
75 UINTN TempSize;\r
ae591c14 76\r
ab94587a 77 if (Path==NULL) {\r
78 return(NULL);\r
79 }\r
ab94587a 80 //\r
5ddca3b9 81 // Fix up the '/' vs '\'\r
ab94587a 82 //\r
83 for (TempString = Path ; TempString != NULL && *TempString != CHAR_NULL ; TempString++) {\r
84 if (*TempString == L'/') {\r
85 *TempString = L'\\';\r
86 }\r
87 }\r
ab94587a 88 //\r
89 // Fix up the ..\r
90 //\r
91 while ((TempString = StrStr(Path, L"\\..\\")) != NULL) {\r
92 *TempString = CHAR_NULL;\r
93 TempString += 4;\r
94 PathRemoveLastItem(Path);\r
95 TempSize = StrSize(TempString);\r
96 CopyMem(Path+StrLen(Path), TempString, TempSize);\r
97 }\r
98 if ((TempString = StrStr(Path, L"\\..")) != NULL && *(TempString + 3) == CHAR_NULL) {\r
99 *TempString = CHAR_NULL;\r
85df6124
QS
100 if (!PathRemoveLastItem(Path)) {\r
101 *TempString = L'\\';\r
102 }\r
ab94587a 103 }\r
ab94587a 104 //\r
105 // Fix up the .\r
106 //\r
107 while ((TempString = StrStr(Path, L"\\.\\")) != NULL) {\r
108 *TempString = CHAR_NULL;\r
109 TempString += 2;\r
110 TempSize = StrSize(TempString);\r
111 CopyMem(Path+StrLen(Path), TempString, TempSize);\r
112 }\r
113 if ((TempString = StrStr(Path, L"\\.")) != NULL && *(TempString + 2) == CHAR_NULL) {\r
1fc3749d 114 *(TempString + 1) = CHAR_NULL;\r
ab94587a 115 }\r
116\r
bbf904d1
JC
117 while ((TempString = StrStr(Path, L"\\\\")) != NULL) {\r
118 *TempString = CHAR_NULL;\r
119 TempString += 1;\r
120 TempSize = StrSize(TempString);\r
121 CopyMem(Path+StrLen(Path), TempString, TempSize);\r
122 }\r
123 if ((TempString = StrStr(Path, L"\\\\")) != NULL && *(TempString + 1) == CHAR_NULL) {\r
124 *(TempString) = CHAR_NULL;\r
125 }\r
ab94587a 126\r
127 return (Path);\r
128}\r
129\r