]>
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 - 2014, 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
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.
18 #include "CommonLib.h"
22 // Functions implementations
27 // BUGBUG: Not fully implemented yet.
37 This function returns the directory path which contains the particular path.
45 This function does not check for the existence of the file.
47 The caller must free the string returned.
51 FilePath Path name of file to get the parent directory for.
65 Length
= strlen (FilePath
);
72 // Check for the root directory case
75 (Length
== 3 && isalpha (FilePath
[0]) && (strcmp(FilePath
+ 1, ":\\") == 0)) ||
76 (strcmp(FilePath
, "/") == 0)
82 // If the path ends with a path separator, then just append ".."
84 Char
= FilePath
[Length
- 1];
85 if (Char
== '/' || Char
== '\\') {
86 return OsPathJoin (FilePath
, "..");
92 for (Offset
= Length
; Offset
> 0; Offset
--) {
93 if ((Return
[Offset
] == '/') || (Return
[Offset
] == '\\')) {
94 Return
[Offset
] = '\0';
104 // BUGBUG: Not fully implemented yet.
107 OsPathNormPathInPlace (
114 This function returns the directory path which contains the particular path.
120 This function does not check for the existence of the file.
124 Path Path name of file to normalize
128 The string is altered in place.
141 Length
= strlen (Path
);
143 for (Offset
= 0; Offset
< Length
; Offset
++) {
144 Remaining
= Length
- Offset
;
147 // Collapse '//' -> '/'
151 ((Offset
> 0) || (Path
[0] != '\\')) &&
152 IsDirSep (Path
[Offset
]) && IsDirSep (Path
[Offset
+ 1])
154 memmove (&Path
[Offset
], &Path
[Offset
+ 1], Remaining
);
160 // Collapse '/./' -> '/'
162 if ((Remaining
>= 3) && IsDirSep (Path
[Offset
]) &&
163 (Path
[Offset
+ 1] == '.') && IsDirSep (Path
[Offset
+ 2])
165 memmove (&Path
[Offset
], &Path
[Offset
+ 1], Remaining
);
171 // Collapse 'a/../b' -> 'b'
173 // BUGBUG: Not implemented yet
179 Return
= CloneString (FilePath
);
180 if (Return
== NULL
) {
184 Length
= strlen (Return
);
187 // Check for the root directory case
190 (Length
== 3 && isalpha (Return
[0]) && (strcmp(Return
+ 1, ":\\") == 0)) ||
191 (strcmp(Return
, "/") == 0)
200 for (Offset
= Length
; Offset
> 0; Offset
--) {
201 if ((Return
[Offset
] == '/') || (Return
[Offset
] == '\\')) {
202 Return
[Offset
] = '\0';
219 This function replaces the final portion of a path with an alternative
220 'peer' filename. For example:
221 "a/b/../c", "peer" -> "a/b/../peer"
222 "a/b/", "peer" -> "a/b/peer"
223 "/a", "peer" -> "/peer"
224 "a", "peer" -> "peer"
226 This function does not check for the existence of the file.
230 OldPath Path name of replace the final segment
231 Peer The new path name to concatinate to become the peer path
235 A CHAR8* string, which must be freed by the caller
242 Result
= (CHAR8
*) malloc (strlen (OldPath
) + strlen (Peer
) + 1);
243 if (Result
== NULL
) {
247 strcpy (Result
, OldPath
);
250 // Search for the last '/' or '\' in the string. If found, replace
251 // everything following it with Peer
253 for (Offset
= strlen (Result
); Offset
>= 0; Offset
--) {
254 if ((Result
[Offset
] == '/') || (Result
[Offset
] == '\\')) {
255 Result
[Offset
+ 1] = '\0';
256 strcat (Result
, Peer
);
262 // Neither a '/' nor a '\' was found. Therefore, we simply return Peer.
264 strcpy (Result
, Peer
);
271 IN CHAR8
*InputFileName
277 Checks if a file exists
281 InputFileName The name of the file to check for existence
286 FALSE The file does not exist
291 InputFile
= fopen (LongFilePath (InputFileName
), "rb");
292 if (InputFile
== NULL
) {