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