(file) Return to FileSystem.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  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 "System.h"
 38              #include "Dir.h"
 39              
 40              PEGASUS_NAMESPACE_BEGIN
 41              
 42              // Clone the path as a C String but discard trailing slash if any:
 43              
 44 kumpf   1.38 static CString _clonePath(const String& path)
 45 mike    1.28 {
 46 kumpf   1.38     String clone = path;
 47 mike    1.28 
 48 kumpf   1.38     if (clone.size() && clone[clone.size()-1] == '/')
 49                      clone.remove(clone.size()-1);
 50 mike    1.28 
 51 kumpf   1.38     return clone.getCString();
 52 mike    1.28 }
 53              
 54              Boolean FileSystem::exists(const String& path)
 55              {
 56 kumpf   1.38     return System::exists(_clonePath(path));
 57 mike    1.28 }
 58              
 59              Boolean FileSystem::getCurrentDirectory(String& path)
 60              {
 61                  path.clear();
 62                  char tmp[4096];
 63              
 64                  if (!System::getCurrentDirectory(tmp, sizeof(tmp) - 1))
 65              	return false;
 66              
 67                  path.append(tmp);
 68                  return true;
 69              }
 70              
 71              Boolean FileSystem::existsNoCase(const String& path, String& realPath)
 72              {
 73                  realPath.clear();
 74 kumpf   1.38     CString cpath = _clonePath(path);
 75                  const char* p = cpath;
 76 mike    1.28 
 77                  const char* dirPath;
 78 kumpf   1.38     const char* fileName;
 79 mike    1.28     char* slash = strrchr(p, '/');
 80              
 81                  if (slash)
 82                  {
 83              	*slash = '\0';
 84              	fileName = slash + 1;
 85              	dirPath = p;
 86              
 87              	if (*fileName == '\0')
 88              	    return false;
 89                  }
 90                  else
 91                  {
 92              	fileName = p;
 93              	dirPath = ".";
 94                  }
 95              
 96              
 97                  for (Dir dir(dirPath); dir.more(); dir.next())
 98                  {
 99              	if (CompareNoCase(fileName, dir.getName()) == 0)
100 mike    1.28 	{
101              	    if (strcmp(dirPath, ".") == 0)
102              		realPath = dir.getName();
103              	    else
104              	    {
105              		realPath = dirPath;
106 kumpf   1.37 		realPath.append('/');
107              		realPath.append(dir.getName());
108 mike    1.28 	    }
109              	    return true;
110              	}
111                  }
112              
113                  return false;
114              }
115              
116              Boolean FileSystem::canRead(const String& path)
117              {
118 kumpf   1.38     return System::canRead(_clonePath(path));
119 mike    1.28 }
120              
121              Boolean FileSystem::canWrite(const String& path)
122              {
123 kumpf   1.38     return System::canWrite(_clonePath(path));
124 mike    1.28 }
125              
126              Boolean FileSystem::getFileSize(const String& path, Uint32& size)
127              {
128 kumpf   1.38     return System::getFileSize(_clonePath(path), size);
129 mike    1.28 }
130              
131              Boolean FileSystem::removeFile(const String& path)
132              {
133 kumpf   1.38     return System::removeFile(_clonePath(path));
134 mike    1.28 }
135              
136              void FileSystem::loadFileToMemory(
137                  Array<Sint8>& array,
138                  const String& fileName)
139              {
140                  Uint32 fileSize;
141              
142                  if (!getFileSize(fileName, fileSize))
143              	throw CannotOpenFile(fileName);
144              
145 kumpf   1.38     FILE* fp = fopen(fileName.getCString(), "rb");
146 mike    1.28 
147                  if (fp == NULL)
148              	throw CannotOpenFile(fileName);
149              
150 kumpf   1.36     array.reserveCapacity(fileSize);
151 mike    1.28     char buffer[4096];
152                  size_t n;
153              
154                  while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0)
155                      array.append(buffer, n);
156              
157                  fclose(fp);
158              }
159              
160              Boolean FileSystem::compareFiles(
161                  const String& path1,
162                  const String& path2)
163              {
164                  Uint32 fileSize1;
165              
166                  if (!getFileSize(path1, fileSize1))
167              	throw CannotOpenFile(path1);
168              
169                  Uint32 fileSize2;
170              
171                  if (!getFileSize(path2, fileSize2))
172 mike    1.28 	throw CannotOpenFile(path2);
173              
174                  if (fileSize1 != fileSize2)
175              	return false;
176              
177 kumpf   1.38     FILE* fp1 = fopen(path1.getCString(), "rb");
178 mike    1.28 
179                  if (fp1 == NULL)
180              	throw CannotOpenFile(path1);
181              
182 kumpf   1.38     FILE* fp2 = fopen(path2.getCString(), "rb");
183 mike    1.28 
184                  if (fp2 == NULL)
185                  {
186              	fclose(fp1);
187              	throw CannotOpenFile(path2);
188                  }
189              
190                  int c1;
191                  int c2;
192              
193                  while ((c1 = fgetc(fp1)) != EOF && (c2 = fgetc(fp2)) != EOF)
194                  {
195              	if (c1 != c2)
196              	{
197              	    fclose(fp1);
198              	    fclose(fp2);
199              	    return false;
200              	}
201                  }
202              
203                  fclose(fp1);
204 mike    1.28     fclose(fp2);
205                  return true;
206              }
207              
208              Boolean FileSystem::renameFile(
209                  const String& oldPath,
210                  const String& newPath)
211              {
212 kumpf   1.38     return System::renameFile(oldPath.getCString(), newPath.getCString());
213 mike    1.28 }
214              
215 mike    1.31 Boolean FileSystem::copyFile(
216                  const String& fromPath,
217                  const String& toPath)
218              {
219 kumpf   1.38     return System::copyFile(fromPath.getCString(), toPath.getCString());
220 mike    1.31 }
221              
222 mike    1.28 Boolean FileSystem::openNoCase(PEGASUS_STD(ifstream)& is, const String& path)
223              {
224                  String realPath;
225              
226                  if (!existsNoCase(path, realPath))
227              	return false;
228              
229 kumpf   1.38     is.open(_clonePath(realPath) PEGASUS_IOS_BINARY);
230 mike    1.31     return !!is;
231              }
232              
233              Boolean FileSystem::openNoCase(
234                  PEGASUS_STD(fstream)& fs, 
235                  const String& path,
236                  int mode)
237              {
238                  String realPath;
239 ramnath 1.30 
240 mike    1.31     if (!existsNoCase(path, realPath))
241              	return false;
242              
243 kumpf   1.38     fs.open(_clonePath(realPath), mode);
244 mike    1.31     return !!fs;
245 mike    1.28 }
246              
247              Boolean FileSystem::isDirectory(const String& path)
248              {
249 kumpf   1.38     return System::isDirectory(_clonePath(path));
250 mike    1.28 }
251              
252              Boolean FileSystem::changeDirectory(const String& path)
253              {
254 kumpf   1.38     return System::changeDirectory(_clonePath(path));
255 mike    1.28 }
256              
257              Boolean FileSystem::makeDirectory(const String& path)
258              {
259 kumpf   1.38     return System::makeDirectory(_clonePath(path));
260 mike    1.28 }
261              
262              Boolean FileSystem::removeDirectory(const String& path)
263              {
264 kumpf   1.38     return System::removeDirectory(_clonePath(path));
265 mike    1.28 }
266              
267              Boolean FileSystem::removeDirectoryHier(const String& path)
268              {
269                  Array<String> fileList;
270              
271                  // Get contents of current directory
272              
273                  if (!FileSystem::getDirectoryContents(path,fileList))
274              	return false;
275              
276                  // for files-in-directory, delete or recall removedir
277              
278                  for (Uint32 i = 0, n = fileList.size(); i < n; i++)
279                  {   
280              	String newPath = path;	 // extend path	to subdir
281              	newPath.append("/");
282              	newPath.append(fileList[i]);
283              	
284              	if (FileSystem::isDirectory(newPath))
285              	{
286 mike    1.28 	    // Recall ourselves with extended path
287              	    if (!FileSystem::removeDirectoryHier(newPath))
288              		return false; 
289              	}
290              
291              	else
292              	{
293                        if (!FileSystem::removeFile(newPath))
294              		return false;
295              	}
296                  }
297              
298                  return removeDirectory(path);	
299              }
300              
301              //
302              //  Get the file list in the directory into the
303              //  array of strings provided
304              //  @return The function should return false under these circumstances:
305              //
306              //
307 mike    1.28 //  1. The directory does not exist.
308              //  2. The file exists but is not a directory.
309              //  3. The directory is inaccessible.
310              //
311              //
312              Boolean FileSystem::getDirectoryContents(
313                  const String& path,
314                  Array<String>& paths)
315              {
316                  paths.clear();
317              
318                  try
319                  { 
320              	for (Dir dir(path); dir.more(); dir.next())
321              	{
322              	    String name = dir.getName();
323              
324              	    if (String::equal(name, ".") || String::equal(name, ".."))
325              		continue;
326              
327              	    paths.append(name);
328 mike    1.28 	}
329              	return true;
330                  }
331              
332                  // Catch the Dir exception
333                  catch(CannotOpenDirectory&)
334                  {
335                  	return false;
336                  }
337              }
338              
339              Boolean FileSystem::isDirectoryEmpty(const String& path)
340              {
341                  for (Dir dir(path); dir.more(); dir.next())
342                  {
343                      const char* name = dir.getName();
344              
345                      if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
346                          return false;
347                  }
348              
349 mike    1.28     return true;
350              }
351              
352              void FileSystem::translateSlashes(String& path)
353              {
354 kumpf   1.35     for (Uint32 i = 0; i < path.size(); i++)
355 mike    1.28     {
356 kumpf   1.35 	if (path[i] == '\\')
357              	    path[i] = '/';
358 mike    1.28     }
359              }
360              
361 kumpf   1.33 Boolean GetLine(PEGASUS_STD(istream)& is, String& line)
362              {
363                  line.clear();
364              
365                  Boolean gotChar = false;
366                  char c;
367              
368                  while (is.get(c))
369                  {
370                      gotChar = true;
371              
372                      if (c == '\n')
373                          break;
374              
375                      line.append(c);
376                  }
377              
378                  return gotChar;
379              }
380              
381 mike    1.28 PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2