1998-12-08 05:15:50 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
|
1998-12-09 01:45:42 +03:00
|
|
|
// First checked in on 98/11/20 by John R. McMullen in the wrong directory.
|
|
|
|
// Checked in again 98/12/04.
|
|
|
|
// Polished version 98/12/08.
|
1998-12-08 05:15:50 +03:00
|
|
|
|
|
|
|
//========================================================================================
|
|
|
|
//
|
1998-12-09 01:45:42 +03:00
|
|
|
// Classes defined:
|
|
|
|
//
|
1999-02-28 04:36:48 +03:00
|
|
|
// nsFilePath, nsFileURL, nsFileSpec, nsPersistentFileDescriptor
|
|
|
|
// nsDirectoryIterator. Oh, and a convenience class nsAutoCString.
|
1999-02-25 23:49:47 +03:00
|
|
|
//
|
|
|
|
// Q. How should I represent files at run time?
|
|
|
|
// A. Use nsFileSpec. Using char* will lose information on some platforms.
|
|
|
|
//
|
|
|
|
// Q. Then what are nsFilePath and nsFileURL for?
|
|
|
|
// A. Only when you need a char* parameter for legacy code.
|
|
|
|
//
|
|
|
|
// Q. How should I represent files in a persistent way (eg, in a disk file)?
|
|
|
|
// A. Use nsPersistentFileDescriptor. Convert to and from nsFileSpec at run time.
|
1998-12-09 01:45:42 +03:00
|
|
|
//
|
|
|
|
// This suite provides the following services:
|
|
|
|
//
|
|
|
|
// 1. Encapsulates all platform-specific file details, so that files can be
|
|
|
|
// described correctly without any platform #ifdefs
|
|
|
|
//
|
|
|
|
// 2. Type safety. This will fix the problems that used to occur because people
|
|
|
|
// confused file paths. They used to use const char*, which could mean three
|
|
|
|
// or four different things. Bugs were introduced as people coded, right up
|
|
|
|
// to the moment Communicator 4.5 shipped.
|
|
|
|
//
|
|
|
|
// 3. Used in conjunction with nsFileStream.h (q.v.), this supports all the power
|
1998-12-19 02:06:54 +03:00
|
|
|
// and readability of the ansi stream syntax.
|
1998-12-09 01:45:42 +03:00
|
|
|
//
|
|
|
|
// Basic example:
|
|
|
|
//
|
|
|
|
// nsFilePath myPath("/Development/iotest.txt");
|
|
|
|
//
|
|
|
|
// nsOutputFileStream testStream(myPath);
|
1998-12-19 02:06:54 +03:00
|
|
|
// testStream << "Hello World" << nsEndl;
|
1998-12-09 01:45:42 +03:00
|
|
|
//
|
|
|
|
// 4. Handy methods for manipulating file specifiers safely, e.g. MakeUnique(),
|
|
|
|
// SetLeafName(), Exists().
|
|
|
|
//
|
|
|
|
// 5. Easy cross-conversion.
|
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
//
|
|
|
|
// Initialize a URL from a string without suffix
|
|
|
|
//
|
|
|
|
// nsFileURL fileURL("file:///Development/MPW/MPW%20Shell");
|
|
|
|
//
|
|
|
|
// Initialize a Unix path from a URL
|
|
|
|
//
|
|
|
|
// nsFilePath filePath(fileURL);
|
|
|
|
//
|
|
|
|
// Initialize a native file spec from a URL
|
1998-12-08 05:15:50 +03:00
|
|
|
//
|
1999-02-25 23:49:47 +03:00
|
|
|
// nsFileSpec fileSpec(fileURL);
|
1998-12-09 01:45:42 +03:00
|
|
|
//
|
|
|
|
// Make the spec unique (this one has no suffix).
|
|
|
|
//
|
|
|
|
// fileSpec.MakeUnique();
|
|
|
|
//
|
|
|
|
// Assign the spec to a URL
|
|
|
|
//
|
|
|
|
// fileURL = fileSpec;
|
|
|
|
//
|
|
|
|
// Assign a unix path using a string with a suffix.
|
|
|
|
//
|
|
|
|
// filePath = "/Development/MPW/SysErrs.err";
|
|
|
|
//
|
|
|
|
// Assign to a file spec using a unix path.
|
|
|
|
//
|
|
|
|
// fileSpec = filePath;
|
|
|
|
//
|
|
|
|
// Make this unique (this one has a suffix).
|
|
|
|
//
|
|
|
|
// fileSpec.MakeUnique();
|
|
|
|
//
|
|
|
|
// 6. Fixes a bug that have been there for a long time, and
|
|
|
|
// is inevitable if you use NSPR alone, where files are described as paths.
|
|
|
|
//
|
|
|
|
// The problem affects platforms (Macintosh) in which a path does not fully
|
|
|
|
// specify a file, because two volumes can have the same name. This
|
|
|
|
// is solved by holding a "private" native file spec inside the
|
|
|
|
// nsFilePath and nsFileURL classes, which is used when appropriate.
|
|
|
|
//
|
|
|
|
// Not yet done:
|
|
|
|
//
|
|
|
|
// Equality operators... much more.
|
1998-12-08 05:15:50 +03:00
|
|
|
//
|
|
|
|
//========================================================================================
|
|
|
|
|
1998-12-09 01:45:42 +03:00
|
|
|
#ifndef _FILESPEC_H_
|
|
|
|
#define _FILESPEC_H_
|
|
|
|
|
1998-12-12 04:25:32 +03:00
|
|
|
#include "nscore.h"
|
1999-02-25 23:49:47 +03:00
|
|
|
#include "nsError.h"
|
1999-02-28 04:36:48 +03:00
|
|
|
#include "nsString.h"
|
1998-12-12 04:25:32 +03:00
|
|
|
|
1998-12-09 08:09:28 +03:00
|
|
|
//========================================================================================
|
|
|
|
// Compiler-specific macros, as needed
|
|
|
|
//========================================================================================
|
1998-12-11 04:42:04 +03:00
|
|
|
#if !defined(NS_USING_NAMESPACE) && (defined(__MWERKS__) || defined(XP_PC))
|
1998-12-09 08:09:28 +03:00
|
|
|
#define NS_USING_NAMESPACE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef NS_USING_NAMESPACE
|
1998-12-11 06:17:47 +03:00
|
|
|
|
1998-12-09 08:09:28 +03:00
|
|
|
#define NS_NAMESPACE_PROTOTYPE
|
|
|
|
#define NS_NAMESPACE namespace
|
|
|
|
#define NS_NAMESPACE_END
|
1999-02-25 23:49:47 +03:00
|
|
|
#define NS_EXPLICIT explicit
|
1998-12-09 08:09:28 +03:00
|
|
|
#else
|
1998-12-11 06:17:47 +03:00
|
|
|
|
1998-12-09 08:09:28 +03:00
|
|
|
#define NS_NAMESPACE_PROTOTYPE static
|
|
|
|
#define NS_NAMESPACE struct
|
|
|
|
#define NS_NAMESPACE_END ;
|
1999-02-25 23:49:47 +03:00
|
|
|
#define NS_EXPLICIT
|
1998-12-11 06:17:47 +03:00
|
|
|
|
1998-12-09 08:09:28 +03:00
|
|
|
#endif
|
|
|
|
//=========================== End Compiler-specific macros ===============================
|
|
|
|
|
1998-12-08 05:15:50 +03:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#include <Files.h>
|
1999-01-07 02:38:21 +03:00
|
|
|
#elif defined(XP_UNIX) || defined (XP_OS2)
|
|
|
|
#include <dirent.h>
|
1999-01-27 02:49:33 +03:00
|
|
|
#elif XP_PC
|
|
|
|
#include "prio.h"
|
1998-12-08 05:15:50 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//========================================================================================
|
|
|
|
// Here are the allowable ways to describe a file.
|
|
|
|
//========================================================================================
|
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
class nsFileSpec; // Preferred. For i/o use nsInputFileStream, nsOutputFileStream
|
|
|
|
class nsFilePath; // This can be passed to NSPR file I/O routines, if you must.
|
1998-12-08 05:15:50 +03:00
|
|
|
class nsFileURL;
|
1999-02-25 23:49:47 +03:00
|
|
|
class nsPersistentFileDescriptor; // Used for storage across program launches.
|
1998-12-08 05:15:50 +03:00
|
|
|
|
|
|
|
#define kFileURLPrefix "file://"
|
|
|
|
#define kFileURLPrefixLength (7)
|
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
class nsOutputStream;
|
|
|
|
class nsInputStream;
|
1999-02-28 04:36:48 +03:00
|
|
|
class nsIOutputStream;
|
|
|
|
class nsIInputStream;
|
1999-02-25 23:49:47 +03:00
|
|
|
class nsOutputFileStream;
|
|
|
|
class nsInputFileStream;
|
1999-02-28 04:36:48 +03:00
|
|
|
class nsString;
|
1998-12-19 02:06:54 +03:00
|
|
|
|
1998-12-08 05:15:50 +03:00
|
|
|
//========================================================================================
|
1999-02-25 23:49:47 +03:00
|
|
|
// Conversion of native file errors to nsresult values. These are really only for use
|
|
|
|
// in the file module, clients of this interface shouldn't really need them.
|
|
|
|
// Error results returned from this interface have, in the low-order 16 bits,
|
|
|
|
// native errors that are masked to 16 bits. Assumption: a native error of 0 is success
|
|
|
|
// on all platforms. Note the way we define this using an inline function. This
|
|
|
|
// avoids multiple evaluation if people go NS_FILE_RESULT(function_call()).
|
|
|
|
#define NS_FILE_RESULT(x) ns_file_convert_result((PRInt32)x)
|
|
|
|
nsresult ns_file_convert_result(PRInt32 nativeErr);
|
|
|
|
#define NS_FILE_FAILURE NS_FILE_RESULT(-1)
|
|
|
|
|
1999-02-28 04:36:48 +03:00
|
|
|
//========================================================================================
|
|
|
|
class NS_BASE nsAutoCString
|
|
|
|
//
|
|
|
|
// This should be in nsString.h, but the owner would not reply to my proposal. After four
|
|
|
|
// weeks, I decided to put it in here.
|
|
|
|
//
|
|
|
|
// This is a quiet little class that acts as a sort of autoptr for
|
|
|
|
// a const char*. If you used to call nsString::ToNewCString(), just
|
|
|
|
// to pass the result a parameter list, it was a nuisance having to
|
|
|
|
// call delete [] on the result after the call. Now you can say
|
|
|
|
// nsString myStr;
|
|
|
|
// ...
|
|
|
|
// f(nsAutoCString(myStr));
|
|
|
|
// where f is declared as void f(const char*); This call will
|
|
|
|
// make a temporary char* pointer on the stack and delete[] it
|
|
|
|
// when the function returns.
|
|
|
|
//========================================================================================
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_EXPLICIT nsAutoCString(const nsString& other) : mCString(other.ToNewCString()) {}
|
|
|
|
virtual ~nsAutoCString();
|
|
|
|
operator const char*() const { return mCString; }
|
|
|
|
operator const char*() { return mCString; }
|
|
|
|
protected:
|
|
|
|
const char* mCString;
|
|
|
|
}; // class nsAutoCString
|
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
//========================================================================================
|
|
|
|
class NS_BASE nsFileSpec
|
1998-12-09 01:45:42 +03:00
|
|
|
// This is whatever each platform really prefers to describe files as. Declared first
|
1999-02-25 23:49:47 +03:00
|
|
|
// because the other two types have an embeded nsFileSpec object.
|
1998-12-08 05:15:50 +03:00
|
|
|
//========================================================================================
|
|
|
|
{
|
1998-12-09 01:45:42 +03:00
|
|
|
public:
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpec();
|
|
|
|
NS_EXPLICIT nsFileSpec(const char* inString, PRBool inCreateDirs = PR_FALSE);
|
1999-02-28 04:36:48 +03:00
|
|
|
NS_EXPLICIT nsFileSpec(const nsString& inString, PRBool inCreateDirs = PR_FALSE);
|
1999-02-25 23:49:47 +03:00
|
|
|
NS_EXPLICIT nsFileSpec(const nsFilePath& inPath);
|
|
|
|
NS_EXPLICIT nsFileSpec(const nsFileURL& inURL);
|
|
|
|
NS_EXPLICIT nsFileSpec(const nsPersistentFileDescriptor& inURL);
|
|
|
|
nsFileSpec(const nsFileSpec& inPath);
|
|
|
|
virtual ~nsFileSpec();
|
1998-12-09 01:45:42 +03:00
|
|
|
|
1998-12-09 11:47:30 +03:00
|
|
|
void operator = (const char* inPath);
|
1999-02-28 04:36:48 +03:00
|
|
|
void operator = (const nsString& inPath)
|
|
|
|
{
|
1999-03-02 22:23:06 +03:00
|
|
|
const nsAutoCString path(inPath);
|
|
|
|
*this = path;
|
1999-02-28 04:36:48 +03:00
|
|
|
}
|
1998-12-09 01:45:42 +03:00
|
|
|
void operator = (const nsFilePath& inPath);
|
|
|
|
void operator = (const nsFileURL& inURL);
|
1999-02-25 23:49:47 +03:00
|
|
|
void operator = (const nsFileSpec& inOther);
|
|
|
|
void operator = (const nsPersistentFileDescriptor& inOther);
|
1998-12-08 05:15:50 +03:00
|
|
|
|
1999-01-27 02:49:33 +03:00
|
|
|
#ifndef XP_MAC
|
|
|
|
operator const char* () const { return mPath; }
|
|
|
|
// This is the only automatic conversion to const char*
|
|
|
|
// that is provided, and it allows the
|
|
|
|
// path to be "passed" to NSPR file routines.
|
|
|
|
#endif
|
|
|
|
|
1998-12-08 05:15:50 +03:00
|
|
|
#ifdef XP_MAC
|
1998-12-09 01:45:42 +03:00
|
|
|
// For Macintosh people, this is meant to be useful in its own right as a C++ version
|
1998-12-09 11:47:30 +03:00
|
|
|
// of the FSSpec struct.
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpec(
|
1998-12-09 01:45:42 +03:00
|
|
|
short vRefNum,
|
|
|
|
long parID,
|
|
|
|
ConstStr255Param name);
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpec(const FSSpec& inSpec)
|
|
|
|
: mSpec(inSpec), mError(NS_OK) {}
|
1998-12-09 01:45:42 +03:00
|
|
|
|
|
|
|
operator FSSpec* () { return &mSpec; }
|
|
|
|
operator const FSSpec* const () { return &mSpec; }
|
1998-12-09 11:47:30 +03:00
|
|
|
operator FSSpec& () { return mSpec; }
|
1998-12-09 01:45:42 +03:00
|
|
|
operator const FSSpec& () const { return mSpec; }
|
1998-12-09 11:47:30 +03:00
|
|
|
void MakeAliasSafe();
|
1999-01-07 02:38:21 +03:00
|
|
|
// Called for the spec of an alias. Copies the alias to
|
|
|
|
// a secret temp directory and modifies the spec to point
|
|
|
|
// to it. Sets mError.
|
1999-02-25 23:49:47 +03:00
|
|
|
void ResolveAlias(PRBool& wasAliased);
|
1999-01-07 02:38:21 +03:00
|
|
|
// Called for the spec of an alias. Modifies the spec to
|
|
|
|
// point to the original. Sets mError.
|
1998-12-09 01:45:42 +03:00
|
|
|
void MakeUnique(ConstStr255Param inSuggestedLeafName);
|
|
|
|
StringPtr GetLeafPName() { return mSpec.name; }
|
|
|
|
ConstStr255Param GetLeafPName() const { return mSpec.name; }
|
1998-12-09 11:47:30 +03:00
|
|
|
#endif // end of Macintosh utility methods.
|
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
PRBool Valid() const { return NS_SUCCEEDED(Error()); }
|
|
|
|
nsresult Error() const { return mError; }
|
1998-12-08 05:15:50 +03:00
|
|
|
|
1999-02-26 03:14:57 +03:00
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
friend NS_BASE nsOutputStream& operator << (
|
|
|
|
nsOutputStream& s,
|
|
|
|
const nsFileSpec& spec); // THIS IS FOR DEBUGGING ONLY.
|
|
|
|
// see PersistentFileDescriptor for the real deal.
|
1999-02-26 03:14:57 +03:00
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Queries and path algebra. These do not modify the disk.
|
|
|
|
//--------------------------------------------------
|
1998-12-09 11:47:30 +03:00
|
|
|
|
|
|
|
char* GetLeafName() const; // Allocated. Use delete [].
|
|
|
|
void SetLeafName(const char* inLeafName);
|
1999-01-07 02:38:21 +03:00
|
|
|
// inLeafName can be a relative path, so this allows
|
|
|
|
// one kind of concatenation of "paths".
|
1999-02-28 04:36:48 +03:00
|
|
|
void SetLeafName(const nsString& inLeafName)
|
|
|
|
{
|
1999-03-02 22:23:06 +03:00
|
|
|
const nsAutoCString leafName(inLeafName);
|
|
|
|
SetLeafName(leafName);
|
1999-02-28 04:36:48 +03:00
|
|
|
}
|
1999-02-25 23:49:47 +03:00
|
|
|
void GetParent(nsFileSpec& outSpec) const;
|
1999-01-07 02:38:21 +03:00
|
|
|
// Return the filespec of the parent directory. Used
|
|
|
|
// in conjunction with GetLeafName(), this lets you
|
|
|
|
// parse a path into a list of node names. Beware,
|
|
|
|
// however, that the top node is still not a name,
|
|
|
|
// but a spec. Volumes on Macintosh can have identical
|
|
|
|
// names. Perhaps could be used for an operator --() ?
|
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpec operator + (const char* inRelativePath) const;
|
1999-02-28 04:36:48 +03:00
|
|
|
nsFileSpec operator + (const nsString& inRelativePath) const
|
|
|
|
{
|
1999-03-02 22:23:06 +03:00
|
|
|
const nsAutoCString
|
|
|
|
relativePath(inRelativePath);
|
|
|
|
return *this + relativePath;
|
1999-02-28 04:36:48 +03:00
|
|
|
}
|
1999-01-07 02:38:21 +03:00
|
|
|
void operator += (const char* inRelativePath);
|
|
|
|
// Concatenate the relative path to this directory.
|
|
|
|
// Used for constructing the filespec of a descendant.
|
|
|
|
// This must be a directory for this to work. This differs
|
|
|
|
// from SetLeafName(), since the latter will work
|
|
|
|
// starting with a sibling of the directory and throws
|
|
|
|
// away its leaf information, whereas this one assumes
|
|
|
|
// this is a directory, and the relative path starts
|
|
|
|
// "below" this.
|
1999-02-28 04:36:48 +03:00
|
|
|
void operator += (const nsString& inRelativePath)
|
|
|
|
{
|
1999-03-02 22:23:06 +03:00
|
|
|
const nsAutoCString relativePath(inRelativePath);
|
|
|
|
*this += relativePath;
|
1999-02-28 04:36:48 +03:00
|
|
|
}
|
1999-01-07 02:38:21 +03:00
|
|
|
|
1998-12-09 01:45:42 +03:00
|
|
|
void MakeUnique();
|
1998-12-09 11:47:30 +03:00
|
|
|
void MakeUnique(const char* inSuggestedLeafName);
|
1999-02-28 04:36:48 +03:00
|
|
|
void MakeUnique(const nsString& inSuggestedLeafName)
|
|
|
|
{
|
1999-03-02 22:23:06 +03:00
|
|
|
const nsAutoCString suggestedLeafName(inSuggestedLeafName);
|
|
|
|
MakeUnique(suggestedLeafName);
|
1999-02-28 04:36:48 +03:00
|
|
|
}
|
1998-12-09 01:45:42 +03:00
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
PRBool IsDirectory() const;
|
1999-01-07 02:38:21 +03:00
|
|
|
// More stringent than Exists()
|
1999-02-25 23:49:47 +03:00
|
|
|
PRBool IsFile() const;
|
1999-01-07 02:38:21 +03:00
|
|
|
// More stringent than Exists()
|
1999-02-25 23:49:47 +03:00
|
|
|
PRBool Exists() const;
|
1999-01-07 02:38:21 +03:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Creation and deletion of objects. These can modify the disk.
|
|
|
|
//--------------------------------------------------
|
|
|
|
|
|
|
|
void CreateDirectory(int mode = 0700 /* for unix */);
|
1999-02-28 04:36:48 +03:00
|
|
|
void Delete(PRBool inRecursive) const;
|
1999-01-07 02:38:21 +03:00
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
nsresult Rename(const char* inNewName); // not const: gets updated
|
1999-02-28 04:36:48 +03:00
|
|
|
nsresult Rename(const nsString& inNewName)
|
|
|
|
{
|
1999-03-02 22:23:06 +03:00
|
|
|
const nsAutoCString newName(inNewName);
|
|
|
|
return Rename(newName);
|
1999-02-28 04:36:48 +03:00
|
|
|
}
|
1999-02-25 23:49:47 +03:00
|
|
|
nsresult Copy(const nsFileSpec& inNewParentDirectory) const;
|
|
|
|
nsresult Move(const nsFileSpec& inNewParentDirectory) const;
|
|
|
|
nsresult Execute(const char* args) const;
|
1999-02-28 04:36:48 +03:00
|
|
|
nsresult Execute(const nsString& args) const
|
|
|
|
{
|
1999-03-02 22:23:06 +03:00
|
|
|
const nsAutoCString argsString(args);
|
|
|
|
return Execute(argsString);
|
1999-02-28 04:36:48 +03:00
|
|
|
}
|
1999-02-25 23:49:47 +03:00
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Data
|
|
|
|
//--------------------------------------------------
|
|
|
|
|
1998-12-09 01:45:42 +03:00
|
|
|
private:
|
|
|
|
friend class nsFilePath;
|
1998-12-08 05:15:50 +03:00
|
|
|
#ifdef XP_MAC
|
1998-12-09 08:09:28 +03:00
|
|
|
FSSpec mSpec;
|
1998-12-11 04:42:04 +03:00
|
|
|
#else
|
1998-12-09 11:47:30 +03:00
|
|
|
char* mPath;
|
1998-12-08 05:15:50 +03:00
|
|
|
#endif
|
1999-02-25 23:49:47 +03:00
|
|
|
nsresult mError;
|
|
|
|
}; // class nsFileSpec
|
|
|
|
|
|
|
|
// FOR HISTORICAL REASONS:
|
|
|
|
|
|
|
|
typedef nsFileSpec nsNativeFileSpec;
|
1998-12-08 05:15:50 +03:00
|
|
|
|
|
|
|
//========================================================================================
|
1998-12-12 04:25:32 +03:00
|
|
|
class NS_BASE nsFileURL
|
1998-12-09 01:45:42 +03:00
|
|
|
// This is an escaped string that looks like "file:///foo/bar/mumble%20fish". Since URLs
|
|
|
|
// are the standard way of doing things in mozilla, this allows a string constructor,
|
|
|
|
// which just stashes the string with no conversion.
|
1998-12-08 05:15:50 +03:00
|
|
|
//========================================================================================
|
|
|
|
{
|
1998-12-09 01:45:42 +03:00
|
|
|
public:
|
|
|
|
nsFileURL(const nsFileURL& inURL);
|
1999-02-25 23:49:47 +03:00
|
|
|
NS_EXPLICIT nsFileURL(const char* inString, PRBool inCreateDirs = PR_FALSE);
|
1999-02-28 04:36:48 +03:00
|
|
|
NS_EXPLICIT nsFileURL(const nsString& inString, PRBool inCreateDirs = PR_FALSE);
|
1999-02-25 23:49:47 +03:00
|
|
|
NS_EXPLICIT nsFileURL(const nsFilePath& inPath);
|
|
|
|
NS_EXPLICIT nsFileURL(const nsFileSpec& inPath);
|
1998-12-09 11:47:30 +03:00
|
|
|
virtual ~nsFileURL();
|
1998-12-09 01:45:42 +03:00
|
|
|
|
1998-12-09 11:47:30 +03:00
|
|
|
// nsString GetString() const { return mPath; }
|
1998-12-09 01:45:42 +03:00
|
|
|
// may be needed for implementation reasons,
|
|
|
|
// but should not provide a conversion constructor.
|
|
|
|
|
|
|
|
void operator = (const nsFileURL& inURL);
|
1998-12-09 11:47:30 +03:00
|
|
|
void operator = (const char* inString);
|
1999-02-28 04:36:48 +03:00
|
|
|
void operator = (const nsString& inString)
|
|
|
|
{
|
1999-03-02 22:23:06 +03:00
|
|
|
const nsAutoCString string(inString);
|
|
|
|
*this = string;
|
1999-02-28 04:36:48 +03:00
|
|
|
}
|
1998-12-09 01:45:42 +03:00
|
|
|
void operator = (const nsFilePath& inOther);
|
1999-02-25 23:49:47 +03:00
|
|
|
void operator = (const nsFileSpec& inOther);
|
1998-12-08 05:15:50 +03:00
|
|
|
|
1999-03-06 01:53:56 +03:00
|
|
|
operator const char* const () { return mURL; }
|
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
friend NS_BASE nsOutputStream& operator << (
|
|
|
|
nsOutputStream& s, const nsFileURL& spec);
|
1998-12-09 11:47:30 +03:00
|
|
|
|
1998-12-08 05:15:50 +03:00
|
|
|
#ifdef XP_MAC
|
1999-02-25 23:49:47 +03:00
|
|
|
// Accessor to allow quick assignment to a mFileSpec
|
|
|
|
const nsFileSpec& GetFileSpec() const { return mFileSpec; }
|
1998-12-08 05:15:50 +03:00
|
|
|
#endif
|
1998-12-09 01:45:42 +03:00
|
|
|
private:
|
|
|
|
// Should not be defined (only nsFilePath is to be treated as strings.
|
1998-12-09 11:47:30 +03:00
|
|
|
operator char* ();
|
1998-12-09 01:45:42 +03:00
|
|
|
private:
|
1999-01-07 02:38:21 +03:00
|
|
|
friend class nsFilePath; // to allow construction of nsFilePath
|
1998-12-09 11:47:30 +03:00
|
|
|
char* mURL;
|
1998-12-08 05:15:50 +03:00
|
|
|
#ifdef XP_MAC
|
1998-12-09 01:45:42 +03:00
|
|
|
// Since the path on the macintosh does not uniquely specify a file (volumes
|
1999-02-25 23:49:47 +03:00
|
|
|
// can have the same name), stash the secret nsFileSpec, too.
|
|
|
|
nsFileSpec mFileSpec;
|
1998-12-08 05:15:50 +03:00
|
|
|
#endif
|
|
|
|
}; // class nsFileURL
|
|
|
|
|
|
|
|
//========================================================================================
|
1998-12-12 04:25:32 +03:00
|
|
|
class NS_BASE nsFilePath
|
1998-12-09 01:45:42 +03:00
|
|
|
// This is a string that looks like "/foo/bar/mumble%20fish". Same as nsFileURL, but
|
|
|
|
// without the "file:// prefix".
|
1998-12-08 05:15:50 +03:00
|
|
|
//========================================================================================
|
|
|
|
{
|
1998-12-09 01:45:42 +03:00
|
|
|
public:
|
|
|
|
nsFilePath(const nsFilePath& inPath);
|
1999-02-25 23:49:47 +03:00
|
|
|
NS_EXPLICIT nsFilePath(const char* inString, PRBool inCreateDirs = PR_FALSE);
|
1999-02-28 04:36:48 +03:00
|
|
|
NS_EXPLICIT nsFilePath(const nsString& inString, PRBool inCreateDirs = PR_FALSE);
|
1999-02-25 23:49:47 +03:00
|
|
|
NS_EXPLICIT nsFilePath(const nsFileURL& inURL);
|
|
|
|
NS_EXPLICIT nsFilePath(const nsFileSpec& inPath);
|
1998-12-09 11:47:30 +03:00
|
|
|
virtual ~nsFilePath();
|
1998-12-09 01:45:42 +03:00
|
|
|
|
|
|
|
|
1998-12-09 11:47:30 +03:00
|
|
|
operator const char* () const { return mPath; }
|
1998-12-09 01:45:42 +03:00
|
|
|
// This is the only automatic conversion to const char*
|
|
|
|
// that is provided, and it allows the
|
|
|
|
// path to be "passed" to NSPR file routines.
|
1998-12-09 11:47:30 +03:00
|
|
|
operator char* () { return mPath; }
|
1998-12-09 01:45:42 +03:00
|
|
|
// This is the only automatic conversion to string
|
|
|
|
// that is provided, because a naked string should
|
|
|
|
// only mean a standard file path.
|
|
|
|
|
|
|
|
void operator = (const nsFilePath& inPath);
|
1998-12-09 11:47:30 +03:00
|
|
|
void operator = (const char* inString);
|
1999-02-28 04:36:48 +03:00
|
|
|
void operator = (const nsString& inString)
|
|
|
|
{
|
1999-03-02 22:23:06 +03:00
|
|
|
const nsAutoCString string(inString);
|
|
|
|
*this = string;
|
1999-02-28 04:36:48 +03:00
|
|
|
}
|
1998-12-09 01:45:42 +03:00
|
|
|
void operator = (const nsFileURL& inURL);
|
1999-02-25 23:49:47 +03:00
|
|
|
void operator = (const nsFileSpec& inOther);
|
1998-12-08 05:15:50 +03:00
|
|
|
|
|
|
|
#ifdef XP_MAC
|
1998-12-09 01:45:42 +03:00
|
|
|
public:
|
1999-02-25 23:49:47 +03:00
|
|
|
// Accessor to allow quick assignment to a mFileSpec
|
|
|
|
const nsFileSpec& GetFileSpec() const { return mFileSpec; }
|
1998-12-08 05:15:50 +03:00
|
|
|
#endif
|
|
|
|
|
1998-12-09 01:45:42 +03:00
|
|
|
private:
|
1998-12-08 05:15:50 +03:00
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
char* mPath;
|
1998-12-08 05:15:50 +03:00
|
|
|
#ifdef XP_MAC
|
1998-12-09 01:45:42 +03:00
|
|
|
// Since the path on the macintosh does not uniquely specify a file (volumes
|
1999-02-25 23:49:47 +03:00
|
|
|
// can have the same name), stash the secret nsFileSpec, too.
|
|
|
|
nsFileSpec mFileSpec;
|
1998-12-08 05:15:50 +03:00
|
|
|
#endif
|
1998-12-09 01:45:42 +03:00
|
|
|
}; // class nsFilePath
|
1998-12-08 05:15:50 +03:00
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
//========================================================================================
|
|
|
|
class NS_BASE nsPersistentFileDescriptor
|
|
|
|
//========================================================================================
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsPersistentFileDescriptor() : mDescriptorString(nsnull) {}
|
|
|
|
// For use prior to reading in from a stream
|
|
|
|
nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inPath);
|
|
|
|
virtual ~nsPersistentFileDescriptor();
|
|
|
|
void operator = (const nsPersistentFileDescriptor& inPath);
|
|
|
|
|
|
|
|
// Conversions
|
|
|
|
nsPersistentFileDescriptor(const nsFileSpec& inPath);
|
|
|
|
void operator = (const nsFileSpec& inPath);
|
|
|
|
|
1999-02-28 04:36:48 +03:00
|
|
|
nsresult Read(nsIInputStream* aStream);
|
|
|
|
nsresult Write(nsIOutputStream* aStream);
|
|
|
|
// writes the data to a file
|
1999-02-25 23:49:47 +03:00
|
|
|
friend NS_BASE nsInputStream& operator >> (nsInputStream&, nsPersistentFileDescriptor&);
|
|
|
|
// reads the data from a file
|
|
|
|
friend NS_BASE nsOutputStream& operator << (nsOutputStream&, const nsPersistentFileDescriptor&);
|
|
|
|
// writes the data to a file
|
|
|
|
friend class nsFileSpec;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Here are the ways to get data in and out of a file.
|
|
|
|
void GetData(void*& outData, PRInt32& outSize) const;
|
|
|
|
// DON'T FREE the returned data!
|
|
|
|
void SetData(const void* inData, PRInt32 inSize);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
char* mDescriptorString;
|
|
|
|
|
|
|
|
}; // class nsPersistentFileDescriptor
|
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
//========================================================================================
|
1999-01-27 02:49:33 +03:00
|
|
|
class NS_BASE nsDirectoryIterator
|
1999-01-07 02:38:21 +03:00
|
|
|
// Example:
|
|
|
|
//
|
1999-02-25 23:49:47 +03:00
|
|
|
// nsFileSpec parentDir(...); // directory over whose children we shall iterate
|
1999-01-07 02:38:21 +03:00
|
|
|
// for (nsDirectoryIterator i(parentDir); i; i++)
|
|
|
|
// {
|
1999-02-25 23:49:47 +03:00
|
|
|
// // do something with (const nsFileSpec&)i
|
1999-01-07 02:38:21 +03:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// or:
|
|
|
|
//
|
1999-02-25 23:49:47 +03:00
|
|
|
// for (nsDirectoryIterator i(parentDir, PR_FALSE); i; i--)
|
1999-01-07 02:38:21 +03:00
|
|
|
// {
|
1999-02-25 23:49:47 +03:00
|
|
|
// // do something with (const nsFileSpec&)i
|
1999-01-07 02:38:21 +03:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Currently, the only platform on which backwards iteration actually goes backwards
|
|
|
|
// is Macintosh. On other platforms, both styles will work, but will go forwards.
|
|
|
|
//========================================================================================
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsDirectoryIterator(
|
1999-02-25 23:49:47 +03:00
|
|
|
const nsFileSpec& parent,
|
1999-01-07 02:38:21 +03:00
|
|
|
int iterateDirection = +1);
|
|
|
|
#ifndef XP_MAC
|
|
|
|
// Macintosh currently doesn't allocate, so needn't clean up.
|
|
|
|
virtual ~nsDirectoryIterator();
|
|
|
|
#endif
|
1999-02-25 23:49:47 +03:00
|
|
|
PRBool Exists() const { return mExists; }
|
1999-01-07 02:38:21 +03:00
|
|
|
nsDirectoryIterator& operator ++(); // moves to the next item, if any.
|
|
|
|
nsDirectoryIterator& operator ++(int) { return ++(*this); } // post-increment.
|
|
|
|
nsDirectoryIterator& operator --(); // moves to the previous item, if any.
|
|
|
|
nsDirectoryIterator& operator --(int) { return --(*this); } // post-decrement.
|
1999-02-25 23:49:47 +03:00
|
|
|
operator nsFileSpec&() { return mCurrent; }
|
1999-01-07 02:38:21 +03:00
|
|
|
private:
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpec mCurrent;
|
1999-02-28 04:36:48 +03:00
|
|
|
PRBool mExists;
|
1999-01-07 02:38:21 +03:00
|
|
|
|
1999-01-27 02:49:33 +03:00
|
|
|
#if defined(XP_UNIX)
|
1999-01-07 02:38:21 +03:00
|
|
|
DIR* mDir;
|
1999-01-27 02:49:33 +03:00
|
|
|
#elif defined(XP_PC)
|
|
|
|
PRDir* mDir; // XXX why not use PRDir for Unix & Mac, too?
|
1999-01-07 02:38:21 +03:00
|
|
|
#elif defined(XP_MAC)
|
|
|
|
OSErr SetToIndex();
|
|
|
|
short mIndex;
|
|
|
|
short mMaxIndex;
|
|
|
|
#endif
|
|
|
|
}; // class nsDirectoryIterator
|
|
|
|
|
1998-12-08 05:15:50 +03:00
|
|
|
#endif // _FILESPEC_H_
|