This commit is contained in:
mcmullen@netscape.com
1999-03-19 23:09:39 +00:00
parent cdb11981ca
commit a9b9bd1efc
14 changed files with 1212 additions and 844 deletions

View File

@@ -206,6 +206,74 @@ protected:
const char* mCString; const char* mCString;
}; // class nsAutoCString }; // class nsAutoCString
//========================================================================================
class NS_BASE nsSimpleCharString
// An envelope for char*: reference counted. Used internally by all the nsFileSpec
// classes below.
//========================================================================================
{
public:
nsSimpleCharString();
nsSimpleCharString(const char*);
nsSimpleCharString(const nsString&);
nsSimpleCharString(const nsSimpleCharString&);
nsSimpleCharString(const char* inData, PRUint32 inLength);
~nsSimpleCharString();
void operator = (const char*);
void operator = (const nsString&);
void operator = (const nsSimpleCharString&);
operator const char*() const { return mData ? mData->mString : 0; }
operator char* ()
{
ReallocData(Length()); // requires detaching if shared...
return mData ? mData->mString : 0;
}
PRBool operator == (const char*);
PRBool operator == (const nsString&);
PRBool operator == (const nsSimpleCharString&);
void operator += (const char* inString);
nsSimpleCharString operator + (const char* inString) const;
char operator [](int i) const { return mData ? mData->mString[i] : 0; }
char& operator [](int i)
{
if (i >= (int)Length())
ReallocData(i + 1);
return mData->mString[i]; // caveat appelator
}
char& operator [](unsigned int i) { return (*this)[(int)i]; }
void Catenate(const char* inString1, const char* inString2);
void SetToEmpty();
PRBool IsEmpty() const { return Length() == 0; }
PRUint32 Length() const { return mData ? mData->mLength : 0; }
void CopyFrom(const char* inData, PRUint32 inLength);
void LeafReplace(char inSeparator, const char* inLeafName);
char* GetLeaf(char inSeparator) const; // use PR_Free()
void Unescape();
protected:
void AddRefData();
void ReleaseData();
void ReallocData(PRUint32 inLength);
// DATA
protected:
struct Data {
int mRefCount;
PRUint32 mLength;
char mString[1];
};
Data* mData;
}; // class nsSimpleCharString
//======================================================================================== //========================================================================================
class NS_BASE nsFileSpec class NS_BASE nsFileSpec
// This is whatever each platform really prefers to describe files as. Declared first // This is whatever each platform really prefers to describe files as. Declared first
@@ -255,7 +323,7 @@ class NS_BASE nsFileSpec
long parID, long parID,
ConstStr255Param name); ConstStr255Param name);
nsFileSpec(const FSSpec& inSpec) nsFileSpec(const FSSpec& inSpec)
: mSpec(inSpec), mError(NS_OK), mPath(nsnull) {} : mSpec(inSpec), mError(NS_OK) {}
void operator = (const FSSpec& inSpec) void operator = (const FSSpec& inSpec)
{ mSpec = inSpec; mError = NS_OK; } { mSpec = inSpec; mError = NS_OK; }
@@ -284,7 +352,7 @@ class NS_BASE nsFileSpec
nsresult Error() const nsresult Error() const
{ {
#ifndef XP_MAC #ifndef XP_MAC
if (!mPath && NS_SUCCEEDED(mError)) if (mPath.IsEmpty() && NS_SUCCEEDED(mError))
((nsFileSpec*)this)->mError = NS_FILE_FAILURE; ((nsFileSpec*)this)->mError = NS_FILE_FAILURE;
#endif #endif
return mError; return mError;
@@ -413,7 +481,7 @@ class NS_BASE nsFileSpec
#ifdef XP_MAC #ifdef XP_MAC
FSSpec mSpec; FSSpec mSpec;
#endif #endif
char* mPath; nsSimpleCharString mPath;
nsresult mError; nsresult mError;
}; // class nsFileSpec }; // class nsFileSpec
@@ -450,8 +518,10 @@ class NS_BASE nsFileURL
void operator = (const nsFilePath& inOther); void operator = (const nsFilePath& inOther);
void operator = (const nsFileSpec& inOther); void operator = (const nsFileSpec& inOther);
operator const char* () const { return mURL; } // deprecated. void operator +=(const char* inRelativeUnixPath);
const char* GetAsString() const { return mURL; } nsFileURL operator +(const char* inRelativeUnixPath) const;
operator const char* () const { return (const char*)mURL; } // deprecated.
const char* GetAsString() const { return (const char*)mURL; }
friend NS_BASE nsOutputStream& operator << ( friend NS_BASE nsOutputStream& operator << (
nsOutputStream& s, const nsFileURL& spec); nsOutputStream& s, const nsFileURL& spec);
@@ -460,12 +530,11 @@ class NS_BASE nsFileURL
// Accessor to allow quick assignment to a mFileSpec // Accessor to allow quick assignment to a mFileSpec
const nsFileSpec& GetFileSpec() const { return mFileSpec; } const nsFileSpec& GetFileSpec() const { return mFileSpec; }
#endif #endif
private:
// Should not be defined (only nsFilePath is to be treated as strings.
operator char* ();
protected: protected:
friend class nsFilePath; // to allow construction of nsFilePath friend class nsFilePath; // to allow construction of nsFilePath
char* mURL; nsSimpleCharString mURL;
#ifdef XP_MAC #ifdef XP_MAC
// Since the path on the macintosh does not uniquely specify a file (volumes // Since the path on the macintosh does not uniquely specify a file (volumes
// can have the same name), stash the secret nsFileSpec, too. // can have the same name), stash the secret nsFileSpec, too.
@@ -493,10 +562,6 @@ class NS_BASE nsFilePath
// This is the only automatic conversion to const char* // This is the only automatic conversion to const char*
// that is provided, and it allows the // that is provided, and it allows the
// path to be "passed" to NSPR file routines. // path to be "passed" to NSPR file routines.
operator char* () { return mPath; }
// 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); void operator = (const nsFilePath& inPath);
void operator = (const char* inString); void operator = (const char* inString);
@@ -508,6 +573,9 @@ class NS_BASE nsFilePath
void operator = (const nsFileURL& inURL); void operator = (const nsFileURL& inURL);
void operator = (const nsFileSpec& inOther); void operator = (const nsFileSpec& inOther);
void operator +=(const char* inRelativeUnixPath);
nsFilePath operator +(const char* inRelativeUnixPath) const;
#ifdef XP_MAC #ifdef XP_MAC
public: public:
// Accessor to allow quick assignment to a mFileSpec // Accessor to allow quick assignment to a mFileSpec
@@ -516,7 +584,7 @@ class NS_BASE nsFilePath
private: private:
char* mPath; nsSimpleCharString mPath;
#ifdef XP_MAC #ifdef XP_MAC
// Since the path on the macintosh does not uniquely specify a file (volumes // Since the path on the macintosh does not uniquely specify a file (volumes
// can have the same name), stash the secret nsFileSpec, too. // can have the same name), stash the secret nsFileSpec, too.
@@ -533,7 +601,7 @@ class NS_BASE nsPersistentFileDescriptor
//======================================================================================== //========================================================================================
{ {
public: public:
nsPersistentFileDescriptor() : mDescriptorString(nsnull) {} nsPersistentFileDescriptor() {}
// For use prior to reading in from a stream // For use prior to reading in from a stream
nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inPath); nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inPath);
virtual ~nsPersistentFileDescriptor(); virtual ~nsPersistentFileDescriptor();
@@ -553,14 +621,13 @@ class NS_BASE nsPersistentFileDescriptor
friend class nsFileSpec; friend class nsFileSpec;
private: private:
// Here are the ways to get data in and out of a file.
void GetData(void*& outData, PRInt32& outSize) const; void GetData(nsSimpleCharString& outData, PRInt32& outSize) const;
// DON'T FREE the returned data! void SetData(const nsSimpleCharString& inData, PRInt32 inSize);
void SetData(const void* inData, PRInt32 inSize);
protected: protected:
char* mDescriptorString; nsSimpleCharString mDescriptorString;
}; // class nsPersistentFileDescriptor }; // class nsPersistentFileDescriptor
@@ -569,16 +636,16 @@ class NS_BASE nsDirectoryIterator
// Example: // Example:
// //
// nsFileSpec parentDir(...); // directory over whose children we shall iterate // nsFileSpec parentDir(...); // directory over whose children we shall iterate
// for (nsDirectoryIterator i(parentDir); i; i++) // for (nsDirectoryIterator i(parentDir); i.Exists(); i++)
// { // {
// // do something with (const nsFileSpec&)i // // do something with i.Spec()
// } // }
// //
// or: // or:
// //
// for (nsDirectoryIterator i(parentDir, PR_FALSE); i; i--) // for (nsDirectoryIterator i(parentDir, PR_FALSE); i.Exists(); i--)
// { // {
// // do something with (const nsFileSpec&)i // // do something with i.Spec()
// } // }
// //
// Currently, the only platform on which backwards iteration actually goes backwards // Currently, the only platform on which backwards iteration actually goes backwards
@@ -599,6 +666,9 @@ class NS_BASE nsDirectoryIterator
nsDirectoryIterator& operator --(); // moves to the previous item, if any. nsDirectoryIterator& operator --(); // moves to the previous item, if any.
nsDirectoryIterator& operator --(int) { return --(*this); } // post-decrement. nsDirectoryIterator& operator --(int) { return --(*this); } // post-decrement.
operator nsFileSpec&() { return mCurrent; } operator nsFileSpec&() { return mCurrent; }
nsFileSpec& Spec() { return mCurrent; }
private: private:
nsFileSpec mCurrent; nsFileSpec mCurrent;
PRBool mExists; PRBool mExists;

View File

