]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/BasePathLib/BasePathLib.c
Refine comments and two code style.
[mirror_edk2.git] / ShellPkg / Library / BasePathLib / BasePathLib.c
CommitLineData
ab94587a 1/** @file\r
2 Provides interface to path manipulation functions.\r
3\r
4 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
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
18\r
19/**\r
20 Removes the last directory or file entry in a path by changing the last\r
21 L'\' to a CHAR_NULL.\r
22\r
4ff7e37b 23 @param[in, out] Path The 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
43 if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {\r
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
55 Function to clean up paths. \r
56 \r
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
61 This will be done inline and the existing buffer may be larger than required \r
62 upon completion.\r
63\r
64 @param[in] Path The pointer to the string containing the path.\r
65\r
66 @retval NULL An error occured.\r
67 @return Path in all other instances.\r
68**/\r
69CHAR16*\r
70EFIAPI\r
71PathCleanUpDirectories(\r
72 IN CHAR16 *Path\r
73 )\r
74{\r
75 CHAR16 *TempString;\r
76 UINTN TempSize;\r
77 if (Path==NULL) {\r
78 return(NULL);\r
79 }\r
80\r
81 //\r
5ddca3b9 82 // Fix up the '/' vs '\'\r
ab94587a 83 //\r
84 for (TempString = Path ; TempString != NULL && *TempString != CHAR_NULL ; TempString++) {\r
85 if (*TempString == L'/') {\r
86 *TempString = L'\\';\r
87 }\r
88 }\r
89\r
90 //\r
91 // Fix up the ..\r
92 //\r
93 while ((TempString = StrStr(Path, L"\\..\\")) != NULL) {\r
94 *TempString = CHAR_NULL;\r
95 TempString += 4;\r
96 PathRemoveLastItem(Path);\r
97 TempSize = StrSize(TempString);\r
98 CopyMem(Path+StrLen(Path), TempString, TempSize);\r
99 }\r
100 if ((TempString = StrStr(Path, L"\\..")) != NULL && *(TempString + 3) == CHAR_NULL) {\r
101 *TempString = CHAR_NULL;\r
102 PathRemoveLastItem(Path);\r
103 }\r
104\r
105 //\r
106 // Fix up the .\r
107 //\r
108 while ((TempString = StrStr(Path, L"\\.\\")) != NULL) {\r
109 *TempString = CHAR_NULL;\r
110 TempString += 2;\r
111 TempSize = StrSize(TempString);\r
112 CopyMem(Path+StrLen(Path), TempString, TempSize);\r
113 }\r
114 if ((TempString = StrStr(Path, L"\\.")) != NULL && *(TempString + 2) == CHAR_NULL) {\r
115 *TempString = CHAR_NULL;\r
116 }\r
117\r
118\r
119\r
120 return (Path);\r
121}\r
122\r