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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2