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