@@ -518,7 +518,6 @@ Clean:
nsFileSpec::nsFileSpec() nsFileSpec::nsFileSpec()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mError(NS_OK) : mError(NS_OK)
, mPath(nsnull)
{ {
mSpec.name[0] = '\0'; mSpec.name[0] = '\0';
} }
@@ -527,7 +526,6 @@ nsFileSpec::nsFileSpec()
nsFileSpec::nsFileSpec(const nsFileSpec& inSpec) nsFileSpec::nsFileSpec(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mSpec(inSpec.mSpec) : mSpec(inSpec.mSpec)
, mPath(nsnull)
, mError(inSpec.Error()) , mError(inSpec.Error())
{ {
} }
@@ -535,7 +533,6 @@ nsFileSpec::nsFileSpec(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs) nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
mSpec.vRefNum = 0; mSpec.vRefNum = 0;
mSpec.parID = 0; mSpec.parID = 0;
@@ -551,7 +548,6 @@ nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs) nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
mSpec.vRefNum = 0; mSpec.vRefNum = 0;
mSpec.parID = 0; mSpec.parID = 0;
@@ -567,7 +563,6 @@ nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(short vRefNum, long parID, ConstStr255Param name) nsFileSpec::nsFileSpec(short vRefNum, long parID, ConstStr255Param name)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
mError = NS_FILE_RESULT(::FSMakeFSSpec(vRefNum, parID, name, &mSpec)); mError = NS_FILE_RESULT(::FSMakeFSSpec(vRefNum, parID, name, &mSpec));
if (mError == NS_FILE_RESULT(fnfErr)) if (mError == NS_FILE_RESULT(fnfErr))
@@ -577,7 +572,6 @@ nsFileSpec::nsFileSpec(short vRefNum, long parID, ConstStr255Param name)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsFilePath& inPath) nsFileSpec::nsFileSpec(const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
*this = inPath.GetFileSpec(); *this = inPath.GetFileSpec();
} }
@@ -586,7 +580,7 @@ nsFileSpec::nsFileSpec(const nsFilePath& inPath)
void nsFileSpec::operator = (const char* inString) void nsFileSpec::operator = (const char* inString)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, nsnull); mPath.SetToEmpty();
mSpec.vRefNum = 0; mSpec.vRefNum = 0;
mSpec.parID = 0; mSpec.parID = 0;
@@ -601,8 +595,10 @@ void nsFileSpec::operator = (const char* inString)
void nsFileSpec::operator = (const nsFileSpec& inSpec) void nsFileSpec::operator = (const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, nsnull); mPath.SetToEmpty();
mSpec = inSpec.mSpec; mSpec.vRefNum = inSpec.mSpec.vRefNum;
mSpec.parID = inSpec.mSpec.parID;
memcpy(mSpec.name, inSpec.mSpec.name, inSpec.mSpec.name[0] + 1);
mError = inSpec.Error(); mError = inSpec.Error();
} // nsFileSpec::operator = } // nsFileSpec::operator =
@@ -662,7 +658,7 @@ void nsFileSpec::SetLeafName(const char* inLeafName)
// In leaf name can actually be a partial path... // In leaf name can actually be a partial path...
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, nsnull); mPath.SetToEmpty();
// what about long relative paths? Hmm? We don't have a routine for this anywhere. // what about long relative paths? Hmm? We don't have a routine for this anywhere.
Str255 partialPath; Str255 partialPath;
@@ -681,14 +677,14 @@ char* nsFileSpec::GetLeafName() const
char leaf[64]; char leaf[64];
memcpy(leaf, &mSpec.name[1], mSpec.name[0]); memcpy(leaf, &mSpec.name[1], mSpec.name[0]);
leaf[mSpec.name[0]] = '\0'; leaf[mSpec.name[0]] = '\0';
return nsFileSpecHelpers::StringDup(leaf); return PL_strdup(leaf);
} // nsFileSpec::GetLeafName } // nsFileSpec::GetLeafName
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileSpec::MakeAliasSafe() void nsFileSpec::MakeAliasSafe()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, nsnull); mPath.SetToEmpty();
mError = NS_FILE_RESULT(MacFileHelpers::MakeAliasSafe(mSpec)); mError = NS_FILE_RESULT(MacFileHelpers::MakeAliasSafe(mSpec));
} // nsFileSpec::MakeAliasSafe } // nsFileSpec::MakeAliasSafe
@@ -696,7 +692,7 @@ void nsFileSpec::MakeAliasSafe()
void nsFileSpec::MakeUnique(ConstStr255Param inSuggestedLeafName) void nsFileSpec::MakeUnique(ConstStr255Param inSuggestedLeafName)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, nsnull); mPath.SetToEmpty();
if (inSuggestedLeafName[0] > 0) if (inSuggestedLeafName[0] > 0)
MacFileHelpers::PLstrcpy(mSpec.name, inSuggestedLeafName); MacFileHelpers::PLstrcpy(mSpec.name, inSuggestedLeafName);
@@ -895,11 +891,11 @@ const char* nsFileSpec::GetCString() const
// cached and freed by the nsFileSpec destructor, so do not delete (or free) it. // cached and freed by the nsFileSpec destructor, so do not delete (or free) it.
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!mPath) if (mPath.IsEmpty())
{ {
const_cast<nsFileSpec*>(this)->mPath const_cast<nsFileSpec*>(this)->mPath
= MacFileHelpers::PathNameFromFSSpec(mSpec, true); = MacFileHelpers::PathNameFromFSSpec(mSpec, true);
if (!mPath) if (mPath.IsEmpty())
const_cast<nsFileSpec*>(this)->mError = NS_ERROR_OUT_OF_MEMORY; const_cast<nsFileSpec*>(this)->mError = NS_ERROR_OUT_OF_MEMORY;
} }
return mPath; return mPath;
@@ -912,29 +908,30 @@ const char* nsFileSpec::GetCString() const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs) nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull) : mFileSpec(inString, inCreateDirs)
, mFileSpec(inString, inCreateDirs)
{ {
// Make canonical and absolute. // Make canonical and absolute.
char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true ); char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true );
mPath = MacFileHelpers::EncodeMacPath(path, true, false); path = MacFileHelpers::EncodeMacPath(path, true, false);
mPath = path;
delete [] path;
} }
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const nsString& inString, PRBool inCreateDirs) nsFilePath::nsFilePath(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull) : mFileSpec(nsAutoCString(inString), inCreateDirs)
, mFileSpec(nsAutoCString(inString), inCreateDirs)
{ {
// Make canonical and absolute. // Make canonical and absolute.
char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true ); char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true );
mPath = MacFileHelpers::EncodeMacPath(path, true, false); path = MacFileHelpers::EncodeMacPath(path, true, false);
mPath = path;
delete [] path;
} }
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const nsFileSpec& inSpec) nsFilePath::nsFilePath(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
*this = inSpec; *this = inSpec;
} }
@@ -942,18 +939,18 @@ nsFilePath::nsFilePath(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const nsFileURL& inOther) nsFilePath::nsFilePath(const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
*this = inOther.GetFileSpec(); *this = inOther;
} }
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFilePath::operator = (const nsFileSpec& inSpec) void nsFilePath::operator = (const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
char * path = MacFileHelpers::PathNameFromFSSpec( inSpec.mSpec, true ); char * path = MacFileHelpers::PathNameFromFSSpec(inSpec, true);
delete [] mPath; path = MacFileHelpers::EncodeMacPath(path, true, false);
mPath = MacFileHelpers::EncodeMacPath(path, true, false); mPath = path;
delete [] path;
mFileSpec = inSpec; mFileSpec = inSpec;
} // nsFilePath::operator = } // nsFilePath::operator =
@@ -961,7 +958,11 @@ void nsFilePath::operator = (const nsFileSpec& inSpec)
void nsFilePath::operator = (const nsFileURL& inOther) void nsFilePath::operator = (const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
*this = inOther.GetFileSpec(); char * path = MacFileHelpers::PathNameFromFSSpec(inOther.mFileSpec, true);
path = MacFileHelpers::EncodeMacPath(path, true, false);
mPath = path;
delete [] path;
mFileSpec = inOther.GetFileSpec();
} }
//======================================================================================== //========================================================================================
@@ -971,11 +972,11 @@ void nsFilePath::operator = (const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs) nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mURL(nsFileSpecHelpers::StringDup(inString)) : mURL(inString)
{ {
NS_ASSERTION(strstr(mURL, kFileURLPrefix) == mURL, "Not a URL!"); NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!");
mFileSpec.mError = NS_FILE_RESULT(MacFileHelpers::FSSpecFromUnixPath( mFileSpec.mError = NS_FILE_RESULT(MacFileHelpers::FSSpecFromUnixPath(
mURL + kFileURLPrefixLength, inString + kFileURLPrefixLength,
mFileSpec.mSpec, mFileSpec.mSpec,
true, // need to decode true, // need to decode
false, // don't resolve alias false, // don't resolve alias
@@ -988,11 +989,14 @@ nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs) nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mURL(nsFileSpecHelpers::StringDup(nsAutoCString(inString))) : mURL(nsnull)
{ {
NS_ASSERTION(strstr(mURL, kFileURLPrefix) == mURL, "Not a URL!"); nsAutoCString autostring(inString);
const char* cstring = (const char*)autostring;
mURL = cstring;
NS_ASSERTION(strstr(cstring, kFileURLPrefix) == cstring, "Not a URL!");
mFileSpec.mError = NS_FILE_RESULT(MacFileHelpers::FSSpecFromUnixPath( mFileSpec.mError = NS_FILE_RESULT(MacFileHelpers::FSSpecFromUnixPath(
mURL + kFileURLPrefixLength, cstring + kFileURLPrefixLength,
mFileSpec.mSpec, mFileSpec.mSpec,
true, // need to decode true, // need to decode
false, // don't resolve alias false, // don't resolve alias
@@ -1005,14 +1009,12 @@ nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsFilePath& inOther) nsFileURL::nsFileURL(const nsFilePath& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mURL(nsnull)
{ {
*this = inOther.GetFileSpec(); *this = inOther.GetFileSpec();
} // nsFileURL::nsFileURL } // nsFileURL::nsFileURL
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsFileSpec& inOther) nsFileURL::nsFileURL(const nsFileSpec& inOther)
: mURL(nsnull)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
*this = inOther; *this = inOther;
@@ -1030,25 +1032,21 @@ void nsFileURL::operator = (const nsFileSpec& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
mFileSpec = inOther; mFileSpec = inOther;
delete [] mURL;
char* path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true ); char* path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true );
char* encodedPath = MacFileHelpers::EncodeMacPath(path, true, true); char* encodedPath = MacFileHelpers::EncodeMacPath(path, true, true);
char* encodedURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, encodedPath); nsSimpleCharString encodedURL(kFileURLPrefix);
encodedURL += encodedPath;
delete [] encodedPath; delete [] encodedPath;
if (encodedURL[strlen(encodedURL) - 1] != '/' && inOther.IsDirectory())
{
mURL = nsFileSpecHelpers::AllocCat(encodedURL, "/");
delete [] encodedURL;
}
else
mURL = encodedURL; mURL = encodedURL;
if (encodedURL[encodedURL.Length() - 1] != '/' && inOther.IsDirectory())
mURL += "/";
} // nsFileURL::operator = } // nsFileURL::operator =
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileURL::operator = (const char* inString) void nsFileURL::operator = (const char* inString)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mURL, inString); mURL = inString;
NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!"); NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!");
mFileSpec.mError = NS_FILE_RESULT(MacFileHelpers::FSSpecFromUnixPath( mFileSpec.mError = NS_FILE_RESULT(MacFileHelpers::FSSpecFromUnixPath(
inString + kFileURLPrefixLength, inString + kFileURLPrefixLength,

View File

@@ -30,6 +30,219 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
//========================================================================================
// class nsSimpleCharString
//========================================================================================
//----------------------------------------------------------------------------------------
nsSimpleCharString::nsSimpleCharString()
//----------------------------------------------------------------------------------------
: mData(nsnull)
{
} // nsSimpleCharString::nsSimpleCharString
//----------------------------------------------------------------------------------------
nsSimpleCharString::nsSimpleCharString(const char* inString)
//----------------------------------------------------------------------------------------
: mData(nsnull)
{
if (inString)
CopyFrom(inString, strlen(inString));
} // nsSimpleCharString::nsSimpleCharString
//----------------------------------------------------------------------------------------
nsSimpleCharString::nsSimpleCharString(const nsString& inString)
//----------------------------------------------------------------------------------------
: mData(nsnull)
{
*this = inString;
} // nsSimpleCharString::nsSimpleCharString
//----------------------------------------------------------------------------------------
nsSimpleCharString::nsSimpleCharString(const nsSimpleCharString& inOther)
//----------------------------------------------------------------------------------------
{
mData = inOther.mData;
AddRefData();
} // nsSimpleCharString::nsSimpleCharString
//----------------------------------------------------------------------------------------
nsSimpleCharString::nsSimpleCharString(const char* inData, PRUint32 inLength)
//----------------------------------------------------------------------------------------
: mData(nsnull)
{
CopyFrom(inData, inLength);
} // nsSimpleCharString::nsSimpleCharString
//----------------------------------------------------------------------------------------
nsSimpleCharString::~nsSimpleCharString()
//----------------------------------------------------------------------------------------
{
ReleaseData();
} // nsSimpleCharString::nsSimpleCharString
//----------------------------------------------------------------------------------------
void nsSimpleCharString::operator = (const char* inString)
//----------------------------------------------------------------------------------------
{
if (inString)
CopyFrom(inString, strlen(inString));
else
SetToEmpty();
} // nsSimpleCharString::operator =
//----------------------------------------------------------------------------------------
void nsSimpleCharString::operator = (const nsString& inString)
//----------------------------------------------------------------------------------------
{
PRUint32 len = inString.Length();
ReallocData(len);
if (!mData)
return;
inString.ToCString(mData->mString, len);
} // nsSimpleCharString::operator =
//----------------------------------------------------------------------------------------
void nsSimpleCharString::operator = (const nsSimpleCharString& inOther)
//----------------------------------------------------------------------------------------
{
if (mData == inOther.mData)
return;
ReleaseData();
mData = inOther.mData;
AddRefData();
} // nsSimpleCharString::operator =
//----------------------------------------------------------------------------------------
void nsSimpleCharString::operator += (const char* inOther)
//----------------------------------------------------------------------------------------
{
if (!inOther)
return;
int newLength = Length() + PL_strlen(inOther);
ReallocData(newLength);
strcat(mData->mString, inOther);
} // nsSimpleCharString::operator =
//----------------------------------------------------------------------------------------
nsSimpleCharString nsSimpleCharString::operator + (const char* inOther) const
//----------------------------------------------------------------------------------------
{
nsSimpleCharString result(*this);
result += inOther;
return result;
} // nsSimpleCharString::operator =
//----------------------------------------------------------------------------------------
void nsSimpleCharString::Catenate(const char* inString1, const char* inString2)
//----------------------------------------------------------------------------------------
{
if (!inString2)
{
*this += inString1;
return;
}
int newLength = Length() + PL_strlen(inString1) + PL_strlen(inString2);
ReallocData(newLength);
strcat(mData->mString, inString1);
strcat(mData->mString, inString2);
} // nsSimpleCharString::operator =
//----------------------------------------------------------------------------------------
void nsSimpleCharString::CopyFrom(const char* inData, PRUint32 inLength)
//----------------------------------------------------------------------------------------
{
if (!inData)
return;
ReallocData(inLength);
if (!mData)
return;
memcpy(mData->mString, inData, inLength);
mData->mString[inLength] = '\0';
} // nsSimpleCharString::CopyFrom
//----------------------------------------------------------------------------------------
void nsSimpleCharString::SetToEmpty()
//----------------------------------------------------------------------------------------
{
ReleaseData();
} // nsSimpleCharString::SetToEmpty
//----------------------------------------------------------------------------------------
void nsSimpleCharString::Unescape()
//----------------------------------------------------------------------------------------
{
if (!mData)
return;
nsUnescape(mData->mString);
mData->mLength = strlen(mData->mString);
} // nsSimpleCharString::Unescape
//----------------------------------------------------------------------------------------
void nsSimpleCharString::AddRefData()
//----------------------------------------------------------------------------------------
{
if (mData)
++mData->mRefCount;
} // nsSimpleCharString::AddRefData
//----------------------------------------------------------------------------------------
void nsSimpleCharString::ReleaseData()
//----------------------------------------------------------------------------------------
{
if (!mData)
return;
NS_ASSERTION(mData->mRefCount > 0, "String deleted too many times!");
if (--mData->mRefCount == 0)
PR_Free(mData);
mData = nsnull;
} // nsSimpleCharString::ReleaseData
//----------------------------------------------------------------------------------------
void nsSimpleCharString::ReallocData(PRUint32 inLength)
// Reallocate mData to a new length. Since this presumably precedes a change to the string,
// we want to detach ourselves if the data is shared by another string, even if the length
// requested would not otherwise require a reallocation.
//----------------------------------------------------------------------------------------
{
// We always allocate for a string of at least kMinStringSize, to prevent a lot of small
// reallocations.
const PRUint32 kMinStringSize = 127;
if (mData)
{
// Re
NS_ASSERTION(mData->mRefCount > 0, "String deleted too many times!");
if (mData->mRefCount == 1)
{
// We are the sole owner, so just change its length, if necessary.
if (inLength > mData->mLength && inLength > kMinStringSize)
mData = (Data*)PR_Realloc(mData, inLength + sizeof(Data));
mData->mLength = inLength;
return;
}
}
PRUint32 allocLength = inLength;
if (allocLength < kMinStringSize)
allocLength = kMinStringSize;
Data* newData = (Data*)PR_Malloc(allocLength + sizeof(Data));
// If data was already allocated when we get to here, then we are cloning the data
// from a shared pointer.
if (mData)
{
memcpy(newData, mData, sizeof(Data) + Length());
mData->mRefCount--; // Say goodbye
}
else
newData->mString[0] = '\0';
mData = newData;
mData->mRefCount = 1;
mData->mLength = inLength;
} // nsSimpleCharString::ReleaseData
//======================================================================================== //========================================================================================
NS_NAMESPACE nsFileSpecHelpers NS_NAMESPACE nsFileSpecHelpers
//======================================================================================== //========================================================================================
@@ -39,22 +252,13 @@ NS_NAMESPACE nsFileSpecHelpers
, kMaxAltDigitLength = 5 , kMaxAltDigitLength = 5
, kMaxCoreLeafNameLength = (kMaxFilenameLength - (kMaxAltDigitLength + 1)) , kMaxCoreLeafNameLength = (kMaxFilenameLength - (kMaxAltDigitLength + 1))
}; };
NS_NAMESPACE_PROTOTYPE void LeafReplace(
char*& ioPath,
char inSeparator,
const char* inLeafName);
#ifndef XP_MAC #ifndef XP_MAC
NS_NAMESPACE_PROTOTYPE void Canonify(char*& ioPath, PRBool inMakeDirs); NS_NAMESPACE_PROTOTYPE void Canonify(nsSimpleCharString& ioPath, PRBool inMakeDirs);
NS_NAMESPACE_PROTOTYPE void MakeAllDirectories(const char* inPath, int mode); NS_NAMESPACE_PROTOTYPE void MakeAllDirectories(const char* inPath, int mode);
#endif #endif
NS_NAMESPACE_PROTOTYPE char* GetLeaf(const char* inPath, char inSeparator); // allocated
NS_NAMESPACE_PROTOTYPE char* StringDup(const char* inString, int allocLength = 0);
NS_NAMESPACE_PROTOTYPE char* AllocCat(const char* inString1, const char* inString2);
NS_NAMESPACE_PROTOTYPE char* StringAssign(char*& ioString, const char* inOther);
NS_NAMESPACE_PROTOTYPE char* ReallocCat(char*& ioString, const char* inString1);
#ifdef XP_PC #ifdef XP_PC
NS_NAMESPACE_PROTOTYPE void NativeToUnix(char*& ioPath); NS_NAMESPACE_PROTOTYPE void NativeToUnix(nsSimpleCharString& ioPath);
NS_NAMESPACE_PROTOTYPE void UnixToNative(char*& ioPath); NS_NAMESPACE_PROTOTYPE void UnixToNative(nsSimpleCharString& ioPath);
#endif #endif
} NS_NAMESPACE_END } NS_NAMESPACE_END
@@ -68,136 +272,80 @@ nsresult ns_file_convert_result(PRInt32 nativeErr)
} }
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
char* nsFileSpecHelpers::StringDup( void nsSimpleCharString::LeafReplace(char inSeparator, const char* inLeafName)
const char* inString,
int allocLength)
//----------------------------------------------------------------------------------------
{
if (!allocLength && inString)
allocLength = strlen(inString);
char* newPath = inString || allocLength ? new char[allocLength + 1] : nsnull;
if (!newPath)
return nsnull;
strcpy(newPath, inString);
return newPath;
} // nsFileSpecHelpers::StringDup
//----------------------------------------------------------------------------------------
char* nsFileSpecHelpers::AllocCat(
const char* inString1,
const char* inString2)
//----------------------------------------------------------------------------------------
{
if (!inString1)
return inString2 ? StringDup(inString2) : (char*)nsnull;
if (!inString2)
return StringDup(inString1);
char* outString = StringDup(inString1, strlen(inString1) + strlen(inString2));
if (outString)
strcat(outString, inString2);
return outString;
} // nsFileSpecHelpers::StringDup
//----------------------------------------------------------------------------------------
char* nsFileSpecHelpers::StringAssign(
char*& ioString,
const char* inString2)
//----------------------------------------------------------------------------------------
{
if (!inString2)
{
delete [] ioString;
ioString = (char*)nsnull;
return ioString;
}
if (!ioString || (strlen(inString2) > strlen(ioString)))
{
delete [] ioString;
ioString = StringDup(inString2);
return ioString;
}
strcpy(ioString, inString2);
return ioString;
} // nsFileSpecHelpers::StringAssign
//----------------------------------------------------------------------------------------
void nsFileSpecHelpers::LeafReplace(
char*& ioPath,
char inSeparator,
const char* inLeafName)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
// Find the existing leaf name // Find the existing leaf name
if (!ioPath) if (IsEmpty())
return; return;
if (!inLeafName) if (!inLeafName)
{ {
*ioPath = '\0'; SetToEmpty();
return; return;
} }
char* lastSeparator = strrchr(ioPath, inSeparator); char* chars = mData->mString;
int oldLength = strlen(ioPath); char* lastSeparator = strrchr(chars, inSeparator);
PRBool trailingSeparator = (lastSeparator + 1 == ioPath + oldLength); int oldLength = Length();
PRBool trailingSeparator = (lastSeparator + 1 == chars + oldLength);
if (trailingSeparator) if (trailingSeparator)
{ {
*lastSeparator = '\0'; *lastSeparator = '\0';
lastSeparator = strrchr(ioPath, inSeparator); lastSeparator = strrchr(chars, inSeparator);
} }
if (lastSeparator) if (lastSeparator)
lastSeparator++; // point at the trailing string lastSeparator++; // point at the trailing string
else else
lastSeparator = ioPath; // the full monty lastSeparator = chars; // the full monty
*lastSeparator = '\0'; // strip the current leaf name *lastSeparator = '\0'; // strip the current leaf name
int newLength = (lastSeparator - ioPath) + strlen(inLeafName) + int(trailingSeparator); int newLength = (lastSeparator - chars) + strlen(inLeafName) + int(trailingSeparator);
if (newLength > oldLength) ReallocData(newLength);
{ chars = mData->mString; // it might have moved.
char* newPath = StringDup(ioPath, newLength + 1);
delete [] ioPath; strcat(chars, inLeafName);
ioPath = newPath;
}
strcat(ioPath, inLeafName);
if (trailingSeparator) if (trailingSeparator)
{ {
// If the original ended in a slash, then the new one should, too. // If the original ended in a slash, then the new one should, too.
char sepStr[2] = "/"; char sepStr[2] = "/";
*sepStr = inSeparator; *sepStr = inSeparator;
strcat(ioPath, sepStr); strcat(chars, sepStr);
} }
} // nsFileSpecHelpers::LeafReplace } // nsSimpleCharString::LeafReplace
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
char* nsFileSpecHelpers::GetLeaf(const char* inPath, char inSeparator) char* nsSimpleCharString::GetLeaf(char inSeparator) const
// Returns a pointer to an allocated string representing the leaf. // Returns a pointer to an allocated string representing the leaf.
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!inPath) if (IsEmpty())
return nsnull; return nsnull;
const char* lastSeparator = strrchr(inPath, inSeparator);
// If there was no separator, then return a copy of the caller's path. char* chars = mData->mString;
const char* lastSeparator = strrchr(chars, inSeparator);
// If there was no separator, then return a copy of our path.
if (!lastSeparator) if (!lastSeparator)
return StringDup(inPath); return PL_strdup(*this);
// So there's at least one separator. What's just after it? // So there's at least one separator. What's just after it?
// If the separator was not the last character, return the trailing string. // If the separator was not the last character, return the trailing string.
const char* leafPointer = lastSeparator + 1; const char* leafPointer = lastSeparator + 1;
if (*leafPointer) if (*leafPointer)
return StringDup(leafPointer); return PL_strdup(leafPointer);
// So now, separator was the last character. Poke in a null instead. // So now, separator was the last character. Poke in a null instead.
*(char*)lastSeparator = '\0'; // Should use const_cast, but Unix has old compiler. *(char*)lastSeparator = '\0'; // Should use const_cast, but Unix has old compiler.
leafPointer = strrchr(inPath, inSeparator); leafPointer = strrchr(chars, inSeparator);
char* result = leafPointer ? StringDup(++leafPointer) : StringDup(inPath); char* result = leafPointer ? PL_strdup(++leafPointer) : PL_strdup(chars);
// Restore the poked null before returning. // Restore the poked null before returning.
*(char*)lastSeparator = inSeparator; *(char*)lastSeparator = inSeparator;
#ifdef XP_PC #ifdef XP_PC
// If it's a drive letter use the colon notation. // If it's a drive letter use the colon notation.
if (!leafPointer && strlen(result) == 2 && result[1] == '|') if (!leafPointer && result[2] == 0 && result[1] == '|')
result[1] = ':'; result[1] = ':';
#endif #endif
return result; return result;
} // nsFileSpecHelpers::GetLeaf } // nsSimpleCharString::GetLeaf
#if defined(XP_UNIX) || defined(XP_PC) #if defined(XP_UNIX) || defined(XP_PC)
@@ -210,7 +358,7 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode)
if (!inPath) if (!inPath)
return; return;
char* pathCopy = nsFileSpecHelpers::StringDup( inPath ); char* pathCopy = PL_strdup( inPath );
if (!pathCopy) if (!pathCopy)
return; return;
@@ -227,7 +375,6 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode)
if (currentEnd) if (currentEnd)
{ {
nsFileSpec spec; nsFileSpec spec;
*currentEnd = '\0'; *currentEnd = '\0';
#ifdef XP_PC #ifdef XP_PC
@@ -237,12 +384,13 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode)
*/ */
if (pathCopy[0] == '/' && pathCopy[2] == '|') if (pathCopy[0] == '/' && pathCopy[2] == '|')
{ {
char* startDir = nsFileSpecHelpers::StringDup( pathCopy, (strlen(pathCopy) + 1) ); char* startDir = (char*)PR_Malloc(strlen(pathCopy) + 2);
strcpy(startDir, pathCopy);
strcat(startDir, "/"); strcat(startDir, "/");
spec = nsFilePath(startDir, PR_FALSE); spec = nsFilePath(startDir, PR_FALSE);
delete [] startDir; PR_Free(startDir);
} }
else else
{ {
@@ -278,16 +426,6 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode)
#endif // XP_PC || XP_UNIX #endif // XP_PC || XP_UNIX
//----------------------------------------------------------------------------------------
char* nsFileSpecHelpers::ReallocCat(char*& ioString, const char* inString1)
//----------------------------------------------------------------------------------------
{
char* newString = AllocCat(ioString, inString1);
delete [] ioString;
ioString = newString;
return ioString;
} // nsFileSpecHelpers::ReallocCat
#if defined(XP_PC) #if defined(XP_PC)
#include "windows/nsFileSpecWin.cpp" // Windows-specific implementations #include "windows/nsFileSpecWin.cpp" // Windows-specific implementations
#elif defined(XP_MAC) #elif defined(XP_MAC)
@@ -304,7 +442,6 @@ char* nsFileSpecHelpers::ReallocCat(char*& ioString, const char* inString1)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs) nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mURL(nsnull)
{ {
if (!inString) if (!inString)
return; return;
@@ -319,7 +456,6 @@ nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs) nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mURL(nsnull)
{ {
const nsAutoCString aString(inString); const nsAutoCString aString(inString);
const char* aCString = (const char*) aString; const char* aCString = (const char*) aString;
@@ -335,7 +471,7 @@ nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsFileURL& inOther) nsFileURL::nsFileURL(const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mURL(nsFileSpecHelpers::StringDup(inOther.mURL)) : mURL(inOther.mURL)
#ifdef XP_MAC #ifdef XP_MAC
, mFileSpec(inOther.GetFileSpec()) , mFileSpec(inOther.GetFileSpec())
#endif #endif
@@ -346,7 +482,6 @@ nsFileURL::nsFileURL(const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsFilePath& inOther) nsFileURL::nsFileURL(const nsFilePath& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mURL(nsnull)
{ {
*this = inOther; *this = inOther;
} // nsFileURL::nsFileURL } // nsFileURL::nsFileURL
@@ -355,7 +490,6 @@ nsFileURL::nsFileURL(const nsFilePath& inOther)
#ifndef XP_MAC #ifndef XP_MAC
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsFileSpec& inOther) nsFileURL::nsFileURL(const nsFileSpec& inOther)
: mURL(nsnull)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
*this = inOther; *this = inOther;
@@ -366,7 +500,6 @@ nsFileURL::nsFileURL(const nsFileSpec& inOther)
nsFileURL::~nsFileURL() nsFileURL::~nsFileURL()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
delete [] mURL;
} }
#ifndef XP_MAC #ifndef XP_MAC
@@ -374,16 +507,37 @@ nsFileURL::~nsFileURL()
void nsFileURL::operator = (const char* inString) void nsFileURL::operator = (const char* inString)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mURL, inString); mURL = inString;
NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!"); NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!");
} // nsFileURL::operator = } // nsFileURL::operator =
#endif #endif
//----------------------------------------------------------------------------------------
void nsFileURL::operator +=(const char* inRelativeUnixPath)
//----------------------------------------------------------------------------------------
{
char* escapedPath = nsEscape(inRelativeUnixPath, url_Path);
mURL += escapedPath;
delete [] escapedPath;
#ifdef XP_MAC
mFileSpec += inRelativeUnixPath;
#endif
} // nsFileURL::operator +=
//----------------------------------------------------------------------------------------
nsFileURL nsFileURL::operator +(const char* inRelativeUnixPath) const
//----------------------------------------------------------------------------------------
{
nsFileURL result(*this);
result += inRelativeUnixPath;
return result;
} // nsFileURL::operator +
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileURL::operator = (const nsFileURL& inOther) void nsFileURL::operator = (const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
mURL = nsFileSpecHelpers::StringAssign(mURL, inOther.mURL); mURL = inOther.mURL;
#ifdef XP_MAC #ifdef XP_MAC
mFileSpec = inOther.GetFileSpec(); mFileSpec = inOther.GetFileSpec();
#endif #endif
@@ -394,7 +548,7 @@ void nsFileURL::operator = (const nsFileURL& inOther)
void nsFileURL::operator = (const nsFilePath& inOther) void nsFileURL::operator = (const nsFilePath& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
delete [] mURL; mURL = kFileURLPrefix;
char* original = (char*)(const char*)inOther; // we shall modify, but restore. char* original = (char*)(const char*)inOther; // we shall modify, but restore.
#ifdef XP_PC #ifdef XP_PC
// because we don't want to escape the '|' character, change it to a letter. // because we don't want to escape the '|' character, change it to a letter.
@@ -407,7 +561,7 @@ void nsFileURL::operator = (const nsFilePath& inOther)
char* escapedPath = nsEscape(original, url_Path); char* escapedPath = nsEscape(original, url_Path);
#endif #endif
if (escapedPath) if (escapedPath)
mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, escapedPath); mURL += escapedPath;
delete [] escapedPath; delete [] escapedPath;
} // nsFileURL::operator = } // nsFileURL::operator =
#endif #endif
@@ -418,6 +572,8 @@ void nsFileURL::operator = (const nsFileSpec& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
*this = nsFilePath(inOther); *this = nsFilePath(inOther);
if (mURL[mURL.Length() - 1] != '/' && inOther.IsDirectory())
mURL += "/";
} // nsFileURL::operator = } // nsFileURL::operator =
#endif #endif
@@ -433,7 +589,7 @@ nsOutputStream& operator << (nsOutputStream& s, const nsFileURL& url)
//======================================================================================== //========================================================================================
nsFilePath::nsFilePath(const nsFilePath& inPath) nsFilePath::nsFilePath(const nsFilePath& inPath)
: mPath(nsFileSpecHelpers::StringDup(inPath.mPath)) : mPath(inPath.mPath)
#ifdef XP_MAC #ifdef XP_MAC
, mFileSpec(inPath.mFileSpec) , mFileSpec(inPath.mFileSpec)
#endif #endif
@@ -444,7 +600,7 @@ nsFilePath::nsFilePath(const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs) nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsFileSpecHelpers::StringDup(inString)) : mPath(inString)
{ {
NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path"); NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path");
@@ -464,7 +620,7 @@ nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const nsString& inString, PRBool inCreateDirs) nsFilePath::nsFilePath(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(inString.ToNewCString()) : mPath(inString)
{ {
NS_ASSERTION(strstr(mPath, kFileURLPrefix) != mPath, "URL passed as path"); NS_ASSERTION(strstr(mPath, kFileURLPrefix) != mPath, "URL passed as path");
@@ -484,8 +640,9 @@ nsFilePath::nsFilePath(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const nsFileURL& inOther) nsFilePath::nsFilePath(const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsFileSpecHelpers::StringDup(inOther.mURL + kFileURLPrefixLength))
{ {
mPath = (const char*)inOther.mURL + kFileURLPrefixLength;
mPath.Unescape();
} }
#endif #endif
@@ -493,7 +650,7 @@ nsFilePath::nsFilePath(const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const nsFileSpec& inOther) nsFilePath::nsFilePath(const nsFileSpec& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsFileSpecHelpers::StringDup(inOther.mPath)) : mPath(inOther.mPath)
{ {
} }
#endif // XP_UNIX #endif // XP_UNIX
@@ -502,7 +659,6 @@ nsFilePath::nsFilePath(const nsFileSpec& inOther)
nsFilePath::~nsFilePath() nsFilePath::~nsFilePath()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
delete [] mPath;
} }
#ifdef XP_UNIX #ifdef XP_UNIX
@@ -510,7 +666,7 @@ nsFilePath::~nsFilePath()
void nsFilePath::operator = (const nsFileSpec& inOther) void nsFilePath::operator = (const nsFileSpec& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
mPath = nsFileSpecHelpers::StringAssign(mPath, inOther.mPath); mPath = inOther.mPath;
} }
#endif // XP_UNIX #endif // XP_UNIX
@@ -521,9 +677,9 @@ void nsFilePath::operator = (const char* inString)
NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path"); NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path");
#ifdef XP_MAC #ifdef XP_MAC
mFileSpec = inString; mFileSpec = inString;
nsFileSpecHelpers::StringAssign(mPath, (const char*)nsFilePath(mFileSpec)); mPath = (const char*)nsFilePath(mFileSpec);
#else #else
nsFileSpecHelpers::StringAssign(mPath, inString); mPath = inString;
#ifdef XP_PC #ifdef XP_PC
nsFileSpecHelpers::UnixToNative(mPath); nsFileSpecHelpers::UnixToNative(mPath);
#endif #endif
@@ -540,7 +696,7 @@ void nsFilePath::operator = (const char* inString)
void nsFilePath::operator = (const nsFileURL& inOther) void nsFilePath::operator = (const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, (const char*)nsFilePath(inOther)); mPath = (const char*)nsFilePath(inOther);
} }
#endif #endif
@@ -548,12 +704,33 @@ void nsFilePath::operator = (const nsFileURL& inOther)
void nsFilePath::operator = (const nsFilePath& inOther) void nsFilePath::operator = (const nsFilePath& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, inOther.mPath); mPath = inOther.mPath;
#ifdef XP_MAC #ifdef XP_MAC
mFileSpec = inOther.GetFileSpec(); mFileSpec = inOther.GetFileSpec();
#endif #endif
} }
//----------------------------------------------------------------------------------------
void nsFilePath::operator +=(const char* inRelativeUnixPath)
//----------------------------------------------------------------------------------------
{
char* escapedPath = nsEscape(inRelativeUnixPath, url_Path);
mPath += escapedPath;
delete [] escapedPath;
#ifdef XP_MAC
mFileSpec += inRelativeUnixPath;
#endif
} // nsFilePath::operator +=
//----------------------------------------------------------------------------------------
nsFilePath nsFilePath::operator +(const char* inRelativeUnixPath) const
//----------------------------------------------------------------------------------------
{
nsFilePath result(*this);
result += inRelativeUnixPath;
return result;
} // nsFilePath::operator +
//======================================================================================== //========================================================================================
// nsFileSpec implementation // nsFileSpec implementation
//======================================================================================== //========================================================================================
@@ -562,8 +739,7 @@ void nsFilePath::operator = (const nsFilePath& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec() nsFileSpec::nsFileSpec()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull) : mError(NS_OK)
, mError(NS_OK)
{ {
} }
#endif #endif
@@ -571,7 +747,6 @@ nsFileSpec::nsFileSpec()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsPersistentFileDescriptor& inDescriptor) nsFileSpec::nsFileSpec(const nsPersistentFileDescriptor& inDescriptor)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
*this = inDescriptor; *this = inDescriptor;
} }
@@ -579,7 +754,6 @@ nsFileSpec::nsFileSpec(const nsPersistentFileDescriptor& inDescriptor)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsFileURL& inURL) nsFileSpec::nsFileSpec(const nsFileURL& inURL)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
*this = nsFilePath(inURL); // convert to unix path first *this = nsFilePath(inURL); // convert to unix path first
} }
@@ -609,7 +783,7 @@ void nsFileSpec::MakeUnique()
char* suffix = ""; char* suffix = "";
if (lastDot) if (lastDot)
{ {
suffix = nsFileSpecHelpers::StringDup(lastDot); // include '.' suffix = PL_strdup(lastDot); // include '.'
*lastDot = '\0'; // strip suffix and dot. *lastDot = '\0'; // strip suffix and dot.
} }
const int kMaxRootLength const int kMaxRootLength
@@ -640,7 +814,7 @@ void nsFileSpec::operator = (const nsPersistentFileDescriptor& inDescriptor)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
void* data; nsSimpleCharString data;
PRInt32 dataSize; PRInt32 dataSize;
inDescriptor.GetData(data, dataSize); inDescriptor.GetData(data, dataSize);
@@ -656,9 +830,9 @@ void nsFileSpec::operator = (const nsPersistentFileDescriptor& inDescriptor)
Boolean changed; Boolean changed;
mError = NS_FILE_RESULT(::ResolveAlias(nsnull, aliasH, &mSpec, &changed)); mError = NS_FILE_RESULT(::ResolveAlias(nsnull, aliasH, &mSpec, &changed));
DisposeHandle((Handle) aliasH); DisposeHandle((Handle) aliasH);
delete [] mPath; mPath.SetToEmpty();
#else #else
nsFileSpecHelpers::StringAssign(mPath, (char*)data); mPath = data;
mError = NS_OK; mError = NS_OK;
#endif #endif
} }
@@ -671,7 +845,7 @@ void nsFileSpec::operator = (const nsPersistentFileDescriptor& inDescriptor)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsFilePath& inPath) nsFileSpec::nsFileSpec(const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsFileSpecHelpers::StringDup((const char*)inPath)) : mPath((const char*)inPath)
, mError(NS_OK) , mError(NS_OK)
{ {
} }
@@ -682,7 +856,7 @@ nsFileSpec::nsFileSpec(const nsFilePath& inPath)
void nsFileSpec::operator = (const nsFilePath& inPath) void nsFileSpec::operator = (const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, (const char*)inPath); mPath = (const char*)inPath;
mError = NS_OK; mError = NS_OK;
} }
#endif //XP_UNIX #endif //XP_UNIX
@@ -691,7 +865,7 @@ void nsFileSpec::operator = (const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsFileSpec& inSpec) nsFileSpec::nsFileSpec(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsFileSpecHelpers::StringDup(inSpec.mPath)) : mPath(inSpec.mPath)
, mError(NS_OK) , mError(NS_OK)
{ {
} }
@@ -701,7 +875,7 @@ nsFileSpec::nsFileSpec(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs) nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsFileSpecHelpers::StringDup(inString)) : mPath(inString)
, mError(NS_OK) , mError(NS_OK)
{ {
// Make canonical and absolute. // Make canonical and absolute.
@@ -713,7 +887,7 @@ nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs) nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(inString.ToNewCString()) : mPath(inString)
, mError(NS_OK) , mError(NS_OK)
{ {
// Make canonical and absolute. // Make canonical and absolute.
@@ -725,7 +899,6 @@ nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs)
nsFileSpec::~nsFileSpec() nsFileSpec::~nsFileSpec()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
delete [] mPath;
} }
#if defined(XP_UNIX) || defined(XP_PC) #if defined(XP_UNIX) || defined(XP_PC)
@@ -733,7 +906,7 @@ nsFileSpec::~nsFileSpec()
void nsFileSpec::operator = (const nsFileSpec& inSpec) void nsFileSpec::operator = (const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
mPath = nsFileSpecHelpers::StringAssign(mPath, inSpec.mPath); mPath = inSpec.mPath;
mError = inSpec.Error(); mError = inSpec.Error();
} }
#endif //XP_UNIX #endif //XP_UNIX
@@ -744,7 +917,7 @@ void nsFileSpec::operator = (const nsFileSpec& inSpec)
void nsFileSpec::operator = (const char* inString) void nsFileSpec::operator = (const char* inString)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
mPath = nsFileSpecHelpers::StringAssign(mPath, inString); mPath = inString;
// Make canonical and absolute. // Make canonical and absolute.
nsFileSpecHelpers::Canonify(mPath, PR_FALSE /* XXX? */); nsFileSpecHelpers::Canonify(mPath, PR_FALSE /* XXX? */);
mError = NS_OK; mError = NS_OK;
@@ -788,8 +961,8 @@ PRBool nsFileSpec::operator == (const nsFileSpec& inOther) const
EqualString(inOther.mSpec.name, mSpec.name, false, true)) EqualString(inOther.mSpec.name, mSpec.name, false, true))
return PR_TRUE; return PR_TRUE;
#else #else
PRBool amEmpty = !mPath || !*mPath; PRBool amEmpty = mPath.IsEmpty();
PRBool heEmpty = !inOther.mPath || !*inOther.mPath; PRBool heEmpty = inOther.mPath.IsEmpty();
if (amEmpty) // we're the same if he's empty... if (amEmpty) // we're the same if he's empty...
return heEmpty; return heEmpty;
if (heEmpty) // ('cuz I'm not...) if (heEmpty) // ('cuz I'm not...)
@@ -835,7 +1008,7 @@ const char* nsFileSpec::GetCString() const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inDesc) nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inDesc)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mDescriptorString(nsFileSpecHelpers::StringDup(inDesc.mDescriptorString)) : mDescriptorString(inDesc.mDescriptorString)
{ {
} // nsPersistentFileDescriptor::nsPersistentFileDescriptor } // nsPersistentFileDescriptor::nsPersistentFileDescriptor
@@ -843,13 +1016,12 @@ nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsPersistentFileDes
void nsPersistentFileDescriptor::operator = (const nsPersistentFileDescriptor& inDesc) void nsPersistentFileDescriptor::operator = (const nsPersistentFileDescriptor& inDesc)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mDescriptorString, inDesc.mDescriptorString); mDescriptorString = inDesc.mDescriptorString;
} // nsPersistentFileDescriptor::operator = } // nsPersistentFileDescriptor::operator =
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsFileSpec& inSpec) nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mDescriptorString(nsnull)
{ {
*this = inSpec; *this = inSpec;
} // nsPersistentFileDescriptor::nsPersistentFileDescriptor } // nsPersistentFileDescriptor::nsPersistentFileDescriptor
@@ -871,10 +1043,10 @@ void nsPersistentFileDescriptor::operator = (const nsFileSpec& inSpec)
char* buf = PL_Base64Encode((const char*)*aliasH, bytes, nsnull); char* buf = PL_Base64Encode((const char*)*aliasH, bytes, nsnull);
DisposeHandle((Handle) aliasH); DisposeHandle((Handle) aliasH);
nsFileSpecHelpers::StringAssign(mDescriptorString, buf); mDescriptorString = buf;
PR_Free(buf); PR_Free(buf);
#else #else
nsFileSpecHelpers::StringAssign(mDescriptorString, inSpec.GetCString()); mDescriptorString = inSpec.GetCString();
#endif // XP_MAC #endif // XP_MAC
} // nsPersistentFileDescriptor::operator = } // nsPersistentFileDescriptor::operator =
@@ -882,27 +1054,21 @@ void nsPersistentFileDescriptor::operator = (const nsFileSpec& inSpec)
nsPersistentFileDescriptor::~nsPersistentFileDescriptor() nsPersistentFileDescriptor::~nsPersistentFileDescriptor()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
delete [] mDescriptorString;
} // nsPersistentFileDescriptor::~nsPersistentFileDescriptor } // nsPersistentFileDescriptor::~nsPersistentFileDescriptor
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsPersistentFileDescriptor::GetData(void*& outData, PRInt32& outSize) const void nsPersistentFileDescriptor::GetData(nsSimpleCharString& outData, PRInt32& outSize) const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
outSize = PL_strlen(mDescriptorString); outSize = mDescriptorString.Length();
outData = mDescriptorString; outData = mDescriptorString;
} }
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsPersistentFileDescriptor::SetData(const void* inData, PRInt32 inSize) void nsPersistentFileDescriptor::SetData(const nsSimpleCharString& inData, PRInt32 inSize)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
delete [] mDescriptorString; mDescriptorString.CopyFrom((const char*)inData, inSize);
mDescriptorString = new char[1 + inSize];
if (!mDescriptorString)
return;
memcpy(mDescriptorString, inData, inSize);
mDescriptorString[inSize] = '\0';
} }
#define MAX_PERSISTENT_DATA_SIZE 1000 #define MAX_PERSISTENT_DATA_SIZE 1000
@@ -937,7 +1103,7 @@ nsInputStream& operator >> (nsInputStream& s, nsPersistentFileDescriptor& d)
if (bytesRead != 8) if (bytesRead != 8)
return s; return s;
bigBuffer[8] = '\0'; bigBuffer[8] = '\0';
sscanf(bigBuffer, "%lx", (PRUint32*)&bytesRead); sscanf(bigBuffer, "%x", (PRUint32*)&bytesRead);
if (bytesRead > MAX_PERSISTENT_DATA_SIZE) if (bytesRead > MAX_PERSISTENT_DATA_SIZE)
return s; // preposterous. return s; // preposterous.
// Now we know how many bytes to read, do it. // Now we know how many bytes to read, do it.
@@ -953,13 +1119,13 @@ nsOutputStream& operator << (nsOutputStream& s, const nsPersistentFileDescriptor
{ {
char littleBuf[9]; char littleBuf[9];
PRInt32 dataSize; PRInt32 dataSize;
void* data; nsSimpleCharString data;
d.GetData(data, dataSize); d.GetData(data, dataSize);
// First write (in hex) the length of the data to follow. Exactly 8 bytes // First write (in hex) the length of the data to follow. Exactly 8 bytes
sprintf(littleBuf, "%0.8x", dataSize); sprintf(littleBuf, "%0.8x", dataSize);
s << littleBuf; s << littleBuf;
// Now write the data itself // Now write the data itself
s << d.mDescriptorString; s << (const char*)data;
return s; return s;
} }

View File

@@ -14,9 +14,9 @@ class BasicStringImpl
{ {
public: public:
BasicStringImpl() BasicStringImpl()
: mResult(NS_OK) : mOffset(0)
, mLastResult(NS_OK)
, mEOF(PR_FALSE) , mEOF(PR_FALSE)
, mOffset(0)
{ {
NS_INIT_REFCNT(); NS_INIT_REFCNT();
} }
@@ -60,16 +60,16 @@ class BasicStringImpl
NS_PRECONDITION(aReadCount != nsnull, "null ptr"); NS_PRECONDITION(aReadCount != nsnull, "null ptr");
if (!aReadCount) if (!aReadCount)
return NS_ERROR_NULL_POINTER; return NS_ERROR_NULL_POINTER;
if (NS_FAILED(mResult)) if (NS_FAILED(mLastResult))
return mResult; return mLastResult;
PRInt32 bytesRead = read(aBuf, aCount); PRInt32 bytesRead = read(aBuf, aCount);
if (NS_FAILED(mResult)) if (NS_FAILED(mLastResult))
{ {
*aReadCount = 0; *aReadCount = 0;
return mResult; return mLastResult;
} }
*aReadCount = bytesRead; *aReadCount = bytesRead;
if (bytesRead < aCount) if (bytesRead < (PRInt32)aCount)
SetAtEOF(PR_TRUE); SetAtEOF(PR_TRUE);
return NS_OK; return NS_OK;
} }
@@ -81,13 +81,13 @@ class BasicStringImpl
NS_PRECONDITION(aBuf != nsnull, "null ptr"); NS_PRECONDITION(aBuf != nsnull, "null ptr");
NS_PRECONDITION(aWriteCount != nsnull, "null ptr"); NS_PRECONDITION(aWriteCount != nsnull, "null ptr");
if (NS_FAILED(mResult)) if (NS_FAILED(mLastResult))
return mResult; return mLastResult;
PRInt32 bytesWrit = write(aBuf, aCount); PRInt32 bytesWrit = write(aBuf, aCount);
if (NS_FAILED(mResult)) if (NS_FAILED(mLastResult))
{ {
*aWriteCount = 0; *aWriteCount = 0;
return mResult; return mLastResult;
} }
*aWriteCount = bytesWrit; *aWriteCount = bytesWrit;
return NS_OK; return NS_OK;
@@ -104,7 +104,7 @@ class BasicStringImpl
public: public:
nsresult get_result() const { return mResult; } nsresult get_result() const { return mLastResult; }
protected: protected:
@@ -113,14 +113,14 @@ class BasicStringImpl
virtual PRInt32 write(const char*, PRUint32) virtual PRInt32 write(const char*, PRUint32)
{ {
NS_ASSERTION(PR_FALSE, "Write to a const string"); NS_ASSERTION(PR_FALSE, "Write to a const string");
mResult = NS_FILE_RESULT(PR_ILLEGAL_ACCESS_ERROR); mLastResult = NS_FILE_RESULT(PR_ILLEGAL_ACCESS_ERROR);
return -1; return -1;
} }
protected: protected:
PRUint32 mOffset; PRUint32 mOffset;
nsresult mResult; nsresult mLastResult;
PRBool mEOF; PRBool mEOF;
}; // class BasicStringImpl }; // class BasicStringImpl
@@ -146,7 +146,7 @@ class ConstCharImpl
virtual PRInt32 read(char* buf, PRUint32 aCount) virtual PRInt32 read(char* buf, PRUint32 aCount)
{ {
PRInt32 maxCount = mLength - mOffset; PRInt32 maxCount = mLength - mOffset;
if (aCount > maxCount) if ((PRInt32)aCount > maxCount)
aCount = maxCount; aCount = maxCount;
memcpy(buf, mConstString + mOffset, aCount); memcpy(buf, mConstString + mOffset, aCount);
mOffset += aCount; mOffset += aCount;
@@ -181,7 +181,7 @@ class CharImpl
mString = new char[mAllocLength]; mString = new char[mAllocLength];
if (!mString) if (!mString)
{ {
mResult = NS_ERROR_OUT_OF_MEMORY; mLastResult = NS_ERROR_OUT_OF_MEMORY;
return; return;
} }
mConstString = mString; mConstString = mString;
@@ -193,17 +193,17 @@ class CharImpl
virtual PRInt32 write(const char* buf, PRUint32 aCount) virtual PRInt32 write(const char* buf, PRUint32 aCount)
{ {
PRInt32 maxCount = mAllocLength - 1 - mOffset; PRInt32 maxCount = mAllocLength - 1 - mOffset;
if (aCount > maxCount) if ((PRInt32)aCount > maxCount)
{ {
do { do {
maxCount += kAllocQuantum; maxCount += kAllocQuantum;
} while (aCount > maxCount); } while ((PRInt32)aCount > maxCount);
mAllocLength = maxCount + 1 + mOffset; mAllocLength = maxCount + 1 + mOffset;
char* newString = new char[mAllocLength]; char* newString = new char[mAllocLength];
if (!newString) if (!newString)
{ {
mResult = NS_ERROR_OUT_OF_MEMORY; mLastResult = NS_ERROR_OUT_OF_MEMORY;
return 0; return 0;
} }
strcpy(newString, mString); strcpy(newString, mString);
@@ -220,8 +220,8 @@ class CharImpl
protected: protected:
char*& mString; char*& mString;
size_t mOriginalLength;
size_t mAllocLength; size_t mAllocLength;
size_t mOriginalLength;
}; // class CharImpl }; // class CharImpl
@@ -271,7 +271,7 @@ class StringImpl
chars.Seek(PR_SEEK_SET, mOffset); chars.Seek(PR_SEEK_SET, mOffset);
// Get the bytecount and result from the CharImpl // Get the bytecount and result from the CharImpl
PRInt32 result = chars.write(buf,count); PRInt32 result = chars.write(buf,count);
mResult = chars.get_result(); mLastResult = chars.get_result();
// Set our string to match the new chars // Set our string to match the new chars
mString = cstring; mString = cstring;
// Set our const string also... // Set our const string also...
@@ -331,7 +331,7 @@ NS_IMETHODIMP BasicStringImpl::QueryInterface(REFNSIID aIID, void** aInstancePtr
NS_IMETHODIMP BasicStringImpl::Seek(PRSeekWhence whence, PRInt32 offset) NS_IMETHODIMP BasicStringImpl::Seek(PRSeekWhence whence, PRInt32 offset)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
mResult = NS_OK; // reset on a seek. mLastResult = NS_OK; // reset on a seek.
mEOF = PR_FALSE; // reset on a seek. mEOF = PR_FALSE; // reset on a seek.
PRInt32 fileSize = length(); PRInt32 fileSize = length();
PRInt32 newPosition; PRInt32 newPosition;
@@ -344,7 +344,7 @@ NS_IMETHODIMP BasicStringImpl::Seek(PRSeekWhence whence, PRInt32 offset)
if (newPosition < 0) if (newPosition < 0)
{ {
newPosition = 0; newPosition = 0;
mResult = NS_FILE_RESULT(PR_FILE_SEEK_ERROR); mLastResult = NS_FILE_RESULT(PR_FILE_SEEK_ERROR);
} }
if (newPosition >= fileSize) if (newPosition >= fileSize)
{ {

View File

@@ -61,39 +61,38 @@ extern "C" int statvfs(const char *, struct statvfs *);
#endif #endif
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileSpecHelpers::Canonify(char*& ioPath, PRBool inMakeDirs) void nsFileSpecHelpers::Canonify(nsSimpleCharString& ioPath, PRBool inMakeDirs)
// Canonify, make absolute, and check whether directories exist // Canonify, make absolute, and check whether directories exist
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!ioPath) if (ioPath.IsEmpty())
return; return;
if (inMakeDirs) if (inMakeDirs)
{ {
const mode_t mode = 0700; const mode_t mode = 0700;
nsFileSpecHelpers::MakeAllDirectories(ioPath, mode); nsFileSpecHelpers::MakeAllDirectories((const char*)ioPath, mode);
} }
char buffer[MAXPATHLEN]; char buffer[MAXPATHLEN];
errno = 0; errno = 0;
*buffer = '\0'; *buffer = '\0';
char* canonicalPath = realpath(ioPath, buffer); char* canonicalPath = realpath((const char*)ioPath, buffer);
if (!canonicalPath) if (!canonicalPath)
{ {
// Linux's realpath() is pathetically buggy. If the reason for the nil // Linux's realpath() is pathetically buggy. If the reason for the nil
// result is just that the leaf does not exist, strip the leaf off, // result is just that the leaf does not exist, strip the leaf off,
// process that, and then add the leaf back. // process that, and then add the leaf back.
char* allButLeaf = nsFileSpecHelpers::StringDup(ioPath); nsSimpleCharString allButLeaf(ioPath);
if (!allButLeaf) if (allButLeaf.IsEmpty())
return; return;
char* lastSeparator = strrchr(allButLeaf, '/'); char* lastSeparator = strrchr((char*)allButLeaf, '/');
if (lastSeparator) if (lastSeparator)
{ {
*lastSeparator = '\0'; *lastSeparator = '\0';
canonicalPath = realpath(allButLeaf, buffer); canonicalPath = realpath((const char*)allButLeaf, buffer);
strcat(buffer, "/"); strcat(buffer, "/");
// Add back the leaf // Add back the leaf
strcat(buffer, ++lastSeparator); strcat(buffer, ++lastSeparator);
} }
delete [] allButLeaf;
} }
if (!canonicalPath && *ioPath != '/' && !inMakeDirs) if (!canonicalPath && *ioPath != '/' && !inMakeDirs)
{ {
@@ -106,21 +105,21 @@ void nsFileSpecHelpers::Canonify(char*& ioPath, PRBool inMakeDirs)
} }
} }
if (canonicalPath) if (canonicalPath)
nsFileSpecHelpers::StringAssign(ioPath, canonicalPath); ioPath = canonicalPath;
} // nsFileSpecHelpers::Canonify } // nsFileSpecHelpers::Canonify
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileSpec::SetLeafName(const char* inLeafName) void nsFileSpec::SetLeafName(const char* inLeafName)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::LeafReplace(mPath, '/', inLeafName); mPath.LeafReplace('/', inLeafName);
} // nsFileSpec::SetLeafName } // nsFileSpec::SetLeafName
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
char* nsFileSpec::GetLeafName() const char* nsFileSpec::GetLeafName() const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
return nsFileSpecHelpers::GetLeaf(mPath, '/'); return mPath.GetLeaf('/');
} // nsFileSpec::GetLeafName } // nsFileSpec::GetLeafName
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
@@ -172,9 +171,9 @@ PRBool nsFileSpec::IsDirectory() const
void nsFileSpec::GetParent(nsFileSpec& outSpec) const void nsFileSpec::GetParent(nsFileSpec& outSpec) const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(outSpec.mPath, mPath); outSpec.mPath = mPath;
char* cp = strrchr(outSpec.mPath, '/'); char* cp = strrchr(outSpec.mPath, '/');
if (cp) if (cp++)
*cp = '\0'; *cp = '\0';
} // nsFileSpec::GetParent } // nsFileSpec::GetParent
@@ -182,14 +181,14 @@ void nsFileSpec::GetParent(nsFileSpec& outSpec) const
void nsFileSpec::operator += (const char* inRelativePath) void nsFileSpec::operator += (const char* inRelativePath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!inRelativePath || !mPath) if (!inRelativePath || mPath.IsEmpty())
return; return;
char endChar = mPath[strlen(mPath) - 1]; char endChar = mPath[strlen(mPath) - 1];
if (endChar == '/') if (endChar == '/')
nsFileSpecHelpers::ReallocCat(mPath, "x"); mPath += "x";
else else
nsFileSpecHelpers::ReallocCat(mPath, "/x"); mPath += "/x";
SetLeafName(inRelativePath); SetLeafName(inRelativePath);
} // nsFileSpec::operator += } // nsFileSpec::operator +=
@@ -304,16 +303,11 @@ nsresult nsFileSpec::Copy(const nsFileSpec& inParentDirectory) const
if (inParentDirectory.IsDirectory() && (! IsDirectory() ) ) if (inParentDirectory.IsDirectory() && (! IsDirectory() ) )
{ {
char *leafname = GetLeafName(); char *leafname = GetLeafName();
char* destPath = nsFileSpecHelpers::StringDup( nsSimpleCharString destPath(inParentDirectory.GetCString());
inParentDirectory.GetCString(), destPath += "/";
strlen(inParentDirectory.GetCString()) + 1 + strlen(leafname)); destPath += leafname;
strcat(destPath, "/");
strcat(destPath, leafname);
delete [] leafname; delete [] leafname;
result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), destPath)); result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), destPath));
delete [] destPath;
} }
return result; return result;
} // nsFileSpec::Copy } // nsFileSpec::Copy
@@ -325,24 +319,20 @@ nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory) const
// We can only copy into a directory, and (for now) can not copy entire directories // We can only copy into a directory, and (for now) can not copy entire directories
nsresult result = NS_FILE_FAILURE; nsresult result = NS_FILE_FAILURE;
if (inNewParentDirectory.IsDirectory() && (! IsDirectory() ) ) if (inNewParentDirectory.IsDirectory() && !IsDirectory())
{ {
char *leafname = GetLeafName(); char *leafname = GetLeafName();
char* destPath nsSimpleCharString destPath(inNewParentDirectory.GetCString());
= nsFileSpecHelpers::StringDup( destPath += "/";
inNewParentDirectory.GetCString(), destPath += leafname;
strlen(inNewParentDirectory.GetCString()) + 1 + strlen(leafname));
strcat(destPath, "/");
strcat(destPath, leafname);
delete [] leafname; delete [] leafname;
result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), destPath)); result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), (const char*)destPath));
if (result == NS_OK) if (result == NS_OK)
{ {
// cast to fix const-ness // cast to fix const-ness
((nsFileSpec*)this)->Delete(PR_FALSE); ((nsFileSpec*)this)->Delete(PR_FALSE);
} }
delete [] destPath;
} }
return result; return result;
} }
@@ -355,13 +345,8 @@ nsresult nsFileSpec::Execute(const char* inArgs ) const
if (! IsDirectory()) if (! IsDirectory())
{ {
char* fileNameWithArgs nsSimpleCharString fileNameWithArgs = mPath + " " + inArgs;
= nsFileSpecHelpers::StringDup(mPath, strlen(mPath) + 1 + strlen(inArgs));
strcat(fileNameWithArgs, " ");
strcat(fileNameWithArgs, inArgs);
result = NS_FILE_RESULT(system(fileNameWithArgs)); result = NS_FILE_RESULT(system(fileNameWithArgs));
delete [] fileNameWithArgs;
} }
return result; return result;
@@ -373,14 +358,14 @@ PRUint32 nsFileSpec::GetDiskSpaceAvailable() const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
char curdir [MAXPATHLEN]; char curdir [MAXPATHLEN];
if (!mPath || !*mPath) if (mPath.IsEmpty())
{ {
(void) getcwd(curdir, MAXPATHLEN); (void) getcwd(curdir, MAXPATHLEN);
if (!curdir) if (!curdir)
return ULONG_MAX; /* hope for the best as we did in cheddar */ return ULONG_MAX; /* hope for the best as we did in cheddar */
} }
else else
sprintf(curdir, "%.200s", mPath); sprintf(curdir, "%.200s", (const char*)mPath);
struct STATFS fs_buf; struct STATFS fs_buf;
if (STATFS(curdir, &fs_buf) < 0) if (STATFS(curdir, &fs_buf) < 0)

