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