1 mike 1.28 //%/////////////////////////////////////////////////////////////////////////////
2 //
|
3 kumpf 1.34 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
4 // The Open Group, Tivoli Systems
|
5 mike 1.28 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 kumpf 1.34 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
10 mike 1.28 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
|
13 kumpf 1.34 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
14 mike 1.28 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
15 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
16 kumpf 1.34 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
19 mike 1.28 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 //==============================================================================
23 //
24 // Author: Mike Brasher (mbrasher@bmc.com)
25 //
26 // Modified By:
|
27 ramnath 1.29 // Ramnath Ravindran(Ramnath.Ravindran@compaq.com)
|
28 mike 1.28 //
29 //%/////////////////////////////////////////////////////////////////////////////
30
31 #include <iostream>
32 #include <cstdio>
33 #include <Pegasus/Common/Config.h>
34 #include <Pegasus/Common/System.h>
35 #include "Destroyer.h"
36 #include "FileSystem.h"
37 #include "Dir.h"
38
39 PEGASUS_NAMESPACE_BEGIN
40
41 // Clone the path as a C String but discard trailing slash if any:
42
|
43 kumpf 1.38 static CString _clonePath(const String& path)
|
44 mike 1.28 {
|
45 kumpf 1.38 String clone = path;
|
46 mike 1.28
|
47 kumpf 1.38 if (clone.size() && clone[clone.size()-1] == '/')
48 clone.remove(clone.size()-1);
|
49 mike 1.28
|
50 kumpf 1.38 return clone.getCString();
|
51 mike 1.28 }
52
53 Boolean FileSystem::exists(const String& path)
54 {
|
55 kumpf 1.38 return System::exists(_clonePath(path));
|
56 mike 1.28 }
57
58 Boolean FileSystem::getCurrentDirectory(String& path)
59 {
60 path.clear();
61 char tmp[4096];
62
63 if (!System::getCurrentDirectory(tmp, sizeof(tmp) - 1))
64 return false;
65
66 path.append(tmp);
67 return true;
68 }
69
70 Boolean FileSystem::existsNoCase(const String& path, String& realPath)
71 {
|
72 chuck 1.41 #ifdef PEGASUS_OS_OS400
73 // The OS/400 file system is case insensitive, so just call exists( ).
74 // This is faster, but the main reason to do this is to
75 // avoid multi-threading problems with the IFS directory APIs
76 // (even though they claim to be threadsafe).
77 realPath = path;
78 return exists(path);
79 #else
|
80 mike 1.28 realPath.clear();
|
81 kumpf 1.38 CString cpath = _clonePath(path);
82 const char* p = cpath;
|
83 mike 1.28
84 const char* dirPath;
|
85 kumpf 1.38 const char* fileName;
|
86 kumpf 1.39 char* slash = (char *) strrchr(p, '/');
|
87 mike 1.28
88 if (slash)
89 {
90 *slash = '\0';
91 fileName = slash + 1;
92 dirPath = p;
93
94 if (*fileName == '\0')
95 return false;
96 }
97 else
98 {
99 fileName = p;
100 dirPath = ".";
101 }
102
103
104 for (Dir dir(dirPath); dir.more(); dir.next())
105 {
|
106 kumpf 1.40 if (System::strcasecmp(fileName, dir.getName()) == 0)
|
107 mike 1.28 {
108 if (strcmp(dirPath, ".") == 0)
109 realPath = dir.getName();
110 else
111 {
112 realPath = dirPath;
|
113 kumpf 1.37 realPath.append('/');
114 realPath.append(dir.getName());
|
115 mike 1.28 }
116 return true;
117 }
118 }
119
120 return false;
|
121 chuck 1.41 #endif
|
122 mike 1.28 }
123
124 Boolean FileSystem::canRead(const String& path)
125 {
|
126 kumpf 1.38 return System::canRead(_clonePath(path));
|
127 mike 1.28 }
128
129 Boolean FileSystem::canWrite(const String& path)
130 {
|
131 kumpf 1.38 return System::canWrite(_clonePath(path));
|
132 mike 1.28 }
133
134 Boolean FileSystem::getFileSize(const String& path, Uint32& size)
135 {
|
136 kumpf 1.38 return System::getFileSize(_clonePath(path), size);
|
137 mike 1.28 }
138
139 Boolean FileSystem::removeFile(const String& path)
140 {
|
141 kumpf 1.38 return System::removeFile(_clonePath(path));
|
142 mike 1.28 }
143
144 void FileSystem::loadFileToMemory(
145 Array<Sint8>& array,
146 const String& fileName)
147 {
148 Uint32 fileSize;
149
150 if (!getFileSize(fileName, fileSize))
151 throw CannotOpenFile(fileName);
152
|
153 kumpf 1.38 FILE* fp = fopen(fileName.getCString(), "rb");
|
154 mike 1.28
155 if (fp == NULL)
156 throw CannotOpenFile(fileName);
157
|
158 kumpf 1.36 array.reserveCapacity(fileSize);
|
159 mike 1.28 char buffer[4096];
160 size_t n;
161
162 while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0)
163 array.append(buffer, n);
164
165 fclose(fp);
166 }
167
168 Boolean FileSystem::compareFiles(
169 const String& path1,
170 const String& path2)
171 {
172 Uint32 fileSize1;
173
174 if (!getFileSize(path1, fileSize1))
175 throw CannotOpenFile(path1);
176
177 Uint32 fileSize2;
178
179 if (!getFileSize(path2, fileSize2))
180 mike 1.28 throw CannotOpenFile(path2);
181
182 if (fileSize1 != fileSize2)
183 return false;
184
|
185 kumpf 1.38 FILE* fp1 = fopen(path1.getCString(), "rb");
|
186 mike 1.28
187 if (fp1 == NULL)
188 throw CannotOpenFile(path1);
189
|
190 kumpf 1.38 FILE* fp2 = fopen(path2.getCString(), "rb");
|
191 mike 1.28
192 if (fp2 == NULL)
193 {
194 fclose(fp1);
195 throw CannotOpenFile(path2);
196 }
197
198 int c1;
199 int c2;
200
201 while ((c1 = fgetc(fp1)) != EOF && (c2 = fgetc(fp2)) != EOF)
202 {
203 if (c1 != c2)
204 {
205 fclose(fp1);
206 fclose(fp2);
207 return false;
208 }
209 }
210
211 fclose(fp1);
212 mike 1.28 fclose(fp2);
213 return true;
214 }
215
216 Boolean FileSystem::renameFile(
217 const String& oldPath,
218 const String& newPath)
219 {
|
220 kumpf 1.38 return System::renameFile(oldPath.getCString(), newPath.getCString());
|
221 mike 1.28 }
222
|
223 mike 1.31 Boolean FileSystem::copyFile(
224 const String& fromPath,
225 const String& toPath)
226 {
|
227 kumpf 1.38 return System::copyFile(fromPath.getCString(), toPath.getCString());
|
228 mike 1.31 }
229
|
230 mike 1.28 Boolean FileSystem::openNoCase(PEGASUS_STD(ifstream)& is, const String& path)
231 {
232 String realPath;
233
234 if (!existsNoCase(path, realPath))
235 return false;
236
|
237 kumpf 1.38 is.open(_clonePath(realPath) PEGASUS_IOS_BINARY);
|
238 mike 1.31 return !!is;
239 }
240
241 Boolean FileSystem::openNoCase(
242 PEGASUS_STD(fstream)& fs,
243 const String& path,
244 int mode)
245 {
246 String realPath;
|
247 ramnath 1.30
|
248 mike 1.31 if (!existsNoCase(path, realPath))
249 return false;
250
|
251 kumpf 1.38 fs.open(_clonePath(realPath), mode);
|
252 mike 1.31 return !!fs;
|
253 mike 1.28 }
254
255 Boolean FileSystem::isDirectory(const String& path)
256 {
|
257 kumpf 1.38 return System::isDirectory(_clonePath(path));
|
258 mike 1.28 }
259
260 Boolean FileSystem::changeDirectory(const String& path)
261 {
|
262 kumpf 1.38 return System::changeDirectory(_clonePath(path));
|
263 mike 1.28 }
264
265 Boolean FileSystem::makeDirectory(const String& path)
266 {
|
267 kumpf 1.38 return System::makeDirectory(_clonePath(path));
|
268 mike 1.28 }
269
270 Boolean FileSystem::removeDirectory(const String& path)
271 {
|
272 kumpf 1.38 return System::removeDirectory(_clonePath(path));
|
273 mike 1.28 }
274
275 Boolean FileSystem::removeDirectoryHier(const String& path)
276 {
277 Array<String> fileList;
278
279 // Get contents of current directory
280
281 if (!FileSystem::getDirectoryContents(path,fileList))
282 return false;
283
284 // for files-in-directory, delete or recall removedir
285
286 for (Uint32 i = 0, n = fileList.size(); i < n; i++)
287 {
288 String newPath = path; // extend path to subdir
289 newPath.append("/");
290 newPath.append(fileList[i]);
291
292 if (FileSystem::isDirectory(newPath))
293 {
294 mike 1.28 // Recall ourselves with extended path
295 if (!FileSystem::removeDirectoryHier(newPath))
296 return false;
297 }
298
299 else
300 {
301 if (!FileSystem::removeFile(newPath))
302 return false;
303 }
304 }
305
306 return removeDirectory(path);
307 }
308
309 //
310 // Get the file list in the directory into the
311 // array of strings provided
312 // @return The function should return false under these circumstances:
313 //
314 //
315 mike 1.28 // 1. The directory does not exist.
316 // 2. The file exists but is not a directory.
317 // 3. The directory is inaccessible.
318 //
319 //
320 Boolean FileSystem::getDirectoryContents(
321 const String& path,
322 Array<String>& paths)
323 {
324 paths.clear();
325
326 try
327 {
328 for (Dir dir(path); dir.more(); dir.next())
329 {
330 String name = dir.getName();
331
332 if (String::equal(name, ".") || String::equal(name, ".."))
333 continue;
334
335 paths.append(name);
336 mike 1.28 }
337 return true;
338 }
339
340 // Catch the Dir exception
341 catch(CannotOpenDirectory&)
342 {
343 return false;
344 }
345 }
346
347 Boolean FileSystem::isDirectoryEmpty(const String& path)
348 {
349 for (Dir dir(path); dir.more(); dir.next())
350 {
351 const char* name = dir.getName();
352
353 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
354 return false;
355 }
356
357 mike 1.28 return true;
358 }
359
360 void FileSystem::translateSlashes(String& path)
361 {
|
362 kumpf 1.35 for (Uint32 i = 0; i < path.size(); i++)
|
363 mike 1.28 {
|
364 kumpf 1.35 if (path[i] == '\\')
365 path[i] = '/';
|
366 mike 1.28 }
367 }
368
|
369 tony 1.42 // Return the just the base name from the path.
370 String FileSystem::extractFileName(const String& path)
371 {
372 char *p_path = new char[path.size() + 1];
373 String basename = System::extract_file_name((const char *)path.getCString(), p_path);
374
375 delete [] p_path;
376
377 return basename;
378 }
379
380 // Return just the path to the file or directory into path
381 String FileSystem::extractFilePath(const String& path)
382 {
383 char *p_path = new char[path.size() + 1];
384 String newpath = System::extract_file_path((const char *)path.getCString(), p_path);
385
386 delete [] p_path;
387
388 return newpath;
389 }
390 tony 1.42
391
|
392 kumpf 1.33 Boolean GetLine(PEGASUS_STD(istream)& is, String& line)
393 {
394 line.clear();
395
396 Boolean gotChar = false;
397 char c;
398
399 while (is.get(c))
400 {
401 gotChar = true;
402
403 if (c == '\n')
404 break;
405
406 line.append(c);
407 }
408
409 return gotChar;
410 }
411
|
412 mike 1.28 PEGASUS_NAMESPACE_END
|