View File

@@ -35,22 +35,21 @@
#endif #endif
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileSpecHelpers::Canonify(char*& ioPath, PRBool inMakeDirs) void nsFileSpecHelpers::Canonify(nsSimpleCharString& ioPath, PRBool inMakeDirs)
// Canonify, make absolute, and check whether directories exist. This // Canonify, make absolute, and check whether directories exist. This
// takes a (possibly relative) native path and converts it into a // takes a (possibly relative) native path and converts it into a
// fully qualified native path. // fully qualified native path.
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!ioPath) if (ioPath.IsEmpty())
return; return;
if (inMakeDirs) if (inMakeDirs)
{ {
const int mode = 0700; const int mode = 0700;
char* unixStylePath = nsFileSpecHelpers::StringDup(ioPath); nsSimpleCharString unixStylePath = ioPath;
nsFileSpecHelpers::NativeToUnix(unixStylePath); nsFileSpecHelpers::NativeToUnix(unixStylePath);
nsFileSpecHelpers::MakeAllDirectories(unixStylePath, mode); nsFileSpecHelpers::MakeAllDirectories((const char*)unixStylePath, mode);
delete[] unixStylePath;
} }
char buffer[_MAX_PATH]; char buffer[_MAX_PATH];
errno = 0; errno = 0;
@@ -61,11 +60,11 @@ void nsFileSpecHelpers::Canonify(char*& ioPath, PRBool inMakeDirs)
if (canonicalPath[0] == '\0') if (canonicalPath[0] == '\0')
return; return;
nsFileSpecHelpers::StringAssign(ioPath, canonicalPath); ioPath = canonicalPath;
} } // nsFileSpecHelpers::Canonify
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileSpecHelpers::UnixToNative(char*& ioPath) void nsFileSpecHelpers::UnixToNative(nsSimpleCharString& ioPath)
// This just does string manipulation. It doesn't check reality, or canonify, or // This just does string manipulation. It doesn't check reality, or canonify, or
// anything // anything
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
@@ -73,55 +72,49 @@ void nsFileSpecHelpers::UnixToNative(char*& ioPath)
// Allow for relative or absolute. We can do this in place, because the // Allow for relative or absolute. We can do this in place, because the
// native path is never longer. // native path is never longer.
if (!ioPath || !*ioPath) if (ioPath.IsEmpty())
return; return;
char* src = ioPath;
if (*ioPath == '/')
{
// Strip initial slash for an absolute path // Strip initial slash for an absolute path
src++; char* src = (char*)ioPath;
if (*src == '/')
{
// Since it was an absolute path, check for the drive letter
char* colonPointer = src + 2;
if (strstr(src, "|/") == colonPointer)
*colonPointer = ':';
// allocate new string by copying from ioPath[1]
nsSimpleCharString temp = src + 1;
ioPath = temp;
} }
// Convert the vertical slash to a colon src = (char*)ioPath;
char* cp = src + 1;
// If it was an absolute path, check for the drive letter
if (*ioPath == '/' && strstr(cp, "|/") == cp)
*cp = ':';
// Convert '/' to '\'. // Convert '/' to '\'.
while (*++cp) while (*++src)
{ {
if (*cp == '/') if (*src == '/')
*cp = '\\'; *src = '\\';
} }
} // nsFileSpecHelpers::UnixToNative
if (*ioPath == '/') {
for (cp = ioPath; *cp; ++cp)
*cp = *(cp + 1);
}
}
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileSpecHelpers::NativeToUnix(char*& ioPath) void nsFileSpecHelpers::NativeToUnix(nsSimpleCharString& ioPath)
// This just does string manipulation. It doesn't check reality, or canonify, or // This just does string manipulation. It doesn't check reality, or canonify, or
// anything. The unix path is longer, so we can't do it in place. // anything. The unix path is longer, so we can't do it in place.
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!ioPath || !*ioPath) if (ioPath.IsEmpty())
return; return;
// Convert the drive-letter separator, if present // Convert the drive-letter separator, if present
char* temp = nsFileSpecHelpers::StringDup("/", 1 + strlen(ioPath)); nsSimpleCharString temp("/");
char* cp = ioPath + 1; char* cp = (char*)ioPath + 1;
if (strstr(cp, ":\\") == cp) { if (strstr(cp, ":\\") == cp)
*cp = '|'; // absolute path *cp = '|'; // absolute path
} else
else { temp[0] = '\0'; // relative path
*temp = '\0'; // relative path
}
// Convert '\' to '/' // Convert '\' to '/'
for (; *cp; cp++) for (; *cp; cp++)
@@ -129,17 +122,14 @@ void nsFileSpecHelpers::NativeToUnix(char*& ioPath)
if (*cp == '\\') if (*cp == '\\')
*cp = '/'; *cp = '/';
} }
// Add the slash in front. // Add the slash in front.
strcat(temp, ioPath); temp += ioPath;
StringAssign(ioPath, temp); ioPath = temp;
delete [] temp;
} }
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsFilePath& inPath) nsFileSpec::nsFileSpec(const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(NULL)
{ {
*this = inPath; *this = inPath;
} }
@@ -148,7 +138,7 @@ nsFileSpec::nsFileSpec(const nsFilePath& inPath)
void nsFileSpec::operator = (const nsFilePath& inPath) void nsFileSpec::operator = (const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, (const char*)inPath); mPath = (const char*)inPath;
nsFileSpecHelpers::UnixToNative(mPath); nsFileSpecHelpers::UnixToNative(mPath);
mError = NS_OK; mError = NS_OK;
} // nsFileSpec::operator = } // nsFileSpec::operator =
@@ -156,7 +146,6 @@ void nsFileSpec::operator = (const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const nsFileSpec& inSpec) nsFilePath::nsFilePath(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(NULL)
{ {
*this = inSpec; *this = inSpec;
} // nsFilePath::nsFilePath } // nsFilePath::nsFilePath
@@ -165,7 +154,7 @@ nsFilePath::nsFilePath(const nsFileSpec& inSpec)
void nsFilePath::operator = (const nsFileSpec& inSpec) void nsFilePath::operator = (const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, inSpec.mPath); mPath = inSpec.mPath;
nsFileSpecHelpers::NativeToUnix(mPath); nsFileSpecHelpers::NativeToUnix(mPath);
} // nsFilePath::operator = } // nsFilePath::operator =
@@ -173,14 +162,14 @@ void nsFilePath::operator = (const nsFileSpec& inSpec)
void nsFileSpec::SetLeafName(const char* inLeafName) void nsFileSpec::SetLeafName(const char* inLeafName)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::LeafReplace(mPath, '\\', inLeafName); mPath.LeafReplace('\\', inLeafName);
} // nsFileSpec::SetLeafName } // nsFileSpec::SetLeafName
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
char* nsFileSpec::GetLeafName() const char* nsFileSpec::GetLeafName() const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
return nsFileSpecHelpers::GetLeaf(mPath, '\\'); return mPath.GetLeaf('\\');
} // nsFileSpec::GetLeafName } // nsFileSpec::GetLeafName
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
@@ -232,9 +221,9 @@ PRBool nsFileSpec::IsDirectory() const
void nsFileSpec::GetParent(nsFileSpec& outSpec) const void nsFileSpec::GetParent(nsFileSpec& outSpec) const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(outSpec.mPath, mPath); outSpec.mPath = mPath;
char* cp = strrchr(outSpec.mPath, '\\'); char* cp = strrchr(outSpec.mPath, '\\');
if (cp) if (cp++)
*cp = '\0'; *cp = '\0';
} // nsFileSpec::GetParent } // nsFileSpec::GetParent
@@ -242,19 +231,18 @@ void nsFileSpec::GetParent(nsFileSpec& outSpec) const
void nsFileSpec::operator += (const char* inRelativePath) void nsFileSpec::operator += (const char* inRelativePath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!inRelativePath || !mPath) if (!inRelativePath || mPath.IsEmpty())
return; return;
if (mPath[strlen(mPath) - 1] == '\\') if (mPath[mPath.Length() - 1] == '\\')
nsFileSpecHelpers::ReallocCat(mPath, "x"); mPath += "x";
else else
nsFileSpecHelpers::ReallocCat(mPath, "\\x"); mPath += "\\x";
// If it's a (unix) relative path, make it native // If it's a (unix) relative path, make it native
char* dosPath = nsFileSpecHelpers::StringDup(inRelativePath); nsSimpleCharString dosPath = inRelativePath;
nsFileSpecHelpers::UnixToNative(dosPath); nsFileSpecHelpers::UnixToNative(dosPath);
SetLeafName(dosPath); SetLeafName(dosPath);
delete [] dosPath;
} // nsFileSpec::operator += } // nsFileSpec::operator +=
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
@@ -309,54 +297,40 @@ nsresult nsFileSpec::Copy(const nsFileSpec& inParentDirectory) const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
// We can only copy into a directory, and (for now) can not copy entire directories // We can only copy into a directory, and (for now) can not copy entire directories
if (inParentDirectory.IsDirectory() && (! IsDirectory() ) ) if (inParentDirectory.IsDirectory() && (! IsDirectory() ) )
{ {
char *leafname = GetLeafName(); char *leafname = GetLeafName();
char* destPath = nsFileSpecHelpers::StringDup(inParentDirectory, ( strlen(inParentDirectory) + 1 + strlen(leafname) ) ); nsSimpleCharString destPath(inParentDirectory.GetCString());
strcat(destPath, "\\"); destPath += "\\";
strcat(destPath, leafname); destPath += leafname;
delete [] leafname; delete [] leafname;
// CopyFile returns non-zero if succeeds // CopyFile returns non-zero if succeeds
int copyOK = CopyFile(*this, destPath, true); int copyOK = CopyFile(GetCString(), destPath, true);
delete[] destPath;
if (copyOK) if (copyOK)
{
return NS_OK; return NS_OK;
} }
}
return NS_FILE_FAILURE; return NS_FILE_FAILURE;
} // nsFileSpec::Copy } // nsFileSpec::Copy
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsresult nsFileSpec::Move(const nsFileSpec& nsNewParentDirectory) const nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory) const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
// We can only copy into a directory, and (for now) can not copy entire directories // We can only copy into a directory, and (for now) can not copy entire directories
if (inNewParentDirectory.IsDirectory() && (! IsDirectory() ) )
if (nsNewParentDirectory.IsDirectory() && (! IsDirectory() ) )
{ {
char *leafname = GetLeafName(); char *leafname = GetLeafName();
char *destPath = nsFileSpecHelpers::StringDup(nsNewParentDirectory, ( strlen(nsNewParentDirectory) + 1 + strlen(leafname) )); nsSimpleCharString destPath(inNewParentDirectory.GetCString());
strcat(destPath, "\\"); destPath += "\\";
strcat(destPath, leafname); destPath += leafname;
delete [] leafname; delete [] leafname;
// MoveFile returns non-zero if succeeds // MoveFile returns non-zero if succeeds
int copyOK = MoveFile(*this, destPath); int copyOK = MoveFile(GetCString(), destPath);
delete [] destPath;
if (copyOK) if (copyOK)
{
return NS_OK; return NS_OK;
} }
}
return NS_FILE_FAILURE; return NS_FILE_FAILURE;
} // nsFileSpec::Move } // nsFileSpec::Move
@@ -364,25 +338,13 @@ nsresult nsFileSpec::Move(const nsFileSpec& nsNewParentDirectory) const
nsresult nsFileSpec::Execute(const char* inArgs ) const nsresult nsFileSpec::Execute(const char* inArgs ) const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!IsDirectory())
if (! IsDirectory())
{ {
char* fileNameWithArgs = NULL; nsSimpleCharString fileNameWithArgs = mPath + " " + inArgs;
fileNameWithArgs = nsFileSpecHelpers::StringDup(mPath, ( strlen(mPath) + 1 + strlen(inArgs) ) );
strcat(fileNameWithArgs, " ");
strcat(fileNameWithArgs, inArgs);
int execResult = WinExec( fileNameWithArgs, SW_NORMAL ); int execResult = WinExec( fileNameWithArgs, SW_NORMAL );
delete [] fileNameWithArgs;
if (execResult > 31) if (execResult > 31)
{
return NS_OK; return NS_OK;
} }
}
return NS_FILE_FAILURE; return NS_FILE_FAILURE;
} // nsFileSpec::Execute } // nsFileSpec::Execute
@@ -391,13 +353,13 @@ PRUint32 nsFileSpec::GetDiskSpaceAvailable() const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
char aDrive[_MAX_DRIVE + 2]; char aDrive[_MAX_DRIVE + 2];
_splitpath( mPath, aDrive, NULL, NULL, NULL); _splitpath( (const char*)mPath, aDrive, NULL, NULL, NULL);
if (aDrive[0] == '\0') if (aDrive[0] == '\0')
{ {
// The back end is always trying to pass us paths that look // The back end is always trying to pass us paths that look
// like /c|/netscape/mail. See if we've got one of them // like /c|/netscape/mail. See if we've got one of them
if (strlen(mPath) > 2 && mPath[0] == '/' && mPath[2] == '|') if (mPath.Length() > 2 && mPath[0] == '/' && mPath[2] == '|')
{ {
aDrive[0] = mPath[1]; aDrive[0] = mPath[1];
aDrive[1] = ':'; aDrive[1] = ':';

View File

@@ -345,11 +345,14 @@ int FilesTest::Parent(
mySpec.GetParent(outParent); mySpec.GetParent(outParent);
nsFilePath parentPath(outParent); nsFilePath parentPath(outParent);
nsFileURL url(parentPath);
mConsole mConsole
<< "GetParent() on " << "GetParent() on "
<< "\n\t" << pathAsString << "\n\t" << pathAsString
<< "\n yields " << "\n yields "
<< "\n\t" << (const char*)parentPath << "\n\t" << (const char*)parentPath
<< "\n or as a URL"
<< "\n\t" << (const char*)url
<< nsEndl; << nsEndl;
Inspect(); Inspect();
return 0; return 0;

View File

@@ -30,6 +30,219 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
//========================================================================================
// class nsSimpleCharString
//========================================================================================
//----------------------------------------------------------------------------------------
nsSimpleCharString::nsSimpleCharString()
//----------------------------------------------------------------------------------------
: mData(nsnull)
{
} // nsSimpleCharString::nsSimpleCharString
//----------------------------------------------------------------------------------------
nsSimpleCharString::nsSimpleCharString(const char* inString)
//----------------------------------------------------------------------------------------
: mData(nsnull)
{
if (inString)
CopyFrom(inString, strlen(inString));
} // nsSimpleCharString::nsSimpleCharString
//----------------------------------------------------------------------------------------
nsSimpleCharString::nsSimpleCharString(const nsString& inString)
//----------------------------------------------------------------------------------------
: mData(nsnull)
{
*this = inString;
} // nsSimpleCharString::nsSimpleCharString
//----------------------------------------------------------------------------------------
nsSimpleCharString::nsSimpleCharString(const nsSimpleCharString& inOther)
//----------------------------------------------------------------------------------------
{
mData = inOther.mData;
AddRefData();
} // nsSimpleCharString::nsSimpleCharString
//----------------------------------------------------------------------------------------
nsSimpleCharString::nsSimpleCharString(const char* inData, PRUint32 inLength)
//----------------------------------------------------------------------------------------
: mData(nsnull)
{
CopyFrom(inData, inLength);
} // nsSimpleCharString::nsSimpleCharString
//----------------------------------------------------------------------------------------
nsSimpleCharString::~nsSimpleCharString()
//----------------------------------------------------------------------------------------
{
ReleaseData();
} // nsSimpleCharString::nsSimpleCharString
//----------------------------------------------------------------------------------------
void nsSimpleCharString::operator = (const char* inString)
//----------------------------------------------------------------------------------------
{
if (inString)
CopyFrom(inString, strlen(inString));
else
SetToEmpty();
} // nsSimpleCharString::operator =
//----------------------------------------------------------------------------------------
void nsSimpleCharString::operator = (const nsString& inString)
//----------------------------------------------------------------------------------------
{
PRUint32 len = inString.Length();
ReallocData(len);
if (!mData)
return;
inString.ToCString(mData->mString, len);
} // nsSimpleCharString::operator =
//----------------------------------------------------------------------------------------
void nsSimpleCharString::operator = (const nsSimpleCharString& inOther)
//----------------------------------------------------------------------------------------
{
if (mData == inOther.mData)
return;
ReleaseData();
mData = inOther.mData;
AddRefData();
} // nsSimpleCharString::operator =
//----------------------------------------------------------------------------------------
void nsSimpleCharString::operator += (const char* inOther)
//----------------------------------------------------------------------------------------
{
if (!inOther)
return;
int newLength = Length() + PL_strlen(inOther);
ReallocData(newLength);
strcat(mData->mString, inOther);
} // nsSimpleCharString::operator =
//----------------------------------------------------------------------------------------
nsSimpleCharString nsSimpleCharString::operator + (const char* inOther) const
//----------------------------------------------------------------------------------------
{
nsSimpleCharString result(*this);
result += inOther;
return result;
} // nsSimpleCharString::operator =
//----------------------------------------------------------------------------------------
void nsSimpleCharString::Catenate(const char* inString1, const char* inString2)
//----------------------------------------------------------------------------------------
{
if (!inString2)
{
*this += inString1;
return;
}
int newLength = Length() + PL_strlen(inString1) + PL_strlen(inString2);
ReallocData(newLength);
strcat(mData->mString, inString1);
strcat(mData->mString, inString2);
} // nsSimpleCharString::operator =
//----------------------------------------------------------------------------------------
void nsSimpleCharString::CopyFrom(const char* inData, PRUint32 inLength)
//----------------------------------------------------------------------------------------
{
if (!inData)
return;
ReallocData(inLength);
if (!mData)
return;
memcpy(mData->mString, inData, inLength);
mData->mString[inLength] = '\0';
} // nsSimpleCharString::CopyFrom
//----------------------------------------------------------------------------------------
void nsSimpleCharString::SetToEmpty()
//----------------------------------------------------------------------------------------
{
ReleaseData();
} // nsSimpleCharString::SetToEmpty
//----------------------------------------------------------------------------------------
void nsSimpleCharString::Unescape()
//----------------------------------------------------------------------------------------
{
if (!mData)
return;
nsUnescape(mData->mString);
mData->mLength = strlen(mData->mString);
} // nsSimpleCharString::Unescape
//----------------------------------------------------------------------------------------
void nsSimpleCharString::AddRefData()
//----------------------------------------------------------------------------------------
{
if (mData)
++mData->mRefCount;
} // nsSimpleCharString::AddRefData
//----------------------------------------------------------------------------------------
void nsSimpleCharString::ReleaseData()
//----------------------------------------------------------------------------------------
{
if (!mData)
return;
NS_ASSERTION(mData->mRefCount > 0, "String deleted too many times!");
if (--mData->mRefCount == 0)
PR_Free(mData);
mData = nsnull;
} // nsSimpleCharString::ReleaseData
//----------------------------------------------------------------------------------------
void nsSimpleCharString::ReallocData(PRUint32 inLength)
// Reallocate mData to a new length. Since this presumably precedes a change to the string,
// we want to detach ourselves if the data is shared by another string, even if the length
// requested would not otherwise require a reallocation.
//----------------------------------------------------------------------------------------
{
// We always allocate for a string of at least kMinStringSize, to prevent a lot of small
// reallocations.
const PRUint32 kMinStringSize = 127;
if (mData)
{
// Re
NS_ASSERTION(mData->mRefCount > 0, "String deleted too many times!");
if (mData->mRefCount == 1)
{
// We are the sole owner, so just change its length, if necessary.
if (inLength > mData->mLength && inLength > kMinStringSize)
mData = (Data*)PR_Realloc(mData, inLength + sizeof(Data));
mData->mLength = inLength;
return;
}
}
PRUint32 allocLength = inLength;
if (allocLength < kMinStringSize)
allocLength = kMinStringSize;
Data* newData = (Data*)PR_Malloc(allocLength + sizeof(Data));
// If data was already allocated when we get to here, then we are cloning the data
// from a shared pointer.
if (mData)
{
memcpy(newData, mData, sizeof(Data) + Length());
mData->mRefCount--; // Say goodbye
}
else
newData->mString[0] = '\0';
mData = newData;
mData->mRefCount = 1;
mData->mLength = inLength;
} // nsSimpleCharString::ReleaseData
//======================================================================================== //========================================================================================
NS_NAMESPACE nsFileSpecHelpers NS_NAMESPACE nsFileSpecHelpers
//======================================================================================== //========================================================================================
@@ -39,22 +252,13 @@ NS_NAMESPACE nsFileSpecHelpers
, kMaxAltDigitLength = 5 , kMaxAltDigitLength = 5
, kMaxCoreLeafNameLength = (kMaxFilenameLength - (kMaxAltDigitLength + 1)) , kMaxCoreLeafNameLength = (kMaxFilenameLength - (kMaxAltDigitLength + 1))
}; };
NS_NAMESPACE_PROTOTYPE void LeafReplace(
char*& ioPath,
char inSeparator,
const char* inLeafName);
#ifndef XP_MAC #ifndef XP_MAC
NS_NAMESPACE_PROTOTYPE void Canonify(char*& ioPath, PRBool inMakeDirs); NS_NAMESPACE_PROTOTYPE void Canonify(nsSimpleCharString& ioPath, PRBool inMakeDirs);
NS_NAMESPACE_PROTOTYPE void MakeAllDirectories(const char* inPath, int mode); NS_NAMESPACE_PROTOTYPE void MakeAllDirectories(const char* inPath, int mode);
#endif #endif
NS_NAMESPACE_PROTOTYPE char* GetLeaf(const char* inPath, char inSeparator); // allocated
NS_NAMESPACE_PROTOTYPE char* StringDup(const char* inString, int allocLength = 0);
NS_NAMESPACE_PROTOTYPE char* AllocCat(const char* inString1, const char* inString2);
NS_NAMESPACE_PROTOTYPE char* StringAssign(char*& ioString, const char* inOther);
NS_NAMESPACE_PROTOTYPE char* ReallocCat(char*& ioString, const char* inString1);
#ifdef XP_PC #ifdef XP_PC
NS_NAMESPACE_PROTOTYPE void NativeToUnix(char*& ioPath); NS_NAMESPACE_PROTOTYPE void NativeToUnix(nsSimpleCharString& ioPath);
NS_NAMESPACE_PROTOTYPE void UnixToNative(char*& ioPath); NS_NAMESPACE_PROTOTYPE void UnixToNative(nsSimpleCharString& ioPath);
#endif #endif
} NS_NAMESPACE_END } NS_NAMESPACE_END
@@ -68,136 +272,80 @@ nsresult ns_file_convert_result(PRInt32 nativeErr)
} }
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
char* nsFileSpecHelpers::StringDup( void nsSimpleCharString::LeafReplace(char inSeparator, const char* inLeafName)
const char* inString,
int allocLength)
//----------------------------------------------------------------------------------------
{
if (!allocLength && inString)
allocLength = strlen(inString);
char* newPath = inString || allocLength ? new char[allocLength + 1] : nsnull;
if (!newPath)
return nsnull;
strcpy(newPath, inString);
return newPath;
} // nsFileSpecHelpers::StringDup
//----------------------------------------------------------------------------------------
char* nsFileSpecHelpers::AllocCat(
const char* inString1,
const char* inString2)
//----------------------------------------------------------------------------------------
{
if (!inString1)
return inString2 ? StringDup(inString2) : (char*)nsnull;
if (!inString2)
return StringDup(inString1);
char* outString = StringDup(inString1, strlen(inString1) + strlen(inString2));
if (outString)
strcat(outString, inString2);
return outString;
} // nsFileSpecHelpers::StringDup
//----------------------------------------------------------------------------------------
char* nsFileSpecHelpers::StringAssign(
char*& ioString,
const char* inString2)
//----------------------------------------------------------------------------------------
{
if (!inString2)
{
delete [] ioString;
ioString = (char*)nsnull;
return ioString;
}
if (!ioString || (strlen(inString2) > strlen(ioString)))
{
delete [] ioString;
ioString = StringDup(inString2);
return ioString;
}
strcpy(ioString, inString2);
return ioString;
} // nsFileSpecHelpers::StringAssign
//----------------------------------------------------------------------------------------
void nsFileSpecHelpers::LeafReplace(
char*& ioPath,
char inSeparator,
const char* inLeafName)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
// Find the existing leaf name // Find the existing leaf name
if (!ioPath) if (IsEmpty())
return; return;
if (!inLeafName) if (!inLeafName)
{ {
*ioPath = '\0'; SetToEmpty();
return; return;
} }
char* lastSeparator = strrchr(ioPath, inSeparator); char* chars = mData->mString;
int oldLength = strlen(ioPath); char* lastSeparator = strrchr(chars, inSeparator);
PRBool trailingSeparator = (lastSeparator + 1 == ioPath + oldLength); int oldLength = Length();
PRBool trailingSeparator = (lastSeparator + 1 == chars + oldLength);
if (trailingSeparator) if (trailingSeparator)
{ {
*lastSeparator = '\0'; *lastSeparator = '\0';
lastSeparator = strrchr(ioPath, inSeparator); lastSeparator = strrchr(chars, inSeparator);
} }
if (lastSeparator) if (lastSeparator)
lastSeparator++; // point at the trailing string lastSeparator++; // point at the trailing string
else else
lastSeparator = ioPath; // the full monty lastSeparator = chars; // the full monty
*lastSeparator = '\0'; // strip the current leaf name *lastSeparator = '\0'; // strip the current leaf name
int newLength = (lastSeparator - ioPath) + strlen(inLeafName) + int(trailingSeparator); int newLength = (lastSeparator - chars) + strlen(inLeafName) + int(trailingSeparator);
if (newLength > oldLength) ReallocData(newLength);
{ chars = mData->mString; // it might have moved.
char* newPath = StringDup(ioPath, newLength + 1);
delete [] ioPath; strcat(chars, inLeafName);
ioPath = newPath;
}
strcat(ioPath, inLeafName);
if (trailingSeparator) if (trailingSeparator)
{ {
// If the original ended in a slash, then the new one should, too. // If the original ended in a slash, then the new one should, too.
char sepStr[2] = "/"; char sepStr[2] = "/";
*sepStr = inSeparator; *sepStr = inSeparator;
strcat(ioPath, sepStr); strcat(chars, sepStr);
} }
} // nsFileSpecHelpers::LeafReplace } // nsSimpleCharString::LeafReplace
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
char* nsFileSpecHelpers::GetLeaf(const char* inPath, char inSeparator) char* nsSimpleCharString::GetLeaf(char inSeparator) const
// Returns a pointer to an allocated string representing the leaf. // Returns a pointer to an allocated string representing the leaf.
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!inPath) if (IsEmpty())
return nsnull; return nsnull;
const char* lastSeparator = strrchr(inPath, inSeparator);
// If there was no separator, then return a copy of the caller's path. char* chars = mData->mString;
const char* lastSeparator = strrchr(chars, inSeparator);
// If there was no separator, then return a copy of our path.
if (!lastSeparator) if (!lastSeparator)
return StringDup(inPath); return PL_strdup(*this);
// So there's at least one separator. What's just after it? // So there's at least one separator. What's just after it?
// If the separator was not the last character, return the trailing string. // If the separator was not the last character, return the trailing string.
const char* leafPointer = lastSeparator + 1; const char* leafPointer = lastSeparator + 1;
if (*leafPointer) if (*leafPointer)
return StringDup(leafPointer); return PL_strdup(leafPointer);
// So now, separator was the last character. Poke in a null instead. // So now, separator was the last character. Poke in a null instead.
*(char*)lastSeparator = '\0'; // Should use const_cast, but Unix has old compiler. *(char*)lastSeparator = '\0'; // Should use const_cast, but Unix has old compiler.
leafPointer = strrchr(inPath, inSeparator); leafPointer = strrchr(chars, inSeparator);
char* result = leafPointer ? StringDup(++leafPointer) : StringDup(inPath); char* result = leafPointer ? PL_strdup(++leafPointer) : PL_strdup(chars);
// Restore the poked null before returning. // Restore the poked null before returning.
*(char*)lastSeparator = inSeparator; *(char*)lastSeparator = inSeparator;
#ifdef XP_PC #ifdef XP_PC
// If it's a drive letter use the colon notation. // If it's a drive letter use the colon notation.
if (!leafPointer && strlen(result) == 2 && result[1] == '|') if (!leafPointer && result[2] == 0 && result[1] == '|')
result[1] = ':'; result[1] = ':';
#endif #endif
return result; return result;
} // nsFileSpecHelpers::GetLeaf } // nsSimpleCharString::GetLeaf
#if defined(XP_UNIX) || defined(XP_PC) #if defined(XP_UNIX) || defined(XP_PC)
@@ -210,7 +358,7 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode)
if (!inPath) if (!inPath)
return; return;
char* pathCopy = nsFileSpecHelpers::StringDup( inPath ); char* pathCopy = PL_strdup( inPath );
if (!pathCopy) if (!pathCopy)
return; return;
@@ -227,7 +375,6 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode)
if (currentEnd) if (currentEnd)
{ {
nsFileSpec spec; nsFileSpec spec;
*currentEnd = '\0'; *currentEnd = '\0';
#ifdef XP_PC #ifdef XP_PC
@@ -237,12 +384,13 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode)
*/ */
if (pathCopy[0] == '/' && pathCopy[2] == '|') if (pathCopy[0] == '/' && pathCopy[2] == '|')
{ {
char* startDir = nsFileSpecHelpers::StringDup( pathCopy, (strlen(pathCopy) + 1) ); char* startDir = (char*)PR_Malloc(strlen(pathCopy) + 2);
strcpy(startDir, pathCopy);
strcat(startDir, "/"); strcat(startDir, "/");
spec = nsFilePath(startDir, PR_FALSE); spec = nsFilePath(startDir, PR_FALSE);
delete [] startDir; PR_Free(startDir);
} }
else else
{ {
@@ -278,16 +426,6 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode)
#endif // XP_PC || XP_UNIX #endif // XP_PC || XP_UNIX
//----------------------------------------------------------------------------------------
char* nsFileSpecHelpers::ReallocCat(char*& ioString, const char* inString1)
//----------------------------------------------------------------------------------------
{
char* newString = AllocCat(ioString, inString1);
delete [] ioString;
ioString = newString;
return ioString;
} // nsFileSpecHelpers::ReallocCat
#if defined(XP_PC) #if defined(XP_PC)
#include "windows/nsFileSpecWin.cpp" // Windows-specific implementations #include "windows/nsFileSpecWin.cpp" // Windows-specific implementations
#elif defined(XP_MAC) #elif defined(XP_MAC)
@@ -304,7 +442,6 @@ char* nsFileSpecHelpers::ReallocCat(char*& ioString, const char* inString1)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs) nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mURL(nsnull)
{ {
if (!inString) if (!inString)
return; return;
@@ -319,7 +456,6 @@ nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs) nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mURL(nsnull)
{ {
const nsAutoCString aString(inString); const nsAutoCString aString(inString);
const char* aCString = (const char*) aString; const char* aCString = (const char*) aString;
@@ -335,7 +471,7 @@ nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsFileURL& inOther) nsFileURL::nsFileURL(const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mURL(nsFileSpecHelpers::StringDup(inOther.mURL)) : mURL(inOther.mURL)
#ifdef XP_MAC #ifdef XP_MAC
, mFileSpec(inOther.GetFileSpec()) , mFileSpec(inOther.GetFileSpec())
#endif #endif
@@ -346,7 +482,6 @@ nsFileURL::nsFileURL(const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsFilePath& inOther) nsFileURL::nsFileURL(const nsFilePath& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mURL(nsnull)
{ {
*this = inOther; *this = inOther;
} // nsFileURL::nsFileURL } // nsFileURL::nsFileURL
@@ -355,7 +490,6 @@ nsFileURL::nsFileURL(const nsFilePath& inOther)
#ifndef XP_MAC #ifndef XP_MAC
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsFileSpec& inOther) nsFileURL::nsFileURL(const nsFileSpec& inOther)
: mURL(nsnull)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
*this = inOther; *this = inOther;
@@ -366,7 +500,6 @@ nsFileURL::nsFileURL(const nsFileSpec& inOther)
nsFileURL::~nsFileURL() nsFileURL::~nsFileURL()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
delete [] mURL;
} }
#ifndef XP_MAC #ifndef XP_MAC
@@ -374,16 +507,37 @@ nsFileURL::~nsFileURL()
void nsFileURL::operator = (const char* inString) void nsFileURL::operator = (const char* inString)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mURL, inString); mURL = inString;
NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!"); NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!");
} // nsFileURL::operator = } // nsFileURL::operator =
#endif #endif
//----------------------------------------------------------------------------------------
void nsFileURL::operator +=(const char* inRelativeUnixPath)
//----------------------------------------------------------------------------------------
{
char* escapedPath = nsEscape(inRelativeUnixPath, url_Path);
mURL += escapedPath;
delete [] escapedPath;
#ifdef XP_MAC
mFileSpec += inRelativeUnixPath;
#endif
} // nsFileURL::operator +=
//----------------------------------------------------------------------------------------
nsFileURL nsFileURL::operator +(const char* inRelativeUnixPath) const
//----------------------------------------------------------------------------------------
{
nsFileURL result(*this);
result += inRelativeUnixPath;
return result;
} // nsFileURL::operator +
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileURL::operator = (const nsFileURL& inOther) void nsFileURL::operator = (const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
mURL = nsFileSpecHelpers::StringAssign(mURL, inOther.mURL); mURL = inOther.mURL;
#ifdef XP_MAC #ifdef XP_MAC
mFileSpec = inOther.GetFileSpec(); mFileSpec = inOther.GetFileSpec();
#endif #endif
@@ -394,7 +548,7 @@ void nsFileURL::operator = (const nsFileURL& inOther)
void nsFileURL::operator = (const nsFilePath& inOther) void nsFileURL::operator = (const nsFilePath& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
delete [] mURL; mURL = kFileURLPrefix;
char* original = (char*)(const char*)inOther; // we shall modify, but restore. char* original = (char*)(const char*)inOther; // we shall modify, but restore.
#ifdef XP_PC #ifdef XP_PC
// because we don't want to escape the '|' character, change it to a letter. // because we don't want to escape the '|' character, change it to a letter.
@@ -407,7 +561,7 @@ void nsFileURL::operator = (const nsFilePath& inOther)
char* escapedPath = nsEscape(original, url_Path); char* escapedPath = nsEscape(original, url_Path);
#endif #endif
if (escapedPath) if (escapedPath)
mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, escapedPath); mURL += escapedPath;
delete [] escapedPath; delete [] escapedPath;
} // nsFileURL::operator = } // nsFileURL::operator =
#endif #endif
@@ -418,6 +572,8 @@ void nsFileURL::operator = (const nsFileSpec& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
*this = nsFilePath(inOther); *this = nsFilePath(inOther);
if (mURL[mURL.Length() - 1] != '/' && inOther.IsDirectory())
mURL += "/";
} // nsFileURL::operator = } // nsFileURL::operator =
#endif #endif
@@ -433,7 +589,7 @@ nsOutputStream& operator << (nsOutputStream& s, const nsFileURL& url)
//======================================================================================== //========================================================================================
nsFilePath::nsFilePath(const nsFilePath& inPath) nsFilePath::nsFilePath(const nsFilePath& inPath)
: mPath(nsFileSpecHelpers::StringDup(inPath.mPath)) : mPath(inPath.mPath)
#ifdef XP_MAC #ifdef XP_MAC
, mFileSpec(inPath.mFileSpec) , mFileSpec(inPath.mFileSpec)
#endif #endif
@@ -444,7 +600,7 @@ nsFilePath::nsFilePath(const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs) nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsFileSpecHelpers::StringDup(inString)) : mPath(inString)
{ {
NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path"); NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path");
@@ -464,7 +620,7 @@ nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const nsString& inString, PRBool inCreateDirs) nsFilePath::nsFilePath(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(inString.ToNewCString()) : mPath(inString)
{ {
NS_ASSERTION(strstr(mPath, kFileURLPrefix) != mPath, "URL passed as path"); NS_ASSERTION(strstr(mPath, kFileURLPrefix) != mPath, "URL passed as path");
@@ -484,8 +640,9 @@ nsFilePath::nsFilePath(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const nsFileURL& inOther) nsFilePath::nsFilePath(const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsFileSpecHelpers::StringDup(inOther.mURL + kFileURLPrefixLength))
{ {
mPath = (const char*)inOther.mURL + kFileURLPrefixLength;
mPath.Unescape();
} }
#endif #endif
@@ -493,7 +650,7 @@ nsFilePath::nsFilePath(const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const nsFileSpec& inOther) nsFilePath::nsFilePath(const nsFileSpec& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsFileSpecHelpers::StringDup(inOther.mPath)) : mPath(inOther.mPath)
{ {
} }
#endif // XP_UNIX #endif // XP_UNIX
@@ -502,7 +659,6 @@ nsFilePath::nsFilePath(const nsFileSpec& inOther)
nsFilePath::~nsFilePath() nsFilePath::~nsFilePath()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
delete [] mPath;
} }
#ifdef XP_UNIX #ifdef XP_UNIX
@@ -510,7 +666,7 @@ nsFilePath::~nsFilePath()
void nsFilePath::operator = (const nsFileSpec& inOther) void nsFilePath::operator = (const nsFileSpec& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
mPath = nsFileSpecHelpers::StringAssign(mPath, inOther.mPath); mPath = inOther.mPath;
} }
#endif // XP_UNIX #endif // XP_UNIX
@@ -521,9 +677,9 @@ void nsFilePath::operator = (const char* inString)
NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path"); NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path");
#ifdef XP_MAC #ifdef XP_MAC
mFileSpec = inString; mFileSpec = inString;
nsFileSpecHelpers::StringAssign(mPath, (const char*)nsFilePath(mFileSpec)); mPath = (const char*)nsFilePath(mFileSpec);
#else #else
nsFileSpecHelpers::StringAssign(mPath, inString); mPath = inString;
#ifdef XP_PC #ifdef XP_PC
nsFileSpecHelpers::UnixToNative(mPath); nsFileSpecHelpers::UnixToNative(mPath);
#endif #endif
@@ -540,7 +696,7 @@ void nsFilePath::operator = (const char* inString)
void nsFilePath::operator = (const nsFileURL& inOther) void nsFilePath::operator = (const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, (const char*)nsFilePath(inOther)); mPath = (const char*)nsFilePath(inOther);
} }
#endif #endif
@@ -548,12 +704,33 @@ void nsFilePath::operator = (const nsFileURL& inOther)
void nsFilePath::operator = (const nsFilePath& inOther) void nsFilePath::operator = (const nsFilePath& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, inOther.mPath); mPath = inOther.mPath;
#ifdef XP_MAC #ifdef XP_MAC
mFileSpec = inOther.GetFileSpec(); mFileSpec = inOther.GetFileSpec();
#endif #endif
} }
//----------------------------------------------------------------------------------------
void nsFilePath::operator +=(const char* inRelativeUnixPath)
//----------------------------------------------------------------------------------------
{
char* escapedPath = nsEscape(inRelativeUnixPath, url_Path);
mPath += escapedPath;
delete [] escapedPath;
#ifdef XP_MAC
mFileSpec += inRelativeUnixPath;
#endif
} // nsFilePath::operator +=
//----------------------------------------------------------------------------------------
nsFilePath nsFilePath::operator +(const char* inRelativeUnixPath) const
//----------------------------------------------------------------------------------------
{
nsFilePath result(*this);
result += inRelativeUnixPath;
return result;
} // nsFilePath::operator +
//======================================================================================== //========================================================================================
// nsFileSpec implementation // nsFileSpec implementation
//======================================================================================== //========================================================================================
@@ -562,8 +739,7 @@ void nsFilePath::operator = (const nsFilePath& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec() nsFileSpec::nsFileSpec()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull) : mError(NS_OK)
, mError(NS_OK)
{ {
} }
#endif #endif
@@ -571,7 +747,6 @@ nsFileSpec::nsFileSpec()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsPersistentFileDescriptor& inDescriptor) nsFileSpec::nsFileSpec(const nsPersistentFileDescriptor& inDescriptor)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
*this = inDescriptor; *this = inDescriptor;
} }
@@ -579,7 +754,6 @@ nsFileSpec::nsFileSpec(const nsPersistentFileDescriptor& inDescriptor)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsFileURL& inURL) nsFileSpec::nsFileSpec(const nsFileURL& inURL)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
*this = nsFilePath(inURL); // convert to unix path first *this = nsFilePath(inURL); // convert to unix path first
} }
@@ -609,7 +783,7 @@ void nsFileSpec::MakeUnique()
char* suffix = ""; char* suffix = "";
if (lastDot) if (lastDot)
{ {
suffix = nsFileSpecHelpers::StringDup(lastDot); // include '.' suffix = PL_strdup(lastDot); // include '.'
*lastDot = '\0'; // strip suffix and dot. *lastDot = '\0'; // strip suffix and dot.
} }
const int kMaxRootLength const int kMaxRootLength
@@ -640,7 +814,7 @@ void nsFileSpec::operator = (const nsPersistentFileDescriptor& inDescriptor)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
void* data; nsSimpleCharString data;
PRInt32 dataSize; PRInt32 dataSize;
inDescriptor.GetData(data, dataSize); inDescriptor.GetData(data, dataSize);
@@ -656,9 +830,9 @@ void nsFileSpec::operator = (const nsPersistentFileDescriptor& inDescriptor)
Boolean changed; Boolean changed;
mError = NS_FILE_RESULT(::ResolveAlias(nsnull, aliasH, &mSpec, &changed)); mError = NS_FILE_RESULT(::ResolveAlias(nsnull, aliasH, &mSpec, &changed));
DisposeHandle((Handle) aliasH); DisposeHandle((Handle) aliasH);
delete [] mPath; mPath.SetToEmpty();
#else #else
nsFileSpecHelpers::StringAssign(mPath, (char*)data); mPath = data;
mError = NS_OK; mError = NS_OK;
#endif #endif
} }
@@ -671,7 +845,7 @@ void nsFileSpec::operator = (const nsPersistentFileDescriptor& inDescriptor)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsFilePath& inPath) nsFileSpec::nsFileSpec(const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsFileSpecHelpers::StringDup((const char*)inPath)) : mPath((const char*)inPath)
, mError(NS_OK) , mError(NS_OK)
{ {
} }
@@ -682,7 +856,7 @@ nsFileSpec::nsFileSpec(const nsFilePath& inPath)
void nsFileSpec::operator = (const nsFilePath& inPath) void nsFileSpec::operator = (const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, (const char*)inPath); mPath = (const char*)inPath;
mError = NS_OK; mError = NS_OK;
} }
#endif //XP_UNIX #endif //XP_UNIX
@@ -691,7 +865,7 @@ void nsFileSpec::operator = (const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsFileSpec& inSpec) nsFileSpec::nsFileSpec(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsFileSpecHelpers::StringDup(inSpec.mPath)) : mPath(inSpec.mPath)
, mError(NS_OK) , mError(NS_OK)
{ {
} }
@@ -701,7 +875,7 @@ nsFileSpec::nsFileSpec(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs) nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsFileSpecHelpers::StringDup(inString)) : mPath(inString)
, mError(NS_OK) , mError(NS_OK)
{ {
// Make canonical and absolute. // Make canonical and absolute.
@@ -713,7 +887,7 @@ nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs) nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(inString.ToNewCString()) : mPath(inString)
, mError(NS_OK) , mError(NS_OK)
{ {
// Make canonical and absolute. // Make canonical and absolute.
@@ -725,7 +899,6 @@ nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs)
nsFileSpec::~nsFileSpec() nsFileSpec::~nsFileSpec()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
delete [] mPath;
} }
#if defined(XP_UNIX) || defined(XP_PC) #if defined(XP_UNIX) || defined(XP_PC)
@@ -733,7 +906,7 @@ nsFileSpec::~nsFileSpec()
void nsFileSpec::operator = (const nsFileSpec& inSpec) void nsFileSpec::operator = (const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
mPath = nsFileSpecHelpers::StringAssign(mPath, inSpec.mPath); mPath = inSpec.mPath;
mError = inSpec.Error(); mError = inSpec.Error();
} }
#endif //XP_UNIX #endif //XP_UNIX
@@ -744,7 +917,7 @@ void nsFileSpec::operator = (const nsFileSpec& inSpec)
void nsFileSpec::operator = (const char* inString) void nsFileSpec::operator = (const char* inString)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
mPath = nsFileSpecHelpers::StringAssign(mPath, inString); mPath = inString;
// Make canonical and absolute. // Make canonical and absolute.
nsFileSpecHelpers::Canonify(mPath, PR_FALSE /* XXX? */); nsFileSpecHelpers::Canonify(mPath, PR_FALSE /* XXX? */);
mError = NS_OK; mError = NS_OK;
@@ -788,8 +961,8 @@ PRBool nsFileSpec::operator == (const nsFileSpec& inOther) const
EqualString(inOther.mSpec.name, mSpec.name, false, true)) EqualString(inOther.mSpec.name, mSpec.name, false, true))
return PR_TRUE; return PR_TRUE;
#else #else
PRBool amEmpty = !mPath || !*mPath; PRBool amEmpty = mPath.IsEmpty();
PRBool heEmpty = !inOther.mPath || !*inOther.mPath; PRBool heEmpty = inOther.mPath.IsEmpty();
if (amEmpty) // we're the same if he's empty... if (amEmpty) // we're the same if he's empty...
return heEmpty; return heEmpty;
if (heEmpty) // ('cuz I'm not...) if (heEmpty) // ('cuz I'm not...)
@@ -835,7 +1008,7 @@ const char* nsFileSpec::GetCString() const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inDesc) nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inDesc)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mDescriptorString(nsFileSpecHelpers::StringDup(inDesc.mDescriptorString)) : mDescriptorString(inDesc.mDescriptorString)
{ {
} // nsPersistentFileDescriptor::nsPersistentFileDescriptor } // nsPersistentFileDescriptor::nsPersistentFileDescriptor
@@ -843,13 +1016,12 @@ nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsPersistentFileDes
void nsPersistentFileDescriptor::operator = (const nsPersistentFileDescriptor& inDesc) void nsPersistentFileDescriptor::operator = (const nsPersistentFileDescriptor& inDesc)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mDescriptorString, inDesc.mDescriptorString); mDescriptorString = inDesc.mDescriptorString;
} // nsPersistentFileDescriptor::operator = } // nsPersistentFileDescriptor::operator =
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsFileSpec& inSpec) nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mDescriptorString(nsnull)
{ {
*this = inSpec; *this = inSpec;
} // nsPersistentFileDescriptor::nsPersistentFileDescriptor } // nsPersistentFileDescriptor::nsPersistentFileDescriptor
@@ -871,10 +1043,10 @@ void nsPersistentFileDescriptor::operator = (const nsFileSpec& inSpec)
char* buf = PL_Base64Encode((const char*)*aliasH, bytes, nsnull); char* buf = PL_Base64Encode((const char*)*aliasH, bytes, nsnull);
DisposeHandle((Handle) aliasH); DisposeHandle((Handle) aliasH);
nsFileSpecHelpers::StringAssign(mDescriptorString, buf); mDescriptorString = buf;
PR_Free(buf); PR_Free(buf);
#else #else
nsFileSpecHelpers::StringAssign(mDescriptorString, inSpec.GetCString()); mDescriptorString = inSpec.GetCString();
#endif // XP_MAC #endif // XP_MAC
} // nsPersistentFileDescriptor::operator = } // nsPersistentFileDescriptor::operator =
@@ -882,27 +1054,21 @@ void nsPersistentFileDescriptor::operator = (const nsFileSpec& inSpec)
nsPersistentFileDescriptor::~nsPersistentFileDescriptor() nsPersistentFileDescriptor::~nsPersistentFileDescriptor()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
delete [] mDescriptorString;
} // nsPersistentFileDescriptor::~nsPersistentFileDescriptor } // nsPersistentFileDescriptor::~nsPersistentFileDescriptor
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsPersistentFileDescriptor::GetData(void*& outData, PRInt32& outSize) const void nsPersistentFileDescriptor::GetData(nsSimpleCharString& outData, PRInt32& outSize) const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
outSize = PL_strlen(mDescriptorString); outSize = mDescriptorString.Length();
outData = mDescriptorString; outData = mDescriptorString;
} }
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsPersistentFileDescriptor::SetData(const void* inData, PRInt32 inSize) void nsPersistentFileDescriptor::SetData(const nsSimpleCharString& inData, PRInt32 inSize)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
delete [] mDescriptorString; mDescriptorString.CopyFrom((const char*)inData, inSize);
mDescriptorString = new char[1 + inSize];
if (!mDescriptorString)
return;
memcpy(mDescriptorString, inData, inSize);
mDescriptorString[inSize] = '\0';
} }
#define MAX_PERSISTENT_DATA_SIZE 1000 #define MAX_PERSISTENT_DATA_SIZE 1000
@@ -937,7 +1103,7 @@ nsInputStream& operator >> (nsInputStream& s, nsPersistentFileDescriptor& d)
if (bytesRead != 8) if (bytesRead != 8)
return s; return s;
bigBuffer[8] = '\0'; bigBuffer[8] = '\0';
sscanf(bigBuffer, "%lx", (PRUint32*)&bytesRead); sscanf(bigBuffer, "%x", (PRUint32*)&bytesRead);
if (bytesRead > MAX_PERSISTENT_DATA_SIZE) if (bytesRead > MAX_PERSISTENT_DATA_SIZE)
return s; // preposterous. return s; // preposterous.
// Now we know how many bytes to read, do it. // Now we know how many bytes to read, do it.
@@ -953,13 +1119,13 @@ nsOutputStream& operator << (nsOutputStream& s, const nsPersistentFileDescriptor
{ {
char littleBuf[9]; char littleBuf[9];
PRInt32 dataSize; PRInt32 dataSize;
void* data; nsSimpleCharString data;
d.GetData(data, dataSize); d.GetData(data, dataSize);
// First write (in hex) the length of the data to follow. Exactly 8 bytes // First write (in hex) the length of the data to follow. Exactly 8 bytes
sprintf(littleBuf, "%0.8x", dataSize); sprintf(littleBuf, "%0.8x", dataSize);
s << littleBuf; s << littleBuf;
// Now write the data itself // Now write the data itself
s << d.mDescriptorString; s << (const char*)data;
return s; return s;
} }

View File

@@ -206,6 +206,74 @@ protected:
const char* mCString; const char* mCString;
}; // class nsAutoCString }; // class nsAutoCString
//========================================================================================
class NS_BASE nsSimpleCharString
// An envelope for char*: reference counted. Used internally by all the nsFileSpec
// classes below.
//========================================================================================
{
public:
nsSimpleCharString();
nsSimpleCharString(const char*);
nsSimpleCharString(const nsString&);
nsSimpleCharString(const nsSimpleCharString&);
nsSimpleCharString(const char* inData, PRUint32 inLength);
~nsSimpleCharString();
void operator = (const char*);
void operator = (const nsString&);
void operator = (const nsSimpleCharString&);
operator const char*() const { return mData ? mData->mString : 0; }
operator char* ()
{
ReallocData(Length()); // requires detaching if shared...
return mData ? mData->mString : 0;
}
PRBool operator == (const char*);
PRBool operator == (const nsString&);
PRBool operator == (const nsSimpleCharString&);
void operator += (const char* inString);
nsSimpleCharString operator + (const char* inString) const;
char operator [](int i) const { return mData ? mData->mString[i] : 0; }
char& operator [](int i)
{
if (i >= (int)Length())
ReallocData(i + 1);
return mData->mString[i]; // caveat appelator
}
char& operator [](unsigned int i) { return (*this)[(int)i]; }
void Catenate(const char* inString1, const char* inString2);
void SetToEmpty();
PRBool IsEmpty() const { return Length() == 0; }
PRUint32 Length() const { return mData ? mData->mLength : 0; }
void CopyFrom(const char* inData, PRUint32 inLength);
void LeafReplace(char inSeparator, const char* inLeafName);
char* GetLeaf(char inSeparator) const; // use PR_Free()
void Unescape();
protected:
void AddRefData();
void ReleaseData();
void ReallocData(PRUint32 inLength);
// DATA
protected:
struct Data {
int mRefCount;
PRUint32 mLength;
char mString[1];
};
Data* mData;
}; // class nsSimpleCharString
//======================================================================================== //========================================================================================
class NS_BASE nsFileSpec class NS_BASE nsFileSpec
// This is whatever each platform really prefers to describe files as. Declared first // This is whatever each platform really prefers to describe files as. Declared first
@@ -255,7 +323,7 @@ class NS_BASE nsFileSpec
long parID, long parID,
ConstStr255Param name); ConstStr255Param name);
nsFileSpec(const FSSpec& inSpec) nsFileSpec(const FSSpec& inSpec)
: mSpec(inSpec), mError(NS_OK), mPath(nsnull) {} : mSpec(inSpec), mError(NS_OK) {}
void operator = (const FSSpec& inSpec) void operator = (const FSSpec& inSpec)
{ mSpec = inSpec; mError = NS_OK; } { mSpec = inSpec; mError = NS_OK; }
@@ -284,7 +352,7 @@ class NS_BASE nsFileSpec
nsresult Error() const nsresult Error() const
{ {
#ifndef XP_MAC #ifndef XP_MAC
if (!mPath && NS_SUCCEEDED(mError)) if (mPath.IsEmpty() && NS_SUCCEEDED(mError))
((nsFileSpec*)this)->mError = NS_FILE_FAILURE; ((nsFileSpec*)this)->mError = NS_FILE_FAILURE;
#endif #endif
return mError; return mError;
@@ -413,7 +481,7 @@ class NS_BASE nsFileSpec
#ifdef XP_MAC #ifdef XP_MAC
FSSpec mSpec; FSSpec mSpec;
#endif #endif
char* mPath; nsSimpleCharString mPath;
nsresult mError; nsresult mError;
}; // class nsFileSpec }; // class nsFileSpec
@@ -450,8 +518,10 @@ class NS_BASE nsFileURL
void operator = (const nsFilePath& inOther); void operator = (const nsFilePath& inOther);
void operator = (const nsFileSpec& inOther); void operator = (const nsFileSpec& inOther);
operator const char* () const { return mURL; } // deprecated. void operator +=(const char* inRelativeUnixPath);
const char* GetAsString() const { return mURL; } nsFileURL operator +(const char* inRelativeUnixPath) const;
operator const char* () const { return (const char*)mURL; } // deprecated.
const char* GetAsString() const { return (const char*)mURL; }
friend NS_BASE nsOutputStream& operator << ( friend NS_BASE nsOutputStream& operator << (
nsOutputStream& s, const nsFileURL& spec); nsOutputStream& s, const nsFileURL& spec);
@@ -460,12 +530,11 @@ class NS_BASE nsFileURL
// Accessor to allow quick assignment to a mFileSpec // Accessor to allow quick assignment to a mFileSpec
const nsFileSpec& GetFileSpec() const { return mFileSpec; } const nsFileSpec& GetFileSpec() const { return mFileSpec; }
#endif #endif
private:
// Should not be defined (only nsFilePath is to be treated as strings.
operator char* ();
protected: protected:
friend class nsFilePath; // to allow construction of nsFilePath friend class nsFilePath; // to allow construction of nsFilePath
char* mURL; nsSimpleCharString mURL;
#ifdef XP_MAC #ifdef XP_MAC
// Since the path on the macintosh does not uniquely specify a file (volumes // Since the path on the macintosh does not uniquely specify a file (volumes
// can have the same name), stash the secret nsFileSpec, too. // can have the same name), stash the secret nsFileSpec, too.
@@ -493,10 +562,6 @@ class NS_BASE nsFilePath
// This is the only automatic conversion to const char* // This is the only automatic conversion to const char*
// that is provided, and it allows the // that is provided, and it allows the
// path to be "passed" to NSPR file routines. // path to be "passed" to NSPR file routines.
operator char* () { return mPath; }
// 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); void operator = (const nsFilePath& inPath);
void operator = (const char* inString); void operator = (const char* inString);
@@ -508,6 +573,9 @@ class NS_BASE nsFilePath
void operator = (const nsFileURL& inURL); void operator = (const nsFileURL& inURL);
void operator = (const nsFileSpec& inOther); void operator = (const nsFileSpec& inOther);
void operator +=(const char* inRelativeUnixPath);
nsFilePath operator +(const char* inRelativeUnixPath) const;
#ifdef XP_MAC #ifdef XP_MAC
public: public:
// Accessor to allow quick assignment to a mFileSpec // Accessor to allow quick assignment to a mFileSpec
@@ -516,7 +584,7 @@ class NS_BASE nsFilePath
private: private:
char* mPath; nsSimpleCharString mPath;
#ifdef XP_MAC #ifdef XP_MAC
// Since the path on the macintosh does not uniquely specify a file (volumes // Since the path on the macintosh does not uniquely specify a file (volumes
// can have the same name), stash the secret nsFileSpec, too. // can have the same name), stash the secret nsFileSpec, too.
@@ -533,7 +601,7 @@ class NS_BASE nsPersistentFileDescriptor
//======================================================================================== //========================================================================================
{ {
public: public:
nsPersistentFileDescriptor() : mDescriptorString(nsnull) {} nsPersistentFileDescriptor() {}
// For use prior to reading in from a stream // For use prior to reading in from a stream
nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inPath); nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inPath);
virtual ~nsPersistentFileDescriptor(); virtual ~nsPersistentFileDescriptor();
@@ -553,14 +621,13 @@ class NS_BASE nsPersistentFileDescriptor
friend class nsFileSpec; friend class nsFileSpec;
private: private:
// Here are the ways to get data in and out of a file.
void GetData(void*& outData, PRInt32& outSize) const; void GetData(nsSimpleCharString& outData, PRInt32& outSize) const;
// DON'T FREE the returned data! void SetData(const nsSimpleCharString& inData, PRInt32 inSize);
void SetData(const void* inData, PRInt32 inSize);
protected: protected:
char* mDescriptorString; nsSimpleCharString mDescriptorString;
}; // class nsPersistentFileDescriptor }; // class nsPersistentFileDescriptor
@@ -569,16 +636,16 @@ class NS_BASE nsDirectoryIterator
// Example: // Example:
// //
// nsFileSpec parentDir(...); // directory over whose children we shall iterate // nsFileSpec parentDir(...); // directory over whose children we shall iterate
// for (nsDirectoryIterator i(parentDir); i; i++) // for (nsDirectoryIterator i(parentDir); i.Exists(); i++)
// { // {
// // do something with (const nsFileSpec&)i // // do something with i.Spec()
// } // }
// //
// or: // or:
// //
// for (nsDirectoryIterator i(parentDir, PR_FALSE); i; i--) // for (nsDirectoryIterator i(parentDir, PR_FALSE); i.Exists(); i--)
// { // {
// // do something with (const nsFileSpec&)i // // do something with i.Spec()
// } // }
// //
// Currently, the only platform on which backwards iteration actually goes backwards // Currently, the only platform on which backwards iteration actually goes backwards
@@ -599,6 +666,9 @@ class NS_BASE nsDirectoryIterator
nsDirectoryIterator& operator --(); // moves to the previous item, if any. nsDirectoryIterator& operator --(); // moves to the previous item, if any.
nsDirectoryIterator& operator --(int) { return --(*this); } // post-decrement. nsDirectoryIterator& operator --(int) { return --(*this); } // post-decrement.
operator nsFileSpec&() { return mCurrent; } operator nsFileSpec&() { return mCurrent; }
nsFileSpec& Spec() { return mCurrent; }
private: private:
nsFileSpec mCurrent; nsFileSpec mCurrent;
PRBool mExists; PRBool mExists;

