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

  1 karl  1.30 //%2003////////////////////////////////////////////////////////////////////////
  2 mike  1.15 //
  3 karl  1.30 // Copyright (c) 2000, 2001, 2002  BMC Software, Hewlett-Packard Development
  4            // Company, L. P., IBM Corp., The Open Group, Tivoli Systems.
  5            // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.;
  6            // IBM Corp.; EMC Corporation, The Open Group.
  7 mike  1.15 //
  8            // Permission is hereby granted, free of charge, to any person obtaining a copy
  9 mike  1.16 // of this software and associated documentation files (the "Software"), to
 10            // deal in the Software without restriction, including without limitation the
 11            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 12 mike  1.15 // sell copies of the Software, and to permit persons to whom the Software is
 13            // furnished to do so, subject to the following conditions:
 14 kumpf 1.19 // 
 15 mike  1.16 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 16 mike  1.15 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 17            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 18 mike  1.16 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 19            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 20            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 21 mike  1.15 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 22            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 23            //
 24            //==============================================================================
 25            //
 26            // Author: Mike Brasher (mbrasher@bmc.com)
 27            //
 28            // Modified By:
 29            //
 30            //%/////////////////////////////////////////////////////////////////////////////
 31            
 32            #ifndef Pegasus_FileSystem_h
 33            #define Pegasus_FileSystem_h
 34            
 35            #include <Pegasus/Common/Config.h>
 36            #include <Pegasus/Common/String.h>
 37 kumpf 1.23 #include <Pegasus/Common/ArrayInternal.h>
 38 kumpf 1.22 #include <Pegasus/Common/InternalException.h>
 39 kumpf 1.26 #include <Pegasus/Common/System.h>
 40 kumpf 1.21 #include <Pegasus/Common/Linkage.h>
 41 mike  1.16 #include <fstream>
 42 david 1.27 #include <cstdio>
 43            #if defined(PEGASUS_OS_OS400)
 44            #include "OS400ConvertChar.h"
 45            #endif
 46 mike  1.15 
 47            PEGASUS_NAMESPACE_BEGIN
 48            
 49            /** The FileSystem class provides methods for manipulating the file system.
 50            
 51                This class provides an methods for:
 52                <ul>
 53            	<li>Manipulating directories (create, remove, change).</li>
 54            	<li>Checking files for ability to read and write.</li>
 55            	<li>Removing files.</li>
 56            	<li>Comparing files.</li>
 57            	<li>Loading files into memory.</li>
 58                </ul>
 59            
 60 mike  1.16     The methods of this class are all static. So there is no need to
 61 mike  1.15     instantiate this class to use it. In fact, instantiation is precluded
 62                by a private default constructor.
 63            
 64                A word about the "NoCase" extensions. Some methods of this class have
 65                a "NoCase" version. For example, there is a canRead() and a canReadNoCase().
 66                The "NoCase" variation ignores the case of the file while it is being
 67                located. For example, suppose there is a file called "File1". Then
 68                canReadNoCase("file1") finds the file called "File1" and returns true (of
 69                course there is a possibility that there really is a file called "file1"
 70 mike  1.16     in the same directory in which case the behavior of this method is
 71                undefined). Notice that Windows does this anyway. These methods were
 72 mike  1.15     developed primarily for Unix which is case sensitive with respect to file
 73                names. It should be noted that the no-case methods are slower (since they
 74                must stat the directory and look at the file names).
 75            
 76                The no-case variations are used by the repository which--according to
 77 mike  1.16     CIM--must treat two classes names with different case characterisits, but
 78 mike  1.15     othererwise similar, as identical. For example, "MyClass", "myclass", and
 79                "MYCLASS" all refer to the same class. Since the default repository
 80                implementation uses disk file names to represent class names (e.g., there
 81                may be a file called "MyClass.#) that there must be a way of opening
 82                a file without regard to its case.
 83            */
 84            class PEGASUS_COMMON_LINKAGE FileSystem
 85            {
 86            public:
 87            
 88                /** Determines whether file exists.
 89            	@param path path of the file.
 90            	@return true if the file exists.
 91                */
 92                static Boolean exists(const String& path);
 93            
 94                /** Determine whether the file exists. Ignores case of the file.
 95            	@param path path of the file.
 96            	@param pathOut path of the file with actual case.
 97            	@return true if the file exists; false otherwise.
 98                */
 99 mike  1.15     static Boolean existsNoCase(const String& path, String& pathOut);
100            
101                /** Determine whether the file exists. Ignores the case of the file.
102            	@param path path of the file.
103            	@return true if the file exists; false otherwise.
104                */
105                static Boolean existsNoCase(const String& path);
106            
107                /** Determines whether the file can be read.
108            	@param path path of the file.
109            	@return true if the file can be read.
110                */
111                static Boolean canRead(const String& path);
112            
113                /** Determines whether the file can be read. Ignores case of file.
114            	@param path path of the file.
115            	@return true if the file can be read.
116                */
117                static Boolean canReadNoCase(const String& path);
118            
119                /** Determines whether the file can be written.
120 mike  1.15 	@param path path of the file.
121            	@return true if the file can be written.
122                */
123                static Boolean canWrite(const String& path);
124            
125                /** Determines whether the file can be written. Ignores case of file.
126            	@param path path of the file.
127            	@return true if the file can be written.
128                */
129                static Boolean canWriteNoCase(const String& path);
130            
131                /** Get the size of the file in bytes.
132            	@param path path of file.
133            	@param size set to size of file.
134            	@return true on success.
135                */
136                static Boolean getFileSize(const String& path, Uint32& size);
137            
138                /** Get the size of the file in bytes.
139            	@param path path of file.
140            	@param size set to size of file.
141 mike  1.15 	@return true on success.
142                */
143                static Boolean getFileSizeNoCase(const String& path, Uint32& size);
144            
145                /** Removes a file.
146            	@param path of file to be removed.
147            	@return true on sucess.
148                */
149                static Boolean removeFile(const String& path);
150            
151                /** Removes a file. Ignores case of file.
152            	@param path of file to be removed.
153            	@return true on sucess.
154                */
155                static Boolean removeFileNoCase(const String& path);
156            
157                /** Loads contents of the file into the array. Note that the file is
158            	opened using binary mode (newline sequences are not expanded to
159            	carriage-return-line-feed sequences on Windows).
160            	@param array set to the contents of the file upon return.
161            	@param fileName name of file to be loaded.
162 mike  1.16 	@exception CannotOpenFile
163 mike  1.15     */
164                static void loadFileToMemory(
165            	Array<Sint8>& array,
166            	const String& fileName);
167            
168                /** Determines whether two files have exactly the same content.
169            	@param path1 path of first file.
170            	@param path2 path of second file.
171            	@return true if files are identical.
172            	@exception CannotOpenFile
173                */
174                static Boolean compareFiles(
175            	const String& path1,
176            	const String& path2);
177            
178                /** Renames a file.
179            	@param oldPath old name of file.
180            	@param newPath new name of file.
181            	@return true on success.
182                */
183                static Boolean renameFile(
184 mike  1.15 	const String& oldPath,
185            	const String& newPath);
186            
187 mike  1.17     /** Same as rename file except that the case of the file referred to
188            	by oldPath is ignored.
189                */
190                static Boolean renameFileNoCase(
191            	const String& oldPath,
192            	const String& newPath);
193            
194                /** Copy a file.
195            	@param fromPath name of existing file.
196            	@param toPath name of new file.
197            	@return true on success.
198                */
199                static Boolean copyFile(
200            	const String& fromPath,
201            	const String& toPath);
202            
203 mike  1.15     /** Opens a file and ignores the case of the file. Note that the file
204            	will be opend in binary mode (no translation of carriage-return-line-
205            	feed sequences on Windows).
206            	@param os file stream to be opend.
207            	@param path path of file to be opened.
208            	@return true on success.
209                */
210 mike  1.17     static Boolean openNoCase(PEGASUS_STD(ifstream)& is, const String& path);
211            
212                /** Opens a file and ignores the case of the file. Note that the file
213            	open mode of the file must be passed in.
214            	@param os file stream to be opend.
215            	@param path path of file to be opened.
216            	@param mode mode to open the file in.
217            	@return true on success.
218                */
219                static Boolean openNoCase(
220            	PEGASUS_STD(fstream)& fs, 
221            	const String& path, 
222            	int mode);
223 mike  1.15 
224                /** Determines whether the path refers to a directory.
225            	@param path path of the directory.
226            	@return true if path refers to a directory.
227                */
228                static Boolean isDirectory(const String& path);
229            
230                /** Changes the current directory.
231            	@param path path of directory to be changed to.
232            	@return true on success.
233                */
234                static Boolean changeDirectory(const String& path);
235            
236 mike  1.16     /** Creates a directory.
237 mike  1.15 	@param path path of directory to be created.
238            	@return true on success.
239                */
240                static Boolean makeDirectory(const String& path);
241            
242 mike  1.16     /** Get the path of the current working Directory.
243 mike  1.15 	@param path set to current working directory upon return.
244            	@return true on success (operation may fail if the current
245            	    working directory becomes stale; this can happen on
246            	    Unix if it is removed but is impossible on Windows
247            	    due to reference counting).
248                */
249                static Boolean getCurrentDirectory(String& path);
250            
251                /** Remove the given directory. The directory must be empty
252            	to be eligible for removal
253            	@param String path is the relative or ablsolute path to
254            	the directory to remove
255            	@return true if directory removed
256                */
257                static Boolean removeDirectory(const String& path);
258            
259                /** Remove a directory and all files and directories under it.
260            	WARNING: This differs significantly from the <tt>removeDirectory</tt>
261            	function in that it removes both directories and files and
262 mike  1.16 	removes a complete hiearchy.  Use with caution.
263 mike  1.15 	@param path path of directory to be removed.
264            	@return true on success.
265 mike  1.16     */
266 mike  1.15     static Boolean removeDirectoryHier(const String& path);
267            
268                /** Gets names of all entries (files and directories) of a directory.
269            	Note that this function excludes the "." and ".." entries.
270            	@param path path path of directory.
271            	@param paths contains list of entry names upon return. Note that
272            	    the entry names only are provided (no path part).
273            	@return true on success.
274                */
275                static Boolean getDirectoryContents(
276            	const String& path,
277            	Array<String>& paths);
278            
279                /** Determines whether the given directory is empty. A directory is
280            	empty if it contains no files or directories.
281            	@param path path of directory.
282            	@return true if directory is empty.
283                */
284                static Boolean isDirectoryEmpty(const String& path);
285            
286 mike  1.16     /** Translate backward slashes to forward slashes.
287 mike  1.15 	@param path to be translated.
288                */
289                static void translateSlashes(String& path);
290            
291 kumpf 1.20     /** Get an absolute path from an absolute directory and a relative or
292                    absolute file name.  If the file name is fully specified, it is
293                    returned unchanged.  Otherwise, the specified directory is prepended
294                    to the file name.
295                */
296                static String getAbsolutePath(const char* path, const String& filename);
297            
298 tony  1.25     /** Return the just the filename to the file name into base.
299                */
300                static String extractFileName(const String& base);
301            
302                /** Return the just the path to the file name into path.
303                */
304                static String extractFilePath(const String& path);
305            
306 mike  1.15 private:
307            
308                FileSystem() { }
309            };
310            
311            inline Boolean FileSystem::existsNoCase(const String& path)
312            {
313                String dummy;
314                return existsNoCase(path, dummy);
315            }
316            
317            inline Boolean FileSystem::canReadNoCase(const String& path)
318            {
319                String realPath;
320            
321                if (!existsNoCase(path, realPath))
322            	return false;
323            
324                return FileSystem::canRead(realPath);
325            }
326            
327 mike  1.15 inline Boolean FileSystem::canWriteNoCase(const String& path)
328            {
329                String realPath;
330            
331                if (!existsNoCase(path, realPath))
332            	return false;
333            
334                return FileSystem::canWrite(realPath);
335            }
336            
337            inline Boolean FileSystem::removeFileNoCase(const String& path)
338            {
339                String realPath;
340            
341                if (!existsNoCase(path, realPath))
342            	return false;
343            
344                return FileSystem::removeFile(realPath);
345 mike  1.17 }
346            
347            inline Boolean FileSystem::renameFileNoCase(
348                const String& oldPath,
349                const String& newPath)
350            {
351                String realPath;
352            
353                if (!existsNoCase(oldPath, realPath))
354            	return false;
355            
356                return FileSystem::renameFile(realPath, newPath);
357 mike  1.15 }
358            
359            inline Boolean FileSystem::getFileSizeNoCase(const String& path, Uint32& size)
360            {
361                String realPath;
362            
363                if (!existsNoCase(path, realPath))
364            	return false;
365            
366                return FileSystem::getFileSize(realPath, size);
367 kumpf 1.20 }
368            
369            inline String FileSystem::getAbsolutePath(
370                const char* path,
371                const String& filename)
372            {
373                String absolutePath;
374            
375                if (filename != String::EMPTY)
376                {
377 kumpf 1.26         if (!System::is_absolute_path(filename.getCString()) && path && path[0])
378 kumpf 1.20         {
379                        absolutePath.append(path);
380                        absolutePath.append('/');
381                    }
382                    absolutePath.append(filename);
383                }
384                translateSlashes(absolutePath);
385            
386                return absolutePath;
387 mike  1.15 }
388            
389 kumpf 1.18 inline Boolean Open(PEGASUS_STD(ifstream)& is, const String& path)
390            {
391 david 1.28 #if defined(PEGASUS_OS_OS400)
392 david 1.29     is.open(path.getCString(), PEGASUS_STD(_CCSID_T(1208)));
393 david 1.27 #else
394 kumpf 1.24     is.open(path.getCString());
395 david 1.27 #endif
396 kumpf 1.18     return !!is;
397            }
398            
399            inline Boolean Open(PEGASUS_STD(ofstream)& os, const String& path)
400            {
401 david 1.28 #if defined(PEGASUS_OS_OS400)
402 david 1.29     os.open(path.getCString(), PEGASUS_STD(_CCSID_T(1208)));
403 david 1.27 #else
404 kumpf 1.24     os.open(path.getCString());
405 david 1.27 #endif
406 kumpf 1.18     return !!os;
407            }
408            
409            inline Boolean OpenAppend(PEGASUS_STD(ofstream)& os, const String& path)
410            {
411 david 1.28 #if defined(PEGASUS_OS_OS400)
412 david 1.29     os.open(path.getCString(), PEGASUS_STD(ios::app), PEGASUS_STD(_CCSID_T(1208)));
413 david 1.27 #else
414 kumpf 1.24     os.open(path.getCString(), PEGASUS_STD(ios::app));
415 david 1.27 #endif
416 kumpf 1.18     return !!os;
417            }
418            
419            /** Get the next line from the input file.
420            */
421            PEGASUS_COMMON_LINKAGE Boolean GetLine(PEGASUS_STD(istream)& is, String& line);
422            
423 mike  1.15 PEGASUS_NAMESPACE_END
424            
425            #endif /* Pegasus_FileSystem_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2