1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/** @file
Functions useful to operate file directories by parsing file path.
Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CommonLib.h"
#include "OsPath.h"
//
// Functions implementations
//
#if 0
//
// BUGBUG: Not fully implemented yet.
//
CHAR8*
OsPathDirName (
IN CHAR8 *FilePath
)
/*++
Routine Description:
This function returns the directory path which contains the particular path.
Some examples:
"a/b/c" -> "a/b"
"a/b/c/" -> "a/b"
"a" -> "."
"." -> ".."
"/" -> NULL
This function does not check for the existence of the file.
The caller must free the string returned.
Arguments:
FilePath Path name of file to get the parent directory for.
Returns:
NULL if error
--*/
{
CHAR8 *Return;
CHAR8 *Pos;
CHAR8 Char;
UINTN Length;
INTN Offset;
Length = strlen (FilePath);
if (Length == 0) {
return NULL;
}
//
// Check for the root directory case
//
if (
(Length == 3 && isalpha (FilePath[0]) && (strcmp(FilePath + 1, ":\\") == 0)) ||
(strcmp(FilePath, "/") == 0)
) {
return NULL;
}
//
// If the path ends with a path separator, then just append ".."
//
Char = FilePath[Length - 1];
if (Char == '/' || Char == '\\') {
return OsPathJoin (FilePath, "..");
}
//
//
//
for (Offset = Length; Offset > 0; Offset--) {
if ((Return[Offset] == '/') || (Return[Offset] == '\\')) {
Return[Offset] = '\0';
return Return;
}
}
}
#endif
#if 0
//
// BUGBUG: Not fully implemented yet.
//
VOID
OsPathNormPathInPlace (
IN CHAR8 *Path
)
/*++
Routine Description:
This function returns the directory path which contains the particular path.
Some examples:
"a/b/../c" -> "a/c"
"a/b//c" -> "a/b/c"
"a/./b" -> "a/b"
This function does not check for the existence of the file.
Arguments:
Path Path name of file to normalize
Returns:
The string is altered in place.
--*/
{
CHAR8 *Pos;
INTN Offset;
BOOLEAN TryAgain;
UINTN Length;
UINTN Remaining;
UINTN SubLength;
do {
TryAgain = FALSE;
Length = strlen (Path);
for (Offset = 0; Offset < Length; Offset++) {
Remaining = Length - Offset;
//
// Collapse '//' -> '/'
//
if (
(Remaining >= 2) &&
((Offset > 0) || (Path[0] != '\\')) &&
IsDirSep (Path[Offset]) && IsDirSep (Path[Offset + 1])
) {
memmove (&Path[Offset], &Path[Offset + 1], Remaining);
TryAgain = TRUE;
break;
}
//
// Collapse '/./' -> '/'
//
if ((Remaining >= 3) && IsDirSep (Path[Offset]) &&
(Path[Offset + 1] == '.') && IsDirSep (Path[Offset + 2])
) {
memmove (&Path[Offset], &Path[Offset + 1], Remaining);
TryAgain = TRUE;
break;
}
//
// Collapse 'a/../b' -> 'b'
//
// BUGBUG: Not implemented yet
}
} while (TryAgain);
Return = CloneString (FilePath);
if (Return == NULL) {
return NULL;
}
Length = strlen (Return);
//
// Check for the root directory case
//
if (
(Length == 3 && isalpha (Return[0]) && (strcmp(Return + 1, ":\\") == 0)) ||
(strcmp(Return, "/") == 0)
) {
free (Return);
return NULL;
}
//
//
//
for (Offset = Length; Offset > 0; Offset--) {
if ((Return[Offset] == '/') || (Return[Offset] == '\\')) {
Return[Offset] = '\0';
return Return;
}
}
}
#endif
CHAR8*
OsPathPeerFilePath (
IN CHAR8 *OldPath,
IN CHAR8 *Peer
)
/*++
Routine Description:
This function replaces the final portion of a path with an alternative
'peer' filename. For example:
"a/b/../c", "peer" -> "a/b/../peer"
"a/b/", "peer" -> "a/b/peer"
"/a", "peer" -> "/peer"
"a", "peer" -> "peer"
This function does not check for the existence of the file.
Arguments:
OldPath Path name of replace the final segment
Peer The new path name to concatenate to become the peer path
Returns:
A CHAR8* string, which must be freed by the caller
--*/
{
CHAR8 *Result;
INTN Offset;
Result = (CHAR8 *) malloc (strlen (OldPath) + strlen (Peer) + 1);
if (Result == NULL) {
return NULL;
}
strcpy (Result, OldPath);
//
// Search for the last '/' or '\' in the string. If found, replace
// everything following it with Peer
//
for (Offset = strlen (Result); Offset >= 0; Offset--) {
if ((Result[Offset] == '/') || (Result[Offset] == '\\')) {
Result[Offset + 1] = '\0';
strcat (Result, Peer);
return Result;
}
}
//
// Neither a '/' nor a '\' was found. Therefore, we simply return Peer.
//
strcpy (Result, Peer);
return Result;
}
BOOLEAN
OsPathExists (
IN CHAR8 *InputFileName
)
/*++
Routine Description:
Checks if a file exists
Arguments:
InputFileName The name of the file to check for existence
Returns:
TRUE The file exists
FALSE The file does not exist
--*/
{
FILE *InputFile;
InputFile = fopen (LongFilePath (InputFileName), "rb");
if (InputFile == NULL) {
return FALSE;
} else {
fclose (InputFile);
return TRUE;
}
}
|