diff --git a/mozilla/base/public/nsFileSpec.h b/mozilla/base/public/nsFileSpec.h index cc590c7c11c..80ba05a9072 100644 --- a/mozilla/base/public/nsFileSpec.h +++ b/mozilla/base/public/nsFileSpec.h @@ -103,7 +103,7 @@ #ifndef _FILESPEC_H_ #define _FILESPEC_H_ -#include +#include //======================================================================================== // Compiler-specific macros, as needed @@ -116,7 +116,6 @@ #define NS_NAMESPACE_PROTOTYPE #define NS_NAMESPACE namespace #define NS_NAMESPACE_END - using std::string; using std::ostream; #else #define NS_NAMESPACE_PROTOTYPE static @@ -126,6 +125,7 @@ //=========================== End Compiler-specific macros =============================== #include "nsDebug.h" + #ifdef XP_MAC #include #endif @@ -149,19 +149,20 @@ class nsNativeFileSpec { public: nsNativeFileSpec(); - explicit nsNativeFileSpec(const string& inString); - nsNativeFileSpec(const nsFilePath& inPath); - nsNativeFileSpec(const nsFileURL& inURL); + explicit nsNativeFileSpec(const char* inString); + explicit nsNativeFileSpec(const nsFilePath& inPath); + explicit nsNativeFileSpec(const nsFileURL& inURL); nsNativeFileSpec(const nsNativeFileSpec& inPath); + virtual ~nsNativeFileSpec(); - void operator = (const string& inPath); + void operator = (const char* inPath); void operator = (const nsFilePath& inPath); void operator = (const nsFileURL& inURL); void operator = (const nsNativeFileSpec& inOther); #ifdef XP_MAC // For Macintosh people, this is meant to be useful in its own right as a C++ version - // of the FSSPec class. + // of the FSSpec struct. nsNativeFileSpec( short vRefNum, long parID, @@ -171,25 +172,34 @@ class nsNativeFileSpec operator FSSpec* () { return &mSpec; } operator const FSSpec* const () { return &mSpec; } - operator FSSpec& () { return mSpec; } + operator FSSpec& () { return mSpec; } operator const FSSpec& () const { return mSpec; } - bool Valid() const { return mError == noErr; } OSErr Error() const { return mError; } + void MakeAliasSafe(); + // Called for the spec of an alias. Copies the alias to + // a secret temp directory and modifies the spec to point + // to it. Sets mError. + void ResolveAlias(bool& wasAliased); + // Called for the spec of an alias. Modifies the spec to + // point to the original. Sets mError. void MakeUnique(ConstStr255Param inSuggestedLeafName); StringPtr GetLeafPName() { return mSpec.name; } ConstStr255Param GetLeafPName() const { return mSpec.name; } +#endif // end of Macintosh utility methods. + +#ifdef XP_MAC + bool Valid() const { return mError == noErr; } #else bool Valid() const { return true; } // Fixme. -#endif +#endif // XP_MAC -#if DEBUG friend ostream& operator << (ostream& s, const nsNativeFileSpec& spec); -#endif - string GetLeafName() const; - void SetLeafName(const string& inLeafName); + + char* GetLeafName() const; // Allocated. Use delete []. + void SetLeafName(const char* inLeafName); bool Exists() const; void MakeUnique(); - void MakeUnique(const string& inSuggestedLeafName); + void MakeUnique(const char* inSuggestedLeafName); private: friend class nsFilePath; @@ -197,7 +207,7 @@ class nsNativeFileSpec FSSpec mSpec; OSErr mError; #elif defined(XP_UNIX) || defined(XP_PC) - string mPath; + char* mPath; #endif }; // class nsNativeFileSpec @@ -210,29 +220,33 @@ class nsFileURL { public: nsFileURL(const nsFileURL& inURL); - explicit nsFileURL(const string& inString); - nsFileURL(const nsFilePath& inPath); - nsFileURL(const nsNativeFileSpec& inPath); + explicit nsFileURL(const char* inString); + explicit nsFileURL(const nsFilePath& inPath); + explicit nsFileURL(const nsNativeFileSpec& inPath); + virtual ~nsFileURL(); -// string GetString() const { return mPath; } +// nsString GetString() const { return mPath; } // may be needed for implementation reasons, // but should not provide a conversion constructor. void operator = (const nsFileURL& inURL); - void operator = (const string& inString); + void operator = (const char* inString); void operator = (const nsFilePath& inOther); void operator = (const nsNativeFileSpec& inOther); + friend ostream& operator << (ostream& s, const nsFileURL& spec); + #ifdef XP_MAC // Accessor to allow quick assignment to a mNativeFileSpec const nsNativeFileSpec& GetNativeSpec() const { return mNativeFileSpec; } #endif private: // Should not be defined (only nsFilePath is to be treated as strings. - operator string& (); + operator char* (); + operator const char* const (); private: - - string mURL; + friend class nsFilePath; // to allow construction of nsFilePath + char* 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 nsNativeFileSpec, too. @@ -248,22 +262,23 @@ class nsFilePath { public: nsFilePath(const nsFilePath& inPath); - explicit nsFilePath(const string& inString); - nsFilePath(const nsFileURL& inURL); - nsFilePath(const nsNativeFileSpec& inPath); + explicit nsFilePath(const char* inString); + explicit nsFilePath(const nsFileURL& inURL); + explicit nsFilePath(const nsNativeFileSpec& inPath); + virtual ~nsFilePath(); - operator const char* () const { return mPath.c_str(); } + operator const char* () const { return mPath; } // This is the only automatic conversion to const char* // that is provided, and it allows the // path to be "passed" to NSPR file routines. - operator string& () { return mPath; } + 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 string& inString); + void operator = (const char* inString); void operator = (const nsFileURL& inURL); void operator = (const nsNativeFileSpec& inOther); @@ -275,7 +290,7 @@ class nsFilePath private: - string mPath; + char* 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 nsNativeFileSpec, too. @@ -283,109 +298,4 @@ class nsFilePath #endif }; // class nsFilePath -#ifdef XP_UNIX -//======================================================================================== -// UNIX nsFilePath implementation -//======================================================================================== - -//---------------------------------------------------------------------------------------- -inline nsFilePath::nsFilePath(const nsNativeFileSpec& inOther) -//---------------------------------------------------------------------------------------- -: mPath((string&)inOther) -{ -} -//---------------------------------------------------------------------------------------- -inline void nsFilePath::operator = (const nsNativeFileSpec& inOther) -//---------------------------------------------------------------------------------------- -{ - mPath = (string&)inOther; -} -#endif // XP_UNIX - -//======================================================================================== -// COMMON nsNativeFileSpec implementation -//======================================================================================== - -//---------------------------------------------------------------------------------------- -inline nsNativeFileSpec::nsNativeFileSpec(const nsFileURL& inURL) -//---------------------------------------------------------------------------------------- -{ - *this = nsFilePath(inURL); // convert to unix path first -} - -//---------------------------------------------------------------------------------------- -inline void nsNativeFileSpec::operator = (const nsFileURL& inURL) -//---------------------------------------------------------------------------------------- -{ - *this = nsFilePath(inURL); // convert to unix path first -} - -//======================================================================================== -// UNIX & WIN nsNativeFileSpec implementation -//======================================================================================== - -#ifdef XP_UNIX -//---------------------------------------------------------------------------------------- -inline nsNativeFileSpec::nsNativeFileSpec(const nsFilePath& inPath) -//---------------------------------------------------------------------------------------- -: mPath((string&)inPath) -{ -} -#endif // XP_UNIX - -#ifdef XP_UNIX -//---------------------------------------------------------------------------------------- -inline void nsNativeFileSpec::operator = (const nsFilePath& inPath) -//---------------------------------------------------------------------------------------- -{ - mPath = (string&)inPath; -} -#endif //XP_UNIX - -#if defined(XP_UNIX) || defined(XP_PC) -//---------------------------------------------------------------------------------------- -inline nsNativeFileSpec::nsNativeFileSpec(const nsNativeFileSpec& inSpec) -//---------------------------------------------------------------------------------------- -: mPath((string&)inSpec) -{ -} -#endif //XP_UNIX - -#if defined(XP_UNIX) || defined(XP_PC) -//---------------------------------------------------------------------------------------- -inline nsNativeFileSpec::nsNativeFileSpec(const string& inString) -//---------------------------------------------------------------------------------------- -: mPath(inString) -{ -} -#endif //XP_UNIX - -#if defined(XP_UNIX) || defined(XP_PC) -//---------------------------------------------------------------------------------------- -inline void nsNativeFileSpec::operator = (const nsNativeFileSpec& inSpec) -//---------------------------------------------------------------------------------------- -{ - mPath = (string&)inSpec; -} -#endif //XP_UNIX - - -#if defined(XP_UNIX) || defined(XP_PC) -//---------------------------------------------------------------------------------------- -inline nsNativeFileSpec::operator = (const string& inString) -//---------------------------------------------------------------------------------------- -{ - mPath = inString; -} -#endif //XP_UNIX - -#if (defined(XP_UNIX) || defined(XP_PC)) && DEBUG -//---------------------------------------------------------------------------------------- -inline ostream& operator << (ostream& s, const nsNativeFileSpec& spec) -//---------------------------------------------------------------------------------------- -{ - return (s << (string&)spec.mPath); -} -#endif // DEBUG && XP_UNIX - #endif // _FILESPEC_H_ diff --git a/mozilla/base/src/mac/nsFileSpecMac.cpp b/mozilla/base/src/mac/nsFileSpecMac.cpp index 22df2e18366..60336496c34 100644 --- a/mozilla/base/src/mac/nsFileSpecMac.cpp +++ b/mozilla/base/src/mac/nsFileSpecMac.cpp @@ -39,6 +39,8 @@ namespace MacFileHelpers memcpy(dst, src, 1 + src[0]); } + void PLstrcpy(Str255 dst, const char* src, int inMaxLen); + void SwapSlashColon(char * s); OSErr FSSpecFromFullUnixPath( const char * unixPath, @@ -67,7 +69,7 @@ namespace MacFileHelpers // to support attaching of aliases in mail. void EnsureAliasHaven(); void SetNoResolve(Boolean inResolve); - Boolean IsAliasSafe(const FSSpec& inSpec); + bool IsAliasSafe(const FSSpec& inSpec); OSErr MakeAliasSafe(FSSpec& inOutSpec); OSErr ResolveAliasFile(FSSpec& inOutSpec, Boolean& wasAliased); @@ -76,6 +78,18 @@ namespace MacFileHelpers short sAliasHavenVRefNum = 0; } // namespace MacFileHelpers +//---------------------------------------------------------------------------------------- +void MacFileHelpers::PLstrcpy(Str255 dst, const char* src, int inMax) +//======================================================================================== +{ + int srcLength = strlen(src); + NS_ASSERTION(srcLength <= inMax, "Oops, string is too long!"); + if (srcLength > inMax) + srcLength = inMax; + dst[0] = srcLength; + memcpy(&dst[1], src, srcLength); +} + //----------------------------------- void MacFileHelpers::SwapSlashColon(char * s) //----------------------------------- @@ -180,16 +194,6 @@ OSErr MacFileHelpers::MakeAliasSafe(FSSpec& inOutSpec) return noErr; } // MacFileHelpers::MakeAliasSafe -//---------------------------------------------------------------------------------------- -void nsNativeFileSpec::MakeUnique(ConstStr255Param inSuggestedLeafName) -//---------------------------------------------------------------------------------------- -{ - if (inSuggestedLeafName[0] > 0) - MacFileHelpers::PLstrcpy(mSpec.name, inSuggestedLeafName); - - MakeUnique(); -} - //---------------------------------------------------------------------------------------- char* MacFileHelpers::MacPathFromUnixPath(const char* unixPath) //---------------------------------------------------------------------------------------- @@ -290,7 +294,7 @@ void MacFileHelpers::EnsureAliasHaven() } // MacFileHelpers::EnsureAliasHaven //---------------------------------------------------------------------------------------- -Boolean MacFileHelpers::IsAliasSafe(const FSSpec& inSpec) +bool MacFileHelpers::IsAliasSafe(const FSSpec& inSpec) // Returns true if the alias is in the alias haven directory, or if alias resolution // has been turned off. //---------------------------------------------------------------------------------------- @@ -453,16 +457,16 @@ nsNativeFileSpec::nsNativeFileSpec() //---------------------------------------------------------------------------------------- nsNativeFileSpec::nsNativeFileSpec(const nsNativeFileSpec& inSpec) //---------------------------------------------------------------------------------------- -: mSpec((FSSpec&)inSpec) +: mSpec(inSpec.mSpec) , mError(inSpec.Error()) { } //---------------------------------------------------------------------------------------- -nsNativeFileSpec::nsNativeFileSpec(const string& inString) +nsNativeFileSpec::nsNativeFileSpec(const char* inString) //---------------------------------------------------------------------------------------- { - mError = MacFileHelpers::FSSpecFromFullUnixPath(inString.c_str(), mSpec, true); + mError = MacFileHelpers::FSSpecFromFullUnixPath(inString, mSpec, true); if (mError == fnfErr) mError = noErr; } // nsNativeFileSpec::nsNativeFileSpec @@ -486,30 +490,27 @@ nsNativeFileSpec::nsNativeFileSpec(const nsFilePath& inPath) *this = inPath.GetNativeSpec(); } -#if DEBUG //---------------------------------------------------------------------------------------- ostream& operator << (ostream& s, const nsNativeFileSpec& spec) //---------------------------------------------------------------------------------------- { s << spec.mSpec.vRefNum << ", " << spec.mSpec.parID << ", \""; - for (int i = 1; i <= spec.mSpec.name[0]; i++) - s.put(spec.mSpec.name[i]); + s.write((const char*)&spec.mSpec.name[1], spec.mSpec.name[0]); return s << "\""; } // ostream& operator << (ostream&, const nsNativeFileSpec&) -#endif // DEBUG //---------------------------------------------------------------------------------------- -void nsNativeFileSpec::operator = (const string& inString) +void nsNativeFileSpec::operator = (const char* inString) //---------------------------------------------------------------------------------------- { - mError = MacFileHelpers::FSSpecFromFullUnixPath(inString.c_str(), mSpec, true); + mError = MacFileHelpers::FSSpecFromFullUnixPath(inString, mSpec, true); } // nsNativeFileSpec::operator = //---------------------------------------------------------------------------------------- void nsNativeFileSpec::operator = (const nsNativeFileSpec& inSpec) //---------------------------------------------------------------------------------------- { - mSpec = inSpec; + mSpec = inSpec.mSpec; mError = inSpec.Error(); } // nsNativeFileSpec::operator = @@ -517,7 +518,7 @@ void nsNativeFileSpec::operator = (const nsNativeFileSpec& inSpec) void nsNativeFileSpec::operator = (const nsFilePath& inPath) //---------------------------------------------------------------------------------------- { - mSpec = inPath.GetNativeSpec(); + *this = inPath.GetNativeSpec(); } // nsNativeFileSpec::operator = //---------------------------------------------------------------------------------------- @@ -529,23 +530,49 @@ bool nsNativeFileSpec::Exists() const } // nsNativeFileSpec::operator = //---------------------------------------------------------------------------------------- -void nsNativeFileSpec::SetLeafName(const string& inLeafName) +void nsNativeFileSpec::SetLeafName(const char* inLeafName) //---------------------------------------------------------------------------------------- { - *mSpec.name = inLeafName.length(); - memcpy(mSpec.name + 1, inLeafName.c_str(), 1 + *mSpec.name); + MacFileHelpers::PLstrcpy(mSpec.name, inLeafName, nsFileSpecHelpers::kMaxFilenameLength); } // nsNativeFileSpec::SetLeafName //---------------------------------------------------------------------------------------- -string nsNativeFileSpec::GetLeafName() const +char* nsNativeFileSpec::GetLeafName() const +// Result needs to be delete[]ed. //---------------------------------------------------------------------------------------- { char leaf[64]; memcpy(leaf, &mSpec.name[1], mSpec.name[0]); leaf[mSpec.name[0]] = '\0'; - return string(leaf); + return nsFileSpecHelpers::StringDup(leaf); } // nsNativeFileSpec::GetLeafName +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::MakeAliasSafe() +//---------------------------------------------------------------------------------------- +{ + mError = MacFileHelpers::MakeAliasSafe(mSpec); +} // nsNativeFileSpec::MakeAliasSafe + +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::MakeUnique(ConstStr255Param inSuggestedLeafName) +//---------------------------------------------------------------------------------------- +{ + if (inSuggestedLeafName[0] > 0) + MacFileHelpers::PLstrcpy(mSpec.name, inSuggestedLeafName); + + MakeUnique(); +} // nsNativeFileSpec::MakeUnique + +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::ResolveAlias(bool& wasAliased) +//---------------------------------------------------------------------------------------- +{ + Boolean wasAliased2; + mError = MacFileHelpers::ResolveAliasFile(mSpec, wasAliased2); + wasAliased = (wasAliased2 != false); +} // nsNativeFileSpec::ResolveAlias + //======================================================================================== // Macintosh nsFilePath implementation //======================================================================================== @@ -553,25 +580,18 @@ string nsNativeFileSpec::GetLeafName() const //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const nsNativeFileSpec& inSpec) //---------------------------------------------------------------------------------------- -#ifdef XP_MAC : mNativeFileSpec(inSpec) -#endif { char * path = MacFileHelpers::PathNameFromFSSpec( inSpec.mSpec, TRUE ); - char * unixPath = MacFileHelpers::EncodeMacPath(path, true, true); - mPath = unixPath; - delete [] unixPath; + mPath = MacFileHelpers::EncodeMacPath(path, true, true); } //---------------------------------------------------------------------------------------- void nsFilePath::operator = (const nsNativeFileSpec& inSpec) //---------------------------------------------------------------------------------------- { + delete [] mPath; char * path = MacFileHelpers::PathNameFromFSSpec( inSpec.mSpec, TRUE ); - char * unixPath = MacFileHelpers::EncodeMacPath(path, true, true); - mPath = unixPath; - delete [] unixPath; -#ifdef XP_MAC + mPath = MacFileHelpers::EncodeMacPath(path, true, true); mNativeFileSpec = inSpec; -#endif } // nsFilePath::operator = diff --git a/mozilla/base/src/nsFileSpec.cpp b/mozilla/base/src/nsFileSpec.cpp index c9234159dd7..7cf82c25a4e 100644 --- a/mozilla/base/src/nsFileSpec.cpp +++ b/mozilla/base/src/nsFileSpec.cpp @@ -26,6 +26,11 @@ #include +#ifdef NS_USING_NAMESPACE + using std::ends; + using std::ostrstream; +#endif + //======================================================================================== NS_NAMESPACE nsFileSpecHelpers //======================================================================================== @@ -33,39 +38,94 @@ NS_NAMESPACE nsFileSpecHelpers enum { kMaxFilenameLength = 31 // should work on Macintosh, Unix, and Win32. , kMaxAltDigitLength = 5 - , kMaxAltNameLength = (kMaxFilenameLength - (kMaxAltDigitLength + 1)) + , kMaxCoreLeafNameLength = (kMaxFilenameLength - (kMaxAltDigitLength + 1)) }; NS_NAMESPACE_PROTOTYPE void LeafReplace( - string& ioPath, + char*& ioPath, char inSeparator, - const string& inLeafName); - NS_NAMESPACE_PROTOTYPE string GetLeaf(const string& inPath, char inSeparator); + const char* inLeafName); + NS_NAMESPACE_PROTOTYPE const char* GetLeaf(const char* inPath, char inSeparator); + 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_END +//---------------------------------------------------------------------------------------- +char* nsFileSpecHelpers::StringDup( + const char* inString, + int allocLength) +//---------------------------------------------------------------------------------------- +{ + if (!allocLength) + allocLength = strlen(inString); + char* newPath = new char[allocLength + 1]; + if (!newPath) + return NULL; + strcpy(newPath, inString); + return newPath; +} // nsFileSpecHelpers::StringDup + +//---------------------------------------------------------------------------------------- +char* nsFileSpecHelpers::AllocCat( + const char* inString1, + const char* inString2) +//---------------------------------------------------------------------------------------- +{ + 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 (strlen(inString2) > strlen(ioString)) + { + delete [] ioString; + ioString = StringDup(inString2); + } + else + strcpy(ioString, inString2); + return ioString; +} // nsFileSpecHelpers::StringAssign + //---------------------------------------------------------------------------------------- void nsFileSpecHelpers::LeafReplace( - string& ioPath, + char*& ioPath, char inSeparator, - const string& inLeafName) + const char* inLeafName) //---------------------------------------------------------------------------------------- { // Find the existing leaf name - string::size_type lastSeparator = ioPath.rfind(inSeparator); - string::size_type myLength = ioPath.length(); - if (lastSeparator < myLength) - ioPath = ioPath.substr(0, lastSeparator + 1) + inLeafName; + if (!ioPath) + return; + char* lastSeparator = strrchr(ioPath, inSeparator); + int oldLength = strlen(ioPath); + *(++lastSeparator) = '\0'; // strip the current leaf name + int newLength = lastSeparator - ioPath + strlen(inLeafName); + if (newLength > oldLength) + { + char* newPath = StringDup(ioPath, newLength + 1); + delete [] ioPath; + ioPath = newPath; + } + strcat(ioPath, inLeafName); } // nsNativeFileSpec::SetLeafName //---------------------------------------------------------------------------------------- -string nsFileSpecHelpers::GetLeaf(const string& inPath, char inSeparator) +const char* nsFileSpecHelpers::GetLeaf(const char* inPath, char inSeparator) +// Returns a pointer to an allocated string representing the leaf. //---------------------------------------------------------------------------------------- { - string::size_type lastSeparator = inPath.rfind(inSeparator); - string::size_type myLength = inPath.length(); - if (lastSeparator < myLength) - return inPath.substr(1 + lastSeparator, myLength - lastSeparator - 1); - return inPath; -} // nsNativeFileSpec::GetLeafName + char* lastSeparator = strrchr(inPath, inSeparator); + if (lastSeparator) + return StringDup(lastSeparator); + return StringDup(inPath); +} // nsNativeFileSpec::GetLeaf #ifdef XP_MAC @@ -81,84 +141,101 @@ string nsFileSpecHelpers::GetLeaf(const string& inPath, char inSeparator) //======================================================================================== //---------------------------------------------------------------------------------------- -nsFileURL::nsFileURL(const string& inString) +nsFileURL::nsFileURL(const char* inString) //---------------------------------------------------------------------------------------- -: mURL(inString) +: mURL(nsFileSpecHelpers::StringDup(inString)) #ifdef XP_MAC -, mNativeFileSpec(inString.substr(kFileURLPrefixLength, inString.length() - kFileURLPrefixLength)) +, mNativeFileSpec(inString + kFileURLPrefixLength) #endif { - NS_ASSERTION(mURL.substr(0, kFileURLPrefixLength) == kFileURLPrefix, "Not a URL!"); -} - -//---------------------------------------------------------------------------------------- -void nsFileURL::operator = (const string& inString) -//---------------------------------------------------------------------------------------- -{ - mURL = inString; - NS_ASSERTION(mURL.substr(0, kFileURLPrefixLength) == kFileURLPrefix, "Not a URL!"); -#ifdef XP_MAC - mNativeFileSpec = - inString.substr(kFileURLPrefixLength, inString.length() - kFileURLPrefixLength); -#endif -} + NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!"); +} // nsFileURL::nsFileURL //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFileURL& inOther) //---------------------------------------------------------------------------------------- -: mURL(inOther.mURL) +: mURL(nsFileSpecHelpers::StringDup(inOther.mURL)) #ifdef XP_MAC , mNativeFileSpec(inOther.GetNativeSpec()) #endif { +} // nsFileURL::nsFileURL + +//---------------------------------------------------------------------------------------- +nsFileURL::nsFileURL(const nsFilePath& inOther) +//---------------------------------------------------------------------------------------- +: mURL(nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)inOther)) +#ifdef XP_MAC +, mNativeFileSpec(inOther.GetNativeSpec()) +#endif +{ +} // nsFileURL::nsFileURL + +//---------------------------------------------------------------------------------------- +nsFileURL::nsFileURL(const nsNativeFileSpec& inOther) +: mURL(nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)nsFilePath(inOther))) +#ifdef XP_MAC +, mNativeFileSpec(inOther) +#endif +//---------------------------------------------------------------------------------------- +{ +} // nsFileURL::nsFileURL + +//---------------------------------------------------------------------------------------- +nsFileURL::~nsFileURL() +//---------------------------------------------------------------------------------------- +{ + delete [] mURL; } +//---------------------------------------------------------------------------------------- +void nsFileURL::operator = (const char* inString) +//---------------------------------------------------------------------------------------- +{ + nsFileSpecHelpers::StringAssign(mURL, inString); + NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!"); +#ifdef XP_MAC + mNativeFileSpec = inString + kFileURLPrefixLength; +#endif +} // nsFileURL::operator = + //---------------------------------------------------------------------------------------- void nsFileURL::operator = (const nsFileURL& inOther) //---------------------------------------------------------------------------------------- { - mURL = inOther.mURL; + mURL = nsFileSpecHelpers::StringAssign(mURL, inOther.mURL); #ifdef XP_MAC mNativeFileSpec = inOther.GetNativeSpec(); #endif -} +} // nsFileURL::operator = -//---------------------------------------------------------------------------------------- -nsFileURL::nsFileURL(const nsFilePath& inOther) -//---------------------------------------------------------------------------------------- -{ - mURL = kFileURLPrefix + ((std::string&)inOther); -#ifdef XP_MAC - mNativeFileSpec = inOther.GetNativeSpec(); -#endif -} //---------------------------------------------------------------------------------------- void nsFileURL::operator = (const nsFilePath& inOther) //---------------------------------------------------------------------------------------- { - mURL = kFileURLPrefix + ((std::string&)inOther); + delete [] mURL; + mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)inOther); #ifdef XP_MAC mNativeFileSpec = inOther.GetNativeSpec(); #endif -} +} // nsFileURL::operator = -//---------------------------------------------------------------------------------------- -nsFileURL::nsFileURL(const nsNativeFileSpec& inOther) -//---------------------------------------------------------------------------------------- -{ - mURL = kFileURLPrefix + (string&)nsFilePath(inOther); -#ifdef XP_MAC - mNativeFileSpec = inOther; -#endif -} //---------------------------------------------------------------------------------------- void nsFileURL::operator = (const nsNativeFileSpec& inOther) //---------------------------------------------------------------------------------------- { - mURL = kFileURLPrefix + (string&)nsFilePath(inOther); + delete [] mURL; + mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)nsFilePath(inOther)); #ifdef XP_MAC mNativeFileSpec = inOther; #endif +} // nsFileURL::operator = + +//---------------------------------------------------------------------------------------- +ostream& operator << (ostream& s, const nsFileURL& url) +//---------------------------------------------------------------------------------------- +{ + return (s << url.mURL); } //======================================================================================== @@ -166,44 +243,67 @@ void nsFileURL::operator = (const nsNativeFileSpec& inOther) //======================================================================================== //---------------------------------------------------------------------------------------- -nsFilePath::nsFilePath(const string& inString) +nsFilePath::nsFilePath(const char* inString) //---------------------------------------------------------------------------------------- -: mPath(inString) +: mPath(nsFileSpecHelpers::StringDup(inString)) #ifdef XP_MAC , mNativeFileSpec(inString) #endif { - NS_ASSERTION(mPath.substr(0, kFileURLPrefixLength) != kFileURLPrefix, "URL passed as path"); + NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path"); } //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const nsFileURL& inOther) //---------------------------------------------------------------------------------------- -: mPath(((std::string&)inOther).substr( - kFileURLPrefixLength, ((std::string&)inOther).length() - kFileURLPrefixLength)) +: mPath(nsFileSpecHelpers::StringDup(inOther.mURL + kFileURLPrefixLength)) #ifdef XP_MAC , mNativeFileSpec(inOther.GetNativeSpec()) #endif { } +#ifdef XP_UNIX //---------------------------------------------------------------------------------------- -void nsFilePath::operator = (const string& inString) +nsFilePath::nsFilePath(const nsNativeFileSpec& inOther) +//---------------------------------------------------------------------------------------- +: mPath(nsFileSpecHelpers::StringDup(inOther.mPath)) +{ +} +#endif // XP_UNIX + +//---------------------------------------------------------------------------------------- +nsFilePath::~nsFilePath() //---------------------------------------------------------------------------------------- { - mPath = inString; + delete [] mPath; +} + +#ifdef XP_UNIX +//---------------------------------------------------------------------------------------- +void nsFilePath::operator = (const nsNativeFileSpec& inOther) +//---------------------------------------------------------------------------------------- +{ + mPath = nsFileSpecHelpers::StringAssign(mPath, inOther.mPath); +} +#endif // XP_UNIX + +//---------------------------------------------------------------------------------------- +void nsFilePath::operator = (const char* inString) +//---------------------------------------------------------------------------------------- +{ + NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path"); + nsFileSpecHelpers::StringAssign(mPath, inString); #ifdef XP_MAC mNativeFileSpec = inString; #endif - NS_ASSERTION(mPath.substr(0, kFileURLPrefixLength) != kFileURLPrefix, "URL passed as path"); } //---------------------------------------------------------------------------------------- void nsFilePath::operator = (const nsFileURL& inOther) //---------------------------------------------------------------------------------------- { - mPath = ((std::string&)inOther).substr( - kFileURLPrefixLength, ((std::string&)inOther).length() - kFileURLPrefixLength); + nsFileSpecHelpers::StringAssign(mPath, (const char*)nsFilePath(inOther)); #ifdef XP_MAC mNativeFileSpec = inOther.GetNativeSpec(); #endif @@ -217,15 +317,26 @@ void nsFilePath::operator = (const nsFileURL& inOther) //---------------------------------------------------------------------------------------- nsNativeFileSpec::nsNativeFileSpec() //---------------------------------------------------------------------------------------- +: mPath(NULL) { } #endif //---------------------------------------------------------------------------------------- -void nsNativeFileSpec::MakeUnique(const string& inSuggestedLeafName) +nsNativeFileSpec::nsNativeFileSpec(const nsFileURL& inURL) +//---------------------------------------------------------------------------------------- +#ifndef XP_MAC +: mPath(NULL) +#endif +{ + *this = nsFilePath(inURL); // convert to unix path first +} + +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::MakeUnique(const char* inSuggestedLeafName) //---------------------------------------------------------------------------------------- { - if (inSuggestedLeafName.length() > 0) + if (inSuggestedLeafName && *inSuggestedLeafName) SetLeafName(inSuggestedLeafName); MakeUnique(); @@ -238,27 +349,115 @@ void nsNativeFileSpec::MakeUnique() if (!Exists()) return; - short index = 0; - string altName = GetLeafName(); - string::size_type lastDot = altName.rfind('.'); - string suffix; - if (lastDot < altName.length()) + char* leafName = GetLeafName(); + if (!leafName) + return; + + char* lastDot = strrchr(leafName, '.'); + char* suffix = ""; + if (lastDot) { - suffix = altName.substr(lastDot, altName.length() - lastDot); // include '.' - altName = altName.substr(0, lastDot); + suffix = nsFileSpecHelpers::StringDup(lastDot); // include '.' + *lastDot = '\0'; // strip suffix and dot. } - const string::size_type kMaxRootLength - = nsFileSpecHelpers::kMaxAltNameLength - suffix.length() - 1; - if (altName.length() > kMaxRootLength) - altName = altName.substr(0, kMaxRootLength); - while (Exists()) + const int kMaxRootLength + = nsFileSpecHelpers::kMaxCoreLeafNameLength - strlen(suffix) - 1; + if (strlen(leafName) > kMaxRootLength) + leafName[kMaxRootLength] = '\0'; + for (short index = 1; index < 1000 && Exists(); index++) { - // start with "Picture-2.jpg" after "Picture.jpg" exists - if ( ++index > 999 ) // something's very wrong - return; + // start with "Picture-1.jpg" after "Picture.jpg" exists char buf[nsFileSpecHelpers::kMaxFilenameLength + 1]; - std::ostrstream newName(buf, nsFileSpecHelpers::kMaxFilenameLength); - newName << altName.c_str() << "-" << index << suffix.c_str() << std::ends; + ostrstream newName(buf, nsFileSpecHelpers::kMaxFilenameLength); + newName << leafName << "-" << index << suffix << ends; SetLeafName(newName.str()); // or: SetLeafName(buf) } + if (*suffix) + delete [] suffix; + delete [] leafName; } // nsNativeFileSpec::MakeUnique + +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::operator = (const nsFileURL& inURL) +//---------------------------------------------------------------------------------------- +{ + *this = nsFilePath(inURL); // convert to unix path first +} + +//======================================================================================== +// UNIX & WIN nsNativeFileSpec implementation +//======================================================================================== + +#ifdef XP_UNIX +//---------------------------------------------------------------------------------------- +nsNativeFileSpec::nsNativeFileSpec(const nsFilePath& inPath) +//---------------------------------------------------------------------------------------- +: mPath(nsFileSpecHelpers::StringDup((char*)inPath)) +{ +} +#endif // XP_UNIX + +#ifdef XP_UNIX +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::operator = (const nsFilePath& inPath) +//---------------------------------------------------------------------------------------- +{ + nsFileSpecHelpers::StringAssign(mPath, (const char*)inPath); +} +#endif //XP_UNIX + +#if defined(XP_UNIX) || defined(XP_PC) +//---------------------------------------------------------------------------------------- +nsNativeFileSpec::nsNativeFileSpec(const nsNativeFileSpec& inSpec) +//---------------------------------------------------------------------------------------- +: mPath(nsFileSpecHelpers::StringDup((char*)inSpec)) +{ +} +#endif //XP_UNIX + +#if defined(XP_UNIX) || defined(XP_PC) +//---------------------------------------------------------------------------------------- +nsNativeFileSpec::nsNativeFileSpec(const char* inString) +//---------------------------------------------------------------------------------------- +: mPath(nsFileSpecHelpers::StringDup(inString)) +{ +} +#endif //XP_UNIX,PC + +//---------------------------------------------------------------------------------------- +nsNativeFileSpec::~nsNativeFileSpec() +//---------------------------------------------------------------------------------------- +{ +#ifndef XP_MAC + delete [] mPath; +#endif +} + +#if defined(XP_UNIX) || defined(XP_PC) +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::operator = (const nsNativeFileSpec& inSpec) +//---------------------------------------------------------------------------------------- +{ + mPath = nsFileSpecHelpers::StringAssign(mPath, inSpec.mPath); +} +#endif //XP_UNIX + + +#if defined(XP_UNIX) || defined(XP_PC) +//---------------------------------------------------------------------------------------- +nsNativeFileSpec::operator = (const char* inString) +//---------------------------------------------------------------------------------------- +{ + mPath = nsFileSpecHelpers::StringAssign(mPath, inString); +} +#endif //XP_UNIX + +#if (defined(XP_UNIX) || defined(XP_PC)) +//---------------------------------------------------------------------------------------- +ostream& operator << (ostream& s, const nsNativeFileSpec& spec) +//---------------------------------------------------------------------------------------- +{ + return (s << (const char*)spec.mPath); +} +#endif // DEBUG && XP_UNIX + diff --git a/mozilla/base/src/unix/nsFileSpecUnix.cpp b/mozilla/base/src/unix/nsFileSpecUnix.cpp index d9bce191a5a..e11b72906a9 100644 --- a/mozilla/base/src/unix/nsFileSpecUnix.cpp +++ b/mozilla/base/src/unix/nsFileSpecUnix.cpp @@ -20,16 +20,23 @@ // implementations. //---------------------------------------------------------------------------------------- -void nsNativeFileSpec::SetLeafName(const string& inLeafName) +void nsNativeFileSpec::SetLeafName(const char* inLeafName) //---------------------------------------------------------------------------------------- { nsFileHelpers::LeafReplace(mPath, '/', inLeafName); } // nsNativeFileSpec::SetLeafName //---------------------------------------------------------------------------------------- -string nsNativeFileSpec::GetLeafName() const +char* nsNativeFileSpec::GetLeafName() const //---------------------------------------------------------------------------------------- { return nsFileHelpers::GetLeaf(mPath, '/'); } // nsNativeFileSpec::GetLeafName +//---------------------------------------------------------------------------------------- +bool nsNativeFileSpec::Exists() const +//---------------------------------------------------------------------------------------- +{ + return false; // fixme +} // nsNativeFileSpec::Exists + diff --git a/mozilla/base/src/windows/nsFileSpecWin.cpp b/mozilla/base/src/windows/nsFileSpecWin.cpp index 89a3cd89eed..b97d62871bb 100644 --- a/mozilla/base/src/windows/nsFileSpecWin.cpp +++ b/mozilla/base/src/windows/nsFileSpecWin.cpp @@ -22,6 +22,7 @@ //---------------------------------------------------------------------------------------- nsNativeFileSpec::nsNativeFileSpec(const nsFilePath& inPath) //---------------------------------------------------------------------------------------- +: mPath(NULL) { *this = inPath; } @@ -30,41 +31,57 @@ nsNativeFileSpec::nsNativeFileSpec(const nsFilePath& inPath) void nsNativeFileSpec::operator = (const nsFilePath& inPath) //---------------------------------------------------------------------------------------- { + delete [] mPath; // Convert '/' to '\' - string& str = (string&)inPath; - for (string::size_type i = 0; i < str.length(); i++) + const char* str = (const char*)inSpec; + mPath = new char[1 + strlen(str); + const char* src = str; + char* dst = mPath; + for (const char* temp = str; *temp; src++,dst++) { - char c = str[i]; - if (c == '/') - c = '\\'; - mPath.append(&c, 1); + if (*src == '/') + *dst = '\\'; + else + *dst = *src; } } // nsNativeFileSpec::operator = //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const nsNativeFileSpec& inSpec) //---------------------------------------------------------------------------------------- +: mPath(NULL) { - // Convert '\' to '/' - string& str = (string&)inSpec; - for (string::size_type i = 0; i < str.length(); i++) - { - char c = str[i]; - if (c == '\\') - c = '/'; - mPath.append(&c, 1); - } + *this = inSpec; } // nsFilePath::nsFilePath //---------------------------------------------------------------------------------------- -void nsNativeFileSpec::SetLeafName(const string& inLeafName) +nsFilePath::operator = (const nsNativeFileSpec& inSpec) +//---------------------------------------------------------------------------------------- +{ + delete [] mPath; + // Convert '\' to '/' + const char* str = (const char*)inSpec; + mPath = new char[1 + strlen(str); + const char* src = str; + char* dst = mPath; + for (const char* temp = str; *temp; src++,dst++) + { + if (*src == '\\') + *dst = '/'; + else + *dst = *src; + } +} // nsFilePath::operator = + +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::SetLeafName(const char* inLeafName) //---------------------------------------------------------------------------------------- { nsFileSpecHelpers::LeafReplace(mPath, '\\', inLeafName); } // nsNativeFileSpec::SetLeafName //---------------------------------------------------------------------------------------- -string nsNativeFileSpec::GetLeafName() const +const char* nsNativeFileSpec::GetLeafName() const //---------------------------------------------------------------------------------------- { return nsFileSpecHelpers::GetLeaf(mPath, '\\'); diff --git a/mozilla/base/tests/FilesTest.cpp b/mozilla/base/tests/FilesTest.cpp index 17cf4af4a31..0c721ee242f 100644 --- a/mozilla/base/tests/FilesTest.cpp +++ b/mozilla/base/tests/FilesTest.cpp @@ -1,12 +1,12 @@ #include "nsFileSpec.h" -#include +#include #include "nsFileStream.h" -namespace FileTest +NS_NAMESPACE FileTest { void WriteStuff(ostream& s); -} +} NS_NAMESPACE_END //---------------------------------------------------------------------------------------- void FileTest::WriteStuff(ostream& s) @@ -14,11 +14,11 @@ void FileTest::WriteStuff(ostream& s) { // Initialize a URL from a string without suffix. Change the path to suit your machine. nsFileURL fileURL("file:///Development/MPW/MPW%20Shell"); - s << "File URL initialized to: \"" << (string&)fileURL << "\""<< endl; + s << "File URL initialized to: \"" << fileURL << "\""<< endl; // Initialize a Unix path from a URL nsFilePath filePath(fileURL); - s << "As a unix path: \"" << (string&)filePath << "\""<< endl; + s << "As a unix path: \"" << (const char*)filePath << "\""<< endl; // Initialize a native file spec from a URL nsNativeFileSpec fileSpec(fileURL); @@ -30,11 +30,11 @@ void FileTest::WriteStuff(ostream& s) // Assign the spec to a URL fileURL = fileSpec; - s << "File URL assigned from spec: \"" << (string&)fileURL << "\""<< endl; + s << "File URL assigned from spec: \"" << fileURL << "\""<< endl; // Assign a unix path using a string with a suffix. filePath = "/Development/MPW/SysErrs.err"; - s << "File path reassigned to: \"" << (string&)filePath << "\""<< endl; + s << "File path reassigned to: \"" << (const char*)filePath << "\""<< endl; // Assign to a file spec using a unix path. fileSpec = filePath; @@ -65,7 +65,7 @@ void main() nsFilePath myTextFilePath("/Development/iotest.txt"); { - cout << "WRITING IDENTICAL OUTPUT TO " << myTextFilePath << endl << endl; + cout << "WRITING IDENTICAL OUTPUT TO " << (const char*)myTextFilePath << endl << endl; nsOutputFileStream testStream(myTextFilePath); FileTest::WriteStuff(testStream); } // <-- Scope closes the stream (and the file). @@ -73,7 +73,7 @@ void main() // Test of nsInputFileStream { - cout << "READING BACK DATA FROM " << myTextFilePath << endl << endl; + cout << "READING BACK DATA FROM " << (const char*)myTextFilePath << endl << endl; nsInputFileStream testStream2(myTextFilePath); char line[1000]; while (!testStream2.eof()) diff --git a/mozilla/xpcom/io/nsFileSpec.cpp b/mozilla/xpcom/io/nsFileSpec.cpp index c9234159dd7..7cf82c25a4e 100644 --- a/mozilla/xpcom/io/nsFileSpec.cpp +++ b/mozilla/xpcom/io/nsFileSpec.cpp @@ -26,6 +26,11 @@ #include +#ifdef NS_USING_NAMESPACE + using std::ends; + using std::ostrstream; +#endif + //======================================================================================== NS_NAMESPACE nsFileSpecHelpers //======================================================================================== @@ -33,39 +38,94 @@ NS_NAMESPACE nsFileSpecHelpers enum { kMaxFilenameLength = 31 // should work on Macintosh, Unix, and Win32. , kMaxAltDigitLength = 5 - , kMaxAltNameLength = (kMaxFilenameLength - (kMaxAltDigitLength + 1)) + , kMaxCoreLeafNameLength = (kMaxFilenameLength - (kMaxAltDigitLength + 1)) }; NS_NAMESPACE_PROTOTYPE void LeafReplace( - string& ioPath, + char*& ioPath, char inSeparator, - const string& inLeafName); - NS_NAMESPACE_PROTOTYPE string GetLeaf(const string& inPath, char inSeparator); + const char* inLeafName); + NS_NAMESPACE_PROTOTYPE const char* GetLeaf(const char* inPath, char inSeparator); + 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_END +//---------------------------------------------------------------------------------------- +char* nsFileSpecHelpers::StringDup( + const char* inString, + int allocLength) +//---------------------------------------------------------------------------------------- +{ + if (!allocLength) + allocLength = strlen(inString); + char* newPath = new char[allocLength + 1]; + if (!newPath) + return NULL; + strcpy(newPath, inString); + return newPath; +} // nsFileSpecHelpers::StringDup + +//---------------------------------------------------------------------------------------- +char* nsFileSpecHelpers::AllocCat( + const char* inString1, + const char* inString2) +//---------------------------------------------------------------------------------------- +{ + 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 (strlen(inString2) > strlen(ioString)) + { + delete [] ioString; + ioString = StringDup(inString2); + } + else + strcpy(ioString, inString2); + return ioString; +} // nsFileSpecHelpers::StringAssign + //---------------------------------------------------------------------------------------- void nsFileSpecHelpers::LeafReplace( - string& ioPath, + char*& ioPath, char inSeparator, - const string& inLeafName) + const char* inLeafName) //---------------------------------------------------------------------------------------- { // Find the existing leaf name - string::size_type lastSeparator = ioPath.rfind(inSeparator); - string::size_type myLength = ioPath.length(); - if (lastSeparator < myLength) - ioPath = ioPath.substr(0, lastSeparator + 1) + inLeafName; + if (!ioPath) + return; + char* lastSeparator = strrchr(ioPath, inSeparator); + int oldLength = strlen(ioPath); + *(++lastSeparator) = '\0'; // strip the current leaf name + int newLength = lastSeparator - ioPath + strlen(inLeafName); + if (newLength > oldLength) + { + char* newPath = StringDup(ioPath, newLength + 1); + delete [] ioPath; + ioPath = newPath; + } + strcat(ioPath, inLeafName); } // nsNativeFileSpec::SetLeafName //---------------------------------------------------------------------------------------- -string nsFileSpecHelpers::GetLeaf(const string& inPath, char inSeparator) +const char* nsFileSpecHelpers::GetLeaf(const char* inPath, char inSeparator) +// Returns a pointer to an allocated string representing the leaf. //---------------------------------------------------------------------------------------- { - string::size_type lastSeparator = inPath.rfind(inSeparator); - string::size_type myLength = inPath.length(); - if (lastSeparator < myLength) - return inPath.substr(1 + lastSeparator, myLength - lastSeparator - 1); - return inPath; -} // nsNativeFileSpec::GetLeafName + char* lastSeparator = strrchr(inPath, inSeparator); + if (lastSeparator) + return StringDup(lastSeparator); + return StringDup(inPath); +} // nsNativeFileSpec::GetLeaf #ifdef XP_MAC @@ -81,84 +141,101 @@ string nsFileSpecHelpers::GetLeaf(const string& inPath, char inSeparator) //======================================================================================== //---------------------------------------------------------------------------------------- -nsFileURL::nsFileURL(const string& inString) +nsFileURL::nsFileURL(const char* inString) //---------------------------------------------------------------------------------------- -: mURL(inString) +: mURL(nsFileSpecHelpers::StringDup(inString)) #ifdef XP_MAC -, mNativeFileSpec(inString.substr(kFileURLPrefixLength, inString.length() - kFileURLPrefixLength)) +, mNativeFileSpec(inString + kFileURLPrefixLength) #endif { - NS_ASSERTION(mURL.substr(0, kFileURLPrefixLength) == kFileURLPrefix, "Not a URL!"); -} - -//---------------------------------------------------------------------------------------- -void nsFileURL::operator = (const string& inString) -//---------------------------------------------------------------------------------------- -{ - mURL = inString; - NS_ASSERTION(mURL.substr(0, kFileURLPrefixLength) == kFileURLPrefix, "Not a URL!"); -#ifdef XP_MAC - mNativeFileSpec = - inString.substr(kFileURLPrefixLength, inString.length() - kFileURLPrefixLength); -#endif -} + NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!"); +} // nsFileURL::nsFileURL //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFileURL& inOther) //---------------------------------------------------------------------------------------- -: mURL(inOther.mURL) +: mURL(nsFileSpecHelpers::StringDup(inOther.mURL)) #ifdef XP_MAC , mNativeFileSpec(inOther.GetNativeSpec()) #endif { +} // nsFileURL::nsFileURL + +//---------------------------------------------------------------------------------------- +nsFileURL::nsFileURL(const nsFilePath& inOther) +//---------------------------------------------------------------------------------------- +: mURL(nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)inOther)) +#ifdef XP_MAC +, mNativeFileSpec(inOther.GetNativeSpec()) +#endif +{ +} // nsFileURL::nsFileURL + +//---------------------------------------------------------------------------------------- +nsFileURL::nsFileURL(const nsNativeFileSpec& inOther) +: mURL(nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)nsFilePath(inOther))) +#ifdef XP_MAC +, mNativeFileSpec(inOther) +#endif +//---------------------------------------------------------------------------------------- +{ +} // nsFileURL::nsFileURL + +//---------------------------------------------------------------------------------------- +nsFileURL::~nsFileURL() +//---------------------------------------------------------------------------------------- +{ + delete [] mURL; } +//---------------------------------------------------------------------------------------- +void nsFileURL::operator = (const char* inString) +//---------------------------------------------------------------------------------------- +{ + nsFileSpecHelpers::StringAssign(mURL, inString); + NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!"); +#ifdef XP_MAC + mNativeFileSpec = inString + kFileURLPrefixLength; +#endif +} // nsFileURL::operator = + //---------------------------------------------------------------------------------------- void nsFileURL::operator = (const nsFileURL& inOther) //---------------------------------------------------------------------------------------- { - mURL = inOther.mURL; + mURL = nsFileSpecHelpers::StringAssign(mURL, inOther.mURL); #ifdef XP_MAC mNativeFileSpec = inOther.GetNativeSpec(); #endif -} +} // nsFileURL::operator = -//---------------------------------------------------------------------------------------- -nsFileURL::nsFileURL(const nsFilePath& inOther) -//---------------------------------------------------------------------------------------- -{ - mURL = kFileURLPrefix + ((std::string&)inOther); -#ifdef XP_MAC - mNativeFileSpec = inOther.GetNativeSpec(); -#endif -} //---------------------------------------------------------------------------------------- void nsFileURL::operator = (const nsFilePath& inOther) //---------------------------------------------------------------------------------------- { - mURL = kFileURLPrefix + ((std::string&)inOther); + delete [] mURL; + mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)inOther); #ifdef XP_MAC mNativeFileSpec = inOther.GetNativeSpec(); #endif -} +} // nsFileURL::operator = -//---------------------------------------------------------------------------------------- -nsFileURL::nsFileURL(const nsNativeFileSpec& inOther) -//---------------------------------------------------------------------------------------- -{ - mURL = kFileURLPrefix + (string&)nsFilePath(inOther); -#ifdef XP_MAC - mNativeFileSpec = inOther; -#endif -} //---------------------------------------------------------------------------------------- void nsFileURL::operator = (const nsNativeFileSpec& inOther) //---------------------------------------------------------------------------------------- { - mURL = kFileURLPrefix + (string&)nsFilePath(inOther); + delete [] mURL; + mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)nsFilePath(inOther)); #ifdef XP_MAC mNativeFileSpec = inOther; #endif +} // nsFileURL::operator = + +//---------------------------------------------------------------------------------------- +ostream& operator << (ostream& s, const nsFileURL& url) +//---------------------------------------------------------------------------------------- +{ + return (s << url.mURL); } //======================================================================================== @@ -166,44 +243,67 @@ void nsFileURL::operator = (const nsNativeFileSpec& inOther) //======================================================================================== //---------------------------------------------------------------------------------------- -nsFilePath::nsFilePath(const string& inString) +nsFilePath::nsFilePath(const char* inString) //---------------------------------------------------------------------------------------- -: mPath(inString) +: mPath(nsFileSpecHelpers::StringDup(inString)) #ifdef XP_MAC , mNativeFileSpec(inString) #endif { - NS_ASSERTION(mPath.substr(0, kFileURLPrefixLength) != kFileURLPrefix, "URL passed as path"); + NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path"); } //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const nsFileURL& inOther) //---------------------------------------------------------------------------------------- -: mPath(((std::string&)inOther).substr( - kFileURLPrefixLength, ((std::string&)inOther).length() - kFileURLPrefixLength)) +: mPath(nsFileSpecHelpers::StringDup(inOther.mURL + kFileURLPrefixLength)) #ifdef XP_MAC , mNativeFileSpec(inOther.GetNativeSpec()) #endif { } +#ifdef XP_UNIX //---------------------------------------------------------------------------------------- -void nsFilePath::operator = (const string& inString) +nsFilePath::nsFilePath(const nsNativeFileSpec& inOther) +//---------------------------------------------------------------------------------------- +: mPath(nsFileSpecHelpers::StringDup(inOther.mPath)) +{ +} +#endif // XP_UNIX + +//---------------------------------------------------------------------------------------- +nsFilePath::~nsFilePath() //---------------------------------------------------------------------------------------- { - mPath = inString; + delete [] mPath; +} + +#ifdef XP_UNIX +//---------------------------------------------------------------------------------------- +void nsFilePath::operator = (const nsNativeFileSpec& inOther) +//---------------------------------------------------------------------------------------- +{ + mPath = nsFileSpecHelpers::StringAssign(mPath, inOther.mPath); +} +#endif // XP_UNIX + +//---------------------------------------------------------------------------------------- +void nsFilePath::operator = (const char* inString) +//---------------------------------------------------------------------------------------- +{ + NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path"); + nsFileSpecHelpers::StringAssign(mPath, inString); #ifdef XP_MAC mNativeFileSpec = inString; #endif - NS_ASSERTION(mPath.substr(0, kFileURLPrefixLength) != kFileURLPrefix, "URL passed as path"); } //---------------------------------------------------------------------------------------- void nsFilePath::operator = (const nsFileURL& inOther) //---------------------------------------------------------------------------------------- { - mPath = ((std::string&)inOther).substr( - kFileURLPrefixLength, ((std::string&)inOther).length() - kFileURLPrefixLength); + nsFileSpecHelpers::StringAssign(mPath, (const char*)nsFilePath(inOther)); #ifdef XP_MAC mNativeFileSpec = inOther.GetNativeSpec(); #endif @@ -217,15 +317,26 @@ void nsFilePath::operator = (const nsFileURL& inOther) //---------------------------------------------------------------------------------------- nsNativeFileSpec::nsNativeFileSpec() //---------------------------------------------------------------------------------------- +: mPath(NULL) { } #endif //---------------------------------------------------------------------------------------- -void nsNativeFileSpec::MakeUnique(const string& inSuggestedLeafName) +nsNativeFileSpec::nsNativeFileSpec(const nsFileURL& inURL) +//---------------------------------------------------------------------------------------- +#ifndef XP_MAC +: mPath(NULL) +#endif +{ + *this = nsFilePath(inURL); // convert to unix path first +} + +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::MakeUnique(const char* inSuggestedLeafName) //---------------------------------------------------------------------------------------- { - if (inSuggestedLeafName.length() > 0) + if (inSuggestedLeafName && *inSuggestedLeafName) SetLeafName(inSuggestedLeafName); MakeUnique(); @@ -238,27 +349,115 @@ void nsNativeFileSpec::MakeUnique() if (!Exists()) return; - short index = 0; - string altName = GetLeafName(); - string::size_type lastDot = altName.rfind('.'); - string suffix; - if (lastDot < altName.length()) + char* leafName = GetLeafName(); + if (!leafName) + return; + + char* lastDot = strrchr(leafName, '.'); + char* suffix = ""; + if (lastDot) { - suffix = altName.substr(lastDot, altName.length() - lastDot); // include '.' - altName = altName.substr(0, lastDot); + suffix = nsFileSpecHelpers::StringDup(lastDot); // include '.' + *lastDot = '\0'; // strip suffix and dot. } - const string::size_type kMaxRootLength - = nsFileSpecHelpers::kMaxAltNameLength - suffix.length() - 1; - if (altName.length() > kMaxRootLength) - altName = altName.substr(0, kMaxRootLength); - while (Exists()) + const int kMaxRootLength + = nsFileSpecHelpers::kMaxCoreLeafNameLength - strlen(suffix) - 1; + if (strlen(leafName) > kMaxRootLength) + leafName[kMaxRootLength] = '\0'; + for (short index = 1; index < 1000 && Exists(); index++) { - // start with "Picture-2.jpg" after "Picture.jpg" exists - if ( ++index > 999 ) // something's very wrong - return; + // start with "Picture-1.jpg" after "Picture.jpg" exists char buf[nsFileSpecHelpers::kMaxFilenameLength + 1]; - std::ostrstream newName(buf, nsFileSpecHelpers::kMaxFilenameLength); - newName << altName.c_str() << "-" << index << suffix.c_str() << std::ends; + ostrstream newName(buf, nsFileSpecHelpers::kMaxFilenameLength); + newName << leafName << "-" << index << suffix << ends; SetLeafName(newName.str()); // or: SetLeafName(buf) } + if (*suffix) + delete [] suffix; + delete [] leafName; } // nsNativeFileSpec::MakeUnique + +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::operator = (const nsFileURL& inURL) +//---------------------------------------------------------------------------------------- +{ + *this = nsFilePath(inURL); // convert to unix path first +} + +//======================================================================================== +// UNIX & WIN nsNativeFileSpec implementation +//======================================================================================== + +#ifdef XP_UNIX +//---------------------------------------------------------------------------------------- +nsNativeFileSpec::nsNativeFileSpec(const nsFilePath& inPath) +//---------------------------------------------------------------------------------------- +: mPath(nsFileSpecHelpers::StringDup((char*)inPath)) +{ +} +#endif // XP_UNIX + +#ifdef XP_UNIX +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::operator = (const nsFilePath& inPath) +//---------------------------------------------------------------------------------------- +{ + nsFileSpecHelpers::StringAssign(mPath, (const char*)inPath); +} +#endif //XP_UNIX + +#if defined(XP_UNIX) || defined(XP_PC) +//---------------------------------------------------------------------------------------- +nsNativeFileSpec::nsNativeFileSpec(const nsNativeFileSpec& inSpec) +//---------------------------------------------------------------------------------------- +: mPath(nsFileSpecHelpers::StringDup((char*)inSpec)) +{ +} +#endif //XP_UNIX + +#if defined(XP_UNIX) || defined(XP_PC) +//---------------------------------------------------------------------------------------- +nsNativeFileSpec::nsNativeFileSpec(const char* inString) +//---------------------------------------------------------------------------------------- +: mPath(nsFileSpecHelpers::StringDup(inString)) +{ +} +#endif //XP_UNIX,PC + +//---------------------------------------------------------------------------------------- +nsNativeFileSpec::~nsNativeFileSpec() +//---------------------------------------------------------------------------------------- +{ +#ifndef XP_MAC + delete [] mPath; +#endif +} + +#if defined(XP_UNIX) || defined(XP_PC) +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::operator = (const nsNativeFileSpec& inSpec) +//---------------------------------------------------------------------------------------- +{ + mPath = nsFileSpecHelpers::StringAssign(mPath, inSpec.mPath); +} +#endif //XP_UNIX + + +#if defined(XP_UNIX) || defined(XP_PC) +//---------------------------------------------------------------------------------------- +nsNativeFileSpec::operator = (const char* inString) +//---------------------------------------------------------------------------------------- +{ + mPath = nsFileSpecHelpers::StringAssign(mPath, inString); +} +#endif //XP_UNIX + +#if (defined(XP_UNIX) || defined(XP_PC)) +//---------------------------------------------------------------------------------------- +ostream& operator << (ostream& s, const nsNativeFileSpec& spec) +//---------------------------------------------------------------------------------------- +{ + return (s << (const char*)spec.mPath); +} +#endif // DEBUG && XP_UNIX + diff --git a/mozilla/xpcom/io/nsFileSpec.h b/mozilla/xpcom/io/nsFileSpec.h index cc590c7c11c..80ba05a9072 100644 --- a/mozilla/xpcom/io/nsFileSpec.h +++ b/mozilla/xpcom/io/nsFileSpec.h @@ -103,7 +103,7 @@ #ifndef _FILESPEC_H_ #define _FILESPEC_H_ -#include +#include //======================================================================================== // Compiler-specific macros, as needed @@ -116,7 +116,6 @@ #define NS_NAMESPACE_PROTOTYPE #define NS_NAMESPACE namespace #define NS_NAMESPACE_END - using std::string; using std::ostream; #else #define NS_NAMESPACE_PROTOTYPE static @@ -126,6 +125,7 @@ //=========================== End Compiler-specific macros =============================== #include "nsDebug.h" + #ifdef XP_MAC #include #endif @@ -149,19 +149,20 @@ class nsNativeFileSpec { public: nsNativeFileSpec(); - explicit nsNativeFileSpec(const string& inString); - nsNativeFileSpec(const nsFilePath& inPath); - nsNativeFileSpec(const nsFileURL& inURL); + explicit nsNativeFileSpec(const char* inString); + explicit nsNativeFileSpec(const nsFilePath& inPath); + explicit nsNativeFileSpec(const nsFileURL& inURL); nsNativeFileSpec(const nsNativeFileSpec& inPath); + virtual ~nsNativeFileSpec(); - void operator = (const string& inPath); + void operator = (const char* inPath); void operator = (const nsFilePath& inPath); void operator = (const nsFileURL& inURL); void operator = (const nsNativeFileSpec& inOther); #ifdef XP_MAC // For Macintosh people, this is meant to be useful in its own right as a C++ version - // of the FSSPec class. + // of the FSSpec struct. nsNativeFileSpec( short vRefNum, long parID, @@ -171,25 +172,34 @@ class nsNativeFileSpec operator FSSpec* () { return &mSpec; } operator const FSSpec* const () { return &mSpec; } - operator FSSpec& () { return mSpec; } + operator FSSpec& () { return mSpec; } operator const FSSpec& () const { return mSpec; } - bool Valid() const { return mError == noErr; } OSErr Error() const { return mError; } + void MakeAliasSafe(); + // Called for the spec of an alias. Copies the alias to + // a secret temp directory and modifies the spec to point + // to it. Sets mError. + void ResolveAlias(bool& wasAliased); + // Called for the spec of an alias. Modifies the spec to + // point to the original. Sets mError. void MakeUnique(ConstStr255Param inSuggestedLeafName); StringPtr GetLeafPName() { return mSpec.name; } ConstStr255Param GetLeafPName() const { return mSpec.name; } +#endif // end of Macintosh utility methods. + +#ifdef XP_MAC + bool Valid() const { return mError == noErr; } #else bool Valid() const { return true; } // Fixme. -#endif +#endif // XP_MAC -#if DEBUG friend ostream& operator << (ostream& s, const nsNativeFileSpec& spec); -#endif - string GetLeafName() const; - void SetLeafName(const string& inLeafName); + + char* GetLeafName() const; // Allocated. Use delete []. + void SetLeafName(const char* inLeafName); bool Exists() const; void MakeUnique(); - void MakeUnique(const string& inSuggestedLeafName); + void MakeUnique(const char* inSuggestedLeafName); private: friend class nsFilePath; @@ -197,7 +207,7 @@ class nsNativeFileSpec FSSpec mSpec; OSErr mError; #elif defined(XP_UNIX) || defined(XP_PC) - string mPath; + char* mPath; #endif }; // class nsNativeFileSpec @@ -210,29 +220,33 @@ class nsFileURL { public: nsFileURL(const nsFileURL& inURL); - explicit nsFileURL(const string& inString); - nsFileURL(const nsFilePath& inPath); - nsFileURL(const nsNativeFileSpec& inPath); + explicit nsFileURL(const char* inString); + explicit nsFileURL(const nsFilePath& inPath); + explicit nsFileURL(const nsNativeFileSpec& inPath); + virtual ~nsFileURL(); -// string GetString() const { return mPath; } +// nsString GetString() const { return mPath; } // may be needed for implementation reasons, // but should not provide a conversion constructor. void operator = (const nsFileURL& inURL); - void operator = (const string& inString); + void operator = (const char* inString); void operator = (const nsFilePath& inOther); void operator = (const nsNativeFileSpec& inOther); + friend ostream& operator << (ostream& s, const nsFileURL& spec); + #ifdef XP_MAC // Accessor to allow quick assignment to a mNativeFileSpec const nsNativeFileSpec& GetNativeSpec() const { return mNativeFileSpec; } #endif private: // Should not be defined (only nsFilePath is to be treated as strings. - operator string& (); + operator char* (); + operator const char* const (); private: - - string mURL; + friend class nsFilePath; // to allow construction of nsFilePath + char* 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 nsNativeFileSpec, too. @@ -248,22 +262,23 @@ class nsFilePath { public: nsFilePath(const nsFilePath& inPath); - explicit nsFilePath(const string& inString); - nsFilePath(const nsFileURL& inURL); - nsFilePath(const nsNativeFileSpec& inPath); + explicit nsFilePath(const char* inString); + explicit nsFilePath(const nsFileURL& inURL); + explicit nsFilePath(const nsNativeFileSpec& inPath); + virtual ~nsFilePath(); - operator const char* () const { return mPath.c_str(); } + operator const char* () const { return mPath; } // This is the only automatic conversion to const char* // that is provided, and it allows the // path to be "passed" to NSPR file routines. - operator string& () { return mPath; } + 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 string& inString); + void operator = (const char* inString); void operator = (const nsFileURL& inURL); void operator = (const nsNativeFileSpec& inOther); @@ -275,7 +290,7 @@ class nsFilePath private: - string mPath; + char* 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 nsNativeFileSpec, too. @@ -283,109 +298,4 @@ class nsFilePath #endif }; // class nsFilePath -#ifdef XP_UNIX -//======================================================================================== -// UNIX nsFilePath implementation -//======================================================================================== - -//---------------------------------------------------------------------------------------- -inline nsFilePath::nsFilePath(const nsNativeFileSpec& inOther) -//---------------------------------------------------------------------------------------- -: mPath((string&)inOther) -{ -} -//---------------------------------------------------------------------------------------- -inline void nsFilePath::operator = (const nsNativeFileSpec& inOther) -//---------------------------------------------------------------------------------------- -{ - mPath = (string&)inOther; -} -#endif // XP_UNIX - -//======================================================================================== -// COMMON nsNativeFileSpec implementation -//======================================================================================== - -//---------------------------------------------------------------------------------------- -inline nsNativeFileSpec::nsNativeFileSpec(const nsFileURL& inURL) -//---------------------------------------------------------------------------------------- -{ - *this = nsFilePath(inURL); // convert to unix path first -} - -//---------------------------------------------------------------------------------------- -inline void nsNativeFileSpec::operator = (const nsFileURL& inURL) -//---------------------------------------------------------------------------------------- -{ - *this = nsFilePath(inURL); // convert to unix path first -} - -//======================================================================================== -// UNIX & WIN nsNativeFileSpec implementation -//======================================================================================== - -#ifdef XP_UNIX -//---------------------------------------------------------------------------------------- -inline nsNativeFileSpec::nsNativeFileSpec(const nsFilePath& inPath) -//---------------------------------------------------------------------------------------- -: mPath((string&)inPath) -{ -} -#endif // XP_UNIX - -#ifdef XP_UNIX -//---------------------------------------------------------------------------------------- -inline void nsNativeFileSpec::operator = (const nsFilePath& inPath) -//---------------------------------------------------------------------------------------- -{ - mPath = (string&)inPath; -} -#endif //XP_UNIX - -#if defined(XP_UNIX) || defined(XP_PC) -//---------------------------------------------------------------------------------------- -inline nsNativeFileSpec::nsNativeFileSpec(const nsNativeFileSpec& inSpec) -//---------------------------------------------------------------------------------------- -: mPath((string&)inSpec) -{ -} -#endif //XP_UNIX - -#if defined(XP_UNIX) || defined(XP_PC) -//---------------------------------------------------------------------------------------- -inline nsNativeFileSpec::nsNativeFileSpec(const string& inString) -//---------------------------------------------------------------------------------------- -: mPath(inString) -{ -} -#endif //XP_UNIX - -#if defined(XP_UNIX) || defined(XP_PC) -//---------------------------------------------------------------------------------------- -inline void nsNativeFileSpec::operator = (const nsNativeFileSpec& inSpec) -//---------------------------------------------------------------------------------------- -{ - mPath = (string&)inSpec; -} -#endif //XP_UNIX - - -#if defined(XP_UNIX) || defined(XP_PC) -//---------------------------------------------------------------------------------------- -inline nsNativeFileSpec::operator = (const string& inString) -//---------------------------------------------------------------------------------------- -{ - mPath = inString; -} -#endif //XP_UNIX - -#if (defined(XP_UNIX) || defined(XP_PC)) && DEBUG -//---------------------------------------------------------------------------------------- -inline ostream& operator << (ostream& s, const nsNativeFileSpec& spec) -//---------------------------------------------------------------------------------------- -{ - return (s << (string&)spec.mPath); -} -#endif // DEBUG && XP_UNIX - #endif // _FILESPEC_H_ diff --git a/mozilla/xpcom/io/nsFileSpecMac.cpp b/mozilla/xpcom/io/nsFileSpecMac.cpp index 22df2e18366..60336496c34 100644 --- a/mozilla/xpcom/io/nsFileSpecMac.cpp +++ b/mozilla/xpcom/io/nsFileSpecMac.cpp @@ -39,6 +39,8 @@ namespace MacFileHelpers memcpy(dst, src, 1 + src[0]); } + void PLstrcpy(Str255 dst, const char* src, int inMaxLen); + void SwapSlashColon(char * s); OSErr FSSpecFromFullUnixPath( const char * unixPath, @@ -67,7 +69,7 @@ namespace MacFileHelpers // to support attaching of aliases in mail. void EnsureAliasHaven(); void SetNoResolve(Boolean inResolve); - Boolean IsAliasSafe(const FSSpec& inSpec); + bool IsAliasSafe(const FSSpec& inSpec); OSErr MakeAliasSafe(FSSpec& inOutSpec); OSErr ResolveAliasFile(FSSpec& inOutSpec, Boolean& wasAliased); @@ -76,6 +78,18 @@ namespace MacFileHelpers short sAliasHavenVRefNum = 0; } // namespace MacFileHelpers +//---------------------------------------------------------------------------------------- +void MacFileHelpers::PLstrcpy(Str255 dst, const char* src, int inMax) +//======================================================================================== +{ + int srcLength = strlen(src); + NS_ASSERTION(srcLength <= inMax, "Oops, string is too long!"); + if (srcLength > inMax) + srcLength = inMax; + dst[0] = srcLength; + memcpy(&dst[1], src, srcLength); +} + //----------------------------------- void MacFileHelpers::SwapSlashColon(char * s) //----------------------------------- @@ -180,16 +194,6 @@ OSErr MacFileHelpers::MakeAliasSafe(FSSpec& inOutSpec) return noErr; } // MacFileHelpers::MakeAliasSafe -//---------------------------------------------------------------------------------------- -void nsNativeFileSpec::MakeUnique(ConstStr255Param inSuggestedLeafName) -//---------------------------------------------------------------------------------------- -{ - if (inSuggestedLeafName[0] > 0) - MacFileHelpers::PLstrcpy(mSpec.name, inSuggestedLeafName); - - MakeUnique(); -} - //---------------------------------------------------------------------------------------- char* MacFileHelpers::MacPathFromUnixPath(const char* unixPath) //---------------------------------------------------------------------------------------- @@ -290,7 +294,7 @@ void MacFileHelpers::EnsureAliasHaven() } // MacFileHelpers::EnsureAliasHaven //---------------------------------------------------------------------------------------- -Boolean MacFileHelpers::IsAliasSafe(const FSSpec& inSpec) +bool MacFileHelpers::IsAliasSafe(const FSSpec& inSpec) // Returns true if the alias is in the alias haven directory, or if alias resolution // has been turned off. //---------------------------------------------------------------------------------------- @@ -453,16 +457,16 @@ nsNativeFileSpec::nsNativeFileSpec() //---------------------------------------------------------------------------------------- nsNativeFileSpec::nsNativeFileSpec(const nsNativeFileSpec& inSpec) //---------------------------------------------------------------------------------------- -: mSpec((FSSpec&)inSpec) +: mSpec(inSpec.mSpec) , mError(inSpec.Error()) { } //---------------------------------------------------------------------------------------- -nsNativeFileSpec::nsNativeFileSpec(const string& inString) +nsNativeFileSpec::nsNativeFileSpec(const char* inString) //---------------------------------------------------------------------------------------- { - mError = MacFileHelpers::FSSpecFromFullUnixPath(inString.c_str(), mSpec, true); + mError = MacFileHelpers::FSSpecFromFullUnixPath(inString, mSpec, true); if (mError == fnfErr) mError = noErr; } // nsNativeFileSpec::nsNativeFileSpec @@ -486,30 +490,27 @@ nsNativeFileSpec::nsNativeFileSpec(const nsFilePath& inPath) *this = inPath.GetNativeSpec(); } -#if DEBUG //---------------------------------------------------------------------------------------- ostream& operator << (ostream& s, const nsNativeFileSpec& spec) //---------------------------------------------------------------------------------------- { s << spec.mSpec.vRefNum << ", " << spec.mSpec.parID << ", \""; - for (int i = 1; i <= spec.mSpec.name[0]; i++) - s.put(spec.mSpec.name[i]); + s.write((const char*)&spec.mSpec.name[1], spec.mSpec.name[0]); return s << "\""; } // ostream& operator << (ostream&, const nsNativeFileSpec&) -#endif // DEBUG //---------------------------------------------------------------------------------------- -void nsNativeFileSpec::operator = (const string& inString) +void nsNativeFileSpec::operator = (const char* inString) //---------------------------------------------------------------------------------------- { - mError = MacFileHelpers::FSSpecFromFullUnixPath(inString.c_str(), mSpec, true); + mError = MacFileHelpers::FSSpecFromFullUnixPath(inString, mSpec, true); } // nsNativeFileSpec::operator = //---------------------------------------------------------------------------------------- void nsNativeFileSpec::operator = (const nsNativeFileSpec& inSpec) //---------------------------------------------------------------------------------------- { - mSpec = inSpec; + mSpec = inSpec.mSpec; mError = inSpec.Error(); } // nsNativeFileSpec::operator = @@ -517,7 +518,7 @@ void nsNativeFileSpec::operator = (const nsNativeFileSpec& inSpec) void nsNativeFileSpec::operator = (const nsFilePath& inPath) //---------------------------------------------------------------------------------------- { - mSpec = inPath.GetNativeSpec(); + *this = inPath.GetNativeSpec(); } // nsNativeFileSpec::operator = //---------------------------------------------------------------------------------------- @@ -529,23 +530,49 @@ bool nsNativeFileSpec::Exists() const } // nsNativeFileSpec::operator = //---------------------------------------------------------------------------------------- -void nsNativeFileSpec::SetLeafName(const string& inLeafName) +void nsNativeFileSpec::SetLeafName(const char* inLeafName) //---------------------------------------------------------------------------------------- { - *mSpec.name = inLeafName.length(); - memcpy(mSpec.name + 1, inLeafName.c_str(), 1 + *mSpec.name); + MacFileHelpers::PLstrcpy(mSpec.name, inLeafName, nsFileSpecHelpers::kMaxFilenameLength); } // nsNativeFileSpec::SetLeafName //---------------------------------------------------------------------------------------- -string nsNativeFileSpec::GetLeafName() const +char* nsNativeFileSpec::GetLeafName() const +// Result needs to be delete[]ed. //---------------------------------------------------------------------------------------- { char leaf[64]; memcpy(leaf, &mSpec.name[1], mSpec.name[0]); leaf[mSpec.name[0]] = '\0'; - return string(leaf); + return nsFileSpecHelpers::StringDup(leaf); } // nsNativeFileSpec::GetLeafName +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::MakeAliasSafe() +//---------------------------------------------------------------------------------------- +{ + mError = MacFileHelpers::MakeAliasSafe(mSpec); +} // nsNativeFileSpec::MakeAliasSafe + +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::MakeUnique(ConstStr255Param inSuggestedLeafName) +//---------------------------------------------------------------------------------------- +{ + if (inSuggestedLeafName[0] > 0) + MacFileHelpers::PLstrcpy(mSpec.name, inSuggestedLeafName); + + MakeUnique(); +} // nsNativeFileSpec::MakeUnique + +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::ResolveAlias(bool& wasAliased) +//---------------------------------------------------------------------------------------- +{ + Boolean wasAliased2; + mError = MacFileHelpers::ResolveAliasFile(mSpec, wasAliased2); + wasAliased = (wasAliased2 != false); +} // nsNativeFileSpec::ResolveAlias + //======================================================================================== // Macintosh nsFilePath implementation //======================================================================================== @@ -553,25 +580,18 @@ string nsNativeFileSpec::GetLeafName() const //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const nsNativeFileSpec& inSpec) //---------------------------------------------------------------------------------------- -#ifdef XP_MAC : mNativeFileSpec(inSpec) -#endif { char * path = MacFileHelpers::PathNameFromFSSpec( inSpec.mSpec, TRUE ); - char * unixPath = MacFileHelpers::EncodeMacPath(path, true, true); - mPath = unixPath; - delete [] unixPath; + mPath = MacFileHelpers::EncodeMacPath(path, true, true); } //---------------------------------------------------------------------------------------- void nsFilePath::operator = (const nsNativeFileSpec& inSpec) //---------------------------------------------------------------------------------------- { + delete [] mPath; char * path = MacFileHelpers::PathNameFromFSSpec( inSpec.mSpec, TRUE ); - char * unixPath = MacFileHelpers::EncodeMacPath(path, true, true); - mPath = unixPath; - delete [] unixPath; -#ifdef XP_MAC + mPath = MacFileHelpers::EncodeMacPath(path, true, true); mNativeFileSpec = inSpec; -#endif } // nsFilePath::operator = diff --git a/mozilla/xpcom/io/nsFileSpecUnix.cpp b/mozilla/xpcom/io/nsFileSpecUnix.cpp index d9bce191a5a..e11b72906a9 100644 --- a/mozilla/xpcom/io/nsFileSpecUnix.cpp +++ b/mozilla/xpcom/io/nsFileSpecUnix.cpp @@ -20,16 +20,23 @@ // implementations. //---------------------------------------------------------------------------------------- -void nsNativeFileSpec::SetLeafName(const string& inLeafName) +void nsNativeFileSpec::SetLeafName(const char* inLeafName) //---------------------------------------------------------------------------------------- { nsFileHelpers::LeafReplace(mPath, '/', inLeafName); } // nsNativeFileSpec::SetLeafName //---------------------------------------------------------------------------------------- -string nsNativeFileSpec::GetLeafName() const +char* nsNativeFileSpec::GetLeafName() const //---------------------------------------------------------------------------------------- { return nsFileHelpers::GetLeaf(mPath, '/'); } // nsNativeFileSpec::GetLeafName +//---------------------------------------------------------------------------------------- +bool nsNativeFileSpec::Exists() const +//---------------------------------------------------------------------------------------- +{ + return false; // fixme +} // nsNativeFileSpec::Exists + diff --git a/mozilla/xpcom/io/nsFileSpecWin.cpp b/mozilla/xpcom/io/nsFileSpecWin.cpp index 89a3cd89eed..b97d62871bb 100644 --- a/mozilla/xpcom/io/nsFileSpecWin.cpp +++ b/mozilla/xpcom/io/nsFileSpecWin.cpp @@ -22,6 +22,7 @@ //---------------------------------------------------------------------------------------- nsNativeFileSpec::nsNativeFileSpec(const nsFilePath& inPath) //---------------------------------------------------------------------------------------- +: mPath(NULL) { *this = inPath; } @@ -30,41 +31,57 @@ nsNativeFileSpec::nsNativeFileSpec(const nsFilePath& inPath) void nsNativeFileSpec::operator = (const nsFilePath& inPath) //---------------------------------------------------------------------------------------- { + delete [] mPath; // Convert '/' to '\' - string& str = (string&)inPath; - for (string::size_type i = 0; i < str.length(); i++) + const char* str = (const char*)inSpec; + mPath = new char[1 + strlen(str); + const char* src = str; + char* dst = mPath; + for (const char* temp = str; *temp; src++,dst++) { - char c = str[i]; - if (c == '/') - c = '\\'; - mPath.append(&c, 1); + if (*src == '/') + *dst = '\\'; + else + *dst = *src; } } // nsNativeFileSpec::operator = //---------------------------------------------------------------------------------------- nsFilePath::nsFilePath(const nsNativeFileSpec& inSpec) //---------------------------------------------------------------------------------------- +: mPath(NULL) { - // Convert '\' to '/' - string& str = (string&)inSpec; - for (string::size_type i = 0; i < str.length(); i++) - { - char c = str[i]; - if (c == '\\') - c = '/'; - mPath.append(&c, 1); - } + *this = inSpec; } // nsFilePath::nsFilePath //---------------------------------------------------------------------------------------- -void nsNativeFileSpec::SetLeafName(const string& inLeafName) +nsFilePath::operator = (const nsNativeFileSpec& inSpec) +//---------------------------------------------------------------------------------------- +{ + delete [] mPath; + // Convert '\' to '/' + const char* str = (const char*)inSpec; + mPath = new char[1 + strlen(str); + const char* src = str; + char* dst = mPath; + for (const char* temp = str; *temp; src++,dst++) + { + if (*src == '\\') + *dst = '/'; + else + *dst = *src; + } +} // nsFilePath::operator = + +//---------------------------------------------------------------------------------------- +void nsNativeFileSpec::SetLeafName(const char* inLeafName) //---------------------------------------------------------------------------------------- { nsFileSpecHelpers::LeafReplace(mPath, '\\', inLeafName); } // nsNativeFileSpec::SetLeafName //---------------------------------------------------------------------------------------- -string nsNativeFileSpec::GetLeafName() const +const char* nsNativeFileSpec::GetLeafName() const //---------------------------------------------------------------------------------------- { return nsFileSpecHelpers::GetLeaf(mPath, '\\'); diff --git a/mozilla/xpcom/tests/FilesTest.cpp b/mozilla/xpcom/tests/FilesTest.cpp index 17cf4af4a31..0c721ee242f 100644 --- a/mozilla/xpcom/tests/FilesTest.cpp +++ b/mozilla/xpcom/tests/FilesTest.cpp @@ -1,12 +1,12 @@ #include "nsFileSpec.h" -#include +#include #include "nsFileStream.h" -namespace FileTest +NS_NAMESPACE FileTest { void WriteStuff(ostream& s); -} +} NS_NAMESPACE_END //---------------------------------------------------------------------------------------- void FileTest::WriteStuff(ostream& s) @@ -14,11 +14,11 @@ void FileTest::WriteStuff(ostream& s) { // Initialize a URL from a string without suffix. Change the path to suit your machine. nsFileURL fileURL("file:///Development/MPW/MPW%20Shell"); - s << "File URL initialized to: \"" << (string&)fileURL << "\""<< endl; + s << "File URL initialized to: \"" << fileURL << "\""<< endl; // Initialize a Unix path from a URL nsFilePath filePath(fileURL); - s << "As a unix path: \"" << (string&)filePath << "\""<< endl; + s << "As a unix path: \"" << (const char*)filePath << "\""<< endl; // Initialize a native file spec from a URL nsNativeFileSpec fileSpec(fileURL); @@ -30,11 +30,11 @@ void FileTest::WriteStuff(ostream& s) // Assign the spec to a URL fileURL = fileSpec; - s << "File URL assigned from spec: \"" << (string&)fileURL << "\""<< endl; + s << "File URL assigned from spec: \"" << fileURL << "\""<< endl; // Assign a unix path using a string with a suffix. filePath = "/Development/MPW/SysErrs.err"; - s << "File path reassigned to: \"" << (string&)filePath << "\""<< endl; + s << "File path reassigned to: \"" << (const char*)filePath << "\""<< endl; // Assign to a file spec using a unix path. fileSpec = filePath; @@ -65,7 +65,7 @@ void main() nsFilePath myTextFilePath("/Development/iotest.txt"); { - cout << "WRITING IDENTICAL OUTPUT TO " << myTextFilePath << endl << endl; + cout << "WRITING IDENTICAL OUTPUT TO " << (const char*)myTextFilePath << endl << endl; nsOutputFileStream testStream(myTextFilePath); FileTest::WriteStuff(testStream); } // <-- Scope closes the stream (and the file). @@ -73,7 +73,7 @@ void main() // Test of nsInputFileStream { - cout << "READING BACK DATA FROM " << myTextFilePath << endl << endl; + cout << "READING BACK DATA FROM " << (const char*)myTextFilePath << endl << endl; nsInputFileStream testStream2(myTextFilePath); char line[1000]; while (!testStream2.eof())