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