View File

@@ -518,7 +518,6 @@ Clean:
nsFileSpec::nsFileSpec() nsFileSpec::nsFileSpec()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mError(NS_OK) : mError(NS_OK)
, mPath(nsnull)
{ {
mSpec.name[0] = '\0'; mSpec.name[0] = '\0';
} }
@@ -527,7 +526,6 @@ nsFileSpec::nsFileSpec()
nsFileSpec::nsFileSpec(const nsFileSpec& inSpec) nsFileSpec::nsFileSpec(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mSpec(inSpec.mSpec) : mSpec(inSpec.mSpec)
, mPath(nsnull)
, mError(inSpec.Error()) , mError(inSpec.Error())
{ {
} }
@@ -535,7 +533,6 @@ nsFileSpec::nsFileSpec(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs) nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
mSpec.vRefNum = 0; mSpec.vRefNum = 0;
mSpec.parID = 0; mSpec.parID = 0;
@@ -551,7 +548,6 @@ nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs) nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
mSpec.vRefNum = 0; mSpec.vRefNum = 0;
mSpec.parID = 0; mSpec.parID = 0;
@@ -567,7 +563,6 @@ nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(short vRefNum, long parID, ConstStr255Param name) nsFileSpec::nsFileSpec(short vRefNum, long parID, ConstStr255Param name)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
mError = NS_FILE_RESULT(::FSMakeFSSpec(vRefNum, parID, name, &mSpec)); mError = NS_FILE_RESULT(::FSMakeFSSpec(vRefNum, parID, name, &mSpec));
if (mError == NS_FILE_RESULT(fnfErr)) if (mError == NS_FILE_RESULT(fnfErr))
@@ -577,7 +572,6 @@ nsFileSpec::nsFileSpec(short vRefNum, long parID, ConstStr255Param name)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsFilePath& inPath) nsFileSpec::nsFileSpec(const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
*this = inPath.GetFileSpec(); *this = inPath.GetFileSpec();
} }
@@ -586,7 +580,7 @@ nsFileSpec::nsFileSpec(const nsFilePath& inPath)
void nsFileSpec::operator = (const char* inString) void nsFileSpec::operator = (const char* inString)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, nsnull); mPath.SetToEmpty();
mSpec.vRefNum = 0; mSpec.vRefNum = 0;
mSpec.parID = 0; mSpec.parID = 0;
@@ -601,8 +595,10 @@ void nsFileSpec::operator = (const char* inString)
void nsFileSpec::operator = (const nsFileSpec& inSpec) void nsFileSpec::operator = (const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, nsnull); mPath.SetToEmpty();
mSpec = inSpec.mSpec; mSpec.vRefNum = inSpec.mSpec.vRefNum;
mSpec.parID = inSpec.mSpec.parID;
memcpy(mSpec.name, inSpec.mSpec.name, inSpec.mSpec.name[0] + 1);
mError = inSpec.Error(); mError = inSpec.Error();
} // nsFileSpec::operator = } // nsFileSpec::operator =
@@ -662,7 +658,7 @@ void nsFileSpec::SetLeafName(const char* inLeafName)
// In leaf name can actually be a partial path... // In leaf name can actually be a partial path...
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, nsnull); mPath.SetToEmpty();
// what about long relative paths? Hmm? We don't have a routine for this anywhere. // what about long relative paths? Hmm? We don't have a routine for this anywhere.
Str255 partialPath; Str255 partialPath;
@@ -681,14 +677,14 @@ char* nsFileSpec::GetLeafName() const
char leaf[64]; char leaf[64];
memcpy(leaf, &mSpec.name[1], mSpec.name[0]); memcpy(leaf, &mSpec.name[1], mSpec.name[0]);
leaf[mSpec.name[0]] = '\0'; leaf[mSpec.name[0]] = '\0';
return nsFileSpecHelpers::StringDup(leaf); return PL_strdup(leaf);
} // nsFileSpec::GetLeafName } // nsFileSpec::GetLeafName
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileSpec::MakeAliasSafe() void nsFileSpec::MakeAliasSafe()
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, nsnull); mPath.SetToEmpty();
mError = NS_FILE_RESULT(MacFileHelpers::MakeAliasSafe(mSpec)); mError = NS_FILE_RESULT(MacFileHelpers::MakeAliasSafe(mSpec));
} // nsFileSpec::MakeAliasSafe } // nsFileSpec::MakeAliasSafe
@@ -696,7 +692,7 @@ void nsFileSpec::MakeAliasSafe()
void nsFileSpec::MakeUnique(ConstStr255Param inSuggestedLeafName) void nsFileSpec::MakeUnique(ConstStr255Param inSuggestedLeafName)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, nsnull); mPath.SetToEmpty();
if (inSuggestedLeafName[0] > 0) if (inSuggestedLeafName[0] > 0)
MacFileHelpers::PLstrcpy(mSpec.name, inSuggestedLeafName); MacFileHelpers::PLstrcpy(mSpec.name, inSuggestedLeafName);
@@ -895,11 +891,11 @@ const char* nsFileSpec::GetCString() const
// cached and freed by the nsFileSpec destructor, so do not delete (or free) it. // cached and freed by the nsFileSpec destructor, so do not delete (or free) it.
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!mPath) if (mPath.IsEmpty())
{ {
const_cast<nsFileSpec*>(this)->mPath const_cast<nsFileSpec*>(this)->mPath
= MacFileHelpers::PathNameFromFSSpec(mSpec, true); = MacFileHelpers::PathNameFromFSSpec(mSpec, true);
if (!mPath) if (mPath.IsEmpty())
const_cast<nsFileSpec*>(this)->mError = NS_ERROR_OUT_OF_MEMORY; const_cast<nsFileSpec*>(this)->mError = NS_ERROR_OUT_OF_MEMORY;
} }
return mPath; return mPath;
@@ -912,29 +908,30 @@ const char* nsFileSpec::GetCString() const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs) nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull) : mFileSpec(inString, inCreateDirs)
, mFileSpec(inString, inCreateDirs)
{ {
// Make canonical and absolute. // Make canonical and absolute.
char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true ); char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true );
mPath = MacFileHelpers::EncodeMacPath(path, true, false); path = MacFileHelpers::EncodeMacPath(path, true, false);
mPath = path;
delete [] path;
} }
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const nsString& inString, PRBool inCreateDirs) nsFilePath::nsFilePath(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull) : mFileSpec(nsAutoCString(inString), inCreateDirs)
, mFileSpec(nsAutoCString(inString), inCreateDirs)
{ {
// Make canonical and absolute. // Make canonical and absolute.
char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true ); char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true );
mPath = MacFileHelpers::EncodeMacPath(path, true, false); path = MacFileHelpers::EncodeMacPath(path, true, false);
mPath = path;
delete [] path;
} }
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const nsFileSpec& inSpec) nsFilePath::nsFilePath(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
*this = inSpec; *this = inSpec;
} }
@@ -942,18 +939,18 @@ nsFilePath::nsFilePath(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const nsFileURL& inOther) nsFilePath::nsFilePath(const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(nsnull)
{ {
*this = inOther.GetFileSpec(); *this = inOther;
} }
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFilePath::operator = (const nsFileSpec& inSpec) void nsFilePath::operator = (const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
char * path = MacFileHelpers::PathNameFromFSSpec( inSpec.mSpec, true ); char * path = MacFileHelpers::PathNameFromFSSpec(inSpec, true);
delete [] mPath; path = MacFileHelpers::EncodeMacPath(path, true, false);
mPath = MacFileHelpers::EncodeMacPath(path, true, false); mPath = path;
delete [] path;
mFileSpec = inSpec; mFileSpec = inSpec;
} // nsFilePath::operator = } // nsFilePath::operator =
@@ -961,7 +958,11 @@ void nsFilePath::operator = (const nsFileSpec& inSpec)
void nsFilePath::operator = (const nsFileURL& inOther) void nsFilePath::operator = (const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
*this = inOther.GetFileSpec(); char * path = MacFileHelpers::PathNameFromFSSpec(inOther.mFileSpec, true);
path = MacFileHelpers::EncodeMacPath(path, true, false);
mPath = path;
delete [] path;
mFileSpec = inOther.GetFileSpec();
} }
//======================================================================================== //========================================================================================
@@ -971,11 +972,11 @@ void nsFilePath::operator = (const nsFileURL& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs) nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mURL(nsFileSpecHelpers::StringDup(inString)) : mURL(inString)
{ {
NS_ASSERTION(strstr(mURL, kFileURLPrefix) == mURL, "Not a URL!"); NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!");
mFileSpec.mError = NS_FILE_RESULT(MacFileHelpers::FSSpecFromUnixPath( mFileSpec.mError = NS_FILE_RESULT(MacFileHelpers::FSSpecFromUnixPath(
mURL + kFileURLPrefixLength, inString + kFileURLPrefixLength,
mFileSpec.mSpec, mFileSpec.mSpec,
true, // need to decode true, // need to decode
false, // don't resolve alias false, // don't resolve alias
@@ -988,11 +989,14 @@ nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs) nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mURL(nsFileSpecHelpers::StringDup(nsAutoCString(inString))) : mURL(nsnull)
{ {
NS_ASSERTION(strstr(mURL, kFileURLPrefix) == mURL, "Not a URL!"); nsAutoCString autostring(inString);
const char* cstring = (const char*)autostring;
mURL = cstring;
NS_ASSERTION(strstr(cstring, kFileURLPrefix) == cstring, "Not a URL!");
mFileSpec.mError = NS_FILE_RESULT(MacFileHelpers::FSSpecFromUnixPath( mFileSpec.mError = NS_FILE_RESULT(MacFileHelpers::FSSpecFromUnixPath(
mURL + kFileURLPrefixLength, cstring + kFileURLPrefixLength,
mFileSpec.mSpec, mFileSpec.mSpec,
true, // need to decode true, // need to decode
false, // don't resolve alias false, // don't resolve alias
@@ -1005,14 +1009,12 @@ nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsFilePath& inOther) nsFileURL::nsFileURL(const nsFilePath& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mURL(nsnull)
{ {
*this = inOther.GetFileSpec(); *this = inOther.GetFileSpec();
} // nsFileURL::nsFileURL } // nsFileURL::nsFileURL
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsFileSpec& inOther) nsFileURL::nsFileURL(const nsFileSpec& inOther)
: mURL(nsnull)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
*this = inOther; *this = inOther;
@@ -1030,25 +1032,21 @@ void nsFileURL::operator = (const nsFileSpec& inOther)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
mFileSpec = inOther; mFileSpec = inOther;
delete [] mURL;
char* path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true ); char* path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true );
char* encodedPath = MacFileHelpers::EncodeMacPath(path, true, true); char* encodedPath = MacFileHelpers::EncodeMacPath(path, true, true);
char* encodedURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, encodedPath); nsSimpleCharString encodedURL(kFileURLPrefix);
encodedURL += encodedPath;
delete [] encodedPath; delete [] encodedPath;
if (encodedURL[strlen(encodedURL) - 1] != '/' && inOther.IsDirectory())
{
mURL = nsFileSpecHelpers::AllocCat(encodedURL, "/");
delete [] encodedURL;
}
else
mURL = encodedURL; mURL = encodedURL;
if (encodedURL[encodedURL.Length() - 1] != '/' && inOther.IsDirectory())
mURL += "/";
} // nsFileURL::operator = } // nsFileURL::operator =
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileURL::operator = (const char* inString) void nsFileURL::operator = (const char* inString)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mURL, inString); mURL = inString;
NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!"); NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!");
mFileSpec.mError = NS_FILE_RESULT(MacFileHelpers::FSSpecFromUnixPath( mFileSpec.mError = NS_FILE_RESULT(MacFileHelpers::FSSpecFromUnixPath(
inString + kFileURLPrefixLength, inString + kFileURLPrefixLength,

View File

@@ -61,39 +61,38 @@ extern "C" int statvfs(const char *, struct statvfs *);
#endif #endif
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileSpecHelpers::Canonify(char*& ioPath, PRBool inMakeDirs) void nsFileSpecHelpers::Canonify(nsSimpleCharString& ioPath, PRBool inMakeDirs)
// Canonify, make absolute, and check whether directories exist // Canonify, make absolute, and check whether directories exist
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!ioPath) if (ioPath.IsEmpty())
return; return;
if (inMakeDirs) if (inMakeDirs)
{ {
const mode_t mode = 0700; const mode_t mode = 0700;
nsFileSpecHelpers::MakeAllDirectories(ioPath, mode); nsFileSpecHelpers::MakeAllDirectories((const char*)ioPath, mode);
} }
char buffer[MAXPATHLEN]; char buffer[MAXPATHLEN];
errno = 0; errno = 0;
*buffer = '\0'; *buffer = '\0';
char* canonicalPath = realpath(ioPath, buffer); char* canonicalPath = realpath((const char*)ioPath, buffer);
if (!canonicalPath) if (!canonicalPath)
{ {
// Linux's realpath() is pathetically buggy. If the reason for the nil // Linux's realpath() is pathetically buggy. If the reason for the nil
// result is just that the leaf does not exist, strip the leaf off, // result is just that the leaf does not exist, strip the leaf off,
// process that, and then add the leaf back. // process that, and then add the leaf back.
char* allButLeaf = nsFileSpecHelpers::StringDup(ioPath); nsSimpleCharString allButLeaf(ioPath);
if (!allButLeaf) if (allButLeaf.IsEmpty())
return; return;
char* lastSeparator = strrchr(allButLeaf, '/'); char* lastSeparator = strrchr((char*)allButLeaf, '/');
if (lastSeparator) if (lastSeparator)
{ {
*lastSeparator = '\0'; *lastSeparator = '\0';
canonicalPath = realpath(allButLeaf, buffer); canonicalPath = realpath((const char*)allButLeaf, buffer);
strcat(buffer, "/"); strcat(buffer, "/");
// Add back the leaf // Add back the leaf
strcat(buffer, ++lastSeparator); strcat(buffer, ++lastSeparator);
} }
delete [] allButLeaf;
} }
if (!canonicalPath && *ioPath != '/' && !inMakeDirs) if (!canonicalPath && *ioPath != '/' && !inMakeDirs)
{ {
@@ -106,21 +105,21 @@ void nsFileSpecHelpers::Canonify(char*& ioPath, PRBool inMakeDirs)
} }
} }
if (canonicalPath) if (canonicalPath)
nsFileSpecHelpers::StringAssign(ioPath, canonicalPath); ioPath = canonicalPath;
} // nsFileSpecHelpers::Canonify } // nsFileSpecHelpers::Canonify
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileSpec::SetLeafName(const char* inLeafName) void nsFileSpec::SetLeafName(const char* inLeafName)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::LeafReplace(mPath, '/', inLeafName); mPath.LeafReplace('/', inLeafName);
} // nsFileSpec::SetLeafName } // nsFileSpec::SetLeafName
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
char* nsFileSpec::GetLeafName() const char* nsFileSpec::GetLeafName() const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
return nsFileSpecHelpers::GetLeaf(mPath, '/'); return mPath.GetLeaf('/');
} // nsFileSpec::GetLeafName } // nsFileSpec::GetLeafName
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
@@ -172,9 +171,9 @@ PRBool nsFileSpec::IsDirectory() const
void nsFileSpec::GetParent(nsFileSpec& outSpec) const void nsFileSpec::GetParent(nsFileSpec& outSpec) const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(outSpec.mPath, mPath); outSpec.mPath = mPath;
char* cp = strrchr(outSpec.mPath, '/'); char* cp = strrchr(outSpec.mPath, '/');
if (cp) if (cp++)
*cp = '\0'; *cp = '\0';
} // nsFileSpec::GetParent } // nsFileSpec::GetParent
@@ -182,14 +181,14 @@ void nsFileSpec::GetParent(nsFileSpec& outSpec) const
void nsFileSpec::operator += (const char* inRelativePath) void nsFileSpec::operator += (const char* inRelativePath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!inRelativePath || !mPath) if (!inRelativePath || mPath.IsEmpty())
return; return;
char endChar = mPath[strlen(mPath) - 1]; char endChar = mPath[strlen(mPath) - 1];
if (endChar == '/') if (endChar == '/')
nsFileSpecHelpers::ReallocCat(mPath, "x"); mPath += "x";
else else
nsFileSpecHelpers::ReallocCat(mPath, "/x"); mPath += "/x";
SetLeafName(inRelativePath); SetLeafName(inRelativePath);
} // nsFileSpec::operator += } // nsFileSpec::operator +=
@@ -304,16 +303,11 @@ nsresult nsFileSpec::Copy(const nsFileSpec& inParentDirectory) const
if (inParentDirectory.IsDirectory() && (! IsDirectory() ) ) if (inParentDirectory.IsDirectory() && (! IsDirectory() ) )
{ {
char *leafname = GetLeafName(); char *leafname = GetLeafName();
char* destPath = nsFileSpecHelpers::StringDup( nsSimpleCharString destPath(inParentDirectory.GetCString());
inParentDirectory.GetCString(), destPath += "/";
strlen(inParentDirectory.GetCString()) + 1 + strlen(leafname)); destPath += leafname;
strcat(destPath, "/");
strcat(destPath, leafname);
delete [] leafname; delete [] leafname;
result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), destPath)); result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), destPath));
delete [] destPath;
} }
return result; return result;
} // nsFileSpec::Copy } // nsFileSpec::Copy
@@ -325,24 +319,20 @@ nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory) const
// We can only copy into a directory, and (for now) can not copy entire directories // We can only copy into a directory, and (for now) can not copy entire directories
nsresult result = NS_FILE_FAILURE; nsresult result = NS_FILE_FAILURE;
if (inNewParentDirectory.IsDirectory() && (! IsDirectory() ) ) if (inNewParentDirectory.IsDirectory() && !IsDirectory())
{ {
char *leafname = GetLeafName(); char *leafname = GetLeafName();
char* destPath nsSimpleCharString destPath(inNewParentDirectory.GetCString());
= nsFileSpecHelpers::StringDup( destPath += "/";
inNewParentDirectory.GetCString(), destPath += leafname;
strlen(inNewParentDirectory.GetCString()) + 1 + strlen(leafname));
strcat(destPath, "/");
strcat(destPath, leafname);
delete [] leafname; delete [] leafname;
result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), destPath)); result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), (const char*)destPath));
if (result == NS_OK) if (result == NS_OK)
{ {
// cast to fix const-ness // cast to fix const-ness
((nsFileSpec*)this)->Delete(PR_FALSE); ((nsFileSpec*)this)->Delete(PR_FALSE);
} }
delete [] destPath;
} }
return result; return result;
} }
@@ -355,13 +345,8 @@ nsresult nsFileSpec::Execute(const char* inArgs ) const
if (! IsDirectory()) if (! IsDirectory())
{ {
char* fileNameWithArgs nsSimpleCharString fileNameWithArgs = mPath + " " + inArgs;
= nsFileSpecHelpers::StringDup(mPath, strlen(mPath) + 1 + strlen(inArgs));
strcat(fileNameWithArgs, " ");
strcat(fileNameWithArgs, inArgs);
result = NS_FILE_RESULT(system(fileNameWithArgs)); result = NS_FILE_RESULT(system(fileNameWithArgs));
delete [] fileNameWithArgs;
} }
return result; return result;
@@ -373,14 +358,14 @@ PRUint32 nsFileSpec::GetDiskSpaceAvailable() const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
char curdir [MAXPATHLEN]; char curdir [MAXPATHLEN];
if (!mPath || !*mPath) if (mPath.IsEmpty())
{ {
(void) getcwd(curdir, MAXPATHLEN); (void) getcwd(curdir, MAXPATHLEN);
if (!curdir) if (!curdir)
return ULONG_MAX; /* hope for the best as we did in cheddar */ return ULONG_MAX; /* hope for the best as we did in cheddar */
} }
else else
sprintf(curdir, "%.200s", mPath); sprintf(curdir, "%.200s", (const char*)mPath);
struct STATFS fs_buf; struct STATFS fs_buf;
if (STATFS(curdir, &fs_buf) < 0) if (STATFS(curdir, &fs_buf) < 0)

