]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Common/OsPath.c
2 Functions useful to operate file directories by parsing file path.
4 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include "CommonLib.h"
16 // Functions implementations
21 // BUGBUG: Not fully implemented yet.
31 This function returns the directory path which contains the particular path.
39 This function does not check for the existence of the file.
41 The caller must free the string returned.
45 FilePath Path name of file to get the parent directory for.
59 Length
= strlen (FilePath
);
66 // Check for the root directory case
69 (Length
== 3 && isalpha (FilePath
[0]) && (strcmp(FilePath
+ 1, ":\\") == 0)) ||
70 (strcmp(FilePath
, "/") == 0)
76 // If the path ends with a path separator, then just append ".."
78 Char
= FilePath
[Length
- 1];
79 if (Char
== '/' || Char
== '\\') {
80 return OsPathJoin (FilePath
, "..");
86 for (Offset
= Length
; Offset
> 0; Offset
--) {
87 if ((Return
[Offset
] == '/') || (Return
[Offset
] == '\\')) {
88 Return
[Offset
] = '\0';
98 // BUGBUG: Not fully implemented yet.
101 OsPathNormPathInPlace (
108 This function returns the directory path which contains the particular path.
114 This function does not check for the existence of the file.
118 Path Path name of file to normalize
122 The string is altered in place.
135 Length
= strlen (Path
);
137 for (Offset
= 0; Offset
< Length
; Offset
++) {
138 Remaining
= Length
- Offset
;
141 // Collapse '//' -> '/'
145 ((Offset
> 0) || (Path
[0] != '\\')) &&
146 IsDirSep (Path
[Offset
]) && IsDirSep (Path
[Offset
+ 1])
148 memmove (&Path
[Offset
], &Path
[Offset
+ 1], Remaining
);
154 // Collapse '/./' -> '/'
156 if ((Remaining
>= 3) && IsDirSep (Path
[Offset
]) &&
157 (Path
[Offset
+ 1] == '.') && IsDirSep (Path
[Offset
+ 2])
159 memmove (&Path
[Offset
], &Path
[Offset
+ 1], Remaining
);
165 // Collapse 'a/../b' -> 'b'
167 // BUGBUG: Not implemented yet
173 Return
= CloneString (FilePath
);
174 if (Return
== NULL
) {
178 Length
= strlen (Return
);
181 // Check for the root directory case
184 (Length
== 3 && isalpha (Return
[0]) && (strcmp(Return
+ 1, ":\\") == 0)) ||
185 (strcmp(Return
, "/") == 0)
194 for (Offset
= Length
; Offset
> 0; Offset
--) {
195 if ((Return
[Offset
] == '/') || (Return
[Offset
] == '\\')) {
196 Return
[Offset
] = '\0';
213 This function replaces the final portion of a path with an alternative
214 'peer' filename. For example:
215 "a/b/../c", "peer" -> "a/b/../peer"
216 "a/b/", "peer" -> "a/b/peer"
217 "/a", "peer" -> "/peer"
218 "a", "peer" -> "peer"
220 This function does not check for the existence of the file.
224 OldPath Path name of replace the final segment
225 Peer The new path name to concatenate to become the peer path
229 A CHAR8* string, which must be freed by the caller
236 Result
= (CHAR8
*) malloc (strlen (OldPath
) + strlen (Peer
) + 1);
237 if (Result
== NULL
) {
241 strcpy (Result
, OldPath
);
244 // Search for the last '/' or '\' in the string. If found, replace
245 // everything following it with Peer
247 for (Offset
= strlen (Result
); Offset
>= 0; Offset
--) {
248 if ((Result
[Offset
] == '/') || (Result
[Offset
] == '\\')) {
249 Result
[Offset
+ 1] = '\0';
250 strcat (Result
, Peer
);
256 // Neither a '/' nor a '\' was found. Therefore, we simply return Peer.
258 strcpy (Result
, Peer
);
265 IN CHAR8
*InputFileName
271 Checks if a file exists
275 InputFileName The name of the file to check for existence
280 FALSE The file does not exist
285 InputFile
= fopen (LongFilePath (InputFileName
), "rb");
286 if (InputFile
== NULL
) {