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 david 1.44
|
239 mike 1.31 return !!is;
240 }
241
242 Boolean FileSystem::openNoCase(
243 PEGASUS_STD(fstream)& fs,
244 const String& path,
245 int mode)
246 {
247 String realPath;
|
248 ramnath 1.30
|
249 mike 1.31 if (!existsNoCase(path, realPath))
250 return false;
|
251 mday 1.43 #if defined(__GNUC__) && GCC_VERSION >= 30200
252 fs.open(_clonePath(realPath), PEGASUS_STD(ios_base::openmode)(mode));
253 #else
|
254 david 1.44 #if defined(PEGASUS_OS_OS400)
|
255 david 1.45 fs.open(_clonePath(realPath), mode, PEGASUS_STD(_CCSID_T(1208)) );
|
256 david 1.44 #else
|
257 kumpf 1.38 fs.open(_clonePath(realPath), mode);
|
258 david 1.44 #endif
|
259 mday 1.43 #endif
|
260 mike 1.31 return !!fs;
|
261 mike 1.28 }
262
263 Boolean FileSystem::isDirectory(const String& path)
264 {
|
265 kumpf 1.38 return System::isDirectory(_clonePath(path));
|
266 mike 1.28 }
267
268 Boolean FileSystem::changeDirectory(const String& path)
269 {
|
270 kumpf 1.38 return System::changeDirectory(_clonePath(path));
|
271 mike 1.28 }
272
273 Boolean FileSystem::makeDirectory(const String& path)
274 {
|
275 kumpf 1.38 return System::makeDirectory(_clonePath(path));
|
276 mike 1.28 }
277
278 Boolean FileSystem::removeDirectory(const String& path)
279 {
|
280 kumpf 1.38 return System::removeDirectory(_clonePath(path));
|
281 mike 1.28 }
282
283 Boolean FileSystem::removeDirectoryHier(const String& path)
284 {
285 Array<String> fileList;
286
287 // Get contents of current directory
288
289 if (!FileSystem::getDirectoryContents(path,fileList))
290 return false;
291
292 // for files-in-directory, delete or recall removedir
293
294 for (Uint32 i = 0, n = fileList.size(); i < n; i++)
295 {
296 String newPath = path; // extend path to subdir
297 newPath.append("/");
298 newPath.append(fileList[i]);
299
300 if (FileSystem::isDirectory(newPath))
301 {
302 mike 1.28 // Recall ourselves with extended path
303 if (!FileSystem::removeDirectoryHier(newPath))
304 return false;
305 }
306
307 else
308 {
309 if (!FileSystem::removeFile(newPath))
310 return false;
311 }
312 }
313
314 return removeDirectory(path);
315 }
316
317 //
318 // Get the file list in the directory into the
319 // array of strings provided
320 // @return The function should return false under these circumstances:
321 //
322 //
323 mike 1.28 // 1. The directory does not exist.
324 // 2. The file exists but is not a directory.
325 // 3. The directory is inaccessible.
326 //
327 //
328 Boolean FileSystem::getDirectoryContents(
329 const String& path,
330 Array<String>& paths)
331 {
332 paths.clear();
333
334 try
335 {
336 for (Dir dir(path); dir.more(); dir.next())
337 {
338 String name = dir.getName();
339
340 if (String::equal(name, ".") || String::equal(name, ".."))
341 continue;
342
343 paths.append(name);
344 mike 1.28 }
345 return true;
346 }
347
348 // Catch the Dir exception
349 catch(CannotOpenDirectory&)
350 {
351 return false;
352 }
353 }
354
355 Boolean FileSystem::isDirectoryEmpty(const String& path)
356 {
357 for (Dir dir(path); dir.more(); dir.next())
358 {
359 const char* name = dir.getName();
360
361 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
362 return false;
363 }
364
365 mike 1.28 return true;
366 }
367
368 void FileSystem::translateSlashes(String& path)
369 {
|
370 kumpf 1.35 for (Uint32 i = 0; i < path.size(); i++)
|
371 mike 1.28 {
|
372 kumpf 1.35 if (path[i] == '\\')
373 path[i] = '/';
|
374 mike 1.28 }
375 }
376
|
377 tony 1.42 // Return the just the base name from the path.
378 String FileSystem::extractFileName(const String& path)
379 {
380 char *p_path = new char[path.size() + 1];
381 String basename = System::extract_file_name((const char *)path.getCString(), p_path);
382
383 delete [] p_path;
384
385 return basename;
386 }
387
388 // Return just the path to the file or directory into path
389 String FileSystem::extractFilePath(const String& path)
390 {
391 char *p_path = new char[path.size() + 1];
392 String newpath = System::extract_file_path((const char *)path.getCString(), p_path);
393
394 delete [] p_path;
395
396 return newpath;
397 }
398 tony 1.42
399
|
400 kumpf 1.33 Boolean GetLine(PEGASUS_STD(istream)& is, String& line)
401 {
402 line.clear();
403
404 Boolean gotChar = false;
405 char c;
406
407 while (is.get(c))
408 {
409 gotChar = true;
410
411 if (c == '\n')
412 break;
413
414 line.append(c);
415 }
416
417 return gotChar;
418 }
419
|
420 mike 1.28 PEGASUS_NAMESPACE_END
|