View File

@@ -35,22 +35,21 @@
#endif #endif
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileSpecHelpers::Canonify(char*& ioPath, PRBool inMakeDirs) void nsFileSpecHelpers::Canonify(nsSimpleCharString& ioPath, PRBool inMakeDirs)
// Canonify, make absolute, and check whether directories exist. This // Canonify, make absolute, and check whether directories exist. This
// takes a (possibly relative) native path and converts it into a // takes a (possibly relative) native path and converts it into a
// fully qualified native path. // fully qualified native path.
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!ioPath) if (ioPath.IsEmpty())
return; return;
if (inMakeDirs) if (inMakeDirs)
{ {
const int mode = 0700; const int mode = 0700;
char* unixStylePath = nsFileSpecHelpers::StringDup(ioPath); nsSimpleCharString unixStylePath = ioPath;
nsFileSpecHelpers::NativeToUnix(unixStylePath); nsFileSpecHelpers::NativeToUnix(unixStylePath);
nsFileSpecHelpers::MakeAllDirectories(unixStylePath, mode); nsFileSpecHelpers::MakeAllDirectories((const char*)unixStylePath, mode);
delete[] unixStylePath;
} }
char buffer[_MAX_PATH]; char buffer[_MAX_PATH];
errno = 0; errno = 0;
@@ -61,11 +60,11 @@ void nsFileSpecHelpers::Canonify(char*& ioPath, PRBool inMakeDirs)
if (canonicalPath[0] == '\0') if (canonicalPath[0] == '\0')
return; return;
nsFileSpecHelpers::StringAssign(ioPath, canonicalPath); ioPath = canonicalPath;
} } // nsFileSpecHelpers::Canonify
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileSpecHelpers::UnixToNative(char*& ioPath) void nsFileSpecHelpers::UnixToNative(nsSimpleCharString& ioPath)
// This just does string manipulation. It doesn't check reality, or canonify, or // This just does string manipulation. It doesn't check reality, or canonify, or
// anything // anything
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
@@ -73,55 +72,49 @@ void nsFileSpecHelpers::UnixToNative(char*& ioPath)
// Allow for relative or absolute. We can do this in place, because the // Allow for relative or absolute. We can do this in place, because the
// native path is never longer. // native path is never longer.
if (!ioPath || !*ioPath) if (ioPath.IsEmpty())
return; return;
char* src = ioPath;
if (*ioPath == '/')
{
// Strip initial slash for an absolute path // Strip initial slash for an absolute path
src++; char* src = (char*)ioPath;
if (*src == '/')
{
// Since it was an absolute path, check for the drive letter
char* colonPointer = src + 2;
if (strstr(src, "|/") == colonPointer)
*colonPointer = ':';
// allocate new string by copying from ioPath[1]
nsSimpleCharString temp = src + 1;
ioPath = temp;
} }
// Convert the vertical slash to a colon src = (char*)ioPath;
char* cp = src + 1;
// If it was an absolute path, check for the drive letter
if (*ioPath == '/' && strstr(cp, "|/") == cp)
*cp = ':';
// Convert '/' to '\'. // Convert '/' to '\'.
while (*++cp) while (*++src)
{ {
if (*cp == '/') if (*src == '/')
*cp = '\\'; *src = '\\';
} }
} // nsFileSpecHelpers::UnixToNative
if (*ioPath == '/') {
for (cp = ioPath; *cp; ++cp)
*cp = *(cp + 1);
}
}
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
void nsFileSpecHelpers::NativeToUnix(char*& ioPath) void nsFileSpecHelpers::NativeToUnix(nsSimpleCharString& ioPath)
// This just does string manipulation. It doesn't check reality, or canonify, or // This just does string manipulation. It doesn't check reality, or canonify, or
// anything. The unix path is longer, so we can't do it in place. // anything. The unix path is longer, so we can't do it in place.
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!ioPath || !*ioPath) if (ioPath.IsEmpty())
return; return;
// Convert the drive-letter separator, if present // Convert the drive-letter separator, if present
char* temp = nsFileSpecHelpers::StringDup("/", 1 + strlen(ioPath)); nsSimpleCharString temp("/");
char* cp = ioPath + 1; char* cp = (char*)ioPath + 1;
if (strstr(cp, ":\\") == cp) { if (strstr(cp, ":\\") == cp)
*cp = '|'; // absolute path *cp = '|'; // absolute path
} else
else { temp[0] = '\0'; // relative path
*temp = '\0'; // relative path
}
// Convert '\' to '/' // Convert '\' to '/'
for (; *cp; cp++) for (; *cp; cp++)
@@ -129,17 +122,14 @@ void nsFileSpecHelpers::NativeToUnix(char*& ioPath)
if (*cp == '\\') if (*cp == '\\')
*cp = '/'; *cp = '/';
} }
// Add the slash in front. // Add the slash in front.
strcat(temp, ioPath); temp += ioPath;
StringAssign(ioPath, temp); ioPath = temp;
delete [] temp;
} }
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFileSpec::nsFileSpec(const nsFilePath& inPath) nsFileSpec::nsFileSpec(const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(NULL)
{ {
*this = inPath; *this = inPath;
} }
@@ -148,7 +138,7 @@ nsFileSpec::nsFileSpec(const nsFilePath& inPath)
void nsFileSpec::operator = (const nsFilePath& inPath) void nsFileSpec::operator = (const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, (const char*)inPath); mPath = (const char*)inPath;
nsFileSpecHelpers::UnixToNative(mPath); nsFileSpecHelpers::UnixToNative(mPath);
mError = NS_OK; mError = NS_OK;
} // nsFileSpec::operator = } // nsFileSpec::operator =
@@ -156,7 +146,6 @@ void nsFileSpec::operator = (const nsFilePath& inPath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsFilePath::nsFilePath(const nsFileSpec& inSpec) nsFilePath::nsFilePath(const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
: mPath(NULL)
{ {
*this = inSpec; *this = inSpec;
} // nsFilePath::nsFilePath } // nsFilePath::nsFilePath
@@ -165,7 +154,7 @@ nsFilePath::nsFilePath(const nsFileSpec& inSpec)
void nsFilePath::operator = (const nsFileSpec& inSpec) void nsFilePath::operator = (const nsFileSpec& inSpec)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(mPath, inSpec.mPath); mPath = inSpec.mPath;
nsFileSpecHelpers::NativeToUnix(mPath); nsFileSpecHelpers::NativeToUnix(mPath);
} // nsFilePath::operator = } // nsFilePath::operator =
@@ -173,14 +162,14 @@ void nsFilePath::operator = (const nsFileSpec& inSpec)
void nsFileSpec::SetLeafName(const char* inLeafName) void nsFileSpec::SetLeafName(const char* inLeafName)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::LeafReplace(mPath, '\\', inLeafName); mPath.LeafReplace('\\', inLeafName);
} // nsFileSpec::SetLeafName } // nsFileSpec::SetLeafName
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
char* nsFileSpec::GetLeafName() const char* nsFileSpec::GetLeafName() const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
return nsFileSpecHelpers::GetLeaf(mPath, '\\'); return mPath.GetLeaf('\\');
} // nsFileSpec::GetLeafName } // nsFileSpec::GetLeafName
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
@@ -232,9 +221,9 @@ PRBool nsFileSpec::IsDirectory() const
void nsFileSpec::GetParent(nsFileSpec& outSpec) const void nsFileSpec::GetParent(nsFileSpec& outSpec) const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
nsFileSpecHelpers::StringAssign(outSpec.mPath, mPath); outSpec.mPath = mPath;
char* cp = strrchr(outSpec.mPath, '\\'); char* cp = strrchr(outSpec.mPath, '\\');
if (cp) if (cp++)
*cp = '\0'; *cp = '\0';
} // nsFileSpec::GetParent } // nsFileSpec::GetParent
@@ -242,19 +231,18 @@ void nsFileSpec::GetParent(nsFileSpec& outSpec) const
void nsFileSpec::operator += (const char* inRelativePath) void nsFileSpec::operator += (const char* inRelativePath)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!inRelativePath || !mPath) if (!inRelativePath || mPath.IsEmpty())
return; return;
if (mPath[strlen(mPath) - 1] == '\\') if (mPath[mPath.Length() - 1] == '\\')
nsFileSpecHelpers::ReallocCat(mPath, "x"); mPath += "x";
else else
nsFileSpecHelpers::ReallocCat(mPath, "\\x"); mPath += "\\x";
// If it's a (unix) relative path, make it native // If it's a (unix) relative path, make it native
char* dosPath = nsFileSpecHelpers::StringDup(inRelativePath); nsSimpleCharString dosPath = inRelativePath;
nsFileSpecHelpers::UnixToNative(dosPath); nsFileSpecHelpers::UnixToNative(dosPath);
SetLeafName(dosPath); SetLeafName(dosPath);
delete [] dosPath;
} // nsFileSpec::operator += } // nsFileSpec::operator +=
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
@@ -309,54 +297,40 @@ nsresult nsFileSpec::Copy(const nsFileSpec& inParentDirectory) const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
// We can only copy into a directory, and (for now) can not copy entire directories // We can only copy into a directory, and (for now) can not copy entire directories
if (inParentDirectory.IsDirectory() && (! IsDirectory() ) ) if (inParentDirectory.IsDirectory() && (! IsDirectory() ) )
{ {
char *leafname = GetLeafName(); char *leafname = GetLeafName();
char* destPath = nsFileSpecHelpers::StringDup(inParentDirectory, ( strlen(inParentDirectory) + 1 + strlen(leafname) ) ); nsSimpleCharString destPath(inParentDirectory.GetCString());
strcat(destPath, "\\"); destPath += "\\";
strcat(destPath, leafname); destPath += leafname;
delete [] leafname; delete [] leafname;
// CopyFile returns non-zero if succeeds // CopyFile returns non-zero if succeeds
int copyOK = CopyFile(*this, destPath, true); int copyOK = CopyFile(GetCString(), destPath, true);
delete[] destPath;
if (copyOK) if (copyOK)
{
return NS_OK; return NS_OK;
} }
}
return NS_FILE_FAILURE; return NS_FILE_FAILURE;
} // nsFileSpec::Copy } // nsFileSpec::Copy
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
nsresult nsFileSpec::Move(const nsFileSpec& nsNewParentDirectory) const nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory) const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
// We can only copy into a directory, and (for now) can not copy entire directories // We can only copy into a directory, and (for now) can not copy entire directories
if (inNewParentDirectory.IsDirectory() && (! IsDirectory() ) )
if (nsNewParentDirectory.IsDirectory() && (! IsDirectory() ) )
{ {
char *leafname = GetLeafName(); char *leafname = GetLeafName();
char *destPath = nsFileSpecHelpers::StringDup(nsNewParentDirectory, ( strlen(nsNewParentDirectory) + 1 + strlen(leafname) )); nsSimpleCharString destPath(inNewParentDirectory.GetCString());
strcat(destPath, "\\"); destPath += "\\";
strcat(destPath, leafname); destPath += leafname;
delete [] leafname; delete [] leafname;
// MoveFile returns non-zero if succeeds // MoveFile returns non-zero if succeeds
int copyOK = MoveFile(*this, destPath); int copyOK = MoveFile(GetCString(), destPath);
delete [] destPath;
if (copyOK) if (copyOK)
{
return NS_OK; return NS_OK;
} }
}
return NS_FILE_FAILURE; return NS_FILE_FAILURE;
} // nsFileSpec::Move } // nsFileSpec::Move
@@ -364,25 +338,13 @@ nsresult nsFileSpec::Move(const nsFileSpec& nsNewParentDirectory) const
nsresult nsFileSpec::Execute(const char* inArgs ) const nsresult nsFileSpec::Execute(const char* inArgs ) const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
if (!IsDirectory())
if (! IsDirectory())
{ {
char* fileNameWithArgs = NULL; nsSimpleCharString fileNameWithArgs = mPath + " " + inArgs;
fileNameWithArgs = nsFileSpecHelpers::StringDup(mPath, ( strlen(mPath) + 1 + strlen(inArgs) ) );
strcat(fileNameWithArgs, " ");
strcat(fileNameWithArgs, inArgs);
int execResult = WinExec( fileNameWithArgs, SW_NORMAL ); int execResult = WinExec( fileNameWithArgs, SW_NORMAL );
delete [] fileNameWithArgs;
if (execResult > 31) if (execResult > 31)
{
return NS_OK; return NS_OK;
} }
}
return NS_FILE_FAILURE; return NS_FILE_FAILURE;
} // nsFileSpec::Execute } // nsFileSpec::Execute
@@ -391,13 +353,13 @@ PRUint32 nsFileSpec::GetDiskSpaceAvailable() const
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
char aDrive[_MAX_DRIVE + 2]; char aDrive[_MAX_DRIVE + 2];
_splitpath( mPath, aDrive, NULL, NULL, NULL); _splitpath( (const char*)mPath, aDrive, NULL, NULL, NULL);
if (aDrive[0] == '\0') if (aDrive[0] == '\0')
{ {
// The back end is always trying to pass us paths that look // The back end is always trying to pass us paths that look
// like /c|/netscape/mail. See if we've got one of them // like /c|/netscape/mail. See if we've got one of them
if (strlen(mPath) > 2 && mPath[0] == '/' && mPath[2] == '|') if (mPath.Length() > 2 && mPath[0] == '/' && mPath[2] == '|')
{ {
aDrive[0] = mPath[1]; aDrive[0] = mPath[1];
aDrive[1] = ':'; aDrive[1] = ':';

View File

@@ -14,9 +14,9 @@ class BasicStringImpl
{ {
public: public:
BasicStringImpl() BasicStringImpl()
: mResult(NS_OK) : mOffset(0)
, mLastResult(NS_OK)
, mEOF(PR_FALSE) , mEOF(PR_FALSE)
, mOffset(0)
{ {
NS_INIT_REFCNT(); NS_INIT_REFCNT();
} }
@@ -60,16 +60,16 @@ class BasicStringImpl
NS_PRECONDITION(aReadCount != nsnull, "null ptr"); NS_PRECONDITION(aReadCount != nsnull, "null ptr");
if (!aReadCount) if (!aReadCount)
return NS_ERROR_NULL_POINTER; return NS_ERROR_NULL_POINTER;
if (NS_FAILED(mResult)) if (NS_FAILED(mLastResult))
return mResult; return mLastResult;
PRInt32 bytesRead = read(aBuf, aCount); PRInt32 bytesRead = read(aBuf, aCount);
if (NS_FAILED(mResult)) if (NS_FAILED(mLastResult))
{ {
*aReadCount = 0; *aReadCount = 0;
return mResult; return mLastResult;
} }
*aReadCount = bytesRead; *aReadCount = bytesRead;
if (bytesRead < aCount) if (bytesRead < (PRInt32)aCount)
SetAtEOF(PR_TRUE); SetAtEOF(PR_TRUE);
return NS_OK; return NS_OK;
} }
@@ -81,13 +81,13 @@ class BasicStringImpl
NS_PRECONDITION(aBuf != nsnull, "null ptr"); NS_PRECONDITION(aBuf != nsnull, "null ptr");
NS_PRECONDITION(aWriteCount != nsnull, "null ptr"); NS_PRECONDITION(aWriteCount != nsnull, "null ptr");
if (NS_FAILED(mResult)) if (NS_FAILED(mLastResult))
return mResult; return mLastResult;
PRInt32 bytesWrit = write(aBuf, aCount); PRInt32 bytesWrit = write(aBuf, aCount);
if (NS_FAILED(mResult)) if (NS_FAILED(mLastResult))
{ {
*aWriteCount = 0; *aWriteCount = 0;
return mResult; return mLastResult;
} }
*aWriteCount = bytesWrit; *aWriteCount = bytesWrit;
return NS_OK; return NS_OK;
@@ -104,7 +104,7 @@ class BasicStringImpl
public: public:
nsresult get_result() const { return mResult; } nsresult get_result() const { return mLastResult; }
protected: protected:
@@ -113,14 +113,14 @@ class BasicStringImpl
virtual PRInt32 write(const char*, PRUint32) virtual PRInt32 write(const char*, PRUint32)
{ {
NS_ASSERTION(PR_FALSE, "Write to a const string"); NS_ASSERTION(PR_FALSE, "Write to a const string");
mResult = NS_FILE_RESULT(PR_ILLEGAL_ACCESS_ERROR); mLastResult = NS_FILE_RESULT(PR_ILLEGAL_ACCESS_ERROR);
return -1; return -1;
} }
protected: protected:
PRUint32 mOffset; PRUint32 mOffset;
nsresult mResult; nsresult mLastResult;
PRBool mEOF; PRBool mEOF;
}; // class BasicStringImpl }; // class BasicStringImpl
@@ -146,7 +146,7 @@ class ConstCharImpl
virtual PRInt32 read(char* buf, PRUint32 aCount) virtual PRInt32 read(char* buf, PRUint32 aCount)
{ {
PRInt32 maxCount = mLength - mOffset; PRInt32 maxCount = mLength - mOffset;
if (aCount > maxCount) if ((PRInt32)aCount > maxCount)
aCount = maxCount; aCount = maxCount;
memcpy(buf, mConstString + mOffset, aCount); memcpy(buf, mConstString + mOffset, aCount);
mOffset += aCount; mOffset += aCount;
@@ -181,7 +181,7 @@ class CharImpl
mString = new char[mAllocLength]; mString = new char[mAllocLength];
if (!mString) if (!mString)
{ {
mResult = NS_ERROR_OUT_OF_MEMORY; mLastResult = NS_ERROR_OUT_OF_MEMORY;
return; return;
} }
mConstString = mString; mConstString = mString;
@@ -193,17 +193,17 @@ class CharImpl
virtual PRInt32 write(const char* buf, PRUint32 aCount) virtual PRInt32 write(const char* buf, PRUint32 aCount)
{ {
PRInt32 maxCount = mAllocLength - 1 - mOffset; PRInt32 maxCount = mAllocLength - 1 - mOffset;
if (aCount > maxCount) if ((PRInt32)aCount > maxCount)
{ {
do { do {
maxCount += kAllocQuantum; maxCount += kAllocQuantum;
} while (aCount > maxCount); } while ((PRInt32)aCount > maxCount);
mAllocLength = maxCount + 1 + mOffset; mAllocLength = maxCount + 1 + mOffset;
char* newString = new char[mAllocLength]; char* newString = new char[mAllocLength];
if (!newString) if (!newString)
{ {
mResult = NS_ERROR_OUT_OF_MEMORY; mLastResult = NS_ERROR_OUT_OF_MEMORY;
return 0; return 0;
} }
strcpy(newString, mString); strcpy(newString, mString);
@@ -220,8 +220,8 @@ class CharImpl
protected: protected:
char*& mString; char*& mString;
size_t mOriginalLength;
size_t mAllocLength; size_t mAllocLength;
size_t mOriginalLength;
}; // class CharImpl }; // class CharImpl
@@ -271,7 +271,7 @@ class StringImpl
chars.Seek(PR_SEEK_SET, mOffset); chars.Seek(PR_SEEK_SET, mOffset);
// Get the bytecount and result from the CharImpl // Get the bytecount and result from the CharImpl
PRInt32 result = chars.write(buf,count); PRInt32 result = chars.write(buf,count);
mResult = chars.get_result(); mLastResult = chars.get_result();
// Set our string to match the new chars // Set our string to match the new chars
mString = cstring; mString = cstring;
// Set our const string also... // Set our const string also...
@@ -331,7 +331,7 @@ NS_IMETHODIMP BasicStringImpl::QueryInterface(REFNSIID aIID, void** aInstancePtr
NS_IMETHODIMP BasicStringImpl::Seek(PRSeekWhence whence, PRInt32 offset) NS_IMETHODIMP BasicStringImpl::Seek(PRSeekWhence whence, PRInt32 offset)
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
{ {
mResult = NS_OK; // reset on a seek. mLastResult = NS_OK; // reset on a seek.
mEOF = PR_FALSE; // reset on a seek. mEOF = PR_FALSE; // reset on a seek.
PRInt32 fileSize = length(); PRInt32 fileSize = length();
PRInt32 newPosition; PRInt32 newPosition;
@@ -344,7 +344,7 @@ NS_IMETHODIMP BasicStringImpl::Seek(PRSeekWhence whence, PRInt32 offset)
if (newPosition < 0) if (newPosition < 0)
{ {
newPosition = 0; newPosition = 0;
mResult = NS_FILE_RESULT(PR_FILE_SEEK_ERROR); mLastResult = NS_FILE_RESULT(PR_FILE_SEEK_ERROR);
} }
if (newPosition >= fileSize) if (newPosition >= fileSize)
{ {

View File

@@ -345,11 +345,14 @@ int FilesTest::Parent(
mySpec.GetParent(outParent); mySpec.GetParent(outParent);
nsFilePath parentPath(outParent); nsFilePath parentPath(outParent);
nsFileURL url(parentPath);
mConsole mConsole
<< "GetParent() on " << "GetParent() on "
<< "\n\t" << pathAsString << "\n\t" << pathAsString
<< "\n yields " << "\n yields "
<< "\n\t" << (const char*)parentPath << "\n\t" << (const char*)parentPath
<< "\n or as a URL"
<< "\n\t" << (const char*)url
<< nsEndl; << nsEndl;
Inspect(); Inspect();
return 0; return 0;