diff --git a/mozilla/base/public/nsFileSpec.h b/mozilla/base/public/nsFileSpec.h index 9710ae3f38a..626ce71c268 100644 --- a/mozilla/base/public/nsFileSpec.h +++ b/mozilla/base/public/nsFileSpec.h @@ -206,6 +206,74 @@ protected: const char* mCString; }; // 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 // This is whatever each platform really prefers to describe files as. Declared first @@ -255,7 +323,7 @@ class NS_BASE nsFileSpec long parID, ConstStr255Param name); nsFileSpec(const FSSpec& inSpec) - : mSpec(inSpec), mError(NS_OK), mPath(nsnull) {} + : mSpec(inSpec), mError(NS_OK) {} void operator = (const FSSpec& inSpec) { mSpec = inSpec; mError = NS_OK; } @@ -284,7 +352,7 @@ class NS_BASE nsFileSpec nsresult Error() const { #ifndef XP_MAC - if (!mPath && NS_SUCCEEDED(mError)) + if (mPath.IsEmpty() && NS_SUCCEEDED(mError)) ((nsFileSpec*)this)->mError = NS_FILE_FAILURE; #endif return mError; @@ -413,7 +481,7 @@ class NS_BASE nsFileSpec #ifdef XP_MAC FSSpec mSpec; #endif - char* mPath; + nsSimpleCharString mPath; nsresult mError; }; // class nsFileSpec @@ -450,8 +518,10 @@ class NS_BASE nsFileURL void operator = (const nsFilePath& inOther); void operator = (const nsFileSpec& inOther); - operator const char* () const { return mURL; } // deprecated. - const char* GetAsString() const { return mURL; } + void operator +=(const char* inRelativeUnixPath); + 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 << ( nsOutputStream& s, const nsFileURL& spec); @@ -460,12 +530,11 @@ class NS_BASE nsFileURL // Accessor to allow quick assignment to a mFileSpec const nsFileSpec& GetFileSpec() const { return mFileSpec; } #endif - private: - // Should not be defined (only nsFilePath is to be treated as strings. - operator char* (); + protected: friend class nsFilePath; // to allow construction of nsFilePath - char* mURL; + nsSimpleCharString mURL; + #ifdef XP_MAC // Since the path on the macintosh does not uniquely specify a file (volumes // 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* // that is provided, and it allows the // 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 char* inString); @@ -508,6 +573,9 @@ class NS_BASE nsFilePath void operator = (const nsFileURL& inURL); void operator = (const nsFileSpec& inOther); + void operator +=(const char* inRelativeUnixPath); + nsFilePath operator +(const char* inRelativeUnixPath) const; + #ifdef XP_MAC public: // Accessor to allow quick assignment to a mFileSpec @@ -516,7 +584,7 @@ class NS_BASE nsFilePath private: - char* mPath; + nsSimpleCharString mPath; #ifdef XP_MAC // Since the path on the macintosh does not uniquely specify a file (volumes // can have the same name), stash the secret nsFileSpec, too. @@ -533,7 +601,7 @@ class NS_BASE nsPersistentFileDescriptor //======================================================================================== { public: - nsPersistentFileDescriptor() : mDescriptorString(nsnull) {} + nsPersistentFileDescriptor() {} // For use prior to reading in from a stream nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inPath); virtual ~nsPersistentFileDescriptor(); @@ -553,14 +621,13 @@ class NS_BASE nsPersistentFileDescriptor friend class nsFileSpec; private: - // Here are the ways to get data in and out of a file. - void GetData(void*& outData, PRInt32& outSize) const; - // DON'T FREE the returned data! - void SetData(const void* inData, PRInt32 inSize); + + void GetData(nsSimpleCharString& outData, PRInt32& outSize) const; + void SetData(const nsSimpleCharString& inData, PRInt32 inSize); protected: - char* mDescriptorString; + nsSimpleCharString mDescriptorString; }; // class nsPersistentFileDescriptor @@ -569,16 +636,16 @@ class NS_BASE nsDirectoryIterator // Example: // // 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: // -// 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 @@ -599,6 +666,9 @@ class NS_BASE nsDirectoryIterator nsDirectoryIterator& operator --(); // moves to the previous item, if any. nsDirectoryIterator& operator --(int) { return --(*this); } // post-decrement. operator nsFileSpec&() { return mCurrent; } + + nsFileSpec& Spec() { return mCurrent; } + private: nsFileSpec mCurrent; PRBool mExists; diff --git a/mozilla/base/src/mac/nsFileSpecMac.cpp b/mozilla/base/src/mac/nsFileSpecMac.cpp index 27c37e258dd..1d7fc34d20d 100644 --- a/mozilla/base/src/mac/nsFileSpecMac.cpp +++ b/mozilla/base/src/mac/nsFileSpecMac.cpp @@ -518,7 +518,6 @@ Clean: nsFileSpec::nsFileSpec() //---------------------------------------------------------------------------------------- : mError(NS_OK) -, mPath(nsnull) { mSpec.name[0] = '\0'; } @@ -527,7 +526,6 @@ nsFileSpec::nsFileSpec() nsFileSpec::nsFileSpec(const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- : mSpec(inSpec.mSpec) -, mPath(nsnull) , mError(inSpec.Error()) { } @@ -535,7 +533,6 @@ nsFileSpec::nsFileSpec(const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { mSpec.vRefNum = 0; mSpec.parID = 0; @@ -551,7 +548,6 @@ nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { mSpec.vRefNum = 0; mSpec.parID = 0; @@ -567,7 +563,6 @@ nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(short vRefNum, long parID, ConstStr255Param name) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { mError = NS_FILE_RESULT(::FSMakeFSSpec(vRefNum, parID, name, &mSpec)); if (mError == NS_FILE_RESULT(fnfErr)) @@ -577,7 +572,6 @@ nsFileSpec::nsFileSpec(short vRefNum, long parID, ConstStr255Param name) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsFilePath& inPath) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { *this = inPath.GetFileSpec(); } @@ -586,7 +580,7 @@ nsFileSpec::nsFileSpec(const nsFilePath& inPath) void nsFileSpec::operator = (const char* inString) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, nsnull); + mPath.SetToEmpty(); mSpec.vRefNum = 0; mSpec.parID = 0; @@ -601,8 +595,10 @@ void nsFileSpec::operator = (const char* inString) void nsFileSpec::operator = (const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, nsnull); - mSpec = inSpec.mSpec; + mPath.SetToEmpty(); + mSpec.vRefNum = inSpec.mSpec.vRefNum; + mSpec.parID = inSpec.mSpec.parID; + memcpy(mSpec.name, inSpec.mSpec.name, inSpec.mSpec.name[0] + 1); mError = inSpec.Error(); } // nsFileSpec::operator = @@ -662,7 +658,7 @@ void nsFileSpec::SetLeafName(const char* inLeafName) // 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. Str255 partialPath; @@ -681,14 +677,14 @@ char* nsFileSpec::GetLeafName() const char leaf[64]; memcpy(leaf, &mSpec.name[1], mSpec.name[0]); leaf[mSpec.name[0]] = '\0'; - return nsFileSpecHelpers::StringDup(leaf); + return PL_strdup(leaf); } // nsFileSpec::GetLeafName //---------------------------------------------------------------------------------------- void nsFileSpec::MakeAliasSafe() //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, nsnull); + mPath.SetToEmpty(); mError = NS_FILE_RESULT(MacFileHelpers::MakeAliasSafe(mSpec)); } // nsFileSpec::MakeAliasSafe @@ -696,7 +692,7 @@ void nsFileSpec::MakeAliasSafe() void nsFileSpec::MakeUnique(ConstStr255Param inSuggestedLeafName) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, nsnull); + mPath.SetToEmpty(); if (inSuggestedLeafName[0] > 0) 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. //---------------------------------------------------------------------------------------- { - if (!mPath) + if (mPath.IsEmpty()) { const_cast(this)->mPath = MacFileHelpers::PathNameFromFSSpec(mSpec, true); - if (!mPath) + if (mPath.IsEmpty()) const_cast(this)->mError = NS_ERROR_OUT_OF_MEMORY; } return mPath; @@ -912,29 +908,30 @@ const char* nsFileSpec::GetCString() const //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mPath(nsnull) -, mFileSpec(inString, inCreateDirs) +: mFileSpec(inString, inCreateDirs) { // Make canonical and absolute. 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) //---------------------------------------------------------------------------------------- -: mPath(nsnull) -, mFileSpec(nsAutoCString(inString), inCreateDirs) +: mFileSpec(nsAutoCString(inString), inCreateDirs) { // Make canonical and absolute. 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) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { *this = inSpec; } @@ -942,18 +939,18 @@ nsFilePath::nsFilePath(const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const nsFileURL& inOther) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { - *this = inOther.GetFileSpec(); + *this = inOther; } //---------------------------------------------------------------------------------------- void nsFilePath::operator = (const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- { - char * path = MacFileHelpers::PathNameFromFSSpec( inSpec.mSpec, true ); - delete [] mPath; - mPath = MacFileHelpers::EncodeMacPath(path, true, false); + char * path = MacFileHelpers::PathNameFromFSSpec(inSpec, true); + path = MacFileHelpers::EncodeMacPath(path, true, false); + mPath = path; + delete [] path; mFileSpec = inSpec; } // nsFilePath::operator = @@ -961,7 +958,11 @@ void nsFilePath::operator = (const nsFileSpec& inSpec) 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) //---------------------------------------------------------------------------------------- -: 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( - mURL + kFileURLPrefixLength, + inString + kFileURLPrefixLength, mFileSpec.mSpec, true, // need to decode false, // don't resolve alias @@ -988,11 +989,14 @@ nsFileURL::nsFileURL(const char* 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( - mURL + kFileURLPrefixLength, + cstring + kFileURLPrefixLength, mFileSpec.mSpec, true, // need to decode false, // don't resolve alias @@ -1005,14 +1009,12 @@ nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFilePath& inOther) //---------------------------------------------------------------------------------------- -: mURL(nsnull) { *this = inOther.GetFileSpec(); } // nsFileURL::nsFileURL //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFileSpec& inOther) -: mURL(nsnull) //---------------------------------------------------------------------------------------- { *this = inOther; @@ -1030,25 +1032,21 @@ void nsFileURL::operator = (const nsFileSpec& inOther) //---------------------------------------------------------------------------------------- { mFileSpec = inOther; - delete [] mURL; char* path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true ); char* encodedPath = MacFileHelpers::EncodeMacPath(path, true, true); - char* encodedURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, encodedPath); + nsSimpleCharString encodedURL(kFileURLPrefix); + encodedURL += 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 = //---------------------------------------------------------------------------------------- void nsFileURL::operator = (const char* inString) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mURL, inString); + mURL = inString; NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!"); mFileSpec.mError = NS_FILE_RESULT(MacFileHelpers::FSSpecFromUnixPath( inString + kFileURLPrefixLength, diff --git a/mozilla/base/src/nsFileSpec.cpp b/mozilla/base/src/nsFileSpec.cpp index e20a931914c..3149de1f1f8 100644 --- a/mozilla/base/src/nsFileSpec.cpp +++ b/mozilla/base/src/nsFileSpec.cpp @@ -30,6 +30,219 @@ #include #include +//======================================================================================== +// 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 //======================================================================================== @@ -39,22 +252,13 @@ NS_NAMESPACE nsFileSpecHelpers , kMaxAltDigitLength = 5 , kMaxCoreLeafNameLength = (kMaxFilenameLength - (kMaxAltDigitLength + 1)) }; - NS_NAMESPACE_PROTOTYPE void LeafReplace( - char*& ioPath, - char inSeparator, - const char* inLeafName); #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); #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 - NS_NAMESPACE_PROTOTYPE void NativeToUnix(char*& ioPath); - NS_NAMESPACE_PROTOTYPE void UnixToNative(char*& ioPath); + NS_NAMESPACE_PROTOTYPE void NativeToUnix(nsSimpleCharString& ioPath); + NS_NAMESPACE_PROTOTYPE void UnixToNative(nsSimpleCharString& ioPath); #endif } NS_NAMESPACE_END @@ -68,136 +272,80 @@ nsresult ns_file_convert_result(PRInt32 nativeErr) } //---------------------------------------------------------------------------------------- -char* nsFileSpecHelpers::StringDup( - 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) +void nsSimpleCharString::LeafReplace(char inSeparator, const char* inLeafName) //---------------------------------------------------------------------------------------- { // Find the existing leaf name - if (!ioPath) + if (IsEmpty()) return; if (!inLeafName) { - *ioPath = '\0'; + SetToEmpty(); return; } - char* lastSeparator = strrchr(ioPath, inSeparator); - int oldLength = strlen(ioPath); - PRBool trailingSeparator = (lastSeparator + 1 == ioPath + oldLength); + char* chars = mData->mString; + char* lastSeparator = strrchr(chars, inSeparator); + int oldLength = Length(); + PRBool trailingSeparator = (lastSeparator + 1 == chars + oldLength); if (trailingSeparator) { *lastSeparator = '\0'; - lastSeparator = strrchr(ioPath, inSeparator); + lastSeparator = strrchr(chars, inSeparator); } if (lastSeparator) lastSeparator++; // point at the trailing string else - lastSeparator = ioPath; // the full monty + lastSeparator = chars; // the full monty *lastSeparator = '\0'; // strip the current leaf name - int newLength = (lastSeparator - ioPath) + strlen(inLeafName) + int(trailingSeparator); - if (newLength > oldLength) - { - char* newPath = StringDup(ioPath, newLength + 1); - delete [] ioPath; - ioPath = newPath; - } - strcat(ioPath, inLeafName); + int newLength = (lastSeparator - chars) + strlen(inLeafName) + int(trailingSeparator); + ReallocData(newLength); + chars = mData->mString; // it might have moved. + + strcat(chars, inLeafName); if (trailingSeparator) { // If the original ended in a slash, then the new one should, too. char sepStr[2] = "/"; *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. //---------------------------------------------------------------------------------------- { - if (!inPath) + if (IsEmpty()) return nsnull; - const char* lastSeparator = strrchr(inPath, inSeparator); + + char* chars = mData->mString; + const char* lastSeparator = strrchr(chars, inSeparator); - // If there was no separator, then return a copy of the caller's path. + // If there was no separator, then return a copy of our path. if (!lastSeparator) - return StringDup(inPath); + return PL_strdup(*this); // So there's at least one separator. What's just after it? // If the separator was not the last character, return the trailing string. const char* leafPointer = lastSeparator + 1; if (*leafPointer) - return StringDup(leafPointer); + return PL_strdup(leafPointer); // So now, separator was the last character. Poke in a null instead. *(char*)lastSeparator = '\0'; // Should use const_cast, but Unix has old compiler. - leafPointer = strrchr(inPath, inSeparator); - char* result = leafPointer ? StringDup(++leafPointer) : StringDup(inPath); + leafPointer = strrchr(chars, inSeparator); + char* result = leafPointer ? PL_strdup(++leafPointer) : PL_strdup(chars); // Restore the poked null before returning. *(char*)lastSeparator = inSeparator; #ifdef XP_PC // 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] = ':'; #endif return result; -} // nsFileSpecHelpers::GetLeaf +} // nsSimpleCharString::GetLeaf #if defined(XP_UNIX) || defined(XP_PC) @@ -210,7 +358,7 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode) if (!inPath) return; - char* pathCopy = nsFileSpecHelpers::StringDup( inPath ); + char* pathCopy = PL_strdup( inPath ); if (!pathCopy) return; @@ -227,7 +375,6 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode) if (currentEnd) { nsFileSpec spec; - *currentEnd = '\0'; #ifdef XP_PC @@ -237,12 +384,13 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode) */ 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, "/"); spec = nsFilePath(startDir, PR_FALSE); - delete [] startDir; + PR_Free(startDir); } else { @@ -278,16 +426,6 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode) #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) #include "windows/nsFileSpecWin.cpp" // Windows-specific implementations #elif defined(XP_MAC) @@ -304,7 +442,6 @@ char* nsFileSpecHelpers::ReallocCat(char*& ioString, const char* inString1) //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mURL(nsnull) { if (!inString) return; @@ -319,7 +456,6 @@ nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mURL(nsnull) { const nsAutoCString aString(inString); const char* aCString = (const char*) aString; @@ -335,7 +471,7 @@ nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFileURL& inOther) //---------------------------------------------------------------------------------------- -: mURL(nsFileSpecHelpers::StringDup(inOther.mURL)) +: mURL(inOther.mURL) #ifdef XP_MAC , mFileSpec(inOther.GetFileSpec()) #endif @@ -346,7 +482,6 @@ nsFileURL::nsFileURL(const nsFileURL& inOther) //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFilePath& inOther) //---------------------------------------------------------------------------------------- -: mURL(nsnull) { *this = inOther; } // nsFileURL::nsFileURL @@ -355,7 +490,6 @@ nsFileURL::nsFileURL(const nsFilePath& inOther) #ifndef XP_MAC //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFileSpec& inOther) -: mURL(nsnull) //---------------------------------------------------------------------------------------- { *this = inOther; @@ -366,7 +500,6 @@ nsFileURL::nsFileURL(const nsFileSpec& inOther) nsFileURL::~nsFileURL() //---------------------------------------------------------------------------------------- { - delete [] mURL; } #ifndef XP_MAC @@ -374,16 +507,37 @@ nsFileURL::~nsFileURL() void nsFileURL::operator = (const char* inString) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mURL, inString); + mURL = inString; NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!"); } // nsFileURL::operator = #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) //---------------------------------------------------------------------------------------- { - mURL = nsFileSpecHelpers::StringAssign(mURL, inOther.mURL); + mURL = inOther.mURL; #ifdef XP_MAC mFileSpec = inOther.GetFileSpec(); #endif @@ -394,7 +548,7 @@ void nsFileURL::operator = (const nsFileURL& inOther) void nsFileURL::operator = (const nsFilePath& inOther) //---------------------------------------------------------------------------------------- { - delete [] mURL; + mURL = kFileURLPrefix; char* original = (char*)(const char*)inOther; // we shall modify, but restore. #ifdef XP_PC // 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); #endif if (escapedPath) - mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, escapedPath); + mURL += escapedPath; delete [] escapedPath; } // nsFileURL::operator = #endif @@ -418,6 +572,8 @@ void nsFileURL::operator = (const nsFileSpec& inOther) //---------------------------------------------------------------------------------------- { *this = nsFilePath(inOther); + if (mURL[mURL.Length() - 1] != '/' && inOther.IsDirectory()) + mURL += "/"; } // nsFileURL::operator = #endif @@ -433,7 +589,7 @@ nsOutputStream& operator << (nsOutputStream& s, const nsFileURL& url) //======================================================================================== nsFilePath::nsFilePath(const nsFilePath& inPath) - : mPath(nsFileSpecHelpers::StringDup(inPath.mPath)) + : mPath(inPath.mPath) #ifdef XP_MAC , mFileSpec(inPath.mFileSpec) #endif @@ -444,7 +600,7 @@ nsFilePath::nsFilePath(const nsFilePath& inPath) //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mPath(nsFileSpecHelpers::StringDup(inString)) +: mPath(inString) { 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) //---------------------------------------------------------------------------------------- -: mPath(inString.ToNewCString()) +: mPath(inString) { 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) //---------------------------------------------------------------------------------------- -: mPath(nsFileSpecHelpers::StringDup(inOther.mURL + kFileURLPrefixLength)) { + mPath = (const char*)inOther.mURL + kFileURLPrefixLength; + mPath.Unescape(); } #endif @@ -493,7 +650,7 @@ nsFilePath::nsFilePath(const nsFileURL& inOther) //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const nsFileSpec& inOther) //---------------------------------------------------------------------------------------- -: mPath(nsFileSpecHelpers::StringDup(inOther.mPath)) +: mPath(inOther.mPath) { } #endif // XP_UNIX @@ -502,7 +659,6 @@ nsFilePath::nsFilePath(const nsFileSpec& inOther) nsFilePath::~nsFilePath() //---------------------------------------------------------------------------------------- { - delete [] mPath; } #ifdef XP_UNIX @@ -510,7 +666,7 @@ nsFilePath::~nsFilePath() void nsFilePath::operator = (const nsFileSpec& inOther) //---------------------------------------------------------------------------------------- { - mPath = nsFileSpecHelpers::StringAssign(mPath, inOther.mPath); + mPath = inOther.mPath; } #endif // XP_UNIX @@ -521,9 +677,9 @@ void nsFilePath::operator = (const char* inString) NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path"); #ifdef XP_MAC mFileSpec = inString; - nsFileSpecHelpers::StringAssign(mPath, (const char*)nsFilePath(mFileSpec)); + mPath = (const char*)nsFilePath(mFileSpec); #else - nsFileSpecHelpers::StringAssign(mPath, inString); + mPath = inString; #ifdef XP_PC nsFileSpecHelpers::UnixToNative(mPath); #endif @@ -540,7 +696,7 @@ void nsFilePath::operator = (const char* inString) void nsFilePath::operator = (const nsFileURL& inOther) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, (const char*)nsFilePath(inOther)); + mPath = (const char*)nsFilePath(inOther); } #endif @@ -548,12 +704,33 @@ void nsFilePath::operator = (const nsFileURL& inOther) void nsFilePath::operator = (const nsFilePath& inOther) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, inOther.mPath); + mPath = inOther.mPath; #ifdef XP_MAC mFileSpec = inOther.GetFileSpec(); #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 //======================================================================================== @@ -562,8 +739,7 @@ void nsFilePath::operator = (const nsFilePath& inOther) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec() //---------------------------------------------------------------------------------------- -: mPath(nsnull) -, mError(NS_OK) +: mError(NS_OK) { } #endif @@ -571,7 +747,6 @@ nsFileSpec::nsFileSpec() //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsPersistentFileDescriptor& inDescriptor) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { *this = inDescriptor; } @@ -579,7 +754,6 @@ nsFileSpec::nsFileSpec(const nsPersistentFileDescriptor& inDescriptor) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsFileURL& inURL) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { *this = nsFilePath(inURL); // convert to unix path first } @@ -609,7 +783,7 @@ void nsFileSpec::MakeUnique() char* suffix = ""; if (lastDot) { - suffix = nsFileSpecHelpers::StringDup(lastDot); // include '.' + suffix = PL_strdup(lastDot); // include '.' *lastDot = '\0'; // strip suffix and dot. } const int kMaxRootLength @@ -640,7 +814,7 @@ void nsFileSpec::operator = (const nsPersistentFileDescriptor& inDescriptor) //---------------------------------------------------------------------------------------- { - void* data; + nsSimpleCharString data; PRInt32 dataSize; inDescriptor.GetData(data, dataSize); @@ -656,9 +830,9 @@ void nsFileSpec::operator = (const nsPersistentFileDescriptor& inDescriptor) Boolean changed; mError = NS_FILE_RESULT(::ResolveAlias(nsnull, aliasH, &mSpec, &changed)); DisposeHandle((Handle) aliasH); - delete [] mPath; + mPath.SetToEmpty(); #else - nsFileSpecHelpers::StringAssign(mPath, (char*)data); + mPath = data; mError = NS_OK; #endif } @@ -671,7 +845,7 @@ void nsFileSpec::operator = (const nsPersistentFileDescriptor& inDescriptor) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsFilePath& inPath) //---------------------------------------------------------------------------------------- -: mPath(nsFileSpecHelpers::StringDup((const char*)inPath)) +: mPath((const char*)inPath) , mError(NS_OK) { } @@ -682,7 +856,7 @@ nsFileSpec::nsFileSpec(const nsFilePath& inPath) void nsFileSpec::operator = (const nsFilePath& inPath) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, (const char*)inPath); + mPath = (const char*)inPath; mError = NS_OK; } #endif //XP_UNIX @@ -691,7 +865,7 @@ void nsFileSpec::operator = (const nsFilePath& inPath) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- -: mPath(nsFileSpecHelpers::StringDup(inSpec.mPath)) +: mPath(inSpec.mPath) , mError(NS_OK) { } @@ -701,7 +875,7 @@ nsFileSpec::nsFileSpec(const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mPath(nsFileSpecHelpers::StringDup(inString)) +: mPath(inString) , mError(NS_OK) { // Make canonical and absolute. @@ -713,7 +887,7 @@ nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mPath(inString.ToNewCString()) +: mPath(inString) , mError(NS_OK) { // Make canonical and absolute. @@ -725,7 +899,6 @@ nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs) nsFileSpec::~nsFileSpec() //---------------------------------------------------------------------------------------- { - delete [] mPath; } #if defined(XP_UNIX) || defined(XP_PC) @@ -733,7 +906,7 @@ nsFileSpec::~nsFileSpec() void nsFileSpec::operator = (const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- { - mPath = nsFileSpecHelpers::StringAssign(mPath, inSpec.mPath); + mPath = inSpec.mPath; mError = inSpec.Error(); } #endif //XP_UNIX @@ -744,7 +917,7 @@ void nsFileSpec::operator = (const nsFileSpec& inSpec) void nsFileSpec::operator = (const char* inString) //---------------------------------------------------------------------------------------- { - mPath = nsFileSpecHelpers::StringAssign(mPath, inString); + mPath = inString; // Make canonical and absolute. nsFileSpecHelpers::Canonify(mPath, PR_FALSE /* XXX? */); mError = NS_OK; @@ -788,8 +961,8 @@ PRBool nsFileSpec::operator == (const nsFileSpec& inOther) const EqualString(inOther.mSpec.name, mSpec.name, false, true)) return PR_TRUE; #else - PRBool amEmpty = !mPath || !*mPath; - PRBool heEmpty = !inOther.mPath || !*inOther.mPath; + PRBool amEmpty = mPath.IsEmpty(); + PRBool heEmpty = inOther.mPath.IsEmpty(); if (amEmpty) // we're the same if he's empty... return heEmpty; if (heEmpty) // ('cuz I'm not...) @@ -835,7 +1008,7 @@ const char* nsFileSpec::GetCString() const //---------------------------------------------------------------------------------------- nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inDesc) //---------------------------------------------------------------------------------------- - : mDescriptorString(nsFileSpecHelpers::StringDup(inDesc.mDescriptorString)) + : mDescriptorString(inDesc.mDescriptorString) { } // nsPersistentFileDescriptor::nsPersistentFileDescriptor @@ -843,13 +1016,12 @@ nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsPersistentFileDes void nsPersistentFileDescriptor::operator = (const nsPersistentFileDescriptor& inDesc) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mDescriptorString, inDesc.mDescriptorString); + mDescriptorString = inDesc.mDescriptorString; } // nsPersistentFileDescriptor::operator = //---------------------------------------------------------------------------------------- nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- - : mDescriptorString(nsnull) { *this = inSpec; } // nsPersistentFileDescriptor::nsPersistentFileDescriptor @@ -871,10 +1043,10 @@ void nsPersistentFileDescriptor::operator = (const nsFileSpec& inSpec) char* buf = PL_Base64Encode((const char*)*aliasH, bytes, nsnull); DisposeHandle((Handle) aliasH); - nsFileSpecHelpers::StringAssign(mDescriptorString, buf); + mDescriptorString = buf; PR_Free(buf); #else - nsFileSpecHelpers::StringAssign(mDescriptorString, inSpec.GetCString()); + mDescriptorString = inSpec.GetCString(); #endif // XP_MAC } // nsPersistentFileDescriptor::operator = @@ -882,27 +1054,21 @@ void nsPersistentFileDescriptor::operator = (const nsFileSpec& inSpec) nsPersistentFileDescriptor::~nsPersistentFileDescriptor() //---------------------------------------------------------------------------------------- { - delete [] mDescriptorString; } // 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; } //---------------------------------------------------------------------------------------- -void nsPersistentFileDescriptor::SetData(const void* inData, PRInt32 inSize) +void nsPersistentFileDescriptor::SetData(const nsSimpleCharString& inData, PRInt32 inSize) //---------------------------------------------------------------------------------------- { - delete [] mDescriptorString; - mDescriptorString = new char[1 + inSize]; - if (!mDescriptorString) - return; - memcpy(mDescriptorString, inData, inSize); - mDescriptorString[inSize] = '\0'; + mDescriptorString.CopyFrom((const char*)inData, inSize); } #define MAX_PERSISTENT_DATA_SIZE 1000 @@ -937,7 +1103,7 @@ nsInputStream& operator >> (nsInputStream& s, nsPersistentFileDescriptor& d) if (bytesRead != 8) return s; bigBuffer[8] = '\0'; - sscanf(bigBuffer, "%lx", (PRUint32*)&bytesRead); + sscanf(bigBuffer, "%x", (PRUint32*)&bytesRead); if (bytesRead > MAX_PERSISTENT_DATA_SIZE) return s; // preposterous. // Now we know how many bytes to read, do it. @@ -953,13 +1119,13 @@ nsOutputStream& operator << (nsOutputStream& s, const nsPersistentFileDescriptor { char littleBuf[9]; PRInt32 dataSize; - void* data; + nsSimpleCharString data; d.GetData(data, dataSize); // First write (in hex) the length of the data to follow. Exactly 8 bytes sprintf(littleBuf, "%0.8x", dataSize); s << littleBuf; // Now write the data itself - s << d.mDescriptorString; + s << (const char*)data; return s; } diff --git a/mozilla/base/src/nsIStringStream.cpp b/mozilla/base/src/nsIStringStream.cpp index 33e661627c4..ea64e6596a4 100644 --- a/mozilla/base/src/nsIStringStream.cpp +++ b/mozilla/base/src/nsIStringStream.cpp @@ -14,9 +14,9 @@ class BasicStringImpl { public: BasicStringImpl() - : mResult(NS_OK) + : mOffset(0) + , mLastResult(NS_OK) , mEOF(PR_FALSE) - , mOffset(0) { NS_INIT_REFCNT(); } @@ -60,16 +60,16 @@ class BasicStringImpl NS_PRECONDITION(aReadCount != nsnull, "null ptr"); if (!aReadCount) return NS_ERROR_NULL_POINTER; - if (NS_FAILED(mResult)) - return mResult; + if (NS_FAILED(mLastResult)) + return mLastResult; PRInt32 bytesRead = read(aBuf, aCount); - if (NS_FAILED(mResult)) + if (NS_FAILED(mLastResult)) { *aReadCount = 0; - return mResult; + return mLastResult; } *aReadCount = bytesRead; - if (bytesRead < aCount) + if (bytesRead < (PRInt32)aCount) SetAtEOF(PR_TRUE); return NS_OK; } @@ -81,13 +81,13 @@ class BasicStringImpl NS_PRECONDITION(aBuf != nsnull, "null ptr"); NS_PRECONDITION(aWriteCount != nsnull, "null ptr"); - if (NS_FAILED(mResult)) - return mResult; + if (NS_FAILED(mLastResult)) + return mLastResult; PRInt32 bytesWrit = write(aBuf, aCount); - if (NS_FAILED(mResult)) + if (NS_FAILED(mLastResult)) { *aWriteCount = 0; - return mResult; + return mLastResult; } *aWriteCount = bytesWrit; return NS_OK; @@ -104,7 +104,7 @@ class BasicStringImpl public: - nsresult get_result() const { return mResult; } + nsresult get_result() const { return mLastResult; } protected: @@ -113,14 +113,14 @@ class BasicStringImpl virtual PRInt32 write(const char*, PRUint32) { 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; } protected: PRUint32 mOffset; - nsresult mResult; + nsresult mLastResult; PRBool mEOF; }; // class BasicStringImpl @@ -146,7 +146,7 @@ class ConstCharImpl virtual PRInt32 read(char* buf, PRUint32 aCount) { PRInt32 maxCount = mLength - mOffset; - if (aCount > maxCount) + if ((PRInt32)aCount > maxCount) aCount = maxCount; memcpy(buf, mConstString + mOffset, aCount); mOffset += aCount; @@ -181,7 +181,7 @@ class CharImpl mString = new char[mAllocLength]; if (!mString) { - mResult = NS_ERROR_OUT_OF_MEMORY; + mLastResult = NS_ERROR_OUT_OF_MEMORY; return; } mConstString = mString; @@ -193,17 +193,17 @@ class CharImpl virtual PRInt32 write(const char* buf, PRUint32 aCount) { PRInt32 maxCount = mAllocLength - 1 - mOffset; - if (aCount > maxCount) + if ((PRInt32)aCount > maxCount) { do { maxCount += kAllocQuantum; - } while (aCount > maxCount); + } while ((PRInt32)aCount > maxCount); mAllocLength = maxCount + 1 + mOffset; char* newString = new char[mAllocLength]; if (!newString) { - mResult = NS_ERROR_OUT_OF_MEMORY; + mLastResult = NS_ERROR_OUT_OF_MEMORY; return 0; } strcpy(newString, mString); @@ -220,8 +220,8 @@ class CharImpl protected: char*& mString; - size_t mOriginalLength; size_t mAllocLength; + size_t mOriginalLength; }; // class CharImpl @@ -271,7 +271,7 @@ class StringImpl chars.Seek(PR_SEEK_SET, mOffset); // Get the bytecount and result from the CharImpl PRInt32 result = chars.write(buf,count); - mResult = chars.get_result(); + mLastResult = chars.get_result(); // Set our string to match the new chars mString = cstring; // 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) //---------------------------------------------------------------------------------------- { - mResult = NS_OK; // reset on a seek. + mLastResult = NS_OK; // reset on a seek. mEOF = PR_FALSE; // reset on a seek. PRInt32 fileSize = length(); PRInt32 newPosition; @@ -344,7 +344,7 @@ NS_IMETHODIMP BasicStringImpl::Seek(PRSeekWhence whence, PRInt32 offset) if (newPosition < 0) { newPosition = 0; - mResult = NS_FILE_RESULT(PR_FILE_SEEK_ERROR); + mLastResult = NS_FILE_RESULT(PR_FILE_SEEK_ERROR); } if (newPosition >= fileSize) { diff --git a/mozilla/base/src/unix/nsFileSpecUnix.cpp b/mozilla/base/src/unix/nsFileSpecUnix.cpp index 5af2eb10a30..8b783a24d77 100644 --- a/mozilla/base/src/unix/nsFileSpecUnix.cpp +++ b/mozilla/base/src/unix/nsFileSpecUnix.cpp @@ -61,39 +61,38 @@ extern "C" int statvfs(const char *, struct statvfs *); #endif //---------------------------------------------------------------------------------------- -void nsFileSpecHelpers::Canonify(char*& ioPath, PRBool inMakeDirs) +void nsFileSpecHelpers::Canonify(nsSimpleCharString& ioPath, PRBool inMakeDirs) // Canonify, make absolute, and check whether directories exist //---------------------------------------------------------------------------------------- { - if (!ioPath) + if (ioPath.IsEmpty()) return; if (inMakeDirs) { const mode_t mode = 0700; - nsFileSpecHelpers::MakeAllDirectories(ioPath, mode); + nsFileSpecHelpers::MakeAllDirectories((const char*)ioPath, mode); } char buffer[MAXPATHLEN]; errno = 0; *buffer = '\0'; - char* canonicalPath = realpath(ioPath, buffer); + char* canonicalPath = realpath((const char*)ioPath, buffer); if (!canonicalPath) { // 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, // process that, and then add the leaf back. - char* allButLeaf = nsFileSpecHelpers::StringDup(ioPath); - if (!allButLeaf) + nsSimpleCharString allButLeaf(ioPath); + if (allButLeaf.IsEmpty()) return; - char* lastSeparator = strrchr(allButLeaf, '/'); + char* lastSeparator = strrchr((char*)allButLeaf, '/'); if (lastSeparator) { *lastSeparator = '\0'; - canonicalPath = realpath(allButLeaf, buffer); + canonicalPath = realpath((const char*)allButLeaf, buffer); strcat(buffer, "/"); // Add back the leaf strcat(buffer, ++lastSeparator); } - delete [] allButLeaf; } if (!canonicalPath && *ioPath != '/' && !inMakeDirs) { @@ -106,21 +105,21 @@ void nsFileSpecHelpers::Canonify(char*& ioPath, PRBool inMakeDirs) } } if (canonicalPath) - nsFileSpecHelpers::StringAssign(ioPath, canonicalPath); + ioPath = canonicalPath; } // nsFileSpecHelpers::Canonify //---------------------------------------------------------------------------------------- void nsFileSpec::SetLeafName(const char* inLeafName) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::LeafReplace(mPath, '/', inLeafName); + mPath.LeafReplace('/', inLeafName); } // nsFileSpec::SetLeafName //---------------------------------------------------------------------------------------- char* nsFileSpec::GetLeafName() const //---------------------------------------------------------------------------------------- { - return nsFileSpecHelpers::GetLeaf(mPath, '/'); + return mPath.GetLeaf('/'); } // nsFileSpec::GetLeafName //---------------------------------------------------------------------------------------- @@ -172,9 +171,9 @@ PRBool nsFileSpec::IsDirectory() const void nsFileSpec::GetParent(nsFileSpec& outSpec) const //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(outSpec.mPath, mPath); + outSpec.mPath = mPath; char* cp = strrchr(outSpec.mPath, '/'); - if (cp) + if (cp++) *cp = '\0'; } // nsFileSpec::GetParent @@ -182,14 +181,14 @@ void nsFileSpec::GetParent(nsFileSpec& outSpec) const void nsFileSpec::operator += (const char* inRelativePath) //---------------------------------------------------------------------------------------- { - if (!inRelativePath || !mPath) + if (!inRelativePath || mPath.IsEmpty()) return; char endChar = mPath[strlen(mPath) - 1]; if (endChar == '/') - nsFileSpecHelpers::ReallocCat(mPath, "x"); + mPath += "x"; else - nsFileSpecHelpers::ReallocCat(mPath, "/x"); + mPath += "/x"; SetLeafName(inRelativePath); } // nsFileSpec::operator += @@ -304,16 +303,11 @@ nsresult nsFileSpec::Copy(const nsFileSpec& inParentDirectory) const if (inParentDirectory.IsDirectory() && (! IsDirectory() ) ) { char *leafname = GetLeafName(); - char* destPath = nsFileSpecHelpers::StringDup( - inParentDirectory.GetCString(), - strlen(inParentDirectory.GetCString()) + 1 + strlen(leafname)); - strcat(destPath, "/"); - strcat(destPath, leafname); + nsSimpleCharString destPath(inParentDirectory.GetCString()); + destPath += "/"; + destPath += leafname; delete [] leafname; - result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), destPath)); - - delete [] destPath; } return result; } // 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 nsresult result = NS_FILE_FAILURE; - if (inNewParentDirectory.IsDirectory() && (! IsDirectory() ) ) + if (inNewParentDirectory.IsDirectory() && !IsDirectory()) { char *leafname = GetLeafName(); - char* destPath - = nsFileSpecHelpers::StringDup( - inNewParentDirectory.GetCString(), - strlen(inNewParentDirectory.GetCString()) + 1 + strlen(leafname)); - strcat(destPath, "/"); - strcat(destPath, leafname); + nsSimpleCharString destPath(inNewParentDirectory.GetCString()); + destPath += "/"; + destPath += leafname; delete [] leafname; - result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), destPath)); + result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), (const char*)destPath)); if (result == NS_OK) - { - // cast to fix const-ness - ((nsFileSpec*)this)->Delete(PR_FALSE); - } - delete [] destPath; + { + // cast to fix const-ness + ((nsFileSpec*)this)->Delete(PR_FALSE); + } } return result; } @@ -355,13 +345,8 @@ nsresult nsFileSpec::Execute(const char* inArgs ) const if (! IsDirectory()) { - char* fileNameWithArgs - = nsFileSpecHelpers::StringDup(mPath, strlen(mPath) + 1 + strlen(inArgs)); - strcat(fileNameWithArgs, " "); - strcat(fileNameWithArgs, inArgs); - + nsSimpleCharString fileNameWithArgs = mPath + " " + inArgs; result = NS_FILE_RESULT(system(fileNameWithArgs)); - delete [] fileNameWithArgs; } return result; @@ -373,14 +358,14 @@ PRUint32 nsFileSpec::GetDiskSpaceAvailable() const //---------------------------------------------------------------------------------------- { char curdir [MAXPATHLEN]; - if (!mPath || !*mPath) + if (mPath.IsEmpty()) { (void) getcwd(curdir, MAXPATHLEN); if (!curdir) return ULONG_MAX; /* hope for the best as we did in cheddar */ } else - sprintf(curdir, "%.200s", mPath); + sprintf(curdir, "%.200s", (const char*)mPath); struct STATFS fs_buf; if (STATFS(curdir, &fs_buf) < 0) diff --git a/mozilla/base/src/windows/nsFileSpecWin.cpp b/mozilla/base/src/windows/nsFileSpecWin.cpp index 9feda5cb815..b95de83b0eb 100644 --- a/mozilla/base/src/windows/nsFileSpecWin.cpp +++ b/mozilla/base/src/windows/nsFileSpecWin.cpp @@ -35,37 +35,36 @@ #endif //---------------------------------------------------------------------------------------- -void nsFileSpecHelpers::Canonify(char*& ioPath, PRBool inMakeDirs) +void nsFileSpecHelpers::Canonify(nsSimpleCharString& ioPath, PRBool inMakeDirs) // Canonify, make absolute, and check whether directories exist. This // takes a (possibly relative) native path and converts it into a // fully qualified native path. //---------------------------------------------------------------------------------------- { - if (!ioPath) - return; + if (ioPath.IsEmpty()) + return; - if (inMakeDirs) - { - const int mode = 0700; - char* unixStylePath = nsFileSpecHelpers::StringDup(ioPath); - nsFileSpecHelpers::NativeToUnix(unixStylePath); - nsFileSpecHelpers::MakeAllDirectories(unixStylePath, mode); - delete[] unixStylePath; - } - char buffer[_MAX_PATH]; - errno = 0; - *buffer = '\0'; - char* canonicalPath = _fullpath(buffer, ioPath, _MAX_PATH); + if (inMakeDirs) + { + const int mode = 0700; + nsSimpleCharString unixStylePath = ioPath; + nsFileSpecHelpers::NativeToUnix(unixStylePath); + nsFileSpecHelpers::MakeAllDirectories((const char*)unixStylePath, mode); + } + char buffer[_MAX_PATH]; + errno = 0; + *buffer = '\0'; + char* canonicalPath = _fullpath(buffer, ioPath, _MAX_PATH); - NS_ASSERTION( canonicalPath[0] != '\0', "Uh oh...couldn't convert" ); - if (canonicalPath[0] == '\0') - return; + NS_ASSERTION( canonicalPath[0] != '\0', "Uh oh...couldn't convert" ); + if (canonicalPath[0] == '\0') + 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 // anything //---------------------------------------------------------------------------------------- @@ -73,55 +72,49 @@ void nsFileSpecHelpers::UnixToNative(char*& ioPath) // Allow for relative or absolute. We can do this in place, because the // native path is never longer. - if (!ioPath || !*ioPath) + if (ioPath.IsEmpty()) return; - char* src = ioPath; - if (*ioPath == '/') - { - // Strip initial slash for an absolute path - src++; - } - - // Convert the vertical slash to a colon - char* cp = src + 1; - - // If it was an absolute path, check for the drive letter - if (*ioPath == '/' && strstr(cp, "|/") == cp) - *cp = ':'; - - // Convert '/' to '\'. - while (*++cp) - { - if (*cp == '/') - *cp = '\\'; - } + // Strip initial slash for an absolute path + 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; + } - if (*ioPath == '/') { - for (cp = ioPath; *cp; ++cp) - *cp = *(cp + 1); - } -} + src = (char*)ioPath; + + // Convert '/' to '\'. + while (*++src) + { + if (*src == '/') + *src = '\\'; + } +} // nsFileSpecHelpers::UnixToNative //---------------------------------------------------------------------------------------- -void nsFileSpecHelpers::NativeToUnix(char*& ioPath) +void nsFileSpecHelpers::NativeToUnix(nsSimpleCharString& ioPath) // 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. //---------------------------------------------------------------------------------------- { - if (!ioPath || !*ioPath) + if (ioPath.IsEmpty()) return; // Convert the drive-letter separator, if present - char* temp = nsFileSpecHelpers::StringDup("/", 1 + strlen(ioPath)); + nsSimpleCharString temp("/"); - char* cp = ioPath + 1; - if (strstr(cp, ":\\") == cp) { + char* cp = (char*)ioPath + 1; + if (strstr(cp, ":\\") == cp) *cp = '|'; // absolute path - } - else { - *temp = '\0'; // relative path - } + else + temp[0] = '\0'; // relative path // Convert '\' to '/' for (; *cp; cp++) @@ -129,17 +122,14 @@ void nsFileSpecHelpers::NativeToUnix(char*& ioPath) if (*cp == '\\') *cp = '/'; } - // Add the slash in front. - strcat(temp, ioPath); - StringAssign(ioPath, temp); - delete [] temp; + temp += ioPath; + ioPath = temp; } //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsFilePath& inPath) //---------------------------------------------------------------------------------------- -: mPath(NULL) { *this = inPath; } @@ -148,7 +138,7 @@ nsFileSpec::nsFileSpec(const nsFilePath& inPath) void nsFileSpec::operator = (const nsFilePath& inPath) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, (const char*)inPath); + mPath = (const char*)inPath; nsFileSpecHelpers::UnixToNative(mPath); mError = NS_OK; } // nsFileSpec::operator = @@ -156,7 +146,6 @@ void nsFileSpec::operator = (const nsFilePath& inPath) //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- -: mPath(NULL) { *this = inSpec; } // nsFilePath::nsFilePath @@ -165,7 +154,7 @@ nsFilePath::nsFilePath(const nsFileSpec& inSpec) void nsFilePath::operator = (const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, inSpec.mPath); + mPath = inSpec.mPath; nsFileSpecHelpers::NativeToUnix(mPath); } // nsFilePath::operator = @@ -173,14 +162,14 @@ void nsFilePath::operator = (const nsFileSpec& inSpec) void nsFileSpec::SetLeafName(const char* inLeafName) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::LeafReplace(mPath, '\\', inLeafName); + mPath.LeafReplace('\\', inLeafName); } // nsFileSpec::SetLeafName //---------------------------------------------------------------------------------------- char* nsFileSpec::GetLeafName() const //---------------------------------------------------------------------------------------- { - return nsFileSpecHelpers::GetLeaf(mPath, '\\'); + return mPath.GetLeaf('\\'); } // nsFileSpec::GetLeafName //---------------------------------------------------------------------------------------- @@ -232,9 +221,9 @@ PRBool nsFileSpec::IsDirectory() const void nsFileSpec::GetParent(nsFileSpec& outSpec) const //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(outSpec.mPath, mPath); + outSpec.mPath = mPath; char* cp = strrchr(outSpec.mPath, '\\'); - if (cp) + if (cp++) *cp = '\0'; } // nsFileSpec::GetParent @@ -242,19 +231,18 @@ void nsFileSpec::GetParent(nsFileSpec& outSpec) const void nsFileSpec::operator += (const char* inRelativePath) //---------------------------------------------------------------------------------------- { - if (!inRelativePath || !mPath) + if (!inRelativePath || mPath.IsEmpty()) return; - if (mPath[strlen(mPath) - 1] == '\\') - nsFileSpecHelpers::ReallocCat(mPath, "x"); + if (mPath[mPath.Length() - 1] == '\\') + mPath += "x"; else - nsFileSpecHelpers::ReallocCat(mPath, "\\x"); + mPath += "\\x"; // If it's a (unix) relative path, make it native - char* dosPath = nsFileSpecHelpers::StringDup(inRelativePath); + nsSimpleCharString dosPath = inRelativePath; nsFileSpecHelpers::UnixToNative(dosPath); SetLeafName(dosPath); - delete [] dosPath; } // nsFileSpec::operator += //---------------------------------------------------------------------------------------- @@ -309,80 +297,54 @@ nsresult nsFileSpec::Copy(const nsFileSpec& inParentDirectory) const //---------------------------------------------------------------------------------------- { // We can only copy into a directory, and (for now) can not copy entire directories - if (inParentDirectory.IsDirectory() && (! IsDirectory() ) ) { char *leafname = GetLeafName(); - char* destPath = nsFileSpecHelpers::StringDup(inParentDirectory, ( strlen(inParentDirectory) + 1 + strlen(leafname) ) ); - strcat(destPath, "\\"); - strcat(destPath, leafname); + nsSimpleCharString destPath(inParentDirectory.GetCString()); + destPath += "\\"; + destPath += leafname; delete [] leafname; // CopyFile returns non-zero if succeeds - int copyOK = CopyFile(*this, destPath, true); - - delete[] destPath; - + int copyOK = CopyFile(GetCString(), destPath, true); if (copyOK) - { return NS_OK; - } } - return NS_FILE_FAILURE; } // 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 - - if (nsNewParentDirectory.IsDirectory() && (! IsDirectory() ) ) + if (inNewParentDirectory.IsDirectory() && (! IsDirectory() ) ) { char *leafname = GetLeafName(); - char *destPath = nsFileSpecHelpers::StringDup(nsNewParentDirectory, ( strlen(nsNewParentDirectory) + 1 + strlen(leafname) )); - strcat(destPath, "\\"); - strcat(destPath, leafname); + nsSimpleCharString destPath(inNewParentDirectory.GetCString()); + destPath += "\\"; + destPath += leafname; delete [] leafname; // MoveFile returns non-zero if succeeds - int copyOK = MoveFile(*this, destPath); - - delete [] destPath; - + int copyOK = MoveFile(GetCString(), destPath); if (copyOK) - { return NS_OK; - } } - return NS_FILE_FAILURE; } // nsFileSpec::Move //---------------------------------------------------------------------------------------- nsresult nsFileSpec::Execute(const char* inArgs ) const //---------------------------------------------------------------------------------------- -{ - - if (! IsDirectory()) +{ + if (!IsDirectory()) { - char* fileNameWithArgs = NULL; - - fileNameWithArgs = nsFileSpecHelpers::StringDup(mPath, ( strlen(mPath) + 1 + strlen(inArgs) ) ); - strcat(fileNameWithArgs, " "); - strcat(fileNameWithArgs, inArgs); - - int execResult = WinExec( fileNameWithArgs, SW_NORMAL ); - - delete [] fileNameWithArgs; - + nsSimpleCharString fileNameWithArgs = mPath + " " + inArgs; + int execResult = WinExec( fileNameWithArgs, SW_NORMAL ); if (execResult > 31) - { return NS_OK; - } } - return NS_FILE_FAILURE; } // nsFileSpec::Execute @@ -391,13 +353,13 @@ PRUint32 nsFileSpec::GetDiskSpaceAvailable() const //---------------------------------------------------------------------------------------- { char aDrive[_MAX_DRIVE + 2]; - _splitpath( mPath, aDrive, NULL, NULL, NULL); + _splitpath( (const char*)mPath, aDrive, NULL, NULL, NULL); if (aDrive[0] == '\0') { // The back end is always trying to pass us paths that look // 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[1] = ':'; diff --git a/mozilla/base/tests/FilesTest.cpp b/mozilla/base/tests/FilesTest.cpp index 4bcdaaef8d4..f453861aa96 100644 --- a/mozilla/base/tests/FilesTest.cpp +++ b/mozilla/base/tests/FilesTest.cpp @@ -345,11 +345,14 @@ int FilesTest::Parent( mySpec.GetParent(outParent); nsFilePath parentPath(outParent); + nsFileURL url(parentPath); mConsole << "GetParent() on " << "\n\t" << pathAsString << "\n yields " << "\n\t" << (const char*)parentPath + << "\n or as a URL" + << "\n\t" << (const char*)url << nsEndl; Inspect(); return 0; diff --git a/mozilla/xpcom/io/nsFileSpec.cpp b/mozilla/xpcom/io/nsFileSpec.cpp index e20a931914c..3149de1f1f8 100644 --- a/mozilla/xpcom/io/nsFileSpec.cpp +++ b/mozilla/xpcom/io/nsFileSpec.cpp @@ -30,6 +30,219 @@ #include #include +//======================================================================================== +// 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 //======================================================================================== @@ -39,22 +252,13 @@ NS_NAMESPACE nsFileSpecHelpers , kMaxAltDigitLength = 5 , kMaxCoreLeafNameLength = (kMaxFilenameLength - (kMaxAltDigitLength + 1)) }; - NS_NAMESPACE_PROTOTYPE void LeafReplace( - char*& ioPath, - char inSeparator, - const char* inLeafName); #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); #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 - NS_NAMESPACE_PROTOTYPE void NativeToUnix(char*& ioPath); - NS_NAMESPACE_PROTOTYPE void UnixToNative(char*& ioPath); + NS_NAMESPACE_PROTOTYPE void NativeToUnix(nsSimpleCharString& ioPath); + NS_NAMESPACE_PROTOTYPE void UnixToNative(nsSimpleCharString& ioPath); #endif } NS_NAMESPACE_END @@ -68,136 +272,80 @@ nsresult ns_file_convert_result(PRInt32 nativeErr) } //---------------------------------------------------------------------------------------- -char* nsFileSpecHelpers::StringDup( - 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) +void nsSimpleCharString::LeafReplace(char inSeparator, const char* inLeafName) //---------------------------------------------------------------------------------------- { // Find the existing leaf name - if (!ioPath) + if (IsEmpty()) return; if (!inLeafName) { - *ioPath = '\0'; + SetToEmpty(); return; } - char* lastSeparator = strrchr(ioPath, inSeparator); - int oldLength = strlen(ioPath); - PRBool trailingSeparator = (lastSeparator + 1 == ioPath + oldLength); + char* chars = mData->mString; + char* lastSeparator = strrchr(chars, inSeparator); + int oldLength = Length(); + PRBool trailingSeparator = (lastSeparator + 1 == chars + oldLength); if (trailingSeparator) { *lastSeparator = '\0'; - lastSeparator = strrchr(ioPath, inSeparator); + lastSeparator = strrchr(chars, inSeparator); } if (lastSeparator) lastSeparator++; // point at the trailing string else - lastSeparator = ioPath; // the full monty + lastSeparator = chars; // the full monty *lastSeparator = '\0'; // strip the current leaf name - int newLength = (lastSeparator - ioPath) + strlen(inLeafName) + int(trailingSeparator); - if (newLength > oldLength) - { - char* newPath = StringDup(ioPath, newLength + 1); - delete [] ioPath; - ioPath = newPath; - } - strcat(ioPath, inLeafName); + int newLength = (lastSeparator - chars) + strlen(inLeafName) + int(trailingSeparator); + ReallocData(newLength); + chars = mData->mString; // it might have moved. + + strcat(chars, inLeafName); if (trailingSeparator) { // If the original ended in a slash, then the new one should, too. char sepStr[2] = "/"; *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. //---------------------------------------------------------------------------------------- { - if (!inPath) + if (IsEmpty()) return nsnull; - const char* lastSeparator = strrchr(inPath, inSeparator); + + char* chars = mData->mString; + const char* lastSeparator = strrchr(chars, inSeparator); - // If there was no separator, then return a copy of the caller's path. + // If there was no separator, then return a copy of our path. if (!lastSeparator) - return StringDup(inPath); + return PL_strdup(*this); // So there's at least one separator. What's just after it? // If the separator was not the last character, return the trailing string. const char* leafPointer = lastSeparator + 1; if (*leafPointer) - return StringDup(leafPointer); + return PL_strdup(leafPointer); // So now, separator was the last character. Poke in a null instead. *(char*)lastSeparator = '\0'; // Should use const_cast, but Unix has old compiler. - leafPointer = strrchr(inPath, inSeparator); - char* result = leafPointer ? StringDup(++leafPointer) : StringDup(inPath); + leafPointer = strrchr(chars, inSeparator); + char* result = leafPointer ? PL_strdup(++leafPointer) : PL_strdup(chars); // Restore the poked null before returning. *(char*)lastSeparator = inSeparator; #ifdef XP_PC // 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] = ':'; #endif return result; -} // nsFileSpecHelpers::GetLeaf +} // nsSimpleCharString::GetLeaf #if defined(XP_UNIX) || defined(XP_PC) @@ -210,7 +358,7 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode) if (!inPath) return; - char* pathCopy = nsFileSpecHelpers::StringDup( inPath ); + char* pathCopy = PL_strdup( inPath ); if (!pathCopy) return; @@ -227,7 +375,6 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode) if (currentEnd) { nsFileSpec spec; - *currentEnd = '\0'; #ifdef XP_PC @@ -237,12 +384,13 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode) */ 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, "/"); spec = nsFilePath(startDir, PR_FALSE); - delete [] startDir; + PR_Free(startDir); } else { @@ -278,16 +426,6 @@ void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode) #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) #include "windows/nsFileSpecWin.cpp" // Windows-specific implementations #elif defined(XP_MAC) @@ -304,7 +442,6 @@ char* nsFileSpecHelpers::ReallocCat(char*& ioString, const char* inString1) //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mURL(nsnull) { if (!inString) return; @@ -319,7 +456,6 @@ nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mURL(nsnull) { const nsAutoCString aString(inString); const char* aCString = (const char*) aString; @@ -335,7 +471,7 @@ nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFileURL& inOther) //---------------------------------------------------------------------------------------- -: mURL(nsFileSpecHelpers::StringDup(inOther.mURL)) +: mURL(inOther.mURL) #ifdef XP_MAC , mFileSpec(inOther.GetFileSpec()) #endif @@ -346,7 +482,6 @@ nsFileURL::nsFileURL(const nsFileURL& inOther) //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFilePath& inOther) //---------------------------------------------------------------------------------------- -: mURL(nsnull) { *this = inOther; } // nsFileURL::nsFileURL @@ -355,7 +490,6 @@ nsFileURL::nsFileURL(const nsFilePath& inOther) #ifndef XP_MAC //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFileSpec& inOther) -: mURL(nsnull) //---------------------------------------------------------------------------------------- { *this = inOther; @@ -366,7 +500,6 @@ nsFileURL::nsFileURL(const nsFileSpec& inOther) nsFileURL::~nsFileURL() //---------------------------------------------------------------------------------------- { - delete [] mURL; } #ifndef XP_MAC @@ -374,16 +507,37 @@ nsFileURL::~nsFileURL() void nsFileURL::operator = (const char* inString) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mURL, inString); + mURL = inString; NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!"); } // nsFileURL::operator = #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) //---------------------------------------------------------------------------------------- { - mURL = nsFileSpecHelpers::StringAssign(mURL, inOther.mURL); + mURL = inOther.mURL; #ifdef XP_MAC mFileSpec = inOther.GetFileSpec(); #endif @@ -394,7 +548,7 @@ void nsFileURL::operator = (const nsFileURL& inOther) void nsFileURL::operator = (const nsFilePath& inOther) //---------------------------------------------------------------------------------------- { - delete [] mURL; + mURL = kFileURLPrefix; char* original = (char*)(const char*)inOther; // we shall modify, but restore. #ifdef XP_PC // 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); #endif if (escapedPath) - mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, escapedPath); + mURL += escapedPath; delete [] escapedPath; } // nsFileURL::operator = #endif @@ -418,6 +572,8 @@ void nsFileURL::operator = (const nsFileSpec& inOther) //---------------------------------------------------------------------------------------- { *this = nsFilePath(inOther); + if (mURL[mURL.Length() - 1] != '/' && inOther.IsDirectory()) + mURL += "/"; } // nsFileURL::operator = #endif @@ -433,7 +589,7 @@ nsOutputStream& operator << (nsOutputStream& s, const nsFileURL& url) //======================================================================================== nsFilePath::nsFilePath(const nsFilePath& inPath) - : mPath(nsFileSpecHelpers::StringDup(inPath.mPath)) + : mPath(inPath.mPath) #ifdef XP_MAC , mFileSpec(inPath.mFileSpec) #endif @@ -444,7 +600,7 @@ nsFilePath::nsFilePath(const nsFilePath& inPath) //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mPath(nsFileSpecHelpers::StringDup(inString)) +: mPath(inString) { 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) //---------------------------------------------------------------------------------------- -: mPath(inString.ToNewCString()) +: mPath(inString) { 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) //---------------------------------------------------------------------------------------- -: mPath(nsFileSpecHelpers::StringDup(inOther.mURL + kFileURLPrefixLength)) { + mPath = (const char*)inOther.mURL + kFileURLPrefixLength; + mPath.Unescape(); } #endif @@ -493,7 +650,7 @@ nsFilePath::nsFilePath(const nsFileURL& inOther) //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const nsFileSpec& inOther) //---------------------------------------------------------------------------------------- -: mPath(nsFileSpecHelpers::StringDup(inOther.mPath)) +: mPath(inOther.mPath) { } #endif // XP_UNIX @@ -502,7 +659,6 @@ nsFilePath::nsFilePath(const nsFileSpec& inOther) nsFilePath::~nsFilePath() //---------------------------------------------------------------------------------------- { - delete [] mPath; } #ifdef XP_UNIX @@ -510,7 +666,7 @@ nsFilePath::~nsFilePath() void nsFilePath::operator = (const nsFileSpec& inOther) //---------------------------------------------------------------------------------------- { - mPath = nsFileSpecHelpers::StringAssign(mPath, inOther.mPath); + mPath = inOther.mPath; } #endif // XP_UNIX @@ -521,9 +677,9 @@ void nsFilePath::operator = (const char* inString) NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path"); #ifdef XP_MAC mFileSpec = inString; - nsFileSpecHelpers::StringAssign(mPath, (const char*)nsFilePath(mFileSpec)); + mPath = (const char*)nsFilePath(mFileSpec); #else - nsFileSpecHelpers::StringAssign(mPath, inString); + mPath = inString; #ifdef XP_PC nsFileSpecHelpers::UnixToNative(mPath); #endif @@ -540,7 +696,7 @@ void nsFilePath::operator = (const char* inString) void nsFilePath::operator = (const nsFileURL& inOther) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, (const char*)nsFilePath(inOther)); + mPath = (const char*)nsFilePath(inOther); } #endif @@ -548,12 +704,33 @@ void nsFilePath::operator = (const nsFileURL& inOther) void nsFilePath::operator = (const nsFilePath& inOther) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, inOther.mPath); + mPath = inOther.mPath; #ifdef XP_MAC mFileSpec = inOther.GetFileSpec(); #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 //======================================================================================== @@ -562,8 +739,7 @@ void nsFilePath::operator = (const nsFilePath& inOther) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec() //---------------------------------------------------------------------------------------- -: mPath(nsnull) -, mError(NS_OK) +: mError(NS_OK) { } #endif @@ -571,7 +747,6 @@ nsFileSpec::nsFileSpec() //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsPersistentFileDescriptor& inDescriptor) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { *this = inDescriptor; } @@ -579,7 +754,6 @@ nsFileSpec::nsFileSpec(const nsPersistentFileDescriptor& inDescriptor) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsFileURL& inURL) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { *this = nsFilePath(inURL); // convert to unix path first } @@ -609,7 +783,7 @@ void nsFileSpec::MakeUnique() char* suffix = ""; if (lastDot) { - suffix = nsFileSpecHelpers::StringDup(lastDot); // include '.' + suffix = PL_strdup(lastDot); // include '.' *lastDot = '\0'; // strip suffix and dot. } const int kMaxRootLength @@ -640,7 +814,7 @@ void nsFileSpec::operator = (const nsPersistentFileDescriptor& inDescriptor) //---------------------------------------------------------------------------------------- { - void* data; + nsSimpleCharString data; PRInt32 dataSize; inDescriptor.GetData(data, dataSize); @@ -656,9 +830,9 @@ void nsFileSpec::operator = (const nsPersistentFileDescriptor& inDescriptor) Boolean changed; mError = NS_FILE_RESULT(::ResolveAlias(nsnull, aliasH, &mSpec, &changed)); DisposeHandle((Handle) aliasH); - delete [] mPath; + mPath.SetToEmpty(); #else - nsFileSpecHelpers::StringAssign(mPath, (char*)data); + mPath = data; mError = NS_OK; #endif } @@ -671,7 +845,7 @@ void nsFileSpec::operator = (const nsPersistentFileDescriptor& inDescriptor) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsFilePath& inPath) //---------------------------------------------------------------------------------------- -: mPath(nsFileSpecHelpers::StringDup((const char*)inPath)) +: mPath((const char*)inPath) , mError(NS_OK) { } @@ -682,7 +856,7 @@ nsFileSpec::nsFileSpec(const nsFilePath& inPath) void nsFileSpec::operator = (const nsFilePath& inPath) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, (const char*)inPath); + mPath = (const char*)inPath; mError = NS_OK; } #endif //XP_UNIX @@ -691,7 +865,7 @@ void nsFileSpec::operator = (const nsFilePath& inPath) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- -: mPath(nsFileSpecHelpers::StringDup(inSpec.mPath)) +: mPath(inSpec.mPath) , mError(NS_OK) { } @@ -701,7 +875,7 @@ nsFileSpec::nsFileSpec(const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mPath(nsFileSpecHelpers::StringDup(inString)) +: mPath(inString) , mError(NS_OK) { // Make canonical and absolute. @@ -713,7 +887,7 @@ nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mPath(inString.ToNewCString()) +: mPath(inString) , mError(NS_OK) { // Make canonical and absolute. @@ -725,7 +899,6 @@ nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs) nsFileSpec::~nsFileSpec() //---------------------------------------------------------------------------------------- { - delete [] mPath; } #if defined(XP_UNIX) || defined(XP_PC) @@ -733,7 +906,7 @@ nsFileSpec::~nsFileSpec() void nsFileSpec::operator = (const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- { - mPath = nsFileSpecHelpers::StringAssign(mPath, inSpec.mPath); + mPath = inSpec.mPath; mError = inSpec.Error(); } #endif //XP_UNIX @@ -744,7 +917,7 @@ void nsFileSpec::operator = (const nsFileSpec& inSpec) void nsFileSpec::operator = (const char* inString) //---------------------------------------------------------------------------------------- { - mPath = nsFileSpecHelpers::StringAssign(mPath, inString); + mPath = inString; // Make canonical and absolute. nsFileSpecHelpers::Canonify(mPath, PR_FALSE /* XXX? */); mError = NS_OK; @@ -788,8 +961,8 @@ PRBool nsFileSpec::operator == (const nsFileSpec& inOther) const EqualString(inOther.mSpec.name, mSpec.name, false, true)) return PR_TRUE; #else - PRBool amEmpty = !mPath || !*mPath; - PRBool heEmpty = !inOther.mPath || !*inOther.mPath; + PRBool amEmpty = mPath.IsEmpty(); + PRBool heEmpty = inOther.mPath.IsEmpty(); if (amEmpty) // we're the same if he's empty... return heEmpty; if (heEmpty) // ('cuz I'm not...) @@ -835,7 +1008,7 @@ const char* nsFileSpec::GetCString() const //---------------------------------------------------------------------------------------- nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inDesc) //---------------------------------------------------------------------------------------- - : mDescriptorString(nsFileSpecHelpers::StringDup(inDesc.mDescriptorString)) + : mDescriptorString(inDesc.mDescriptorString) { } // nsPersistentFileDescriptor::nsPersistentFileDescriptor @@ -843,13 +1016,12 @@ nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsPersistentFileDes void nsPersistentFileDescriptor::operator = (const nsPersistentFileDescriptor& inDesc) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mDescriptorString, inDesc.mDescriptorString); + mDescriptorString = inDesc.mDescriptorString; } // nsPersistentFileDescriptor::operator = //---------------------------------------------------------------------------------------- nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- - : mDescriptorString(nsnull) { *this = inSpec; } // nsPersistentFileDescriptor::nsPersistentFileDescriptor @@ -871,10 +1043,10 @@ void nsPersistentFileDescriptor::operator = (const nsFileSpec& inSpec) char* buf = PL_Base64Encode((const char*)*aliasH, bytes, nsnull); DisposeHandle((Handle) aliasH); - nsFileSpecHelpers::StringAssign(mDescriptorString, buf); + mDescriptorString = buf; PR_Free(buf); #else - nsFileSpecHelpers::StringAssign(mDescriptorString, inSpec.GetCString()); + mDescriptorString = inSpec.GetCString(); #endif // XP_MAC } // nsPersistentFileDescriptor::operator = @@ -882,27 +1054,21 @@ void nsPersistentFileDescriptor::operator = (const nsFileSpec& inSpec) nsPersistentFileDescriptor::~nsPersistentFileDescriptor() //---------------------------------------------------------------------------------------- { - delete [] mDescriptorString; } // 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; } //---------------------------------------------------------------------------------------- -void nsPersistentFileDescriptor::SetData(const void* inData, PRInt32 inSize) +void nsPersistentFileDescriptor::SetData(const nsSimpleCharString& inData, PRInt32 inSize) //---------------------------------------------------------------------------------------- { - delete [] mDescriptorString; - mDescriptorString = new char[1 + inSize]; - if (!mDescriptorString) - return; - memcpy(mDescriptorString, inData, inSize); - mDescriptorString[inSize] = '\0'; + mDescriptorString.CopyFrom((const char*)inData, inSize); } #define MAX_PERSISTENT_DATA_SIZE 1000 @@ -937,7 +1103,7 @@ nsInputStream& operator >> (nsInputStream& s, nsPersistentFileDescriptor& d) if (bytesRead != 8) return s; bigBuffer[8] = '\0'; - sscanf(bigBuffer, "%lx", (PRUint32*)&bytesRead); + sscanf(bigBuffer, "%x", (PRUint32*)&bytesRead); if (bytesRead > MAX_PERSISTENT_DATA_SIZE) return s; // preposterous. // Now we know how many bytes to read, do it. @@ -953,13 +1119,13 @@ nsOutputStream& operator << (nsOutputStream& s, const nsPersistentFileDescriptor { char littleBuf[9]; PRInt32 dataSize; - void* data; + nsSimpleCharString data; d.GetData(data, dataSize); // First write (in hex) the length of the data to follow. Exactly 8 bytes sprintf(littleBuf, "%0.8x", dataSize); s << littleBuf; // Now write the data itself - s << d.mDescriptorString; + s << (const char*)data; return s; } diff --git a/mozilla/xpcom/io/nsFileSpec.h b/mozilla/xpcom/io/nsFileSpec.h index 9710ae3f38a..626ce71c268 100644 --- a/mozilla/xpcom/io/nsFileSpec.h +++ b/mozilla/xpcom/io/nsFileSpec.h @@ -206,6 +206,74 @@ protected: const char* mCString; }; // 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 // This is whatever each platform really prefers to describe files as. Declared first @@ -255,7 +323,7 @@ class NS_BASE nsFileSpec long parID, ConstStr255Param name); nsFileSpec(const FSSpec& inSpec) - : mSpec(inSpec), mError(NS_OK), mPath(nsnull) {} + : mSpec(inSpec), mError(NS_OK) {} void operator = (const FSSpec& inSpec) { mSpec = inSpec; mError = NS_OK; } @@ -284,7 +352,7 @@ class NS_BASE nsFileSpec nsresult Error() const { #ifndef XP_MAC - if (!mPath && NS_SUCCEEDED(mError)) + if (mPath.IsEmpty() && NS_SUCCEEDED(mError)) ((nsFileSpec*)this)->mError = NS_FILE_FAILURE; #endif return mError; @@ -413,7 +481,7 @@ class NS_BASE nsFileSpec #ifdef XP_MAC FSSpec mSpec; #endif - char* mPath; + nsSimpleCharString mPath; nsresult mError; }; // class nsFileSpec @@ -450,8 +518,10 @@ class NS_BASE nsFileURL void operator = (const nsFilePath& inOther); void operator = (const nsFileSpec& inOther); - operator const char* () const { return mURL; } // deprecated. - const char* GetAsString() const { return mURL; } + void operator +=(const char* inRelativeUnixPath); + 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 << ( nsOutputStream& s, const nsFileURL& spec); @@ -460,12 +530,11 @@ class NS_BASE nsFileURL // Accessor to allow quick assignment to a mFileSpec const nsFileSpec& GetFileSpec() const { return mFileSpec; } #endif - private: - // Should not be defined (only nsFilePath is to be treated as strings. - operator char* (); + protected: friend class nsFilePath; // to allow construction of nsFilePath - char* mURL; + nsSimpleCharString mURL; + #ifdef XP_MAC // Since the path on the macintosh does not uniquely specify a file (volumes // 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* // that is provided, and it allows the // 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 char* inString); @@ -508,6 +573,9 @@ class NS_BASE nsFilePath void operator = (const nsFileURL& inURL); void operator = (const nsFileSpec& inOther); + void operator +=(const char* inRelativeUnixPath); + nsFilePath operator +(const char* inRelativeUnixPath) const; + #ifdef XP_MAC public: // Accessor to allow quick assignment to a mFileSpec @@ -516,7 +584,7 @@ class NS_BASE nsFilePath private: - char* mPath; + nsSimpleCharString mPath; #ifdef XP_MAC // Since the path on the macintosh does not uniquely specify a file (volumes // can have the same name), stash the secret nsFileSpec, too. @@ -533,7 +601,7 @@ class NS_BASE nsPersistentFileDescriptor //======================================================================================== { public: - nsPersistentFileDescriptor() : mDescriptorString(nsnull) {} + nsPersistentFileDescriptor() {} // For use prior to reading in from a stream nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inPath); virtual ~nsPersistentFileDescriptor(); @@ -553,14 +621,13 @@ class NS_BASE nsPersistentFileDescriptor friend class nsFileSpec; private: - // Here are the ways to get data in and out of a file. - void GetData(void*& outData, PRInt32& outSize) const; - // DON'T FREE the returned data! - void SetData(const void* inData, PRInt32 inSize); + + void GetData(nsSimpleCharString& outData, PRInt32& outSize) const; + void SetData(const nsSimpleCharString& inData, PRInt32 inSize); protected: - char* mDescriptorString; + nsSimpleCharString mDescriptorString; }; // class nsPersistentFileDescriptor @@ -569,16 +636,16 @@ class NS_BASE nsDirectoryIterator // Example: // // 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: // -// 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 @@ -599,6 +666,9 @@ class NS_BASE nsDirectoryIterator nsDirectoryIterator& operator --(); // moves to the previous item, if any. nsDirectoryIterator& operator --(int) { return --(*this); } // post-decrement. operator nsFileSpec&() { return mCurrent; } + + nsFileSpec& Spec() { return mCurrent; } + private: nsFileSpec mCurrent; PRBool mExists; diff --git a/mozilla/xpcom/io/nsFileSpecMac.cpp b/mozilla/xpcom/io/nsFileSpecMac.cpp index 27c37e258dd..1d7fc34d20d 100644 --- a/mozilla/xpcom/io/nsFileSpecMac.cpp +++ b/mozilla/xpcom/io/nsFileSpecMac.cpp @@ -518,7 +518,6 @@ Clean: nsFileSpec::nsFileSpec() //---------------------------------------------------------------------------------------- : mError(NS_OK) -, mPath(nsnull) { mSpec.name[0] = '\0'; } @@ -527,7 +526,6 @@ nsFileSpec::nsFileSpec() nsFileSpec::nsFileSpec(const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- : mSpec(inSpec.mSpec) -, mPath(nsnull) , mError(inSpec.Error()) { } @@ -535,7 +533,6 @@ nsFileSpec::nsFileSpec(const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { mSpec.vRefNum = 0; mSpec.parID = 0; @@ -551,7 +548,6 @@ nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { mSpec.vRefNum = 0; mSpec.parID = 0; @@ -567,7 +563,6 @@ nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(short vRefNum, long parID, ConstStr255Param name) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { mError = NS_FILE_RESULT(::FSMakeFSSpec(vRefNum, parID, name, &mSpec)); if (mError == NS_FILE_RESULT(fnfErr)) @@ -577,7 +572,6 @@ nsFileSpec::nsFileSpec(short vRefNum, long parID, ConstStr255Param name) //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsFilePath& inPath) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { *this = inPath.GetFileSpec(); } @@ -586,7 +580,7 @@ nsFileSpec::nsFileSpec(const nsFilePath& inPath) void nsFileSpec::operator = (const char* inString) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, nsnull); + mPath.SetToEmpty(); mSpec.vRefNum = 0; mSpec.parID = 0; @@ -601,8 +595,10 @@ void nsFileSpec::operator = (const char* inString) void nsFileSpec::operator = (const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, nsnull); - mSpec = inSpec.mSpec; + mPath.SetToEmpty(); + mSpec.vRefNum = inSpec.mSpec.vRefNum; + mSpec.parID = inSpec.mSpec.parID; + memcpy(mSpec.name, inSpec.mSpec.name, inSpec.mSpec.name[0] + 1); mError = inSpec.Error(); } // nsFileSpec::operator = @@ -662,7 +658,7 @@ void nsFileSpec::SetLeafName(const char* inLeafName) // 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. Str255 partialPath; @@ -681,14 +677,14 @@ char* nsFileSpec::GetLeafName() const char leaf[64]; memcpy(leaf, &mSpec.name[1], mSpec.name[0]); leaf[mSpec.name[0]] = '\0'; - return nsFileSpecHelpers::StringDup(leaf); + return PL_strdup(leaf); } // nsFileSpec::GetLeafName //---------------------------------------------------------------------------------------- void nsFileSpec::MakeAliasSafe() //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, nsnull); + mPath.SetToEmpty(); mError = NS_FILE_RESULT(MacFileHelpers::MakeAliasSafe(mSpec)); } // nsFileSpec::MakeAliasSafe @@ -696,7 +692,7 @@ void nsFileSpec::MakeAliasSafe() void nsFileSpec::MakeUnique(ConstStr255Param inSuggestedLeafName) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, nsnull); + mPath.SetToEmpty(); if (inSuggestedLeafName[0] > 0) 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. //---------------------------------------------------------------------------------------- { - if (!mPath) + if (mPath.IsEmpty()) { const_cast(this)->mPath = MacFileHelpers::PathNameFromFSSpec(mSpec, true); - if (!mPath) + if (mPath.IsEmpty()) const_cast(this)->mError = NS_ERROR_OUT_OF_MEMORY; } return mPath; @@ -912,29 +908,30 @@ const char* nsFileSpec::GetCString() const //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- -: mPath(nsnull) -, mFileSpec(inString, inCreateDirs) +: mFileSpec(inString, inCreateDirs) { // Make canonical and absolute. 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) //---------------------------------------------------------------------------------------- -: mPath(nsnull) -, mFileSpec(nsAutoCString(inString), inCreateDirs) +: mFileSpec(nsAutoCString(inString), inCreateDirs) { // Make canonical and absolute. 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) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { *this = inSpec; } @@ -942,18 +939,18 @@ nsFilePath::nsFilePath(const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const nsFileURL& inOther) //---------------------------------------------------------------------------------------- -: mPath(nsnull) { - *this = inOther.GetFileSpec(); + *this = inOther; } //---------------------------------------------------------------------------------------- void nsFilePath::operator = (const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- { - char * path = MacFileHelpers::PathNameFromFSSpec( inSpec.mSpec, true ); - delete [] mPath; - mPath = MacFileHelpers::EncodeMacPath(path, true, false); + char * path = MacFileHelpers::PathNameFromFSSpec(inSpec, true); + path = MacFileHelpers::EncodeMacPath(path, true, false); + mPath = path; + delete [] path; mFileSpec = inSpec; } // nsFilePath::operator = @@ -961,7 +958,11 @@ void nsFilePath::operator = (const nsFileSpec& inSpec) 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) //---------------------------------------------------------------------------------------- -: 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( - mURL + kFileURLPrefixLength, + inString + kFileURLPrefixLength, mFileSpec.mSpec, true, // need to decode false, // don't resolve alias @@ -988,11 +989,14 @@ nsFileURL::nsFileURL(const char* 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( - mURL + kFileURLPrefixLength, + cstring + kFileURLPrefixLength, mFileSpec.mSpec, true, // need to decode false, // don't resolve alias @@ -1005,14 +1009,12 @@ nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs) //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFilePath& inOther) //---------------------------------------------------------------------------------------- -: mURL(nsnull) { *this = inOther.GetFileSpec(); } // nsFileURL::nsFileURL //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFileSpec& inOther) -: mURL(nsnull) //---------------------------------------------------------------------------------------- { *this = inOther; @@ -1030,25 +1032,21 @@ void nsFileURL::operator = (const nsFileSpec& inOther) //---------------------------------------------------------------------------------------- { mFileSpec = inOther; - delete [] mURL; char* path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true ); char* encodedPath = MacFileHelpers::EncodeMacPath(path, true, true); - char* encodedURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, encodedPath); + nsSimpleCharString encodedURL(kFileURLPrefix); + encodedURL += 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 = //---------------------------------------------------------------------------------------- void nsFileURL::operator = (const char* inString) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mURL, inString); + mURL = inString; NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!"); mFileSpec.mError = NS_FILE_RESULT(MacFileHelpers::FSSpecFromUnixPath( inString + kFileURLPrefixLength, diff --git a/mozilla/xpcom/io/nsFileSpecUnix.cpp b/mozilla/xpcom/io/nsFileSpecUnix.cpp index 5af2eb10a30..8b783a24d77 100644 --- a/mozilla/xpcom/io/nsFileSpecUnix.cpp +++ b/mozilla/xpcom/io/nsFileSpecUnix.cpp @@ -61,39 +61,38 @@ extern "C" int statvfs(const char *, struct statvfs *); #endif //---------------------------------------------------------------------------------------- -void nsFileSpecHelpers::Canonify(char*& ioPath, PRBool inMakeDirs) +void nsFileSpecHelpers::Canonify(nsSimpleCharString& ioPath, PRBool inMakeDirs) // Canonify, make absolute, and check whether directories exist //---------------------------------------------------------------------------------------- { - if (!ioPath) + if (ioPath.IsEmpty()) return; if (inMakeDirs) { const mode_t mode = 0700; - nsFileSpecHelpers::MakeAllDirectories(ioPath, mode); + nsFileSpecHelpers::MakeAllDirectories((const char*)ioPath, mode); } char buffer[MAXPATHLEN]; errno = 0; *buffer = '\0'; - char* canonicalPath = realpath(ioPath, buffer); + char* canonicalPath = realpath((const char*)ioPath, buffer); if (!canonicalPath) { // 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, // process that, and then add the leaf back. - char* allButLeaf = nsFileSpecHelpers::StringDup(ioPath); - if (!allButLeaf) + nsSimpleCharString allButLeaf(ioPath); + if (allButLeaf.IsEmpty()) return; - char* lastSeparator = strrchr(allButLeaf, '/'); + char* lastSeparator = strrchr((char*)allButLeaf, '/'); if (lastSeparator) { *lastSeparator = '\0'; - canonicalPath = realpath(allButLeaf, buffer); + canonicalPath = realpath((const char*)allButLeaf, buffer); strcat(buffer, "/"); // Add back the leaf strcat(buffer, ++lastSeparator); } - delete [] allButLeaf; } if (!canonicalPath && *ioPath != '/' && !inMakeDirs) { @@ -106,21 +105,21 @@ void nsFileSpecHelpers::Canonify(char*& ioPath, PRBool inMakeDirs) } } if (canonicalPath) - nsFileSpecHelpers::StringAssign(ioPath, canonicalPath); + ioPath = canonicalPath; } // nsFileSpecHelpers::Canonify //---------------------------------------------------------------------------------------- void nsFileSpec::SetLeafName(const char* inLeafName) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::LeafReplace(mPath, '/', inLeafName); + mPath.LeafReplace('/', inLeafName); } // nsFileSpec::SetLeafName //---------------------------------------------------------------------------------------- char* nsFileSpec::GetLeafName() const //---------------------------------------------------------------------------------------- { - return nsFileSpecHelpers::GetLeaf(mPath, '/'); + return mPath.GetLeaf('/'); } // nsFileSpec::GetLeafName //---------------------------------------------------------------------------------------- @@ -172,9 +171,9 @@ PRBool nsFileSpec::IsDirectory() const void nsFileSpec::GetParent(nsFileSpec& outSpec) const //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(outSpec.mPath, mPath); + outSpec.mPath = mPath; char* cp = strrchr(outSpec.mPath, '/'); - if (cp) + if (cp++) *cp = '\0'; } // nsFileSpec::GetParent @@ -182,14 +181,14 @@ void nsFileSpec::GetParent(nsFileSpec& outSpec) const void nsFileSpec::operator += (const char* inRelativePath) //---------------------------------------------------------------------------------------- { - if (!inRelativePath || !mPath) + if (!inRelativePath || mPath.IsEmpty()) return; char endChar = mPath[strlen(mPath) - 1]; if (endChar == '/') - nsFileSpecHelpers::ReallocCat(mPath, "x"); + mPath += "x"; else - nsFileSpecHelpers::ReallocCat(mPath, "/x"); + mPath += "/x"; SetLeafName(inRelativePath); } // nsFileSpec::operator += @@ -304,16 +303,11 @@ nsresult nsFileSpec::Copy(const nsFileSpec& inParentDirectory) const if (inParentDirectory.IsDirectory() && (! IsDirectory() ) ) { char *leafname = GetLeafName(); - char* destPath = nsFileSpecHelpers::StringDup( - inParentDirectory.GetCString(), - strlen(inParentDirectory.GetCString()) + 1 + strlen(leafname)); - strcat(destPath, "/"); - strcat(destPath, leafname); + nsSimpleCharString destPath(inParentDirectory.GetCString()); + destPath += "/"; + destPath += leafname; delete [] leafname; - result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), destPath)); - - delete [] destPath; } return result; } // 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 nsresult result = NS_FILE_FAILURE; - if (inNewParentDirectory.IsDirectory() && (! IsDirectory() ) ) + if (inNewParentDirectory.IsDirectory() && !IsDirectory()) { char *leafname = GetLeafName(); - char* destPath - = nsFileSpecHelpers::StringDup( - inNewParentDirectory.GetCString(), - strlen(inNewParentDirectory.GetCString()) + 1 + strlen(leafname)); - strcat(destPath, "/"); - strcat(destPath, leafname); + nsSimpleCharString destPath(inNewParentDirectory.GetCString()); + destPath += "/"; + destPath += leafname; delete [] leafname; - result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), destPath)); + result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), (const char*)destPath)); if (result == NS_OK) - { - // cast to fix const-ness - ((nsFileSpec*)this)->Delete(PR_FALSE); - } - delete [] destPath; + { + // cast to fix const-ness + ((nsFileSpec*)this)->Delete(PR_FALSE); + } } return result; } @@ -355,13 +345,8 @@ nsresult nsFileSpec::Execute(const char* inArgs ) const if (! IsDirectory()) { - char* fileNameWithArgs - = nsFileSpecHelpers::StringDup(mPath, strlen(mPath) + 1 + strlen(inArgs)); - strcat(fileNameWithArgs, " "); - strcat(fileNameWithArgs, inArgs); - + nsSimpleCharString fileNameWithArgs = mPath + " " + inArgs; result = NS_FILE_RESULT(system(fileNameWithArgs)); - delete [] fileNameWithArgs; } return result; @@ -373,14 +358,14 @@ PRUint32 nsFileSpec::GetDiskSpaceAvailable() const //---------------------------------------------------------------------------------------- { char curdir [MAXPATHLEN]; - if (!mPath || !*mPath) + if (mPath.IsEmpty()) { (void) getcwd(curdir, MAXPATHLEN); if (!curdir) return ULONG_MAX; /* hope for the best as we did in cheddar */ } else - sprintf(curdir, "%.200s", mPath); + sprintf(curdir, "%.200s", (const char*)mPath); struct STATFS fs_buf; if (STATFS(curdir, &fs_buf) < 0) diff --git a/mozilla/xpcom/io/nsFileSpecWin.cpp b/mozilla/xpcom/io/nsFileSpecWin.cpp index 9feda5cb815..b95de83b0eb 100644 --- a/mozilla/xpcom/io/nsFileSpecWin.cpp +++ b/mozilla/xpcom/io/nsFileSpecWin.cpp @@ -35,37 +35,36 @@ #endif //---------------------------------------------------------------------------------------- -void nsFileSpecHelpers::Canonify(char*& ioPath, PRBool inMakeDirs) +void nsFileSpecHelpers::Canonify(nsSimpleCharString& ioPath, PRBool inMakeDirs) // Canonify, make absolute, and check whether directories exist. This // takes a (possibly relative) native path and converts it into a // fully qualified native path. //---------------------------------------------------------------------------------------- { - if (!ioPath) - return; + if (ioPath.IsEmpty()) + return; - if (inMakeDirs) - { - const int mode = 0700; - char* unixStylePath = nsFileSpecHelpers::StringDup(ioPath); - nsFileSpecHelpers::NativeToUnix(unixStylePath); - nsFileSpecHelpers::MakeAllDirectories(unixStylePath, mode); - delete[] unixStylePath; - } - char buffer[_MAX_PATH]; - errno = 0; - *buffer = '\0'; - char* canonicalPath = _fullpath(buffer, ioPath, _MAX_PATH); + if (inMakeDirs) + { + const int mode = 0700; + nsSimpleCharString unixStylePath = ioPath; + nsFileSpecHelpers::NativeToUnix(unixStylePath); + nsFileSpecHelpers::MakeAllDirectories((const char*)unixStylePath, mode); + } + char buffer[_MAX_PATH]; + errno = 0; + *buffer = '\0'; + char* canonicalPath = _fullpath(buffer, ioPath, _MAX_PATH); - NS_ASSERTION( canonicalPath[0] != '\0', "Uh oh...couldn't convert" ); - if (canonicalPath[0] == '\0') - return; + NS_ASSERTION( canonicalPath[0] != '\0', "Uh oh...couldn't convert" ); + if (canonicalPath[0] == '\0') + 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 // anything //---------------------------------------------------------------------------------------- @@ -73,55 +72,49 @@ void nsFileSpecHelpers::UnixToNative(char*& ioPath) // Allow for relative or absolute. We can do this in place, because the // native path is never longer. - if (!ioPath || !*ioPath) + if (ioPath.IsEmpty()) return; - char* src = ioPath; - if (*ioPath == '/') - { - // Strip initial slash for an absolute path - src++; - } - - // Convert the vertical slash to a colon - char* cp = src + 1; - - // If it was an absolute path, check for the drive letter - if (*ioPath == '/' && strstr(cp, "|/") == cp) - *cp = ':'; - - // Convert '/' to '\'. - while (*++cp) - { - if (*cp == '/') - *cp = '\\'; - } + // Strip initial slash for an absolute path + 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; + } - if (*ioPath == '/') { - for (cp = ioPath; *cp; ++cp) - *cp = *(cp + 1); - } -} + src = (char*)ioPath; + + // Convert '/' to '\'. + while (*++src) + { + if (*src == '/') + *src = '\\'; + } +} // nsFileSpecHelpers::UnixToNative //---------------------------------------------------------------------------------------- -void nsFileSpecHelpers::NativeToUnix(char*& ioPath) +void nsFileSpecHelpers::NativeToUnix(nsSimpleCharString& ioPath) // 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. //---------------------------------------------------------------------------------------- { - if (!ioPath || !*ioPath) + if (ioPath.IsEmpty()) return; // Convert the drive-letter separator, if present - char* temp = nsFileSpecHelpers::StringDup("/", 1 + strlen(ioPath)); + nsSimpleCharString temp("/"); - char* cp = ioPath + 1; - if (strstr(cp, ":\\") == cp) { + char* cp = (char*)ioPath + 1; + if (strstr(cp, ":\\") == cp) *cp = '|'; // absolute path - } - else { - *temp = '\0'; // relative path - } + else + temp[0] = '\0'; // relative path // Convert '\' to '/' for (; *cp; cp++) @@ -129,17 +122,14 @@ void nsFileSpecHelpers::NativeToUnix(char*& ioPath) if (*cp == '\\') *cp = '/'; } - // Add the slash in front. - strcat(temp, ioPath); - StringAssign(ioPath, temp); - delete [] temp; + temp += ioPath; + ioPath = temp; } //---------------------------------------------------------------------------------------- nsFileSpec::nsFileSpec(const nsFilePath& inPath) //---------------------------------------------------------------------------------------- -: mPath(NULL) { *this = inPath; } @@ -148,7 +138,7 @@ nsFileSpec::nsFileSpec(const nsFilePath& inPath) void nsFileSpec::operator = (const nsFilePath& inPath) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, (const char*)inPath); + mPath = (const char*)inPath; nsFileSpecHelpers::UnixToNative(mPath); mError = NS_OK; } // nsFileSpec::operator = @@ -156,7 +146,6 @@ void nsFileSpec::operator = (const nsFilePath& inPath) //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- -: mPath(NULL) { *this = inSpec; } // nsFilePath::nsFilePath @@ -165,7 +154,7 @@ nsFilePath::nsFilePath(const nsFileSpec& inSpec) void nsFilePath::operator = (const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(mPath, inSpec.mPath); + mPath = inSpec.mPath; nsFileSpecHelpers::NativeToUnix(mPath); } // nsFilePath::operator = @@ -173,14 +162,14 @@ void nsFilePath::operator = (const nsFileSpec& inSpec) void nsFileSpec::SetLeafName(const char* inLeafName) //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::LeafReplace(mPath, '\\', inLeafName); + mPath.LeafReplace('\\', inLeafName); } // nsFileSpec::SetLeafName //---------------------------------------------------------------------------------------- char* nsFileSpec::GetLeafName() const //---------------------------------------------------------------------------------------- { - return nsFileSpecHelpers::GetLeaf(mPath, '\\'); + return mPath.GetLeaf('\\'); } // nsFileSpec::GetLeafName //---------------------------------------------------------------------------------------- @@ -232,9 +221,9 @@ PRBool nsFileSpec::IsDirectory() const void nsFileSpec::GetParent(nsFileSpec& outSpec) const //---------------------------------------------------------------------------------------- { - nsFileSpecHelpers::StringAssign(outSpec.mPath, mPath); + outSpec.mPath = mPath; char* cp = strrchr(outSpec.mPath, '\\'); - if (cp) + if (cp++) *cp = '\0'; } // nsFileSpec::GetParent @@ -242,19 +231,18 @@ void nsFileSpec::GetParent(nsFileSpec& outSpec) const void nsFileSpec::operator += (const char* inRelativePath) //---------------------------------------------------------------------------------------- { - if (!inRelativePath || !mPath) + if (!inRelativePath || mPath.IsEmpty()) return; - if (mPath[strlen(mPath) - 1] == '\\') - nsFileSpecHelpers::ReallocCat(mPath, "x"); + if (mPath[mPath.Length() - 1] == '\\') + mPath += "x"; else - nsFileSpecHelpers::ReallocCat(mPath, "\\x"); + mPath += "\\x"; // If it's a (unix) relative path, make it native - char* dosPath = nsFileSpecHelpers::StringDup(inRelativePath); + nsSimpleCharString dosPath = inRelativePath; nsFileSpecHelpers::UnixToNative(dosPath); SetLeafName(dosPath); - delete [] dosPath; } // nsFileSpec::operator += //---------------------------------------------------------------------------------------- @@ -309,80 +297,54 @@ nsresult nsFileSpec::Copy(const nsFileSpec& inParentDirectory) const //---------------------------------------------------------------------------------------- { // We can only copy into a directory, and (for now) can not copy entire directories - if (inParentDirectory.IsDirectory() && (! IsDirectory() ) ) { char *leafname = GetLeafName(); - char* destPath = nsFileSpecHelpers::StringDup(inParentDirectory, ( strlen(inParentDirectory) + 1 + strlen(leafname) ) ); - strcat(destPath, "\\"); - strcat(destPath, leafname); + nsSimpleCharString destPath(inParentDirectory.GetCString()); + destPath += "\\"; + destPath += leafname; delete [] leafname; // CopyFile returns non-zero if succeeds - int copyOK = CopyFile(*this, destPath, true); - - delete[] destPath; - + int copyOK = CopyFile(GetCString(), destPath, true); if (copyOK) - { return NS_OK; - } } - return NS_FILE_FAILURE; } // 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 - - if (nsNewParentDirectory.IsDirectory() && (! IsDirectory() ) ) + if (inNewParentDirectory.IsDirectory() && (! IsDirectory() ) ) { char *leafname = GetLeafName(); - char *destPath = nsFileSpecHelpers::StringDup(nsNewParentDirectory, ( strlen(nsNewParentDirectory) + 1 + strlen(leafname) )); - strcat(destPath, "\\"); - strcat(destPath, leafname); + nsSimpleCharString destPath(inNewParentDirectory.GetCString()); + destPath += "\\"; + destPath += leafname; delete [] leafname; // MoveFile returns non-zero if succeeds - int copyOK = MoveFile(*this, destPath); - - delete [] destPath; - + int copyOK = MoveFile(GetCString(), destPath); if (copyOK) - { return NS_OK; - } } - return NS_FILE_FAILURE; } // nsFileSpec::Move //---------------------------------------------------------------------------------------- nsresult nsFileSpec::Execute(const char* inArgs ) const //---------------------------------------------------------------------------------------- -{ - - if (! IsDirectory()) +{ + if (!IsDirectory()) { - char* fileNameWithArgs = NULL; - - fileNameWithArgs = nsFileSpecHelpers::StringDup(mPath, ( strlen(mPath) + 1 + strlen(inArgs) ) ); - strcat(fileNameWithArgs, " "); - strcat(fileNameWithArgs, inArgs); - - int execResult = WinExec( fileNameWithArgs, SW_NORMAL ); - - delete [] fileNameWithArgs; - + nsSimpleCharString fileNameWithArgs = mPath + " " + inArgs; + int execResult = WinExec( fileNameWithArgs, SW_NORMAL ); if (execResult > 31) - { return NS_OK; - } } - return NS_FILE_FAILURE; } // nsFileSpec::Execute @@ -391,13 +353,13 @@ PRUint32 nsFileSpec::GetDiskSpaceAvailable() const //---------------------------------------------------------------------------------------- { char aDrive[_MAX_DRIVE + 2]; - _splitpath( mPath, aDrive, NULL, NULL, NULL); + _splitpath( (const char*)mPath, aDrive, NULL, NULL, NULL); if (aDrive[0] == '\0') { // The back end is always trying to pass us paths that look // 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[1] = ':'; diff --git a/mozilla/xpcom/io/nsIStringStream.cpp b/mozilla/xpcom/io/nsIStringStream.cpp index 33e661627c4..ea64e6596a4 100644 --- a/mozilla/xpcom/io/nsIStringStream.cpp +++ b/mozilla/xpcom/io/nsIStringStream.cpp @@ -14,9 +14,9 @@ class BasicStringImpl { public: BasicStringImpl() - : mResult(NS_OK) + : mOffset(0) + , mLastResult(NS_OK) , mEOF(PR_FALSE) - , mOffset(0) { NS_INIT_REFCNT(); } @@ -60,16 +60,16 @@ class BasicStringImpl NS_PRECONDITION(aReadCount != nsnull, "null ptr"); if (!aReadCount) return NS_ERROR_NULL_POINTER; - if (NS_FAILED(mResult)) - return mResult; + if (NS_FAILED(mLastResult)) + return mLastResult; PRInt32 bytesRead = read(aBuf, aCount); - if (NS_FAILED(mResult)) + if (NS_FAILED(mLastResult)) { *aReadCount = 0; - return mResult; + return mLastResult; } *aReadCount = bytesRead; - if (bytesRead < aCount) + if (bytesRead < (PRInt32)aCount) SetAtEOF(PR_TRUE); return NS_OK; } @@ -81,13 +81,13 @@ class BasicStringImpl NS_PRECONDITION(aBuf != nsnull, "null ptr"); NS_PRECONDITION(aWriteCount != nsnull, "null ptr"); - if (NS_FAILED(mResult)) - return mResult; + if (NS_FAILED(mLastResult)) + return mLastResult; PRInt32 bytesWrit = write(aBuf, aCount); - if (NS_FAILED(mResult)) + if (NS_FAILED(mLastResult)) { *aWriteCount = 0; - return mResult; + return mLastResult; } *aWriteCount = bytesWrit; return NS_OK; @@ -104,7 +104,7 @@ class BasicStringImpl public: - nsresult get_result() const { return mResult; } + nsresult get_result() const { return mLastResult; } protected: @@ -113,14 +113,14 @@ class BasicStringImpl virtual PRInt32 write(const char*, PRUint32) { 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; } protected: PRUint32 mOffset; - nsresult mResult; + nsresult mLastResult; PRBool mEOF; }; // class BasicStringImpl @@ -146,7 +146,7 @@ class ConstCharImpl virtual PRInt32 read(char* buf, PRUint32 aCount) { PRInt32 maxCount = mLength - mOffset; - if (aCount > maxCount) + if ((PRInt32)aCount > maxCount) aCount = maxCount; memcpy(buf, mConstString + mOffset, aCount); mOffset += aCount; @@ -181,7 +181,7 @@ class CharImpl mString = new char[mAllocLength]; if (!mString) { - mResult = NS_ERROR_OUT_OF_MEMORY; + mLastResult = NS_ERROR_OUT_OF_MEMORY; return; } mConstString = mString; @@ -193,17 +193,17 @@ class CharImpl virtual PRInt32 write(const char* buf, PRUint32 aCount) { PRInt32 maxCount = mAllocLength - 1 - mOffset; - if (aCount > maxCount) + if ((PRInt32)aCount > maxCount) { do { maxCount += kAllocQuantum; - } while (aCount > maxCount); + } while ((PRInt32)aCount > maxCount); mAllocLength = maxCount + 1 + mOffset; char* newString = new char[mAllocLength]; if (!newString) { - mResult = NS_ERROR_OUT_OF_MEMORY; + mLastResult = NS_ERROR_OUT_OF_MEMORY; return 0; } strcpy(newString, mString); @@ -220,8 +220,8 @@ class CharImpl protected: char*& mString; - size_t mOriginalLength; size_t mAllocLength; + size_t mOriginalLength; }; // class CharImpl @@ -271,7 +271,7 @@ class StringImpl chars.Seek(PR_SEEK_SET, mOffset); // Get the bytecount and result from the CharImpl PRInt32 result = chars.write(buf,count); - mResult = chars.get_result(); + mLastResult = chars.get_result(); // Set our string to match the new chars mString = cstring; // 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) //---------------------------------------------------------------------------------------- { - mResult = NS_OK; // reset on a seek. + mLastResult = NS_OK; // reset on a seek. mEOF = PR_FALSE; // reset on a seek. PRInt32 fileSize = length(); PRInt32 newPosition; @@ -344,7 +344,7 @@ NS_IMETHODIMP BasicStringImpl::Seek(PRSeekWhence whence, PRInt32 offset) if (newPosition < 0) { newPosition = 0; - mResult = NS_FILE_RESULT(PR_FILE_SEEK_ERROR); + mLastResult = NS_FILE_RESULT(PR_FILE_SEEK_ERROR); } if (newPosition >= fileSize) { diff --git a/mozilla/xpcom/tests/FilesTest.cpp b/mozilla/xpcom/tests/FilesTest.cpp index 4bcdaaef8d4..f453861aa96 100644 --- a/mozilla/xpcom/tests/FilesTest.cpp +++ b/mozilla/xpcom/tests/FilesTest.cpp @@ -345,11 +345,14 @@ int FilesTest::Parent( mySpec.GetParent(outParent); nsFilePath parentPath(outParent); + nsFileURL url(parentPath); mConsole << "GetParent() on " << "\n\t" << pathAsString << "\n yields " << "\n\t" << (const char*)parentPath + << "\n or as a URL" + << "\n\t" << (const char*)url << nsEndl; Inspect(); return 0;