]>
Commit | Line | Data |
---|---|---|
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 <Library/BaseMemoryLib.h>\r |
14 | #include <Library/BaseLib.h>\r | |
ab94587a | 15 | \r |
16 | /**\r | |
17 | Removes the last directory or file entry in a path by changing the last\r | |
54b1e0ec RN |
18 | L'\' to a CHAR_NULL. For a path which is like L"fs0:startup.nsh",\r |
19 | 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 | |
26 | BOOLEAN\r | |
27 | EFIAPI\r | |
28 | PathRemoveLastItem(\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 | |
54b1e0ec | 41 | if ((*Walker == L'\\' || *Walker == L':') && *(Walker + 1) != CHAR_NULL) {\r |
ab94587a | 42 | LastSlash = Walker+1;\r |
43 | }\r | |
44 | }\r | |
45 | if (LastSlash != NULL) {\r | |
46 | *LastSlash = CHAR_NULL;\r | |
47 | return (TRUE);\r | |
48 | }\r | |
49 | return (FALSE);\r | |
50 | }\r | |
51 | \r | |
52 | /**\r | |
ae591c14 DM |
53 | Function to clean up paths.\r |
54 | \r | |
ab94587a | 55 | - Single periods in the path are removed.\r |
56 | - Double periods in the path are removed along with a single parent directory.\r | |
57 | - Forward slashes L'/' are converted to backward slashes L'\'.\r | |
58 | \r | |
ae591c14 | 59 | This will be done inline and the existing buffer may be larger than required\r |
ab94587a | 60 | upon completion.\r |
61 | \r | |
62 | @param[in] Path The pointer to the string containing the path.\r | |
63 | \r | |
ae591c14 | 64 | @return Returns Path, otherwise returns NULL to indicate that an error has occured.\r |
ab94587a | 65 | **/\r |
66 | CHAR16*\r | |
67 | EFIAPI\r | |
68 | PathCleanUpDirectories(\r | |
69 | IN CHAR16 *Path\r | |
70 | )\r | |
71 | {\r | |
72 | CHAR16 *TempString;\r | |
73 | UINTN TempSize;\r | |
ae591c14 | 74 | \r |
ab94587a | 75 | if (Path==NULL) {\r |
76 | return(NULL);\r | |
77 | }\r | |
ab94587a | 78 | //\r |
5ddca3b9 | 79 | // Fix up the '/' vs '\'\r |
ab94587a | 80 | //\r |
81 | for (TempString = Path ; TempString != NULL && *TempString != CHAR_NULL ; TempString++) {\r | |
82 | if (*TempString == L'/') {\r | |
83 | *TempString = L'\\';\r | |
84 | }\r | |
85 | }\r | |
ab94587a | 86 | //\r |
87 | // Fix up the ..\r | |
88 | //\r | |
89 | while ((TempString = StrStr(Path, L"\\..\\")) != NULL) {\r | |
90 | *TempString = CHAR_NULL;\r | |
91 | TempString += 4;\r | |
92 | PathRemoveLastItem(Path);\r | |
93 | TempSize = StrSize(TempString);\r | |
94 | CopyMem(Path+StrLen(Path), TempString, TempSize);\r | |
95 | }\r | |
96 | if ((TempString = StrStr(Path, L"\\..")) != NULL && *(TempString + 3) == CHAR_NULL) {\r | |
97 | *TempString = CHAR_NULL;\r | |
85df6124 QS |
98 | if (!PathRemoveLastItem(Path)) {\r |
99 | *TempString = L'\\';\r | |
100 | }\r | |
ab94587a | 101 | }\r |
ab94587a | 102 | //\r |
103 | // Fix up the .\r | |
104 | //\r | |
105 | while ((TempString = StrStr(Path, L"\\.\\")) != NULL) {\r | |
106 | *TempString = CHAR_NULL;\r | |
107 | TempString += 2;\r | |
108 | TempSize = StrSize(TempString);\r | |
109 | CopyMem(Path+StrLen(Path), TempString, TempSize);\r | |
110 | }\r | |
111 | if ((TempString = StrStr(Path, L"\\.")) != NULL && *(TempString + 2) == CHAR_NULL) {\r | |
1fc3749d | 112 | *(TempString + 1) = CHAR_NULL;\r |
ab94587a | 113 | }\r |
114 | \r | |
bbf904d1 JC |
115 | while ((TempString = StrStr(Path, L"\\\\")) != NULL) {\r |
116 | *TempString = CHAR_NULL;\r | |
117 | TempString += 1;\r | |
118 | TempSize = StrSize(TempString);\r | |
119 | CopyMem(Path+StrLen(Path), TempString, TempSize);\r | |
120 | }\r | |
121 | if ((TempString = StrStr(Path, L"\\\\")) != NULL && *(TempString + 1) == CHAR_NULL) {\r | |
122 | *(TempString) = CHAR_NULL;\r | |
123 | }\r | |
ab94587a | 124 | \r |
125 | return (Path);\r | |
126 | }\r | |
127 | \r |