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"
|
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
|
|
|
//========================================================================================
|
1998-12-09 08:09:28 +03:00
|
|
|
NS_NAMESPACE nsFileSpecHelpers
|
1998-12-08 05:22:54 +03:00
|
|
|
//========================================================================================
|
|
|
|
{
|
1999-01-07 02:38:21 +03:00
|
|
|
enum
|
|
|
|
{ kMaxFilenameLength = 31 // should work on Macintosh, Unix, and Win32.
|
|
|
|
, kMaxAltDigitLength = 5
|
|
|
|
, kMaxCoreLeafNameLength = (kMaxFilenameLength - (kMaxAltDigitLength + 1))
|
|
|
|
};
|
|
|
|
NS_NAMESPACE_PROTOTYPE void LeafReplace(
|
|
|
|
char*& ioPath,
|
|
|
|
char inSeparator,
|
|
|
|
const char* inLeafName);
|
|
|
|
#ifndef XP_MAC
|
1999-02-25 23:49:47 +03:00
|
|
|
NS_NAMESPACE_PROTOTYPE void Canonify(char*& ioPath, PRBool inMakeDirs);
|
1999-01-07 02:38:21 +03:00
|
|
|
NS_NAMESPACE_PROTOTYPE void MakeAllDirectories(const char* inPath, int mode);
|
|
|
|
#endif
|
|
|
|
NS_NAMESPACE_PROTOTYPE char* GetLeaf(const char* inPath, char inSeparator); // allocated
|
|
|
|
NS_NAMESPACE_PROTOTYPE char* StringDup(const char* inString, int allocLength = 0);
|
|
|
|
NS_NAMESPACE_PROTOTYPE char* AllocCat(const char* inString1, const char* inString2);
|
|
|
|
NS_NAMESPACE_PROTOTYPE char* StringAssign(char*& ioString, const char* inOther);
|
|
|
|
NS_NAMESPACE_PROTOTYPE char* ReallocCat(char*& ioString, const char* inString1);
|
1999-01-27 02:49:33 +03:00
|
|
|
#ifdef XP_PC
|
|
|
|
NS_NAMESPACE_PROTOTYPE void NativeToUnix(char*& ioPath);
|
|
|
|
NS_NAMESPACE_PROTOTYPE void UnixToNative(char*& ioPath);
|
|
|
|
#endif
|
1998-12-09 08:09:28 +03:00
|
|
|
} NS_NAMESPACE_END
|
1998-12-08 05:22:54 +03:00
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsresult ns_file_convert_result(PRInt32 nativeErr)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
return nativeErr ?
|
|
|
|
NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_FILES,((nativeErr)&0xFFFF))
|
|
|
|
: NS_OK;
|
|
|
|
}
|
1999-01-07 02:38:21 +03:00
|
|
|
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
char* nsFileSpecHelpers::StringDup(
|
1999-01-07 02:38:21 +03:00
|
|
|
const char* inString,
|
|
|
|
int allocLength)
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-01-07 02:38:21 +03:00
|
|
|
if (!allocLength && inString)
|
|
|
|
allocLength = strlen(inString);
|
|
|
|
char* newPath = inString || allocLength ? new char[allocLength + 1] : nsnull;
|
|
|
|
if (!newPath)
|
1999-02-25 23:49:47 +03:00
|
|
|
return nsnull;
|
1999-01-07 02:38:21 +03:00
|
|
|
strcpy(newPath, inString);
|
|
|
|
return newPath;
|
1998-12-09 11:47:30 +03:00
|
|
|
} // nsFileSpecHelpers::StringDup
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
char* nsFileSpecHelpers::AllocCat(
|
1999-01-07 02:38:21 +03:00
|
|
|
const char* inString1,
|
|
|
|
const char* inString2)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (!inString1)
|
1999-02-25 23:49:47 +03:00
|
|
|
return inString2 ? StringDup(inString2) : (char*)nsnull;
|
1999-01-07 02:38:21 +03:00
|
|
|
if (!inString2)
|
|
|
|
return StringDup(inString1);
|
|
|
|
char* outString = StringDup(inString1, strlen(inString1) + strlen(inString2));
|
|
|
|
if (outString)
|
|
|
|
strcat(outString, inString2);
|
|
|
|
return outString;
|
1998-12-09 11:47:30 +03:00
|
|
|
} // nsFileSpecHelpers::StringDup
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
char* nsFileSpecHelpers::StringAssign(
|
1999-01-07 02:38:21 +03:00
|
|
|
char*& ioString,
|
|
|
|
const char* inString2)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (!inString2)
|
|
|
|
{
|
|
|
|
delete [] ioString;
|
1999-02-25 23:49:47 +03:00
|
|
|
ioString = (char*)nsnull;
|
1999-01-07 02:38:21 +03:00
|
|
|
return ioString;
|
|
|
|
}
|
|
|
|
if (!ioString || (strlen(inString2) > strlen(ioString)))
|
|
|
|
{
|
|
|
|
delete [] ioString;
|
|
|
|
ioString = StringDup(inString2);
|
|
|
|
return ioString;
|
|
|
|
}
|
|
|
|
strcpy(ioString, inString2);
|
|
|
|
return ioString;
|
1998-12-09 11:47:30 +03:00
|
|
|
} // nsFileSpecHelpers::StringAssign
|
|
|
|
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsFileSpecHelpers::LeafReplace(
|
1999-01-07 02:38:21 +03:00
|
|
|
char*& ioPath,
|
|
|
|
char inSeparator,
|
|
|
|
const char* inLeafName)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// Find the existing leaf name
|
|
|
|
if (!ioPath)
|
|
|
|
return;
|
|
|
|
if (!inLeafName)
|
|
|
|
{
|
|
|
|
*ioPath = '\0';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char* lastSeparator = strrchr(ioPath, inSeparator);
|
|
|
|
int oldLength = strlen(ioPath);
|
1999-02-25 23:49:47 +03:00
|
|
|
PRBool trailingSeparator = (lastSeparator + 1 == ioPath + oldLength);
|
|
|
|
if (trailingSeparator)
|
|
|
|
{
|
|
|
|
*lastSeparator = '\0';
|
|
|
|
lastSeparator = strrchr(ioPath, inSeparator);
|
|
|
|
}
|
|
|
|
if (lastSeparator)
|
|
|
|
lastSeparator++; // point at the trailing string
|
|
|
|
else
|
|
|
|
lastSeparator = ioPath; // the full monty
|
|
|
|
*lastSeparator = '\0'; // strip the current leaf name
|
|
|
|
|
|
|
|
int newLength = (lastSeparator - ioPath) + strlen(inLeafName) + int(trailingSeparator);
|
1999-01-07 02:38:21 +03:00
|
|
|
if (newLength > oldLength)
|
|
|
|
{
|
|
|
|
char* newPath = StringDup(ioPath, newLength + 1);
|
|
|
|
delete [] ioPath;
|
|
|
|
ioPath = newPath;
|
|
|
|
}
|
|
|
|
strcat(ioPath, 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;
|
|
|
|
strcat(ioPath, sepStr);
|
|
|
|
}
|
|
|
|
} // nsFileSpecHelpers::LeafReplace
|
1998-12-08 05:22:54 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1998-12-11 04:42:04 +03:00
|
|
|
char* nsFileSpecHelpers::GetLeaf(const char* inPath, char inSeparator)
|
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-01-07 02:38:21 +03:00
|
|
|
if (!inPath)
|
1999-02-25 23:49:47 +03:00
|
|
|
return nsnull;
|
|
|
|
const char* lastSeparator = strrchr(inPath, inSeparator);
|
|
|
|
|
|
|
|
// If there was no separator, then return a copy of the caller's path.
|
|
|
|
if (!lastSeparator)
|
|
|
|
return StringDup(inPath);
|
|
|
|
|
|
|
|
// So there's at least one separator. What's just after it?
|
|
|
|
// If the separator was not the last character, return the trailing string.
|
|
|
|
const char* leafPointer = lastSeparator + 1;
|
|
|
|
if (*leafPointer)
|
|
|
|
return StringDup(leafPointer);
|
|
|
|
|
|
|
|
// So now, separator was the last character. Poke in a null instead.
|
|
|
|
*(char*)lastSeparator = '\0'; // Should use const_cast, but Unix has old compiler.
|
|
|
|
leafPointer = strrchr(inPath, inSeparator);
|
1999-03-10 01:33:36 +03:00
|
|
|
char* result = leafPointer ? StringDup(++leafPointer) : StringDup(inPath);
|
1999-02-25 23:49:47 +03:00
|
|
|
// Restore the poked null before returning.
|
|
|
|
*(char*)lastSeparator = inSeparator;
|
|
|
|
#ifdef XP_PC
|
|
|
|
// If it's a drive letter use the colon notation.
|
|
|
|
if (!leafPointer && strlen(result) == 2 && result[1] == '|')
|
|
|
|
result[1] = ':';
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
} // nsFileSpecHelpers::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.
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (!inPath)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char* pathCopy = nsFileSpecHelpers::StringDup( inPath );
|
|
|
|
if (!pathCopy)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const char kSeparator = '/'; // I repeat: this should be a unix-style path.
|
|
|
|
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-01-07 02:38:21 +03: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] == '|')
|
|
|
|
{
|
|
|
|
char* startDir = nsFileSpecHelpers::StringDup( pathCopy, (strlen(pathCopy) + 1) );
|
|
|
|
strcat(startDir, "/");
|
|
|
|
|
|
|
|
spec = nsFilePath(startDir, PR_FALSE);
|
|
|
|
|
|
|
|
delete [] startDir;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
spec = nsFilePath(pathCopy, PR_FALSE);
|
|
|
|
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
spec = nsFilePath(pathCopy, PR_FALSE);
|
|
|
|
#endif
|
|
|
|
do
|
1999-01-07 02:38:21 +03: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);
|
|
|
|
if (!spec.Exists())
|
|
|
|
{
|
|
|
|
NS_ASSERTION(spec.Exists(), "Could not create the directory?");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
currentStart = ++currentEnd;
|
|
|
|
currentEnd = strchr(currentStart, kSeparator);
|
|
|
|
if (!currentEnd)
|
|
|
|
break;
|
1999-03-05 23:20:22 +03:00
|
|
|
|
|
|
|
*currentEnd = '\0';
|
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
spec += currentStart; // "lengthen" the path, adding the next node.
|
|
|
|
} while (currentEnd);
|
|
|
|
}
|
|
|
|
delete [] pathCopy;
|
|
|
|
} // nsFileSpecHelpers::MakeAllDirectories
|
|
|
|
|
|
|
|
#endif // XP_PC || XP_UNIX
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
char* nsFileSpecHelpers::ReallocCat(char*& ioString, const char* inString1)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
char* newString = AllocCat(ioString, inString1);
|
|
|
|
delete [] ioString;
|
|
|
|
ioString = newString;
|
|
|
|
return ioString;
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsFileSpecHelpers::ReallocCat
|
1998-12-08 05:22:54 +03:00
|
|
|
|
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
|
|
|
: mURL(nsnull)
|
1998-12-08 05:22:54 +03:00
|
|
|
{
|
1999-01-07 02:38:21 +03:00
|
|
|
if (!inString)
|
|
|
|
return;
|
|
|
|
NS_ASSERTION(strstr(inString, kFileURLPrefix) == inString, "Not a URL!");
|
|
|
|
// Make canonical and absolute.
|
|
|
|
nsFilePath path(inString + kFileURLPrefixLength, inCreateDirs);
|
|
|
|
*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)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
: mURL(nsnull)
|
|
|
|
{
|
|
|
|
const nsAutoCString aString(inString);
|
1999-02-28 05:17:55 +03:00
|
|
|
const char* aCString = (const char*) aString;
|
1999-02-28 04:36:48 +03:00
|
|
|
if (!inString)
|
|
|
|
return;
|
1999-02-28 05:51:53 +03:00
|
|
|
NS_ASSERTION(strstr(aCString, kFileURLPrefix) == aCString, "Not a URL!");
|
1999-02-28 04:36:48 +03:00
|
|
|
// Make canonical and absolute.
|
|
|
|
nsFilePath path(aCString + kFileURLPrefixLength, inCreateDirs);
|
|
|
|
*this = path;
|
|
|
|
} // 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-01-07 02:38:21 +03:00
|
|
|
: mURL(nsFileSpecHelpers::StringDup(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-01-07 02:38:21 +03:00
|
|
|
: mURL(nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)inOther))
|
1998-12-08 05:22:54 +03:00
|
|
|
{
|
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)
|
1999-01-07 02:38:21 +03:00
|
|
|
: mURL(nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)nsFilePath(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()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-01-07 02:38:21 +03:00
|
|
|
delete [] mURL;
|
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-01-07 02:38:21 +03:00
|
|
|
nsFileSpecHelpers::StringAssign(mURL, inString);
|
|
|
|
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
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1998-12-09 11:47:30 +03:00
|
|
|
void nsFileURL::operator = (const nsFileURL& inOther)
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-01-07 02:38:21 +03:00
|
|
|
mURL = nsFileSpecHelpers::StringAssign(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-01-07 02:38:21 +03:00
|
|
|
delete [] mURL;
|
|
|
|
mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)inOther);
|
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-01-07 02:38:21 +03:00
|
|
|
delete [] mURL;
|
|
|
|
mURL = nsFileSpecHelpers::AllocCat(kFileURLPrefix, (const char*)nsFilePath(inOther));
|
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-02-17 21:20:27 +03:00
|
|
|
nsFilePath::nsFilePath(const nsFilePath& inPath)
|
|
|
|
: mPath(nsFileSpecHelpers::StringDup(inPath.mPath))
|
|
|
|
#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-01-07 02:38:21 +03:00
|
|
|
: mPath(nsFileSpecHelpers::StringDup(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)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
: mPath(inString.ToNewCString())
|
|
|
|
{
|
1999-02-28 05:51:53 +03:00
|
|
|
NS_ASSERTION(strstr(mPath, kFileURLPrefix) != 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-01-07 02:38:21 +03:00
|
|
|
: mPath(nsFileSpecHelpers::StringDup(inOther.mURL + kFileURLPrefixLength))
|
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
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
: mPath(nsFileSpecHelpers::StringDup(inOther.mPath))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif // XP_UNIX
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsFilePath::~nsFilePath()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-01-07 02:38:21 +03:00
|
|
|
delete [] mPath;
|
1998-12-09 11:47:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1998-12-09 11:47:30 +03:00
|
|
|
mPath = nsFileSpecHelpers::StringAssign(mPath, inOther.mPath);
|
|
|
|
}
|
|
|
|
#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;
|
|
|
|
nsFileSpecHelpers::StringAssign(mPath, (const char*)nsFilePath(mFileSpec));
|
1999-01-07 02:38:21 +03:00
|
|
|
#else
|
1999-02-28 04:36:48 +03:00
|
|
|
nsFileSpecHelpers::StringAssign(mPath, inString);
|
1999-01-27 02:49:33 +03:00
|
|
|
#ifdef XP_PC
|
|
|
|
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
|
|
|
|
nsFileSpecHelpers::NativeToUnix(mPath);
|
|
|
|
#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-01-07 02:38:21 +03:00
|
|
|
nsFileSpecHelpers::StringAssign(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)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
nsFileSpecHelpers::StringAssign(mPath, inOther.mPath);
|
|
|
|
#ifdef XP_MAC
|
1999-02-26 01:04:50 +03:00
|
|
|
mFileSpec = inOther.GetFileSpec();
|
1999-02-23 06:10:29 +03:00
|
|
|
#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
|
|
|
//========================================================================================
|
|
|
|
|
|
|
|
#ifndef XP_MAC
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpec::nsFileSpec()
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
: mPath(nsnull)
|
|
|
|
, 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
|
|
|
: mPath(nsnull)
|
|
|
|
{
|
|
|
|
*this = inDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsFileSpec::nsFileSpec(const nsFileURL& inURL)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
: mPath(nsnull)
|
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)
|
|
|
|
{
|
|
|
|
suffix = nsFileSpecHelpers::StringDup(lastDot); // include '.'
|
|
|
|
*lastDot = '\0'; // strip suffix and dot.
|
|
|
|
}
|
|
|
|
const int kMaxRootLength
|
|
|
|
= nsFileSpecHelpers::kMaxCoreLeafNameLength - strlen(suffix) - 1;
|
1999-02-28 05:17:55 +03:00
|
|
|
if ((int)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)
|
|
|
|
delete [] suffix;
|
|
|
|
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)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
|
|
|
|
void* data;
|
|
|
|
PRInt32 dataSize;
|
|
|
|
inDescriptor.GetData(data, dataSize);
|
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
char* decodedData = PL_Base64Decode((const char*)data, (int)dataSize, nsnull);
|
|
|
|
// Cast to an alias record and resolve.
|
|
|
|
AliasHandle aliasH = nsnull;
|
|
|
|
mError = NS_FILE_RESULT(PtrToHand(decodedData, &(Handle)aliasH, (dataSize * 3) / 4));
|
|
|
|
PR_Free(decodedData);
|
|
|
|
if (NS_SUCCEEDED(mError))
|
|
|
|
return; // not enough memory?
|
|
|
|
|
|
|
|
Boolean changed;
|
|
|
|
mError = NS_FILE_RESULT(::ResolveAlias(nsnull, aliasH, &mSpec, &changed));
|
|
|
|
DisposeHandle((Handle) aliasH);
|
1999-03-13 09:38:57 +03:00
|
|
|
delete [] mPath;
|
1999-02-25 23:49:47 +03:00
|
|
|
#else
|
|
|
|
nsFileSpecHelpers::StringAssign(mPath, (char*)data);
|
|
|
|
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
|
|
|
//----------------------------------------------------------------------------------------
|
1998-12-11 04:42:04 +03:00
|
|
|
: mPath(nsFileSpecHelpers::StringDup((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
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
nsFileSpecHelpers::StringAssign(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
|
|
|
//----------------------------------------------------------------------------------------
|
1998-12-11 06:17:47 +03:00
|
|
|
: mPath(nsFileSpecHelpers::StringDup(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
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
: mPath(nsFileSpecHelpers::StringDup(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)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
: mPath(inString.ToNewCString())
|
|
|
|
, 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
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-01-07 02:38:21 +03:00
|
|
|
delete [] mPath;
|
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
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
mPath = nsFileSpecHelpers::StringAssign(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
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
mPath = nsFileSpecHelpers::StringAssign(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-13 09:38:57 +03:00
|
|
|
PRBool amEmpty = !mPath || !*mPath;
|
|
|
|
PRBool heEmpty = !inOther.mPath || !*inOther.mPath;
|
|
|
|
if (amEmpty) // we're the same if he's empty...
|
|
|
|
return heEmpty;
|
|
|
|
if (heEmpty) // ('cuz I'm not...)
|
|
|
|
return PR_FALSE;
|
1999-03-11 00:02:58 +03: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
|
|
|
|
#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
|
|
|
//========================================================================================
|
|
|
|
// class nsPersistentFileDescriptor
|
|
|
|
//========================================================================================
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inDesc)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
: mDescriptorString(nsFileSpecHelpers::StringDup(inDesc.mDescriptorString))
|
|
|
|
{
|
|
|
|
} // nsPersistentFileDescriptor::nsPersistentFileDescriptor
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsPersistentFileDescriptor::operator = (const nsPersistentFileDescriptor& inDesc)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
nsFileSpecHelpers::StringAssign(mDescriptorString, inDesc.mDescriptorString);
|
|
|
|
} // nsPersistentFileDescriptor::operator =
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsPersistentFileDescriptor::nsPersistentFileDescriptor(const nsFileSpec& inSpec)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
: mDescriptorString(nsnull)
|
|
|
|
{
|
|
|
|
*this = inSpec;
|
|
|
|
} // nsPersistentFileDescriptor::nsPersistentFileDescriptor
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsPersistentFileDescriptor::operator = (const nsFileSpec& inSpec)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
#ifdef XP_MAC
|
|
|
|
if (inSpec.Error())
|
|
|
|
return;
|
|
|
|
AliasHandle aliasH;
|
1999-03-13 09:38:57 +03:00
|
|
|
OSErr err = NewAlias(nil, inSpec.GetFSSpecPtr(), &aliasH);
|
1999-02-25 23:49:47 +03:00
|
|
|
if (err != noErr)
|
|
|
|
return;
|
1999-02-26 01:17:25 +03:00
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
PRUint32 bytes = GetHandleSize((Handle) aliasH);
|
|
|
|
HLock((Handle) aliasH);
|
|
|
|
char* buf = PL_Base64Encode((const char*)*aliasH, bytes, nsnull);
|
|
|
|
DisposeHandle((Handle) aliasH);
|
|
|
|
|
|
|
|
nsFileSpecHelpers::StringAssign(mDescriptorString, buf);
|
|
|
|
PR_Free(buf);
|
|
|
|
#else
|
1999-03-13 09:38:57 +03:00
|
|
|
nsFileSpecHelpers::StringAssign(mDescriptorString, inSpec.GetCString());
|
1999-02-25 23:49:47 +03:00
|
|
|
#endif // XP_MAC
|
|
|
|
} // nsPersistentFileDescriptor::operator =
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsPersistentFileDescriptor::~nsPersistentFileDescriptor()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
delete [] mDescriptorString;
|
|
|
|
} // nsPersistentFileDescriptor::~nsPersistentFileDescriptor
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsPersistentFileDescriptor::GetData(void*& outData, PRInt32& outSize) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
outSize = PL_strlen(mDescriptorString);
|
|
|
|
outData = mDescriptorString;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsPersistentFileDescriptor::SetData(const void* inData, PRInt32 inSize)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
delete [] mDescriptorString;
|
|
|
|
mDescriptorString = new char[1 + inSize];
|
|
|
|
if (!mDescriptorString)
|
|
|
|
return;
|
|
|
|
memcpy(mDescriptorString, inData, inSize);
|
|
|
|
mDescriptorString[inSize] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
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)
|
1999-02-28 05:17:55 +03:00
|
|
|
return s;
|
1999-02-25 23:49:47 +03:00
|
|
|
bigBuffer[8] = '\0';
|
1999-03-13 09:38:57 +03:00
|
|
|
sscanf(bigBuffer, "%lx", (PRUint32*)&bytesRead);
|
1999-02-25 23:49:47 +03:00
|
|
|
if (bytesRead > MAX_PERSISTENT_DATA_SIZE)
|
1999-02-28 05:17:55 +03:00
|
|
|
return s; // preposterous.
|
1999-02-25 23:49:47 +03:00
|
|
|
// Now we know how many bytes to read, do it.
|
|
|
|
s.read(bigBuffer, bytesRead);
|
|
|
|
d.SetData(bigBuffer, bytesRead);
|
1999-02-28 04:36:48 +03:00
|
|
|
return s;
|
1999-02-25 23:49:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsOutputStream& operator << (nsOutputStream& s, const nsPersistentFileDescriptor& d)
|
|
|
|
// writes the data to a file
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
char littleBuf[9];
|
|
|
|
PRInt32 dataSize;
|
|
|
|
void* data;
|
|
|
|
d.GetData(data, dataSize);
|
|
|
|
// First write (in hex) the length of the data to follow. Exactly 8 bytes
|
|
|
|
sprintf(littleBuf, "%0.8x", dataSize);
|
|
|
|
s << littleBuf;
|
|
|
|
// Now write the data itself
|
|
|
|
s << d.mDescriptorString;
|
|
|
|
return s;
|
|
|
|
}
|
1999-02-28 04:36:48 +03:00
|
|
|
|
|
|
|
//========================================================================================
|
|
|
|
// class nsAutoCString
|
|
|
|
//========================================================================================
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsAutoCString::~nsAutoCString()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-02-28 05:51:53 +03:00
|
|
|
delete [] (char*)mCString;
|
1999-02-28 04:36:48 +03:00
|
|
|
}
|