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