1999-01-27 02:49:33 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
1998-12-08 05:22:54 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1998-12-08 05:22:54 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1998-12-08 05:22:54 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1998-12-08 05:22:54 +03:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1998-12-08 05:22:54 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsFileSpec.h"
|
|
|
|
|
1998-12-29 03:24:32 +03:00
|
|
|
#include "nsDebug.h"
|
1999-03-16 22:12:51 +03:00
|
|
|
#include "nsEscape.h"
|
1999-08-30 04:32:27 +04:00
|
|
|
#include "nsIAllocator.h"
|
1998-12-19 02:06:54 +03:00
|
|
|
|
1998-12-08 05:22:54 +03:00
|
|
|
#include "prtypes.h"
|
1999-02-25 23:49:47 +03:00
|
|
|
#include "plstr.h"
|
|
|
|
#include "plbase64.h"
|
|
|
|
#include "prmem.h"
|
1998-12-08 05:22:54 +03:00
|
|
|
|
2000-02-11 04:08:56 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#define NS_IMPL_IDS
|
|
|
|
#include "nsIPlatformCharset.h"
|
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
|
1998-12-11 06:17:47 +03:00
|
|
|
#include <string.h>
|
1998-12-29 03:24:32 +03:00
|
|
|
#include <stdio.h>
|
1998-12-11 06:17:47 +03:00
|
|
|
|
1999-10-12 01:19:06 +04:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#include <Aliases.h>
|
|
|
|
#include <TextUtils.h>
|
|
|
|
#endif
|
|
|
|
|
1998-12-08 05:22:54 +03:00
|
|
|
//========================================================================================
|
1999-03-20 02:09:39 +03:00
|
|
|
// class nsSimpleCharString
|
1998-12-08 05:22:54 +03:00
|
|
|
//========================================================================================
|
1999-03-20 02:09:39 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsSimpleCharString::nsSimpleCharString()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
: mData(nsnull)
|
1998-12-08 05:22:54 +03:00
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
|
|
|
|
} // nsSimpleCharString::nsSimpleCharString
|
1998-12-08 05:22:54 +03:00
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
nsSimpleCharString::nsSimpleCharString(const char* inString)
|
1999-02-25 23:49:47 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
: mData(nsnull)
|
1999-02-25 23:49:47 +03:00
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
if (inString)
|
1999-04-26 23:05:00 +04:00
|
|
|
CopyFrom(inString, nsCRT::strlen(inString));
|
1999-03-20 02:09:39 +03:00
|
|
|
} // nsSimpleCharString::nsSimpleCharString
|
1999-01-07 02:38:21 +03:00
|
|
|
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
nsSimpleCharString::nsSimpleCharString(const nsString& inString)
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
: mData(nsnull)
|
1998-12-09 11:47:30 +03:00
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
*this = inString;
|
|
|
|
} // nsSimpleCharString::nsSimpleCharString
|
1998-12-09 11:47:30 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
nsSimpleCharString::nsSimpleCharString(const nsSimpleCharString& inOther)
|
1999-01-07 02:38:21 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
mData = inOther.mData;
|
|
|
|
AddRefData();
|
|
|
|
} // nsSimpleCharString::nsSimpleCharString
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsSimpleCharString::nsSimpleCharString(const char* inData, PRUint32 inLength)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
: mData(nsnull)
|
|
|
|
{
|
|
|
|
CopyFrom(inData, inLength);
|
|
|
|
} // nsSimpleCharString::nsSimpleCharString
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsSimpleCharString::~nsSimpleCharString()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
ReleaseData();
|
|
|
|
} // nsSimpleCharString::nsSimpleCharString
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsSimpleCharString::operator = (const char* inString)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (inString)
|
1999-04-26 23:05:00 +04:00
|
|
|
CopyFrom(inString, nsCRT::strlen(inString));
|
1999-03-20 02:09:39 +03:00
|
|
|
else
|
|
|
|
SetToEmpty();
|
|
|
|
} // nsSimpleCharString::operator =
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsSimpleCharString::operator = (const nsString& inString)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
PRUint32 len = inString.Length();
|
|
|
|
ReallocData(len);
|
|
|
|
if (!mData)
|
|
|
|
return;
|
1999-03-31 09:04:28 +04:00
|
|
|
inString.ToCString(mData->mString, len + 1);
|
1999-03-20 02:09:39 +03:00
|
|
|
} // nsSimpleCharString::operator =
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsSimpleCharString::operator = (const nsSimpleCharString& inOther)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (mData == inOther.mData)
|
|
|
|
return;
|
|
|
|
ReleaseData();
|
|
|
|
mData = inOther.mData;
|
|
|
|
AddRefData();
|
|
|
|
} // nsSimpleCharString::operator =
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsSimpleCharString::operator += (const char* inOther)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (!inOther)
|
|
|
|
return;
|
1999-04-26 23:05:00 +04:00
|
|
|
int newLength = Length() + nsCRT::strlen(inOther);
|
1999-03-20 02:09:39 +03:00
|
|
|
ReallocData(newLength);
|
|
|
|
strcat(mData->mString, inOther);
|
|
|
|
} // nsSimpleCharString::operator =
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsSimpleCharString nsSimpleCharString::operator + (const char* inOther) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
nsSimpleCharString result(*this);
|
|
|
|
result += inOther;
|
|
|
|
return result;
|
|
|
|
} // nsSimpleCharString::operator =
|
1998-12-09 11:47:30 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
void nsSimpleCharString::Catenate(const char* inString1, const char* inString2)
|
1999-01-07 02:38:21 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (!inString2)
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
*this += inString1;
|
|
|
|
return;
|
|
|
|
}
|
1999-04-26 23:05:00 +04:00
|
|
|
int newLength = Length() + nsCRT::strlen(inString1) + nsCRT::strlen(inString2);
|
1999-03-20 02:09:39 +03:00
|
|
|
ReallocData(newLength);
|
|
|
|
strcat(mData->mString, inString1);
|
|
|
|
strcat(mData->mString, inString2);
|
|
|
|
} // nsSimpleCharString::operator =
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsSimpleCharString::CopyFrom(const char* inData, PRUint32 inLength)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (!inData)
|
|
|
|
return;
|
|
|
|
ReallocData(inLength);
|
|
|
|
if (!mData)
|
|
|
|
return;
|
1999-04-26 23:05:00 +04:00
|
|
|
nsCRT::memcpy(mData->mString, inData, inLength);
|
1999-03-20 02:09:39 +03:00
|
|
|
mData->mString[inLength] = '\0';
|
|
|
|
} // nsSimpleCharString::CopyFrom
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsSimpleCharString::SetToEmpty()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
ReleaseData();
|
|
|
|
} // nsSimpleCharString::SetToEmpty
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsSimpleCharString::Unescape()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-04-24 01:32:57 +04:00
|
|
|
if (!mData)
|
|
|
|
return;
|
|
|
|
ReallocData(mData->mLength);
|
1999-03-20 02:09:39 +03:00
|
|
|
if (!mData)
|
|
|
|
return;
|
|
|
|
nsUnescape(mData->mString);
|
1999-04-26 23:05:00 +04:00
|
|
|
mData->mLength = nsCRT::strlen(mData->mString);
|
1999-03-20 02:09:39 +03:00
|
|
|
} // nsSimpleCharString::Unescape
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsSimpleCharString::AddRefData()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (mData)
|
|
|
|
++mData->mRefCount;
|
|
|
|
} // nsSimpleCharString::AddRefData
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsSimpleCharString::ReleaseData()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (!mData)
|
|
|
|
return;
|
|
|
|
NS_ASSERTION(mData->mRefCount > 0, "String deleted too many times!");
|
|
|
|
if (--mData->mRefCount == 0)
|
|
|
|
PR_Free(mData);
|
|
|
|
mData = nsnull;
|
|
|
|
} // nsSimpleCharString::ReleaseData
|
|
|
|
|
1999-04-20 23:09:22 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
inline PRUint32 CalculateAllocLength(PRUint32 logicalLength)
|
|
|
|
// Round up to the next multiple of 256.
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
return ((1 + (logicalLength >> 8)) << 8);
|
|
|
|
}
|
|
|
|
|
1999-03-20 02:09:39 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsSimpleCharString::ReallocData(PRUint32 inLength)
|
|
|
|
// Reallocate mData to a new length. Since this presumably precedes a change to the string,
|
|
|
|
// we want to detach ourselves if the data is shared by another string, even if the length
|
|
|
|
// requested would not otherwise require a reallocation.
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-04-20 23:09:22 +04:00
|
|
|
PRUint32 newAllocLength = CalculateAllocLength(inLength);
|
|
|
|
PRUint32 oldAllocLength = CalculateAllocLength(Length());
|
1999-03-20 02:09:39 +03:00
|
|
|
if (mData)
|
|
|
|
{
|
1999-04-26 23:05:00 +04:00
|
|
|
NS_ASSERTION(mData->mRefCount > 0, "String deleted too many times!");
|
|
|
|
if (mData->mRefCount == 1)
|
|
|
|
{
|
|
|
|
// We are the sole owner, so just change its length, if necessary.
|
|
|
|
if (newAllocLength > oldAllocLength)
|
|
|
|
mData = (Data*)PR_Realloc(mData, newAllocLength + sizeof(Data));
|
|
|
|
mData->mLength = inLength;
|
|
|
|
mData->mString[inLength] = '\0'; // we may be truncating
|
|
|
|
return;
|
|
|
|
}
|
1999-01-07 02:38:21 +03:00
|
|
|
}
|
1999-04-26 23:05:00 +04:00
|
|
|
PRUint32 copyLength = Length();
|
|
|
|
if (inLength < copyLength)
|
|
|
|
copyLength = inLength;
|
1999-04-20 23:09:22 +04:00
|
|
|
Data* newData = (Data*)PR_Malloc(newAllocLength + sizeof(Data));
|
1999-03-20 02:09:39 +03:00
|
|
|
// If data was already allocated when we get to here, then we are cloning the data
|
|
|
|
// from a shared pointer.
|
|
|
|
if (mData)
|
1999-01-07 02:38:21 +03:00
|
|
|
{
|
1999-04-26 23:05:00 +04:00
|
|
|
nsCRT::memcpy(newData, mData, sizeof(Data) + copyLength);
|
1999-03-20 02:09:39 +03:00
|
|
|
mData->mRefCount--; // Say goodbye
|
1999-01-07 02:38:21 +03:00
|
|
|
}
|
1999-03-20 02:09:39 +03:00
|
|
|
else
|
|
|
|
newData->mString[0] = '\0';
|
|
|
|
|
|
|
|
mData = newData;
|
|
|
|
mData->mRefCount = 1;
|
|
|
|
mData->mLength = inLength;
|
|
|
|
} // nsSimpleCharString::ReleaseData
|
|
|
|
|
1999-10-12 01:19:06 +04:00
|
|
|
|
1999-03-20 02:09:39 +03:00
|
|
|
//========================================================================================
|
|
|
|
NS_NAMESPACE nsFileSpecHelpers
|
|
|
|
//========================================================================================
|
|
|
|
{
|
|
|
|
enum
|
|
|
|
{ kMaxFilenameLength = 31 // should work on Macintosh, Unix, and Win32.
|
|
|
|
, kMaxAltDigitLength = 5
|
|
|
|
, kMaxCoreLeafNameLength = (kMaxFilenameLength - (kMaxAltDigitLength + 1))
|
|
|
|
};
|
|
|
|
#ifndef XP_MAC
|
|
|
|
NS_NAMESPACE_PROTOTYPE void Canonify(nsSimpleCharString& ioPath, PRBool inMakeDirs);
|
|
|
|
NS_NAMESPACE_PROTOTYPE void MakeAllDirectories(const char* inPath, int mode);
|
|
|
|
#endif
|
|
|
|
#ifdef XP_PC
|
|
|
|
NS_NAMESPACE_PROTOTYPE void NativeToUnix(nsSimpleCharString& ioPath);
|
|
|
|
NS_NAMESPACE_PROTOTYPE void UnixToNative(nsSimpleCharString& ioPath);
|
|
|
|
#endif
|
|
|
|
} NS_NAMESPACE_END
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsresult ns_file_convert_result(PRInt32 nativeErr)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
return nativeErr ?
|
|
|
|
NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_FILES,((nativeErr)&0xFFFF))
|
|
|
|
: NS_OK;
|
|
|
|
}
|
1998-12-09 11:47:30 +03:00
|
|
|
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
void nsSimpleCharString::LeafReplace(char inSeparator, const char* inLeafName)
|
1999-01-07 02:38:21 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// Find the existing leaf name
|
1999-03-20 02:09:39 +03:00
|
|
|
if (IsEmpty())
|
1999-01-07 02:38:21 +03:00
|
|
|
return;
|
|
|
|
if (!inLeafName)
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
SetToEmpty();
|
1999-01-07 02:38:21 +03:00
|
|
|
return;
|
|
|
|
}
|
1999-03-20 02:09:39 +03:00
|
|
|
char* chars = mData->mString;
|
|
|
|
char* lastSeparator = strrchr(chars, inSeparator);
|
|
|
|
int oldLength = Length();
|
|
|
|
PRBool trailingSeparator = (lastSeparator + 1 == chars + oldLength);
|
1999-02-25 23:49:47 +03:00
|
|
|
if (trailingSeparator)
|
|
|
|
{
|
1999-04-26 23:05:00 +04:00
|
|
|
char savedCh = *lastSeparator;
|
|
|
|
char *savedLastSeparator = lastSeparator;
|
1999-02-25 23:49:47 +03:00
|
|
|
*lastSeparator = '\0';
|
1999-03-20 02:09:39 +03:00
|
|
|
lastSeparator = strrchr(chars, inSeparator);
|
1999-04-26 23:05:00 +04:00
|
|
|
*savedLastSeparator = savedCh;
|
1999-02-25 23:49:47 +03:00
|
|
|
}
|
|
|
|
if (lastSeparator)
|
|
|
|
lastSeparator++; // point at the trailing string
|
|
|
|
else
|
1999-03-20 02:09:39 +03:00
|
|
|
lastSeparator = chars; // the full monty
|
1999-02-25 23:49:47 +03:00
|
|
|
|
1999-04-26 23:05:00 +04:00
|
|
|
PRUint32 savedLastSeparatorOffset = (lastSeparator - chars);
|
|
|
|
int newLength =
|
|
|
|
(lastSeparator - chars) + nsCRT::strlen(inLeafName) + (trailingSeparator != 0);
|
1999-03-20 02:09:39 +03:00
|
|
|
ReallocData(newLength);
|
1999-03-22 22:15:30 +03:00
|
|
|
|
1999-03-20 02:09:39 +03:00
|
|
|
chars = mData->mString; // it might have moved.
|
1999-03-22 22:15:30 +03:00
|
|
|
chars[savedLastSeparatorOffset] = '\0'; // strip the current leaf name
|
1999-03-20 02:09:39 +03:00
|
|
|
|
|
|
|
strcat(chars, inLeafName);
|
1999-02-25 23:49:47 +03:00
|
|
|
if (trailingSeparator)
|
|
|
|
{
|
|
|
|
// If the original ended in a slash, then the new one should, too.
|
|
|
|
char sepStr[2] = "/";
|
|
|
|
*sepStr = inSeparator;
|
1999-03-20 02:09:39 +03:00
|
|
|
strcat(chars, sepStr);
|
1999-02-25 23:49:47 +03:00
|
|
|
}
|
1999-03-20 02:09:39 +03:00
|
|
|
} // nsSimpleCharString::LeafReplace
|
1998-12-08 05:22:54 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
char* nsSimpleCharString::GetLeaf(char inSeparator) const
|
1998-12-09 11:47:30 +03:00
|
|
|
// Returns a pointer to an allocated string representing the leaf.
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
if (IsEmpty())
|
1999-02-25 23:49:47 +03:00
|
|
|
return nsnull;
|
1999-03-20 02:09:39 +03:00
|
|
|
|
|
|
|
char* chars = mData->mString;
|
|
|
|
const char* lastSeparator = strrchr(chars, inSeparator);
|
1999-02-25 23:49:47 +03:00
|
|
|
|
1999-03-20 02:09:39 +03:00
|
|
|
// If there was no separator, then return a copy of our path.
|
1999-02-25 23:49:47 +03:00
|
|
|
if (!lastSeparator)
|
1999-04-26 23:05:00 +04:00
|
|
|
return nsCRT::strdup(*this);
|
1999-02-25 23:49:47 +03:00
|
|
|
|
|
|
|
// So there's at least one separator. What's just after it?
|
|
|
|
// If the separator was not the last character, return the trailing string.
|
|
|
|
const char* leafPointer = lastSeparator + 1;
|
|
|
|
if (*leafPointer)
|
1999-04-26 23:05:00 +04:00
|
|
|
return nsCRT::strdup(leafPointer);
|
1999-02-25 23:49:47 +03:00
|
|
|
|
|
|
|
// So now, separator was the last character. Poke in a null instead.
|
|
|
|
*(char*)lastSeparator = '\0'; // Should use const_cast, but Unix has old compiler.
|
1999-03-20 02:09:39 +03:00
|
|
|
leafPointer = strrchr(chars, inSeparator);
|
1999-04-26 23:05:00 +04:00
|
|
|
char* result = leafPointer ? nsCRT::strdup(++leafPointer) : nsCRT::strdup(chars);
|
1999-02-25 23:49:47 +03:00
|
|
|
// Restore the poked null before returning.
|
|
|
|
*(char*)lastSeparator = inSeparator;
|
|
|
|
#ifdef XP_PC
|
1999-04-26 23:05:00 +04:00
|
|
|
// If it's a drive letter use the colon notation.
|
|
|
|
if (!leafPointer && result[2] == 0 && result[1] == '|')
|
|
|
|
result[1] = ':';
|
1999-02-25 23:49:47 +03:00
|
|
|
#endif
|
|
|
|
return result;
|
1999-03-20 02:09:39 +03:00
|
|
|
} // nsSimpleCharString::GetLeaf
|
1998-12-08 05:22:54 +03:00
|
|
|
|
1999-10-12 01:19:06 +04:00
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
1999-06-29 14:27:58 +04:00
|
|
|
#if defined(XP_UNIX) || defined(XP_PC) || defined(XP_BEOS)
|
1999-01-07 02:38:21 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsFileSpecHelpers::MakeAllDirectories(const char* inPath, int mode)
|
|
|
|
// Make the path a valid one by creating all the intermediate directories. Does NOT
|
|
|
|
// make the leaf into a directory. This should be a unix path.
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-04-26 23:05:00 +04:00
|
|
|
if (!inPath)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char* pathCopy = nsCRT::strdup( inPath );
|
1999-01-07 02:38:21 +03:00
|
|
|
if (!pathCopy)
|
1999-04-26 23:05:00 +04:00
|
|
|
return;
|
1999-01-07 02:38:21 +03:00
|
|
|
|
1999-04-26 23:05:00 +04:00
|
|
|
const char kSeparator = '/'; // I repeat: this should be a unix-style path.
|
1999-01-07 02:38:21 +03:00
|
|
|
const int kSkipFirst = 1;
|
|
|
|
|
1999-01-27 02:49:33 +03:00
|
|
|
#ifdef XP_PC
|
|
|
|
// Either this is a relative path, or we ensure that it has
|
|
|
|
// a drive letter specifier.
|
|
|
|
NS_ASSERTION( pathCopy[0] != '/' || pathCopy[2] == '|', "No drive letter!" );
|
1999-01-07 02:38:21 +03:00
|
|
|
#endif
|
|
|
|
char* currentStart = pathCopy;
|
|
|
|
char* currentEnd = strchr(currentStart + kSkipFirst, kSeparator);
|
|
|
|
if (currentEnd)
|
|
|
|
{
|
1999-03-05 23:20:22 +03:00
|
|
|
nsFileSpec spec;
|
1999-04-26 23:05:00 +04:00
|
|
|
*currentEnd = '\0';
|
1999-03-05 23:20:22 +03:00
|
|
|
|
|
|
|
#ifdef XP_PC
|
|
|
|
/*
|
|
|
|
if we have a drive letter path, we must make sure that the inital path has a '/' on it, or
|
|
|
|
Canonify will turn "/c|" into a path relative to the running executable.
|
|
|
|
*/
|
|
|
|
if (pathCopy[0] == '/' && pathCopy[2] == '|')
|
|
|
|
{
|
1999-04-26 23:05:00 +04:00
|
|
|
char* startDir = (char*)PR_Malloc(nsCRT::strlen(pathCopy) + 2);
|
1999-03-20 02:09:39 +03:00
|
|
|
strcpy(startDir, pathCopy);
|
1999-03-05 23:20:22 +03:00
|
|
|
strcat(startDir, "/");
|
|
|
|
|
|
|
|
spec = nsFilePath(startDir, PR_FALSE);
|
1999-04-26 23:05:00 +04:00
|
|
|
|
1999-03-20 02:09:39 +03:00
|
|
|
PR_Free(startDir);
|
1999-03-05 23:20:22 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
spec = nsFilePath(pathCopy, PR_FALSE);
|
|
|
|
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
spec = nsFilePath(pathCopy, PR_FALSE);
|
|
|
|
#endif
|
|
|
|
do
|
1999-04-26 23:05:00 +04:00
|
|
|
{
|
|
|
|
// If the node doesn't exist, and it is not the initial node in a full path,
|
|
|
|
// then make a directory (We cannot make the initial (volume) node).
|
|
|
|
if (!spec.Exists() && *currentStart != kSeparator)
|
|
|
|
spec.CreateDirectory(mode);
|
|
|
|
|
1999-04-10 04:29:20 +04:00
|
|
|
currentStart = ++currentEnd;
|
1999-04-26 23:05:00 +04:00
|
|
|
currentEnd = strchr(currentStart, kSeparator);
|
|
|
|
if (!currentEnd)
|
|
|
|
break;
|
1999-03-05 23:20:22 +03:00
|
|
|
|
|
|
|
*currentEnd = '\0';
|
|
|
|
|
1999-04-26 23:05:00 +04:00
|
|
|
spec += currentStart; // "lengthen" the path, adding the next node.
|
|
|
|
} while (currentEnd);
|
1999-01-07 02:38:21 +03:00
|
|
|
}
|
1999-04-26 23:05:00 +04:00
|
|
|
nsCRT::free(pathCopy);
|
1999-01-07 02:38:21 +03:00
|
|
|
} // nsFileSpecHelpers::MakeAllDirectories
|
|
|
|
|
|
|
|
#endif // XP_PC || XP_UNIX
|
|
|
|
|
1999-10-12 01:19:06 +04:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
1998-12-11 06:17:47 +03:00
|
|
|
#if defined(XP_PC)
|
1999-05-26 05:38:36 +04:00
|
|
|
#include "nsFileSpecWin.cpp" // Windows-specific implementations
|
1998-12-11 06:17:47 +03:00
|
|
|
#elif defined(XP_MAC)
|
1999-10-12 01:19:06 +04:00
|
|
|
//#include "nsFileSpecMac.cpp" // Macintosh-specific implementations
|
|
|
|
// we include the .cpp file in the project now.
|
1999-06-29 14:27:58 +04:00
|
|
|
#elif defined(XP_BEOS)
|
|
|
|
#include "nsFileSpecBeOS.cpp" // BeOS-specific implementations
|
1998-12-08 05:22:54 +03:00
|
|
|
#elif defined(XP_UNIX)
|
1999-05-26 05:38:36 +04:00
|
|
|
#include "nsFileSpecUnix.cpp" // Unix-specific implementations
|
1998-12-08 05:22:54 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//========================================================================================
|
1999-01-07 02:38:21 +03:00
|
|
|
// nsFileURL implementation
|
1998-12-08 05:22:54 +03:00
|
|
|
//========================================================================================
|
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
#ifndef XP_MAC
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileURL::nsFileURL(const char* inString, PRBool inCreateDirs)
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-01-07 02:38:21 +03:00
|
|
|
if (!inString)
|
1999-04-26 23:05:00 +04:00
|
|
|
return;
|
1999-01-07 02:38:21 +03:00
|
|
|
NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!");
|
1999-05-04 23:11:31 +04:00
|
|
|
// Make canonical and absolute. Since it's a parameter to this constructor,
|
|
|
|
// inString is escaped. We want to make an nsFilePath, which requires
|
|
|
|
// an unescaped string.
|
|
|
|
nsSimpleCharString unescapedPath(inString + kFileURLPrefixLength);
|
1999-05-04 23:39:15 +04:00
|
|
|
unescapedPath.Unescape();
|
1999-09-15 09:28:31 +04:00
|
|
|
nsFilePath path(unescapedPath, inCreateDirs);
|
1999-04-26 23:05:00 +04:00
|
|
|
*this = path;
|
1998-12-09 11:47:30 +03:00
|
|
|
} // nsFileURL::nsFileURL
|
1999-01-07 02:38:21 +03:00
|
|
|
#endif
|
1998-12-08 05:22:54 +03:00
|
|
|
|
1999-02-28 04:36:48 +03:00
|
|
|
#ifndef XP_MAC
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsFileURL::nsFileURL(const nsString& inString, PRBool inCreateDirs)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
const nsAutoCString aString(inString);
|
1999-02-28 05:17:55 +03:00
|
|
|
const char* aCString = (const char*) aString;
|
1999-04-05 12:06:54 +04:00
|
|
|
if (!inString.Length())
|
1999-04-26 23:05:00 +04:00
|
|
|
return;
|
1999-02-28 05:51:53 +03:00
|
|
|
NS_ASSERTION(strstr(aCString, kFileURLPrefix) == aCString, "Not a URL!");
|
1999-05-04 23:11:31 +04:00
|
|
|
// Make canonical and absolute. Since it's a parameter to this constructor,
|
|
|
|
// inString is escaped. We want to make an nsFilePath, which requires
|
|
|
|
// an unescaped string.
|
|
|
|
nsSimpleCharString unescapedPath(aCString + kFileURLPrefixLength);
|
1999-05-04 23:39:15 +04:00
|
|
|
unescapedPath.Unescape();
|
1999-09-15 09:28:31 +04:00
|
|
|
nsFilePath path(unescapedPath, inCreateDirs);
|
1999-04-26 23:05:00 +04:00
|
|
|
*this = path;
|
1999-02-28 04:36:48 +03:00
|
|
|
} // nsFileURL::nsFileURL
|
|
|
|
#endif
|
|
|
|
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1998-12-09 11:47:30 +03:00
|
|
|
nsFileURL::nsFileURL(const nsFileURL& inOther)
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
: mURL(inOther.mURL)
|
1998-12-08 05:22:54 +03:00
|
|
|
#ifdef XP_MAC
|
1999-02-25 23:49:47 +03:00
|
|
|
, mFileSpec(inOther.GetFileSpec())
|
1998-12-08 05:22:54 +03:00
|
|
|
#endif
|
1998-12-09 11:47:30 +03:00
|
|
|
{
|
|
|
|
} // nsFileURL::nsFileURL
|
1998-12-08 05:22:54 +03:00
|
|
|
|
1999-03-13 09:38:57 +03:00
|
|
|
#ifndef XP_MAC
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1998-12-09 11:47:30 +03:00
|
|
|
nsFileURL::nsFileURL(const nsFilePath& inOther)
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-16 22:12:51 +03:00
|
|
|
*this = inOther;
|
1998-12-09 11:47:30 +03:00
|
|
|
} // nsFileURL::nsFileURL
|
1999-03-13 09:38:57 +03:00
|
|
|
#endif
|
1998-12-09 11:47:30 +03:00
|
|
|
|
1999-03-13 09:38:57 +03:00
|
|
|
#ifndef XP_MAC
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileURL::nsFileURL(const nsFileSpec& inOther)
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-16 22:12:51 +03:00
|
|
|
*this = inOther;
|
1998-12-09 11:47:30 +03:00
|
|
|
} // nsFileURL::nsFileURL
|
1999-03-13 09:38:57 +03:00
|
|
|
#endif
|
1998-12-09 11:47:30 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsFileURL::~nsFileURL()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1998-12-08 05:22:54 +03:00
|
|
|
}
|
|
|
|
|
1999-03-13 09:38:57 +03:00
|
|
|
#ifndef XP_MAC
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1998-12-09 11:47:30 +03:00
|
|
|
void nsFileURL::operator = (const char* inString)
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-09-20 03:46:35 +04:00
|
|
|
// XXX is this called by nsFileSpecImpl.cpp::SetURLString?
|
|
|
|
// if so, there's a bug...
|
|
|
|
|
1999-03-20 02:09:39 +03:00
|
|
|
mURL = inString;
|
1999-01-07 02:38:21 +03:00
|
|
|
NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!");
|
1998-12-09 11:47:30 +03:00
|
|
|
} // nsFileURL::operator =
|
1999-03-13 09:38:57 +03:00
|
|
|
#endif
|
1998-12-08 05:22:54 +03:00
|
|
|
|
1999-03-20 02:09:39 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsFileURL::operator +=(const char* inRelativeUnixPath)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
char* escapedPath = nsEscape(inRelativeUnixPath, url_Path);
|
|
|
|
mURL += escapedPath;
|
1999-09-04 21:10:10 +04:00
|
|
|
nsCRT::free(escapedPath);
|
1999-03-20 02:09:39 +03:00
|
|
|
#ifdef XP_MAC
|
|
|
|
mFileSpec += inRelativeUnixPath;
|
|
|
|
#endif
|
|
|
|
} // nsFileURL::operator +=
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsFileURL nsFileURL::operator +(const char* inRelativeUnixPath) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
nsFileURL result(*this);
|
|
|
|
result += inRelativeUnixPath;
|
|
|
|
return result;
|
|
|
|
} // nsFileURL::operator +
|
|
|
|
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1998-12-09 11:47:30 +03:00
|
|
|
void nsFileURL::operator = (const nsFileURL& inOther)
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
mURL = inOther.mURL;
|
1998-12-08 05:22:54 +03:00
|
|
|
#ifdef XP_MAC
|
1999-02-25 23:49:47 +03:00
|
|
|
mFileSpec = inOther.GetFileSpec();
|
1998-12-08 05:22:54 +03:00
|
|
|
#endif
|
1998-12-09 11:47:30 +03:00
|
|
|
} // nsFileURL::operator =
|
|
|
|
|
1999-03-13 09:38:57 +03:00
|
|
|
#ifndef XP_MAC
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1998-12-09 01:45:42 +03:00
|
|
|
void nsFileURL::operator = (const nsFilePath& inOther)
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
mURL = kFileURLPrefix;
|
1999-04-26 23:05:00 +04:00
|
|
|
char* original = (char*)(const char*)inOther; // we shall modify, but restore.
|
1999-07-09 07:41:27 +04:00
|
|
|
if (!original || !*original) return;
|
1999-03-16 22:12:51 +03:00
|
|
|
#ifdef XP_PC
|
1999-04-26 23:05:00 +04:00
|
|
|
// because we don't want to escape the '|' character, change it to a letter.
|
1999-03-16 22:12:51 +03:00
|
|
|
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)
|
1999-04-26 23:05:00 +04:00
|
|
|
mURL += escapedPath;
|
1999-09-04 21:10:10 +04:00
|
|
|
nsCRT::free(escapedPath);
|
1998-12-09 11:47:30 +03:00
|
|
|
} // nsFileURL::operator =
|
1999-03-13 09:38:57 +03:00
|
|
|
#endif
|
1998-12-08 05:22:54 +03:00
|
|
|
|
1999-03-13 09:38:57 +03:00
|
|
|
#ifndef XP_MAC
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
void nsFileURL::operator = (const nsFileSpec& inOther)
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-16 22:12:51 +03:00
|
|
|
*this = nsFilePath(inOther);
|
1999-04-26 23:05:00 +04:00
|
|
|
if (mURL[mURL.Length() - 1] != '/' && inOther.IsDirectory())
|
|
|
|
mURL += "/";
|
1998-12-09 11:47:30 +03:00
|
|
|
} // nsFileURL::operator =
|
1999-03-13 09:38:57 +03:00
|
|
|
#endif
|
1999-02-26 03:14:57 +03:00
|
|
|
|
1999-10-12 01:19:06 +04:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
1998-12-08 05:22:54 +03:00
|
|
|
//========================================================================================
|
1999-01-07 02:38:21 +03:00
|
|
|
// nsFilePath implementation
|
1998-12-08 05:22:54 +03:00
|
|
|
//========================================================================================
|
|
|
|
|
1999-04-02 00:07:52 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-17 21:20:27 +03:00
|
|
|
nsFilePath::nsFilePath(const nsFilePath& inPath)
|
1999-04-02 00:07:52 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
: mPath(inPath.mPath)
|
1999-02-17 21:20:27 +03:00
|
|
|
#ifdef XP_MAC
|
1999-02-25 23:49:47 +03:00
|
|
|
, mFileSpec(inPath.mFileSpec)
|
1999-02-17 21:20:27 +03:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
#ifndef XP_MAC
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFilePath::nsFilePath(const char* inString, PRBool inCreateDirs)
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
: mPath(inString)
|
1998-12-08 05:22:54 +03:00
|
|
|
{
|
1999-05-13 02:56:24 +04:00
|
|
|
if (mPath.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path");
|
1999-01-27 02:49:33 +03:00
|
|
|
|
|
|
|
#ifdef XP_PC
|
|
|
|
nsFileSpecHelpers::UnixToNative(mPath);
|
|
|
|
#endif
|
1999-01-07 02:38:21 +03:00
|
|
|
// Make canonical and absolute.
|
|
|
|
nsFileSpecHelpers::Canonify(mPath, inCreateDirs);
|
1999-01-27 02:49:33 +03:00
|
|
|
#ifdef XP_PC
|
|
|
|
NS_ASSERTION( mPath[1] == ':', "unexpected canonical path" );
|
|
|
|
nsFileSpecHelpers::NativeToUnix(mPath);
|
|
|
|
#endif
|
1998-12-08 05:22:54 +03:00
|
|
|
}
|
1999-01-07 02:38:21 +03:00
|
|
|
#endif
|
1998-12-08 05:22:54 +03:00
|
|
|
|
1999-02-28 04:36:48 +03:00
|
|
|
#ifndef XP_MAC
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsFilePath::nsFilePath(const nsString& inString, PRBool inCreateDirs)
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
: mPath(inString)
|
1999-02-28 04:36:48 +03:00
|
|
|
{
|
1999-05-13 02:56:24 +04:00
|
|
|
if (mPath.IsEmpty())
|
|
|
|
return;
|
1999-02-28 04:36:48 +03:00
|
|
|
|
1999-09-15 09:28:31 +04:00
|
|
|
NS_ASSERTION(strstr((const char*)mPath, kFileURLPrefix) != (const char*)mPath, "URL passed as path");
|
1999-02-28 04:36:48 +03:00
|
|
|
#ifdef XP_PC
|
|
|
|
nsFileSpecHelpers::UnixToNative(mPath);
|
|
|
|
#endif
|
|
|
|
// Make canonical and absolute.
|
|
|
|
nsFileSpecHelpers::Canonify(mPath, inCreateDirs);
|
|
|
|
#ifdef XP_PC
|
|
|
|
NS_ASSERTION( mPath[1] == ':', "unexpected canonical path" );
|
|
|
|
nsFileSpecHelpers::NativeToUnix(mPath);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-03-13 09:38:57 +03:00
|
|
|
#ifndef XP_MAC
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1998-12-09 01:45:42 +03:00
|
|
|
nsFilePath::nsFilePath(const nsFileURL& inOther)
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
mPath = (const char*)inOther.mURL + kFileURLPrefixLength;
|
|
|
|
mPath.Unescape();
|
1998-12-08 05:22:54 +03:00
|
|
|
}
|
1999-03-13 09:38:57 +03:00
|
|
|
#endif
|
1998-12-08 05:22:54 +03:00
|
|
|
|
1999-06-29 14:27:58 +04:00
|
|
|
#if defined XP_UNIX || defined XP_BEOS
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFilePath::nsFilePath(const nsFileSpec& inOther)
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
: mPath(inOther.mPath)
|
1998-12-09 11:47:30 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif // XP_UNIX
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsFilePath::~nsFilePath()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-06-29 14:27:58 +04:00
|
|
|
#if defined XP_UNIX || defined XP_BEOS
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
void nsFilePath::operator = (const nsFileSpec& inOther)
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-09-20 03:46:35 +04:00
|
|
|
// XXX bug here, again if.
|
|
|
|
|
1999-03-20 02:09:39 +03:00
|
|
|
mPath = inOther.mPath;
|
1998-12-09 11:47:30 +03:00
|
|
|
}
|
|
|
|
#endif // XP_UNIX
|
|
|
|
|
1999-05-08 05:18:23 +04:00
|
|
|
#ifndef XP_MAC
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsFilePath::operator = (const char* inString)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-05-13 02:56:24 +04:00
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path");
|
1999-04-26 23:05:00 +04:00
|
|
|
mPath = inString;
|
1999-05-13 02:56:24 +04:00
|
|
|
if (mPath.IsEmpty())
|
|
|
|
return;
|
1999-01-27 02:49:33 +03:00
|
|
|
#ifdef XP_PC
|
1999-04-26 23:05:00 +04:00
|
|
|
nsFileSpecHelpers::UnixToNative(mPath);
|
1998-12-08 05:22:54 +03:00
|
|
|
#endif
|
1999-01-27 02:49:33 +03:00
|
|
|
// Make canonical and absolute.
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpecHelpers::Canonify(mPath, PR_FALSE /* XXX? */);
|
1999-01-27 02:49:33 +03:00
|
|
|
#ifdef XP_PC
|
1999-04-26 23:05:00 +04:00
|
|
|
nsFileSpecHelpers::NativeToUnix(mPath);
|
1999-01-27 02:49:33 +03:00
|
|
|
#endif
|
1998-12-08 05:22:54 +03:00
|
|
|
}
|
1999-05-08 05:18:23 +04:00
|
|
|
#endif // XP_MAC
|
1998-12-08 05:22:54 +03:00
|
|
|
|
1999-03-13 09:38:57 +03:00
|
|
|
#ifndef XP_MAC
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1998-12-09 01:45:42 +03:00
|
|
|
void nsFilePath::operator = (const nsFileURL& inOther)
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
mPath = (const char*)nsFilePath(inOther);
|
1998-12-08 05:22:54 +03:00
|
|
|
}
|
1999-03-13 09:38:57 +03:00
|
|
|
#endif
|
1998-12-08 05:22:54 +03:00
|
|
|
|
1999-02-23 06:10:29 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsFilePath::operator = (const nsFilePath& inOther)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
mPath = inOther.mPath;
|
1999-02-23 06:10:29 +03:00
|
|
|
#ifdef XP_MAC
|
1999-02-26 01:04:50 +03:00
|
|
|
mFileSpec = inOther.GetFileSpec();
|
1999-02-23 06:10:29 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1999-03-20 02:09:39 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsFilePath::operator +=(const char* inRelativeUnixPath)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-10-12 01:19:06 +04:00
|
|
|
NS_ASSERTION(inRelativeUnixPath, "Attempt append relative path with null path");
|
|
|
|
|
1999-03-20 02:09:39 +03:00
|
|
|
char* escapedPath = nsEscape(inRelativeUnixPath, url_Path);
|
|
|
|
mPath += escapedPath;
|
1999-09-04 21:10:10 +04:00
|
|
|
nsCRT::free(escapedPath);
|
1999-03-20 02:09:39 +03:00
|
|
|
#ifdef XP_MAC
|
|
|
|
mFileSpec += inRelativeUnixPath;
|
|
|
|
#endif
|
|
|
|
} // nsFilePath::operator +=
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsFilePath nsFilePath::operator +(const char* inRelativeUnixPath) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-10-12 01:19:06 +04:00
|
|
|
NS_ASSERTION(inRelativeUnixPath, "Attempt append relative path with null path");
|
|
|
|
|
|
|
|
nsFilePath resultPath(*this);
|
|
|
|
resultPath += inRelativeUnixPath;
|
|
|
|
return resultPath;
|
1999-03-20 02:09:39 +03:00
|
|
|
} // nsFilePath::operator +
|
|
|
|
|
1999-10-12 01:19:06 +04:00
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
1998-12-08 05:22:54 +03:00
|
|
|
//========================================================================================
|
1999-02-25 23:49:47 +03:00
|
|
|
// nsFileSpec implementation
|
1998-12-08 05:22:54 +03:00
|
|
|
//========================================================================================
|
|
|
|
|
2000-02-11 04:08:56 +03:00
|
|
|
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
|
|
|
|
|
1998-12-08 05:22:54 +03:00
|
|
|
#ifndef XP_MAC
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpec::nsFileSpec()
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-10-12 01:19:06 +04:00
|
|
|
: mError(NS_OK) // XXX shouldn't this be NS_ERROR_NOT_INITIALIZED?
|
1998-12-08 05:22:54 +03:00
|
|
|
{
|
|
|
|
}
|
1999-10-12 01:19:06 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsFileSpec::Clear()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
mPath.SetToEmpty();
|
|
|
|
mError = NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
1998-12-08 05:22:54 +03:00
|
|
|
#endif
|
|
|
|
|
1999-10-12 01:19:06 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsFileSpec::~nsFileSpec()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// mPath cleans itself up
|
|
|
|
}
|
|
|
|
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpec::nsFileSpec(const nsPersistentFileDescriptor& inDescriptor)
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
{
|
1999-04-26 23:05:00 +04:00
|
|
|
*this = inDescriptor;
|
1999-02-25 23:49:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsFileSpec::nsFileSpec(const nsFileURL& inURL)
|
|
|
|
//----------------------------------------------------------------------------------------
|
1998-12-08 05:22:54 +03:00
|
|
|
{
|
1998-12-09 11:47:30 +03:00
|
|
|
*this = nsFilePath(inURL); // convert to unix path first
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
void nsFileSpec::MakeUnique(const char* inSuggestedLeafName)
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-01-07 02:38:21 +03:00
|
|
|
if (inSuggestedLeafName && *inSuggestedLeafName)
|
|
|
|
SetLeafName(inSuggestedLeafName);
|
1998-12-08 05:22:54 +03:00
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
MakeUnique();
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsFileSpec::MakeUnique
|
1998-12-08 05:22:54 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
void nsFileSpec::MakeUnique()
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-01-07 02:38:21 +03:00
|
|
|
if (!Exists())
|
|
|
|
return;
|
|
|
|
|
|
|
|
char* leafName = GetLeafName();
|
|
|
|
if (!leafName)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char* lastDot = strrchr(leafName, '.');
|
|
|
|
char* suffix = "";
|
|
|
|
if (lastDot)
|
|
|
|
{
|
1999-04-26 23:05:00 +04:00
|
|
|
suffix = nsCRT::strdup(lastDot); // include '.'
|
1999-01-07 02:38:21 +03:00
|
|
|
*lastDot = '\0'; // strip suffix and dot.
|
|
|
|
}
|
|
|
|
const int kMaxRootLength
|
1999-04-26 23:05:00 +04:00
|
|
|
= nsFileSpecHelpers::kMaxCoreLeafNameLength - nsCRT::strlen(suffix) - 1;
|
|
|
|
if ((int)nsCRT::strlen(leafName) > (int)kMaxRootLength)
|
1999-01-07 02:38:21 +03:00
|
|
|
leafName[kMaxRootLength] = '\0';
|
1999-10-09 00:41:19 +04:00
|
|
|
for (short indx = 1; indx < 1000 && Exists(); indx++)
|
1999-01-07 02:38:21 +03:00
|
|
|
{
|
|
|
|
// start with "Picture-1.jpg" after "Picture.jpg" exists
|
|
|
|
char newName[nsFileSpecHelpers::kMaxFilenameLength + 1];
|
1999-10-09 00:41:19 +04:00
|
|
|
sprintf(newName, "%s-%d%s", leafName, indx, suffix);
|
1999-01-07 02:38:21 +03:00
|
|
|
SetLeafName(newName);
|
|
|
|
}
|
|
|
|
if (*suffix)
|
1999-04-26 23:05:00 +04:00
|
|
|
nsCRT::free(suffix);
|
1999-07-09 07:41:27 +04:00
|
|
|
nsCRT::free(leafName);
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsFileSpec::MakeUnique
|
1998-12-09 11:47:30 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
void nsFileSpec::operator = (const nsFileURL& inURL)
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
*this = nsFilePath(inURL); // convert to unix path first
|
|
|
|
}
|
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsFileSpec::operator = (const nsPersistentFileDescriptor& inDescriptor)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
|
1999-04-26 23:05:00 +04:00
|
|
|
nsSimpleCharString data;
|
|
|
|
PRInt32 dataSize;
|
1999-02-25 23:49:47 +03:00
|
|
|
inDescriptor.GetData(data, dataSize);
|
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
char* decodedData = PL_Base64Decode((const char*)data, (int)dataSize, nsnull);
|
|
|
|
// Cast to an alias record and resolve.
|
1999-04-26 23:05:00 +04:00
|
|
|
AliasHandle aliasH = nsnull;
|
|
|
|
mError = NS_FILE_RESULT(PtrToHand(decodedData, &(Handle)aliasH, (dataSize * 3) / 4));
|
|
|
|
PR_Free(decodedData);
|
|
|
|
if (NS_FAILED(mError))
|
|
|
|
return; // not enough memory?
|
|
|
|
|
|
|
|
Boolean changed;
|
|
|
|
mError = NS_FILE_RESULT(::ResolveAlias(nsnull, aliasH, &mSpec, &changed));
|
|
|
|
DisposeHandle((Handle) aliasH);
|
|
|
|
mPath.SetToEmpty();
|
1999-02-25 23:49:47 +03:00
|
|
|
#else
|
1999-03-20 02:09:39 +03:00
|
|
|
mPath = data;
|
1999-02-25 23:49:47 +03:00
|
|
|
mError = NS_OK;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1998-12-09 11:47:30 +03:00
|
|
|
//========================================================================================
|
1999-02-25 23:49:47 +03:00
|
|
|
// UNIX & WIN nsFileSpec implementation
|
1998-12-09 11:47:30 +03:00
|
|
|
//========================================================================================
|
|
|
|
|
1999-06-29 14:27:58 +04:00
|
|
|
#if defined XP_UNIX || defined XP_BEOS
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpec::nsFileSpec(const nsFilePath& inPath)
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
: mPath((const char*)inPath)
|
1999-02-25 23:49:47 +03:00
|
|
|
, mError(NS_OK)
|
1998-12-09 11:47:30 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
void nsFileSpec::operator = (const nsFilePath& inPath)
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
mPath = (const char*)inPath;
|
1999-02-25 23:49:47 +03:00
|
|
|
mError = NS_OK;
|
1998-12-09 11:47:30 +03:00
|
|
|
}
|
|
|
|
#endif //XP_UNIX
|
|
|
|
|
1999-06-29 14:27:58 +04:00
|
|
|
#if defined(XP_UNIX) || defined(XP_PC) || defined(XP_BEOS)
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpec::nsFileSpec(const nsFileSpec& inSpec)
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
: mPath(inSpec.mPath)
|
1999-02-25 23:49:47 +03:00
|
|
|
, mError(NS_OK)
|
1998-12-09 11:47:30 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpec::nsFileSpec(const char* inString, PRBool inCreateDirs)
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
: mPath(inString)
|
1999-02-25 23:49:47 +03:00
|
|
|
, mError(NS_OK)
|
1998-12-09 11:47:30 +03:00
|
|
|
{
|
1999-01-07 02:38:21 +03:00
|
|
|
// Make canonical and absolute.
|
|
|
|
nsFileSpecHelpers::Canonify(mPath, inCreateDirs);
|
1998-12-09 11:47:30 +03:00
|
|
|
}
|
|
|
|
|
1999-02-28 04:36:48 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsFileSpec::nsFileSpec(const nsString& inString, PRBool inCreateDirs)
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
: mPath(inString)
|
1999-02-28 04:36:48 +03:00
|
|
|
, mError(NS_OK)
|
|
|
|
{
|
|
|
|
// Make canonical and absolute.
|
|
|
|
nsFileSpecHelpers::Canonify(mPath, inCreateDirs);
|
|
|
|
}
|
|
|
|
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
void nsFileSpec::operator = (const nsFileSpec& inSpec)
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
mPath = inSpec.mPath;
|
1999-02-25 23:49:47 +03:00
|
|
|
mError = inSpec.Error();
|
1998-12-09 11:47:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
void nsFileSpec::operator = (const char* inString)
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
mPath = inString;
|
1999-01-27 02:49:33 +03:00
|
|
|
// Make canonical and absolute.
|
1999-03-10 05:50:16 +03:00
|
|
|
nsFileSpecHelpers::Canonify(mPath, PR_FALSE /* XXX? */);
|
1999-02-25 23:49:47 +03:00
|
|
|
mError = NS_OK;
|
1998-12-09 11:47:30 +03:00
|
|
|
}
|
1999-10-12 01:19:06 +04:00
|
|
|
#endif //XP_UNIX,PC,XP_BEOS
|
1998-12-09 11:47:30 +03:00
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpec nsFileSpec::operator + (const char* inRelativePath) const
|
1999-01-07 02:38:21 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-10-12 01:19:06 +04:00
|
|
|
NS_ASSERTION(inRelativePath, "Attempt to append name with a null string");
|
|
|
|
|
|
|
|
nsFileSpec resultSpec = *this;
|
|
|
|
resultSpec += inRelativePath;
|
|
|
|
return resultSpec;
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsFileSpec::operator +
|
|
|
|
|
1999-03-10 01:33:36 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
PRBool nsFileSpec::operator == (const nsFileSpec& inOther) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef XP_MAC
|
1999-03-13 09:38:57 +03:00
|
|
|
if ( inOther.mSpec.vRefNum == mSpec.vRefNum &&
|
1999-03-10 01:33:36 +03:00
|
|
|
inOther.mSpec.parID == mSpec.parID &&
|
1999-03-13 09:38:57 +03:00
|
|
|
EqualString(inOther.mSpec.name, mSpec.name, false, true))
|
1999-03-11 00:02:58 +03:00
|
|
|
return PR_TRUE;
|
1999-03-10 01:33:36 +03:00
|
|
|
#else
|
1999-03-20 02:09:39 +03:00
|
|
|
PRBool amEmpty = mPath.IsEmpty();
|
|
|
|
PRBool heEmpty = inOther.mPath.IsEmpty();
|
1999-03-13 09:38:57 +03:00
|
|
|
if (amEmpty) // we're the same if he's empty...
|
|
|
|
return heEmpty;
|
|
|
|
if (heEmpty) // ('cuz I'm not...)
|
|
|
|
return PR_FALSE;
|
1999-08-30 02:05:11 +04:00
|
|
|
|
|
|
|
nsSimpleCharString str = mPath;
|
|
|
|
nsSimpleCharString inStr = inOther.mPath;
|
|
|
|
|
|
|
|
// Length() is size of buffer, not length of string
|
|
|
|
PRUint32 strLast = str.Length() - 1, inLast = inStr.Length() - 1;
|
|
|
|
#if defined(XP_PC)
|
|
|
|
#define DIR_SEPARATOR '\\' // XXX doesn't NSPR have this?
|
|
|
|
/* windows does not care about case. */
|
|
|
|
#define DIR_STRCMP _stricmp
|
|
|
|
#else
|
|
|
|
#define DIR_SEPARATOR '/'
|
2000-01-27 00:55:13 +03:00
|
|
|
#if defined(VMS)
|
|
|
|
#define DIR_STRCMP strcasecmp
|
|
|
|
#else
|
1999-08-30 02:05:11 +04:00
|
|
|
#define DIR_STRCMP strcmp
|
2000-01-27 00:55:13 +03:00
|
|
|
#endif
|
1999-08-30 02:05:11 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if(str[strLast] == DIR_SEPARATOR)
|
|
|
|
str[strLast] = '\0';
|
|
|
|
|
|
|
|
if(inStr[inLast] == DIR_SEPARATOR)
|
|
|
|
inStr[inLast] = '\0';
|
|
|
|
|
1999-09-15 09:28:31 +04:00
|
|
|
if (DIR_STRCMP(str, inStr ) == 0)
|
1999-04-26 23:05:00 +04:00
|
|
|
return PR_TRUE;
|
1999-08-30 02:05:11 +04:00
|
|
|
#undef DIR_SEPARATOR
|
|
|
|
#undef DIR_STRCMP
|
1999-03-11 00:02:58 +03:00
|
|
|
#endif
|
|
|
|
return PR_FALSE;
|
1999-03-10 01:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
PRBool nsFileSpec::operator != (const nsFileSpec& inOther) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
return (! (*this == inOther) );
|
|
|
|
}
|
|
|
|
|
1999-03-13 09:38:57 +03:00
|
|
|
#ifndef XP_MAC
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
// This is the only automatic conversion to const char*
|
|
|
|
// that is provided, and it allows the
|
|
|
|
// path to be "passed" to NSPR file routines. This practice
|
|
|
|
// is VERY EVIL and should only be used to support legacy
|
|
|
|
// code. Using it guarantees bugs on Macintosh. The path is NOT allocated, so do
|
|
|
|
// not even think of deleting (or freeing) it.
|
1999-10-12 01:19:06 +04:00
|
|
|
const char* nsFileSpec::GetCString() const
|
1999-03-13 09:38:57 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
return mPath;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-10-12 01:19:06 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
// Is our spec a child of the provided parent?
|
|
|
|
PRBool nsFileSpec::IsChildOf(nsFileSpec &possibleParent)
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-09-04 03:44:59 +04:00
|
|
|
{
|
|
|
|
nsFileSpec iter = *this, parent;
|
|
|
|
#ifdef DEBUG
|
|
|
|
int depth = 0;
|
|
|
|
#endif
|
|
|
|
while (1) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
// sanity
|
|
|
|
NS_ASSERTION(depth < 100, "IsChildOf has lost its little mind");
|
|
|
|
if (depth > 100)
|
|
|
|
return PR_FALSE;
|
|
|
|
#endif
|
|
|
|
if (iter == possibleParent)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
iter.GetParent(parent); // shouldn't this be an error on parent?
|
|
|
|
if (iter.Failed())
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
if (iter == parent) // hit bottom
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
iter = parent;
|
|
|
|
#ifdef DEBUG
|
|
|
|
depth++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1999-10-12 01:19:06 +04:00
|
|
|
// not reached, but I bet some compiler will whine
|
1999-09-04 03:44:59 +04:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2000-02-11 04:08:56 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsFileSpec::GetFileSystemCharset(nsString & fileSystemCharset)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// From mozilla/widget/src/windows/nsFileWidget.cpp
|
|
|
|
|
|
|
|
static nsAutoString aCharset;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (aCharset.Length() < 1) {
|
|
|
|
nsCOMPtr <nsIPlatformCharset> platformCharset;
|
|
|
|
rv = nsComponentManager::CreateInstance(NS_PLATFORMCHARSET_PROGID, nsnull,
|
|
|
|
NS_GET_IID(nsIPlatformCharset), getter_AddRefs(platformCharset));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = platformCharset->GetCharset(kPlatformCharsetSel_FileName, aCharset);
|
|
|
|
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "error getting platform charset");
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
aCharset.SetString("ISO-8859-1");
|
|
|
|
}
|
|
|
|
fileSystemCharset = aCharset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
PRUnichar * nsFileSpec::ConvertFromFileSystemCharset(const char *inString)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// From mozilla/widget/src/windows/nsFileWidget.cpp
|
|
|
|
|
|
|
|
nsIUnicodeDecoder *aUnicodeDecoder = nsnull;
|
|
|
|
PRUnichar *outString = nsnull;
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// get file system charset and create a unicode encoder
|
|
|
|
nsAutoString fileSystemCharset;
|
|
|
|
GetFileSystemCharset(fileSystemCharset);
|
|
|
|
|
|
|
|
NS_WITH_SERVICE(nsICharsetConverterManager, ccm, kCharsetConverterManagerCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = ccm->GetUnicodeDecoder(&fileSystemCharset, &aUnicodeDecoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
// converts from the file system charset to unicode
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRInt32 inLength = nsCRT::strlen(inString);
|
|
|
|
PRInt32 outLength;
|
|
|
|
rv = aUnicodeDecoder->GetMaxLength(inString, inLength, &outLength);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
outString = new PRUnichar[outLength+1];
|
|
|
|
if (nsnull == outString) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
rv = aUnicodeDecoder->Convert(inString, &inLength, outString, &outLength);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
outString[outLength] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_IF_RELEASE(aUnicodeDecoder);
|
|
|
|
|
|
|
|
return NS_SUCCEEDED(rv) ? outString : nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsFileSpec::GetNativePathString(nsString &nativePathString)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
const char *path = GetCString();
|
|
|
|
if (nsnull == path) {
|
|
|
|
nativePathString.SetString("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PRUnichar *converted = ConvertFromFileSystemCharset(path);
|
|
|
|
if (nsnull != converted) {
|
|
|
|
nativePathString.SetString(converted);
|
|
|
|
delete [] converted;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nativePathString.SetString(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-12 01:19:06 +04:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
1999-09-04 03:44:59 +04:00
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
//========================================================================================
|
1999-04-26 23:05:00 +04:00
|
|
|
// class nsPersistentFileDescriptor
|
1999-02-25 23:49:47 +03:00
|
|
|
//========================================================================================
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inDesc)
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
: mDescriptorString(inDesc.mDescriptorString)
|
1999-02-25 23:49:47 +03:00
|
|
|
{
|
|
|
|
} // nsPersistentFileDescriptor::nsPersistentFileDescriptor
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsPersistentFileDescriptor::operator = (const nsPersistentFileDescriptor& inDesc)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
mDescriptorString = inDesc.mDescriptorString;
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsPersistentFileDescriptor::operator =
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsFileSpec& inSpec)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-04-26 23:05:00 +04:00
|
|
|
*this = inSpec;
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsPersistentFileDescriptor::nsPersistentFileDescriptor
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsPersistentFileDescriptor::operator = (const nsFileSpec& inSpec)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
#ifdef XP_MAC
|
|
|
|
if (inSpec.Error())
|
1999-04-26 23:05:00 +04:00
|
|
|
return;
|
|
|
|
AliasHandle aliasH;
|
|
|
|
OSErr err = NewAlias(nil, inSpec.GetFSSpecPtr(), &aliasH);
|
|
|
|
if (err != noErr)
|
|
|
|
return;
|
1999-02-26 01:17:25 +03:00
|
|
|
|
1999-04-26 23:05:00 +04:00
|
|
|
PRUint32 bytes = GetHandleSize((Handle) aliasH);
|
|
|
|
HLock((Handle) aliasH);
|
|
|
|
char* buf = PL_Base64Encode((const char*)*aliasH, bytes, nsnull);
|
|
|
|
DisposeHandle((Handle) aliasH);
|
1999-02-25 23:49:47 +03:00
|
|
|
|
1999-03-20 02:09:39 +03:00
|
|
|
mDescriptorString = buf;
|
1999-02-25 23:49:47 +03:00
|
|
|
PR_Free(buf);
|
|
|
|
#else
|
1999-03-20 02:09:39 +03:00
|
|
|
mDescriptorString = inSpec.GetCString();
|
1999-02-25 23:49:47 +03:00
|
|
|
#endif // XP_MAC
|
|
|
|
} // nsPersistentFileDescriptor::operator =
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsPersistentFileDescriptor::~nsPersistentFileDescriptor()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
} // nsPersistentFileDescriptor::~nsPersistentFileDescriptor
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-05-08 05:18:23 +04:00
|
|
|
void nsPersistentFileDescriptor::GetData(nsSimpleCharString& outData) const
|
1999-02-25 23:49:47 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
outData = mDescriptorString;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-05-08 05:18:23 +04:00
|
|
|
void nsPersistentFileDescriptor::SetData(const nsSimpleCharString& inData)
|
1999-02-28 04:36:48 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-05-08 05:18:23 +04:00
|
|
|
SetData(inData, inData.Length());
|
1999-02-28 04:36:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-05-08 05:18:23 +04:00
|
|
|
void nsPersistentFileDescriptor::GetData(nsSimpleCharString& outData, PRInt32& outSize) const
|
1999-02-25 23:49:47 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-05-08 05:18:23 +04:00
|
|
|
outSize = mDescriptorString.Length();
|
|
|
|
outData = mDescriptorString;
|
1999-02-25 23:49:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-05-08 05:18:23 +04:00
|
|
|
void nsPersistentFileDescriptor::SetData(const nsSimpleCharString& inData, PRInt32 inSize)
|
1999-02-25 23:49:47 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-05-08 05:18:23 +04:00
|
|
|
mDescriptorString.CopyFrom((const char*)inData, inSize);
|
1999-02-25 23:49:47 +03:00
|
|
|
}
|
1999-02-28 04:36:48 +03:00
|
|
|
|
1999-06-03 05:40:47 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsPersistentFileDescriptor::SetData(const char* inData, PRInt32 inSize)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
mDescriptorString.CopyFrom(inData, inSize);
|
|
|
|
}
|
|
|
|
|
1999-02-28 04:36:48 +03:00
|
|
|
//========================================================================================
|
1999-04-26 23:05:00 +04:00
|
|
|
// class nsAutoCString
|
1999-02-28 04:36:48 +03:00
|
|
|
//========================================================================================
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsAutoCString::~nsAutoCString()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-08-30 05:06:54 +04:00
|
|
|
nsAllocator::Free(NS_REINTERPRET_CAST(void*, NS_CONST_CAST(char*, mCString)));
|
1999-02-28 04:36:48 +03:00
|
|
|
}
|
1999-04-10 04:29:20 +04:00
|
|
|
|
|
|
|
//========================================================================================
|
1999-05-08 05:18:23 +04:00
|
|
|
// class nsNSPRPath
|
1999-04-10 04:29:20 +04:00
|
|
|
//========================================================================================
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-05-08 05:18:23 +04:00
|
|
|
nsNSPRPath::operator const char*() const
|
1999-04-26 23:05:00 +04:00
|
|
|
// NSPR expects a UNIX path on unix and Macintosh, but a native path on windows. NSPR
|
|
|
|
// cannot be changed, so we have to do the dirty work.
|
1999-04-10 04:29:20 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
#ifdef XP_PC
|
1999-04-26 23:05:00 +04:00
|
|
|
if (!modifiedNSPRPath)
|
|
|
|
{
|
|
|
|
// If this is the first call, initialize modifiedNSPRPath. Start by cloning
|
|
|
|
// mFilePath, but strip the leading separator, if present
|
|
|
|
const char* unixPath = (const char*)mFilePath;
|
|
|
|
if (!unixPath)
|
|
|
|
return nsnull;
|
|
|
|
|
1999-05-08 05:18:23 +04:00
|
|
|
((nsNSPRPath*)this)->modifiedNSPRPath
|
1999-04-26 23:05:00 +04:00
|
|
|
= nsCRT::strdup(*unixPath == '/' ? unixPath + 1: unixPath);
|
|
|
|
|
|
|
|
// Replace the bar
|
|
|
|
if (modifiedNSPRPath[1] == '|')
|
|
|
|
modifiedNSPRPath[1] = ':';
|
|
|
|
|
|
|
|
// Remove the ending separator only if it is not the last separator
|
|
|
|
int len = nsCRT::strlen(modifiedNSPRPath);
|
|
|
|
if (modifiedNSPRPath[len - 1 ] == '/' && modifiedNSPRPath[len - 2 ] != ':')
|
|
|
|
modifiedNSPRPath[len - 1 ] = '\0';
|
|
|
|
}
|
|
|
|
return modifiedNSPRPath;
|
1999-04-10 04:29:20 +04:00
|
|
|
#else
|
1999-04-26 23:05:00 +04:00
|
|
|
return (const char*)mFilePath;
|
1999-04-10 04:29:20 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-05-08 05:18:23 +04:00
|
|
|
nsNSPRPath::~nsNSPRPath()
|
1999-04-10 04:29:20 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-04-15 23:39:23 +04:00
|
|
|
#ifdef XP_PC
|
1999-04-26 23:05:00 +04:00
|
|
|
if (modifiedNSPRPath)
|
|
|
|
nsCRT::free(modifiedNSPRPath);
|
1999-04-15 23:39:23 +04:00
|
|
|
#endif
|
1999-04-13 10:35:42 +04:00
|
|
|
}
|