]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/FilePaths.c
MdePkg/BaseLib: Support IA32 processors without CLFLUSH
[mirror_edk2.git] / MdePkg / Library / BaseLib / FilePaths.c
CommitLineData
ab94587a 1/** @file\r
ae591c14 2 Defines file-path manipulation functions.\r
ab94587a 3\r
e8c737ae 4 Copyright (c) 2011 - 2014, 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 <Uefi/UefiBaseType.h>\r
14#include <Library/BaseMemoryLib.h>\r
15#include <Library/BaseLib.h>\r
16#include <Protocol/SimpleTextIn.h>\r
ab94587a 17\r
18/**\r
19 Removes the last directory or file entry in a path by changing the last\r
20 L'\' to a CHAR_NULL.\r
21\r
ae591c14 22 @param[in,out] Path A pointer to the path to modify.\r
ab94587a 23\r
24 @retval FALSE Nothing was found to remove.\r
25 @retval TRUE A directory or file was removed.\r
26**/\r
27BOOLEAN\r
28EFIAPI\r
29PathRemoveLastItem(\r
30 IN OUT CHAR16 *Path\r
31 )\r
32{\r
33 CHAR16 *Walker;\r
34 CHAR16 *LastSlash;\r
35 //\r
36 // get directory name from path... ('chop' off extra)\r
37 //\r
38 for ( Walker = Path, LastSlash = NULL\r
39 ; Walker != NULL && *Walker != CHAR_NULL\r
40 ; Walker++\r
41 ){\r
42 if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {\r
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
71 )\r
72{\r
73 CHAR16 *TempString;\r
74 UINTN TempSize;\r
ae591c14 75\r
ab94587a 76 if (Path==NULL) {\r
77 return(NULL);\r
78 }\r
ab94587a 79 //\r
5ddca3b9 80 // Fix up the '/' vs '\'\r
ab94587a 81 //\r
82 for (TempString = Path ; TempString != NULL && *TempString != CHAR_NULL ; TempString++) {\r
83 if (*TempString == L'/') {\r
84 *TempString = L'\\';\r
85 }\r
86 }\r
ab94587a 87 //\r
88 // Fix up the ..\r
89 //\r
90 while ((TempString = StrStr(Path, L"\\..\\")) != NULL) {\r
91 *TempString = CHAR_NULL;\r
92 TempString += 4;\r
93 PathRemoveLastItem(Path);\r
94 TempSize = StrSize(TempString);\r
95 CopyMem(Path+StrLen(Path), TempString, TempSize);\r
96 }\r
97 if ((TempString = StrStr(Path, L"\\..")) != NULL && *(TempString + 3) == CHAR_NULL) {\r
98 *TempString = CHAR_NULL;\r
99 PathRemoveLastItem(Path);\r
100 }\r
ab94587a 101 //\r
102 // Fix up the .\r
103 //\r
104 while ((TempString = StrStr(Path, L"\\.\\")) != NULL) {\r
105 *TempString = CHAR_NULL;\r
106 TempString += 2;\r
107 TempSize = StrSize(TempString);\r
108 CopyMem(Path+StrLen(Path), TempString, TempSize);\r
109 }\r
110 if ((TempString = StrStr(Path, L"\\.")) != NULL && *(TempString + 2) == CHAR_NULL) {\r
1fc3749d 111 *(TempString + 1) = CHAR_NULL;\r
ab94587a 112 }\r
113\r
bbf904d1
JC
114 while ((TempString = StrStr(Path, L"\\\\")) != NULL) {\r
115 *TempString = CHAR_NULL;\r
116 TempString += 1;\r
117 TempSize = StrSize(TempString);\r
118 CopyMem(Path+StrLen(Path), TempString, TempSize);\r
119 }\r
120 if ((TempString = StrStr(Path, L"\\\\")) != NULL && *(TempString + 1) == CHAR_NULL) {\r
121 *(TempString) = CHAR_NULL;\r
122 }\r
ab94587a 123\r
124 return (Path);\r
125}\r
126\r