From 296a662cd4af52a154b66211b27b749234d9a283 Mon Sep 17 00:00:00 2001 From: "mcmullen%netscape.com" Date: Tue, 16 Mar 1999 19:12:51 +0000 Subject: [PATCH] Fixes for bugs #3779,#2784. Member initialization of nsFileURL for windows compiler, escaping of url when made from path or spec, add file:// on macintosh when making a url from a spec. [bugs introduced when, for dp, I changed nsFilePath not to escape]. a=chofmann. --- base/public/nsFileSpec.h | 2 +- base/src/mac/nsFileSpecMac.cpp | 20 +++++--- base/src/nsEscape.cpp | 92 ++++++++++++++++++++-------------- base/src/nsFileSpec.cpp | 25 +++++++-- xpcom/io/nsEscape.cpp | 92 ++++++++++++++++++++-------------- xpcom/io/nsFileSpec.cpp | 25 +++++++-- xpcom/io/nsFileSpec.h | 2 +- xpcom/io/nsFileSpecMac.cpp | 20 +++++--- 8 files changed, 178 insertions(+), 100 deletions(-) diff --git a/base/public/nsFileSpec.h b/base/public/nsFileSpec.h index 5d5eacf820d..9710ae3f38a 100644 --- a/base/public/nsFileSpec.h +++ b/base/public/nsFileSpec.h @@ -255,7 +255,7 @@ class NS_BASE nsFileSpec long parID, ConstStr255Param name); nsFileSpec(const FSSpec& inSpec) - : mSpec(inSpec), mError(NS_OK) {} + : mSpec(inSpec), mError(NS_OK), mPath(nsnull) {} void operator = (const FSSpec& inSpec) { mSpec = inSpec; mError = NS_OK; } diff --git a/base/src/mac/nsFileSpecMac.cpp b/base/src/mac/nsFileSpecMac.cpp index 68dc06c24a9..27c37e258dd 100644 --- a/base/src/mac/nsFileSpecMac.cpp +++ b/base/src/mac/nsFileSpecMac.cpp @@ -22,7 +22,6 @@ #include "FullPath.h" #include "FileCopy.h" #include "MoreFilesExtras.h" -#include "nsEscape.h" #include #include @@ -917,7 +916,7 @@ nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs) , mFileSpec(inString, inCreateDirs) { // Make canonical and absolute. - char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, TRUE ); + char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true ); mPath = MacFileHelpers::EncodeMacPath(path, true, false); } @@ -928,7 +927,7 @@ nsFilePath::nsFilePath(const nsString& inString, PRBool inCreateDirs) , mFileSpec(nsAutoCString(inString), inCreateDirs) { // Make canonical and absolute. - char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, TRUE ); + char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true ); mPath = MacFileHelpers::EncodeMacPath(path, true, false); } @@ -952,7 +951,7 @@ nsFilePath::nsFilePath(const nsFileURL& inOther) void nsFilePath::operator = (const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- { - char * path = MacFileHelpers::PathNameFromFSSpec( inSpec.mSpec, TRUE ); + char * path = MacFileHelpers::PathNameFromFSSpec( inSpec.mSpec, true ); delete [] mPath; mPath = MacFileHelpers::EncodeMacPath(path, true, false); mFileSpec = inSpec; @@ -1032,8 +1031,17 @@ void nsFileURL::operator = (const nsFileSpec& inOther) { mFileSpec = inOther; delete [] mURL; - char* path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, TRUE ); - mURL = MacFileHelpers::EncodeMacPath(path, true, true); + char* path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true ); + char* encodedPath = MacFileHelpers::EncodeMacPath(path, true, true); + char* encodedURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, encodedPath); + delete [] encodedPath; + if (encodedURL[strlen(encodedURL) - 1] != '/' && inOther.IsDirectory()) + { + mURL = nsFileSpecHelpers::AllocCat(encodedURL, "/"); + delete [] encodedURL; + } + else + mURL = encodedURL; } // nsFileURL::operator = //---------------------------------------------------------------------------------------- diff --git a/base/src/nsEscape.cpp b/base/src/nsEscape.cpp index 47961451199..650cae67515 100644 --- a/base/src/nsEscape.cpp +++ b/base/src/nsEscape.cpp @@ -62,45 +62,64 @@ NS_BASE char* nsEscape(const char * str, nsEscapeMask mask) } //---------------------------------------------------------------------------------------- -NS_BASE char* nsEscapeCount(const char * str, PRInt32 len, nsEscapeMask mask, PRInt32 * out_len) +NS_BASE char* nsEscapeCount( + const char * str, + PRInt32 len, + nsEscapeMask mask, + PRInt32* out_len) //---------------------------------------------------------------------------------------- { - int32 i, extra = 0; - char *hexChars = "0123456789ABCDEF"; + if (!str) + return 0; - if(!str) - return(0); + int i, extra = 0; + char* hexChars = "0123456789ABCDEF"; - register const unsigned char* src = (unsigned char *) str; + register const unsigned char* src = (const unsigned char *) str; for (i = 0; i < len; i++) - { - if (!IS_OK(src[i])) - extra+=2; /* the escape, plus an extra byte for each nibble */ - } + { + if (!IS_OK(*src++)) + extra += 2; /* the escape, plus an extra byte for each nibble */ + } char* result = new char[len + extra + 1]; if (!result) - return(0); + return 0; register unsigned char* dst = (unsigned char *) result; - for (i = 0; i < len; i++) - { - unsigned char c = src[i]; - if (IS_OK(c)) - { - *dst++ = c; - } - else if (mask == url_XPAlphas && c == ' ') - { - *dst++ = '+'; /* convert spaces to pluses */ - } - else - { - *dst++ = HEX_ESCAPE; - *dst++ = hexChars[c >> 4]; /* high nibble */ - *dst++ = hexChars[c & 0x0f]; /* low nibble */ - } - } + src = (const unsigned char *) str; + if (mask == url_XPAlphas) + { + for (i = 0; i < len; i++) + { + unsigned char c = *src++; + if (IS_OK(c)) + *dst++ = c; + else if (c == ' ') + *dst++ = '+'; /* convert spaces to pluses */ + else + { + *dst++ = HEX_ESCAPE; + *dst++ = hexChars[c >> 4]; /* high nibble */ + *dst++ = hexChars[c & 0x0f]; /* low nibble */ + } + } + } + else + { + for (i = 0; i < len; i++) + { + unsigned char c = *src++; + if (IS_OK(c)) + *dst++ = c; + else + { + *dst++ = HEX_ESCAPE; + *dst++ = hexChars[c >> 4]; /* high nibble */ + *dst++ = hexChars[c & 0x0f]; /* low nibble */ + } + } + } *dst = '\0'; /* tack on eos */ if(out_len) @@ -125,27 +144,24 @@ NS_BASE PRInt32 nsUnescapeCount(char * str) while (*src) if (*src != HEX_ESCAPE) - { *dst++ = *src++; - } else - { + { src++; /* walk over escape */ if (*src) - { + { *dst = UNHEX(*src) << 4; src++; - } + } if (*src) - { + { *dst = (*dst + UNHEX(*src)); src++; - } + } dst++; - } + } *dst = 0; - return (int)(dst - str); } /* NET_UnEscapeCnt */ diff --git a/base/src/nsFileSpec.cpp b/base/src/nsFileSpec.cpp index 8a195923198..e20a931914c 100644 --- a/base/src/nsFileSpec.cpp +++ b/base/src/nsFileSpec.cpp @@ -20,6 +20,7 @@ #include "nsFileStream.h" #include "nsDebug.h" +#include "nsEscape.h" #include "prtypes.h" #include "plstr.h" @@ -345,17 +346,19 @@ nsFileURL::nsFileURL(const nsFileURL& inOther) //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFilePath& inOther) //---------------------------------------------------------------------------------------- -: mURL(nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)inOther)) +: mURL(nsnull) { + *this = inOther; } // nsFileURL::nsFileURL #endif #ifndef XP_MAC //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFileSpec& inOther) -: mURL(nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)nsFilePath(inOther))) +: mURL(nsnull) //---------------------------------------------------------------------------------------- { + *this = inOther; } // nsFileURL::nsFileURL #endif @@ -392,7 +395,20 @@ void nsFileURL::operator = (const nsFilePath& inOther) //---------------------------------------------------------------------------------------- { delete [] mURL; - mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)inOther); + char* original = (char*)(const char*)inOther; // we shall modify, but restore. +#ifdef XP_PC + // because we don't want to escape the '|' character, change it to a letter. + NS_ASSERTION(original[2] == '|', "No drive letter part!"); + original[2] = 'x'; + char* escapedPath = nsEscape(original, url_Path); + original[2] = '|'; // restore it + escapedPath[2] = '|'; +#else + char* escapedPath = nsEscape(original, url_Path); +#endif + if (escapedPath) + mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, escapedPath); + delete [] escapedPath; } // nsFileURL::operator = #endif @@ -401,8 +417,7 @@ void nsFileURL::operator = (const nsFilePath& inOther) void nsFileURL::operator = (const nsFileSpec& inOther) //---------------------------------------------------------------------------------------- { - delete [] mURL; - mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)nsFilePath(inOther)); + *this = nsFilePath(inOther); } // nsFileURL::operator = #endif diff --git a/xpcom/io/nsEscape.cpp b/xpcom/io/nsEscape.cpp index 47961451199..650cae67515 100644 --- a/xpcom/io/nsEscape.cpp +++ b/xpcom/io/nsEscape.cpp @@ -62,45 +62,64 @@ NS_BASE char* nsEscape(const char * str, nsEscapeMask mask) } //---------------------------------------------------------------------------------------- -NS_BASE char* nsEscapeCount(const char * str, PRInt32 len, nsEscapeMask mask, PRInt32 * out_len) +NS_BASE char* nsEscapeCount( + const char * str, + PRInt32 len, + nsEscapeMask mask, + PRInt32* out_len) //---------------------------------------------------------------------------------------- { - int32 i, extra = 0; - char *hexChars = "0123456789ABCDEF"; + if (!str) + return 0; - if(!str) - return(0); + int i, extra = 0; + char* hexChars = "0123456789ABCDEF"; - register const unsigned char* src = (unsigned char *) str; + register const unsigned char* src = (const unsigned char *) str; for (i = 0; i < len; i++) - { - if (!IS_OK(src[i])) - extra+=2; /* the escape, plus an extra byte for each nibble */ - } + { + if (!IS_OK(*src++)) + extra += 2; /* the escape, plus an extra byte for each nibble */ + } char* result = new char[len + extra + 1]; if (!result) - return(0); + return 0; register unsigned char* dst = (unsigned char *) result; - for (i = 0; i < len; i++) - { - unsigned char c = src[i]; - if (IS_OK(c)) - { - *dst++ = c; - } - else if (mask == url_XPAlphas && c == ' ') - { - *dst++ = '+'; /* convert spaces to pluses */ - } - else - { - *dst++ = HEX_ESCAPE; - *dst++ = hexChars[c >> 4]; /* high nibble */ - *dst++ = hexChars[c & 0x0f]; /* low nibble */ - } - } + src = (const unsigned char *) str; + if (mask == url_XPAlphas) + { + for (i = 0; i < len; i++) + { + unsigned char c = *src++; + if (IS_OK(c)) + *dst++ = c; + else if (c == ' ') + *dst++ = '+'; /* convert spaces to pluses */ + else + { + *dst++ = HEX_ESCAPE; + *dst++ = hexChars[c >> 4]; /* high nibble */ + *dst++ = hexChars[c & 0x0f]; /* low nibble */ + } + } + } + else + { + for (i = 0; i < len; i++) + { + unsigned char c = *src++; + if (IS_OK(c)) + *dst++ = c; + else + { + *dst++ = HEX_ESCAPE; + *dst++ = hexChars[c >> 4]; /* high nibble */ + *dst++ = hexChars[c & 0x0f]; /* low nibble */ + } + } + } *dst = '\0'; /* tack on eos */ if(out_len) @@ -125,27 +144,24 @@ NS_BASE PRInt32 nsUnescapeCount(char * str) while (*src) if (*src != HEX_ESCAPE) - { *dst++ = *src++; - } else - { + { src++; /* walk over escape */ if (*src) - { + { *dst = UNHEX(*src) << 4; src++; - } + } if (*src) - { + { *dst = (*dst + UNHEX(*src)); src++; - } + } dst++; - } + } *dst = 0; - return (int)(dst - str); } /* NET_UnEscapeCnt */ diff --git a/xpcom/io/nsFileSpec.cpp b/xpcom/io/nsFileSpec.cpp index 8a195923198..e20a931914c 100644 --- a/xpcom/io/nsFileSpec.cpp +++ b/xpcom/io/nsFileSpec.cpp @@ -20,6 +20,7 @@ #include "nsFileStream.h" #include "nsDebug.h" +#include "nsEscape.h" #include "prtypes.h" #include "plstr.h" @@ -345,17 +346,19 @@ nsFileURL::nsFileURL(const nsFileURL& inOther) //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFilePath& inOther) //---------------------------------------------------------------------------------------- -: mURL(nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)inOther)) +: mURL(nsnull) { + *this = inOther; } // nsFileURL::nsFileURL #endif #ifndef XP_MAC //---------------------------------------------------------------------------------------- nsFileURL::nsFileURL(const nsFileSpec& inOther) -: mURL(nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)nsFilePath(inOther))) +: mURL(nsnull) //---------------------------------------------------------------------------------------- { + *this = inOther; } // nsFileURL::nsFileURL #endif @@ -392,7 +395,20 @@ void nsFileURL::operator = (const nsFilePath& inOther) //---------------------------------------------------------------------------------------- { delete [] mURL; - mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)inOther); + char* original = (char*)(const char*)inOther; // we shall modify, but restore. +#ifdef XP_PC + // because we don't want to escape the '|' character, change it to a letter. + NS_ASSERTION(original[2] == '|', "No drive letter part!"); + original[2] = 'x'; + char* escapedPath = nsEscape(original, url_Path); + original[2] = '|'; // restore it + escapedPath[2] = '|'; +#else + char* escapedPath = nsEscape(original, url_Path); +#endif + if (escapedPath) + mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, escapedPath); + delete [] escapedPath; } // nsFileURL::operator = #endif @@ -401,8 +417,7 @@ void nsFileURL::operator = (const nsFilePath& inOther) void nsFileURL::operator = (const nsFileSpec& inOther) //---------------------------------------------------------------------------------------- { - delete [] mURL; - mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)nsFilePath(inOther)); + *this = nsFilePath(inOther); } // nsFileURL::operator = #endif diff --git a/xpcom/io/nsFileSpec.h b/xpcom/io/nsFileSpec.h index 5d5eacf820d..9710ae3f38a 100644 --- a/xpcom/io/nsFileSpec.h +++ b/xpcom/io/nsFileSpec.h @@ -255,7 +255,7 @@ class NS_BASE nsFileSpec long parID, ConstStr255Param name); nsFileSpec(const FSSpec& inSpec) - : mSpec(inSpec), mError(NS_OK) {} + : mSpec(inSpec), mError(NS_OK), mPath(nsnull) {} void operator = (const FSSpec& inSpec) { mSpec = inSpec; mError = NS_OK; } diff --git a/xpcom/io/nsFileSpecMac.cpp b/xpcom/io/nsFileSpecMac.cpp index 68dc06c24a9..27c37e258dd 100644 --- a/xpcom/io/nsFileSpecMac.cpp +++ b/xpcom/io/nsFileSpecMac.cpp @@ -22,7 +22,6 @@ #include "FullPath.h" #include "FileCopy.h" #include "MoreFilesExtras.h" -#include "nsEscape.h" #include #include @@ -917,7 +916,7 @@ nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs) , mFileSpec(inString, inCreateDirs) { // Make canonical and absolute. - char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, TRUE ); + char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true ); mPath = MacFileHelpers::EncodeMacPath(path, true, false); } @@ -928,7 +927,7 @@ nsFilePath::nsFilePath(const nsString& inString, PRBool inCreateDirs) , mFileSpec(nsAutoCString(inString), inCreateDirs) { // Make canonical and absolute. - char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, TRUE ); + char * path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true ); mPath = MacFileHelpers::EncodeMacPath(path, true, false); } @@ -952,7 +951,7 @@ nsFilePath::nsFilePath(const nsFileURL& inOther) void nsFilePath::operator = (const nsFileSpec& inSpec) //---------------------------------------------------------------------------------------- { - char * path = MacFileHelpers::PathNameFromFSSpec( inSpec.mSpec, TRUE ); + char * path = MacFileHelpers::PathNameFromFSSpec( inSpec.mSpec, true ); delete [] mPath; mPath = MacFileHelpers::EncodeMacPath(path, true, false); mFileSpec = inSpec; @@ -1032,8 +1031,17 @@ void nsFileURL::operator = (const nsFileSpec& inOther) { mFileSpec = inOther; delete [] mURL; - char* path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, TRUE ); - mURL = MacFileHelpers::EncodeMacPath(path, true, true); + char* path = MacFileHelpers::PathNameFromFSSpec( mFileSpec, true ); + char* encodedPath = MacFileHelpers::EncodeMacPath(path, true, true); + char* encodedURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, encodedPath); + delete [] encodedPath; + if (encodedURL[strlen(encodedURL) - 1] != '/' && inOther.IsDirectory()) + { + mURL = nsFileSpecHelpers::AllocCat(encodedURL, "/"); + delete [] encodedURL; + } + else + mURL = encodedURL; } // nsFileURL::operator = //----------------------------------------------------------------------------------------