]>
Commit | Line | Data |
---|---|---|
ab94587a | 1 | /** @file\r |
2 | Provides interface to path manipulation functions.\r | |
3 | \r | |
e8c737ae | 4 | Copyright (c) 2011 - 2014, 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 | |
13 | \r | |
14 | #include <Base.h>\r | |
15 | #include <Library/BaseMemoryLib.h>\r | |
16 | #include <Library/PathLib.h>\r | |
17 | #include <Library/BaseLib.h>\r | |
e8c737ae | 18 | #include <Protocol/SimpleTextIn.h> \r |
ab94587a | 19 | \r |
20 | /**\r | |
21 | Removes the last directory or file entry in a path by changing the last\r | |
22 | L'\' to a CHAR_NULL.\r | |
23 | \r | |
4ff7e37b | 24 | @param[in, out] Path The pointer to the path to modify.\r |
ab94587a | 25 | \r |
26 | @retval FALSE Nothing was found to remove.\r | |
27 | @retval TRUE A directory or file was removed.\r | |
28 | **/\r | |
29 | BOOLEAN\r | |
30 | EFIAPI\r | |
31 | PathRemoveLastItem(\r | |
32 | IN OUT CHAR16 *Path\r | |
33 | )\r | |
34 | {\r | |
35 | CHAR16 *Walker;\r | |
36 | CHAR16 *LastSlash;\r | |
37 | //\r | |
38 | // get directory name from path... ('chop' off extra)\r | |
39 | //\r | |
40 | for ( Walker = Path, LastSlash = NULL\r | |
41 | ; Walker != NULL && *Walker != CHAR_NULL\r | |
42 | ; Walker++\r | |
43 | ){\r | |
44 | if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {\r | |
45 | LastSlash = Walker+1;\r | |
46 | }\r | |
47 | }\r | |
48 | if (LastSlash != NULL) {\r | |
49 | *LastSlash = CHAR_NULL;\r | |
50 | return (TRUE);\r | |
51 | }\r | |
52 | return (FALSE);\r | |
53 | }\r | |
54 | \r | |
55 | /**\r | |
56 | Function to clean up paths. \r | |
57 | \r | |
58 | - Single periods in the path are removed.\r | |
59 | - Double periods in the path are removed along with a single parent directory.\r | |
60 | - Forward slashes L'/' are converted to backward slashes L'\'.\r | |
61 | \r | |
62 | This will be done inline and the existing buffer may be larger than required \r | |
63 | upon completion.\r | |
64 | \r | |
65 | @param[in] Path The pointer to the string containing the path.\r | |
66 | \r | |
67 | @retval NULL An error occured.\r | |
68 | @return Path in all other instances.\r | |
69 | **/\r | |
70 | CHAR16*\r | |
71 | EFIAPI\r | |
72 | PathCleanUpDirectories(\r | |
73 | IN CHAR16 *Path\r | |
74 | )\r | |
75 | {\r | |
76 | CHAR16 *TempString;\r | |
77 | UINTN TempSize;\r | |
78 | if (Path==NULL) {\r | |
79 | return(NULL);\r | |
80 | }\r | |
81 | \r | |
82 | //\r | |
5ddca3b9 | 83 | // Fix up the '/' vs '\'\r |
ab94587a | 84 | //\r |
85 | for (TempString = Path ; TempString != NULL && *TempString != CHAR_NULL ; TempString++) {\r | |
86 | if (*TempString == L'/') {\r | |
87 | *TempString = L'\\';\r | |
88 | }\r | |
89 | }\r | |
90 | \r | |
91 | //\r | |
92 | // Fix up the ..\r | |
93 | //\r | |
94 | while ((TempString = StrStr(Path, L"\\..\\")) != NULL) {\r | |
95 | *TempString = CHAR_NULL;\r | |
96 | TempString += 4;\r | |
97 | PathRemoveLastItem(Path);\r | |
98 | TempSize = StrSize(TempString);\r | |
99 | CopyMem(Path+StrLen(Path), TempString, TempSize);\r | |
100 | }\r | |
101 | if ((TempString = StrStr(Path, L"\\..")) != NULL && *(TempString + 3) == CHAR_NULL) {\r | |
102 | *TempString = CHAR_NULL;\r | |
103 | PathRemoveLastItem(Path);\r | |
104 | }\r | |
105 | \r | |
106 | //\r | |
107 | // Fix up the .\r | |
108 | //\r | |
109 | while ((TempString = StrStr(Path, L"\\.\\")) != NULL) {\r | |
110 | *TempString = CHAR_NULL;\r | |
111 | TempString += 2;\r | |
112 | TempSize = StrSize(TempString);\r | |
113 | CopyMem(Path+StrLen(Path), TempString, TempSize);\r | |
114 | }\r | |
115 | if ((TempString = StrStr(Path, L"\\.")) != NULL && *(TempString + 2) == CHAR_NULL) {\r | |
1fc3749d | 116 | *(TempString + 1) = CHAR_NULL;\r |
ab94587a | 117 | }\r |
118 | \r | |
bbf904d1 JC |
119 | while ((TempString = StrStr(Path, L"\\\\")) != NULL) {\r |
120 | *TempString = CHAR_NULL;\r | |
121 | TempString += 1;\r | |
122 | TempSize = StrSize(TempString);\r | |
123 | CopyMem(Path+StrLen(Path), TempString, TempSize);\r | |
124 | }\r | |
125 | if ((TempString = StrStr(Path, L"\\\\")) != NULL && *(TempString + 1) == CHAR_NULL) {\r | |
126 | *(TempString) = CHAR_NULL;\r | |
127 | }\r | |
ab94587a | 128 | \r |
129 | return (Path);\r | |
130 | }\r | |
131 | \r |