]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Common/OsPath.c
3 Copyright (c) 2007 - 2008, Intel Corporation
4 All rights reserved. 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.
28 // Functions implementations
33 // BUGBUG: Not fully implemented yet.
43 This function returns the directory path which contains the particular path.
51 This function does not check for the existence of the file.
53 The caller must free the string returned.
57 FilePath Path name of file to get the parent directory for.
71 Length
= strlen (FilePath
);
78 // Check for the root directory case
81 (Length
== 3 && isalpha (FilePath
[0]) && (strcmp(FilePath
+ 1, ":\\") == 0)) ||
82 (strcmp(FilePath
, "/") == 0)
88 // If the path ends with a path separator, then just append ".."
90 Char
= FilePath
[Length
- 1];
91 if (Char
== '/' || Char
== '\\') {
92 return OsPathJoin (FilePath
, "..");
98 for (Offset
= Length
; Offset
> 0; Offset
--) {
99 if ((Return
[Offset
] == '/') || (Return
[Offset
] == '\\')) {
100 Return
[Offset
] = '\0';
110 // BUGBUG: Not fully implemented yet.
113 OsPathNormPathInPlace (
120 This function returns the directory path which contains the particular path.
126 This function does not check for the existence of the file.
130 Path Path name of file to normalize
134 The string is altered in place.
147 Length
= strlen (Path
);
149 for (Offset
= 0; Offset
< Length
; Offset
++) {
150 Remaining
= Length
- Offset
;
153 // Collapse '//' -> '/'
157 ((Offset
> 0) || (Path
[0] != '\\')) &&
158 IsDirSep (Path
[Offset
]) && IsDirSep (Path
[Offset
+ 1])
160 memmove (&Path
[Offset
], &Path
[Offset
+ 1], Remaining
);
166 // Collapse '/./' -> '/'
168 if ((Remaining
>= 3) && IsDirSep (Path
[Offset
]) &&
169 (Path
[Offset
+ 1] == '.') && IsDirSep (Path
[Offset
+ 2])
171 memmove (&Path
[Offset
], &Path
[Offset
+ 1], Remaining
);
177 // Collapse 'a/../b' -> 'b'
179 // BUGBUG: Not implemented yet
185 Return
= CloneString (FilePath
);
186 if (Return
== NULL
) {
190 Length
= strlen (Return
);
193 // Check for the root directory case
196 (Length
== 3 && isalpha (Return
[0]) && (strcmp(Return
+ 1, ":\\") == 0)) ||
197 (strcmp(Return
, "/") == 0)
206 for (Offset
= Length
; Offset
> 0; Offset
--) {
207 if ((Return
[Offset
] == '/') || (Return
[Offset
] == '\\')) {
208 Return
[Offset
] = '\0';
225 This function replaces the final portion of a path with an alternative
226 'peer' filename. For example:
227 "a/b/../c", "peer" -> "a/b/../peer"
228 "a/b/", "peer" -> "a/b/peer"
229 "/a", "peer" -> "/peer"
230 "a", "peer" -> "peer"
232 This function does not check for the existence of the file.
236 OldPath Path name of replace the final segment
237 Peer The new path name to concatinate to become the peer path
241 A CHAR8* string, which must be freed by the caller
248 Result
= (CHAR8
*) malloc (strlen (OldPath
) + strlen (Peer
) + 1);
249 if (Result
== NULL
) {
253 strcpy (Result
, OldPath
);
256 // Search for the last '/' or '\' in the string. If found, replace
257 // everything following it with Peer
259 for (Offset
= strlen (Result
); Offset
>= 0; Offset
--) {
260 if ((Result
[Offset
] == '/') || (Result
[Offset
] == '\\')) {
261 Result
[Offset
+ 1] = '\0';
262 strcat (Result
, Peer
);
268 // Neither a '/' nor a '\' was found. Therefore, we simply return Peer.
270 strcpy (Result
, Peer
);
277 IN CHAR8
*InputFileName
283 Checks if a file exists
287 InputFileName The name of the file to check for existence
292 FALSE The file does not exist
297 InputFile
= fopen (InputFileName
, "rb");
298 if (InputFile
== NULL
) {