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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2