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
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsFileSpec.h"
|
|
|
|
|
1998-12-19 02:06:54 +03:00
|
|
|
#include "nsFileStream.h"
|
1998-12-29 03:24:32 +03:00
|
|
|
#include "nsDebug.h"
|
1999-03-16 22:12:51 +03:00
|
|
|
#include "nsEscape.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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
//========================================================================================
|
|
|
|
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-01-07 02:38:21 +03:00
|
|
|
#if defined(XP_UNIX) || defined(XP_PC)
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
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
|
|
|
|
|
1998-12-11 06:17:47 +03:00
|
|
|
#if defined(XP_PC)
|
1998-12-22 23:01:41 +03:00
|
|
|
#include "windows/nsFileSpecWin.cpp" // Windows-specific implementations
|
1998-12-11 06:17:47 +03:00
|
|
|
#elif defined(XP_MAC)
|
|
|
|
#include "nsFileSpecMac.cpp" // Macintosh-specific implementations
|
1998-12-08 05:22:54 +03:00
|
|
|
#elif defined(XP_UNIX)
|
1998-12-16 01:45:09 +03:00
|
|
|
#include "unix/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-05-04 23:11: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-05-04 23:11: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-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;
|
|
|
|
delete [] escapedPath;
|
|
|
|
#ifdef XP_MAC
|
|
|
|
mFileSpec += inRelativeUnixPath;
|
|
|
|
#endif
|
|
|
|
} // nsFileURL::operator +=
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsFileURL nsFileURL::operator +(const char* inRelativeUnixPath) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
nsFileURL result(*this);
|
|
|
|
result += inRelativeUnixPath;
|
|
|
|
return result;
|
|
|
|
} // nsFileURL::operator +
|
|
|
|
|
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-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-03-16 22:12:51 +03:00
|
|
|
delete [] 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
|
|
|
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
nsOutputStream& operator << (nsOutputStream& s, const nsFileURL& url)
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1998-12-09 11:47:30 +03:00
|
|
|
return (s << url.mURL);
|
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-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-03-20 04:13:35 +03: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
|
|
|
|
1998-12-09 11:47:30 +03:00
|
|
|
#ifdef XP_UNIX
|
|
|
|
//----------------------------------------------------------------------------------------
|
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()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_UNIX
|
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-03-20 02:09:39 +03:00
|
|
|
mPath = inOther.mPath;
|
1998-12-09 11:47:30 +03:00
|
|
|
}
|
|
|
|
#endif // XP_UNIX
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsFilePath::operator = (const char* inString)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-01-07 02:38:21 +03:00
|
|
|
NS_ASSERTION(strstr(inString, kFileURLPrefix) != inString, "URL passed as path");
|
1998-12-08 05:22:54 +03:00
|
|
|
#ifdef XP_MAC
|
1999-02-25 23:49:47 +03:00
|
|
|
mFileSpec = inString;
|
1999-04-26 23:05:00 +04:00
|
|
|
mPath = (const char*)nsFilePath(mFileSpec);
|
1999-01-07 02:38:21 +03:00
|
|
|
#else
|
1999-04-26 23:05:00 +04:00
|
|
|
mPath = inString;
|
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
|
|
|
|
#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)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
char* escapedPath = nsEscape(inRelativeUnixPath, url_Path);
|
|
|
|
mPath += escapedPath;
|
|
|
|
delete [] escapedPath;
|
|
|
|
#ifdef XP_MAC
|
|
|
|
mFileSpec += inRelativeUnixPath;
|
|
|
|
#endif
|
|
|
|
} // nsFilePath::operator +=
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsFilePath nsFilePath::operator +(const char* inRelativeUnixPath) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
nsFilePath result(*this);
|
|
|
|
result += inRelativeUnixPath;
|
|
|
|
return result;
|
|
|
|
} // nsFilePath::operator +
|
|
|
|
|
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
|
|
|
//========================================================================================
|
|
|
|
|
|
|
|
#ifndef XP_MAC
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpec::nsFileSpec()
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
: mError(NS_OK)
|
1998-12-08 05:22:54 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
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';
|
|
|
|
for (short index = 1; index < 1000 && Exists(); index++)
|
|
|
|
{
|
|
|
|
// start with "Picture-1.jpg" after "Picture.jpg" exists
|
|
|
|
char newName[nsFileSpecHelpers::kMaxFilenameLength + 1];
|
|
|
|
sprintf(newName, "%s-%d%s", leafName, index, suffix);
|
|
|
|
SetLeafName(newName);
|
|
|
|
}
|
|
|
|
if (*suffix)
|
1999-04-26 23:05:00 +04:00
|
|
|
nsCRT::free(suffix);
|
1999-01-07 02:38:21 +03:00
|
|
|
delete [] 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
|
|
|
//========================================================================================
|
|
|
|
|
|
|
|
#ifdef XP_UNIX
|
|
|
|
//----------------------------------------------------------------------------------------
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif // XP_UNIX
|
|
|
|
|
|
|
|
#ifdef XP_UNIX
|
|
|
|
//----------------------------------------------------------------------------------------
|
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
|
|
|
|
|
|
|
|
#if defined(XP_UNIX) || defined(XP_PC)
|
|
|
|
//----------------------------------------------------------------------------------------
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif //XP_UNIX
|
|
|
|
|
|
|
|
#if defined(XP_UNIX) || defined(XP_PC)
|
|
|
|
//----------------------------------------------------------------------------------------
|
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
|
|
|
}
|
|
|
|
#endif //XP_UNIX,PC
|
|
|
|
|
1999-02-28 04:36:48 +03:00
|
|
|
#if defined(XP_UNIX) || defined(XP_PC)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
#endif //XP_UNIX,PC
|
|
|
|
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpec::~nsFileSpec()
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(XP_UNIX) || defined(XP_PC)
|
|
|
|
//----------------------------------------------------------------------------------------
|
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
|
|
|
}
|
|
|
|
#endif //XP_UNIX
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(XP_UNIX) || defined(XP_PC)
|
|
|
|
//----------------------------------------------------------------------------------------
|
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
|
|
|
}
|
|
|
|
#endif //XP_UNIX
|
|
|
|
|
|
|
|
#if (defined(XP_UNIX) || defined(XP_PC))
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
nsOutputStream& operator << (nsOutputStream& s, const nsFileSpec& spec)
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-13 09:38:57 +03:00
|
|
|
#ifdef NS_DEBUG
|
|
|
|
static PRBool warnedOnce = PR_FALSE;
|
|
|
|
if (!warnedOnce)
|
|
|
|
{
|
|
|
|
NS_WARNING("This is for debugging only. Do not call this in shipped version!");
|
|
|
|
warnedOnce = PR_TRUE;
|
|
|
|
}
|
|
|
|
#endif // NS_DEBUG
|
|
|
|
return (s << spec.GetCString());
|
1998-12-09 11:47:30 +03:00
|
|
|
}
|
1999-03-10 01:33:36 +03:00
|
|
|
#endif // DEBUG ONLY!
|
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-02-25 23:49:47 +03:00
|
|
|
nsFileSpec result = *this;
|
1999-01-07 02:38:21 +03:00
|
|
|
result += inRelativePath;
|
|
|
|
return result;
|
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-04-26 23:05:00 +04:00
|
|
|
#if defined(XP_PC)
|
|
|
|
// windows does not care about case.
|
|
|
|
if (_stricmp(mPath, inOther.mPath ) == 0)
|
|
|
|
return PR_TRUE;
|
|
|
|
#else
|
|
|
|
if (strcmp(mPath, inOther.mPath ) == 0)
|
|
|
|
return PR_TRUE;
|
|
|
|
#endif
|
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
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
const char* nsFileSpec::GetCString() const
|
|
|
|
// 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.
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
return mPath;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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-03-20 02:09:39 +03:00
|
|
|
void nsPersistentFileDescriptor::GetData(nsSimpleCharString& outData, PRInt32& outSize) const
|
1999-02-25 23:49:47 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
outSize = mDescriptorString.Length();
|
1999-02-25 23:49:47 +03:00
|
|
|
outData = mDescriptorString;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
void nsPersistentFileDescriptor::SetData(const nsSimpleCharString& inData, PRInt32 inSize)
|
1999-02-25 23:49:47 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-04-26 23:05:00 +04:00
|
|
|
mDescriptorString.CopyFrom((const char*)inData, inSize);
|
1999-02-25 23:49:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_PERSISTENT_DATA_SIZE 1000
|
|
|
|
|
1999-02-28 04:36:48 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsresult nsPersistentFileDescriptor::Read(nsIInputStream* aStream)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-02 04:41:45 +03:00
|
|
|
nsInputStream inputStream(aStream);
|
|
|
|
inputStream >> *this;
|
1999-02-28 04:36:48 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsresult nsPersistentFileDescriptor::Write(nsIOutputStream* aStream)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-02 04:41:45 +03:00
|
|
|
nsOutputStream outputStream(aStream);
|
|
|
|
outputStream << *this;
|
1999-02-28 04:36:48 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsInputStream& operator >> (nsInputStream& s, nsPersistentFileDescriptor& d)
|
|
|
|
// reads the data from a file
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-04-26 23:05:00 +04:00
|
|
|
char bigBuffer[MAX_PERSISTENT_DATA_SIZE + 1];
|
|
|
|
// The first 8 bytes of the data should be a hex version of the data size to follow.
|
|
|
|
PRInt32 bytesRead = 8;
|
|
|
|
bytesRead = s.read(bigBuffer, bytesRead);
|
|
|
|
if (bytesRead != 8)
|
|
|
|
return s;
|
|
|
|
bigBuffer[8] = '\0';
|
|
|
|
sscanf(bigBuffer, "%x", (PRUint32*)&bytesRead);
|
|
|
|
if (bytesRead > MAX_PERSISTENT_DATA_SIZE)
|
|
|
|
{
|
|
|
|
// Try to tolerate encoded values with no length header
|
1999-04-20 23:09:22 +04:00
|
|
|
bytesRead = 8 + s.read(bigBuffer + 8, MAX_PERSISTENT_DATA_SIZE - 8);
|
1999-04-26 23:05:00 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Now we know how many bytes to read, do it.
|
|
|
|
bytesRead = s.read(bigBuffer, bytesRead);
|
|
|
|
}
|
|
|
|
d.SetData(bigBuffer, bytesRead);
|
|
|
|
return s;
|
1999-02-25 23:49:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsOutputStream& operator << (nsOutputStream& s, const nsPersistentFileDescriptor& d)
|
|
|
|
// writes the data to a file
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-04-26 23:05:00 +04:00
|
|
|
char littleBuf[9];
|
|
|
|
PRInt32 dataSize;
|
|
|
|
nsSimpleCharString data;
|
|
|
|
d.GetData(data, dataSize);
|
|
|
|
// First write (in hex) the length of the data to follow. Exactly 8 bytes
|
|
|
|
sprintf(littleBuf, "%0.8x", dataSize);
|
|
|
|
s << littleBuf;
|
|
|
|
// Now write the data itself
|
|
|
|
s << (const char*)data;
|
|
|
|
return s;
|
1999-02-25 23:49:47 +03:00
|
|
|
}
|
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-04-26 23:05:00 +04:00
|
|
|
delete [] (char*)mCString;
|
1999-02-28 04:36:48 +03:00
|
|
|
}
|
1999-04-10 04:29:20 +04:00
|
|
|
|
|
|
|
//========================================================================================
|
1999-04-26 23:05:00 +04:00
|
|
|
// class nsprPath
|
1999-04-10 04:29:20 +04:00
|
|
|
//========================================================================================
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-04-26 23:05:00 +04:00
|
|
|
nsprPath::operator const char*() const
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
((nsprPath*)this)->modifiedNSPRPath
|
|
|
|
= 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
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsprPath::~nsprPath()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
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
|
|
|
}
|