]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Common/OsPath.c
07b47f09b4037be0fdadf7ca418cbde9d4b63cbe
3 Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Functions useful to operate file directories by parsing file path.
25 #include "CommonLib.h"
29 // Functions implementations
34 // BUGBUG: Not fully implemented yet.
44 This function returns the directory path which contains the particular path.
52 This function does not check for the existence of the file.
54 The caller must free the string returned.
58 FilePath Path name of file to get the parent directory for.
72 Length
= strlen (FilePath
);
79 // Check for the root directory case
82 (Length
== 3 && isalpha (FilePath
[0]) && (strcmp(FilePath
+ 1, ":\\") == 0)) ||
83 (strcmp(FilePath
, "/") == 0)
89 // If the path ends with a path separator, then just append ".."
91 Char
= FilePath
[Length
- 1];
92 if (Char
== '/' || Char
== '\\') {
93 return OsPathJoin (FilePath
, "..");
99 for (Offset
= Length
; Offset
> 0; Offset
--) {
100 if ((Return
[Offset
] == '/') || (Return
[Offset
] == '\\')) {
101 Return
[Offset
] = '\0';
111 // BUGBUG: Not fully implemented yet.
114 OsPathNormPathInPlace (
121 This function returns the directory path which contains the particular path.
127 This function does not check for the existence of the file.
131 Path Path name of file to normalize
135 The string is altered in place.
148 Length
= strlen (Path
);
150 for (Offset
= 0; Offset
< Length
; Offset
++) {
151 Remaining
= Length
- Offset
;
154 // Collapse '//' -> '/'
158 ((Offset
> 0) || (Path
[0] != '\\')) &&
159 IsDirSep (Path
[Offset
]) && IsDirSep (Path
[Offset
+ 1])
161 memmove (&Path
[Offset
], &Path
[Offset
+ 1], Remaining
);
167 // Collapse '/./' -> '/'
169 if ((Remaining
>= 3) && IsDirSep (Path
[Offset
]) &&
170 (Path
[Offset
+ 1] == '.') && IsDirSep (Path
[Offset
+ 2])
172 memmove (&Path
[Offset
], &Path
[Offset
+ 1], Remaining
);
178 // Collapse 'a/../b' -> 'b'
180 // BUGBUG: Not implemented yet
186 Return
= CloneString (FilePath
);
187 if (Return
== NULL
) {
191 Length
= strlen (Return
);
194 // Check for the root directory case
197 (Length
== 3 && isalpha (Return
[0]) && (strcmp(Return
+ 1, ":\\") == 0)) ||
198 (strcmp(Return
, "/") == 0)
207 for (Offset
= Length
; Offset
> 0; Offset
--) {
208 if ((Return
[Offset
] == '/') || (Return
[Offset
] == '\\')) {
209 Return
[Offset
] = '\0';
226 This function replaces the final portion of a path with an alternative
227 'peer' filename. For example:
228 "a/b/../c", "peer" -> "a/b/../peer"
229 "a/b/", "peer" -> "a/b/peer"
230 "/a", "peer" -> "/peer"
231 "a", "peer" -> "peer"
233 This function does not check for the existence of the file.
237 OldPath Path name of replace the final segment
238 Peer The new path name to concatinate to become the peer path
242 A CHAR8* string, which must be freed by the caller
249 Result
= (CHAR8
*) malloc (strlen (OldPath
) + strlen (Peer
) + 1);
250 if (Result
== NULL
) {
254 strcpy (Result
, OldPath
);
257 // Search for the last '/' or '\' in the string. If found, replace
258 // everything following it with Peer
260 for (Offset
= strlen (Result
); Offset
>= 0; Offset
--) {
261 if ((Result
[Offset
] == '/') || (Result
[Offset
] == '\\')) {
262 Result
[Offset
+ 1] = '\0';
263 strcat (Result
, Peer
);
269 // Neither a '/' nor a '\' was found. Therefore, we simply return Peer.
271 strcpy (Result
, Peer
);
278 IN CHAR8
*InputFileName
284 Checks if a file exists
288 InputFileName The name of the file to check for existence
293 FALSE The file does not exist
298 InputFile
= fopen (LongFilePath (InputFileName
), "rb");
299 if (InputFile
== NULL
) {