This commit is contained in:
mcmullen%netscape.com 1998-11-21 01:24:55 +00:00
Родитель c9c4d1c52b
Коммит 20641d86a2
12 изменённых файлов: 1332 добавлений и 0 удалений

21
modules/files/Makefile Normal file
Просмотреть файл

@ -0,0 +1,21 @@
#
# 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.
#
DEPTH = ../..
DIRS = public src
include $(DEPTH)/config/rules.mk

28
modules/files/Makefile.in Normal file
Просмотреть файл

@ -0,0 +1,28 @@
#!gmake
#
# 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.
DEPTH = ../..
topsrcdir = @top_srcdir@
VPATH = @srcdir@
srcdir = @srcdir@
include $(DEPTH)/config/autoconf.mk
DIRS = include src
include $(topsrcdir)/config/rules.mk

Просмотреть файл

@ -0,0 +1,272 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* 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.
*/
// First checked in on 98/11/20 by John R. McMullen.
#ifndef _FILESPEC_H_
#define _FILESPEC_H_
//========================================================================================
// This is intended to be part of the API for all C++ in the mozilla code base from now on.
// Since I am going away for a week or so, I'm checking it in before I feel it is complete.
// So far it provides
// * Type-safe ways of describing files (no more char* parameters)
// * Conversions between these
// * Methods for testing existence and for forcing uniqueness.
//
// A file specification can come from two outside sources:
// 1. A file:// URL, or
// 2. A native spec (such as an OS-based save/open dialog and the like).
// Therefore, these are the only ingredients one can use to make a file specification.
//
// Once one of our spec types has been made, conversions are provided between them
//
// In addition, string accessors are provided, because people like to manipulate
// nsFileURL and nsUnixFilePath strings directly.
//========================================================================================
#include <string>
#include "xpassert.h"
#ifdef XP_MAC
#include <Files.h>
#endif
//========================================================================================
// Here are the allowable ways to describe a file.
//========================================================================================
class nsFileURL; // This can be passed to NSPR file I/O routines.
class nsUnixFilePath;
class nsNativeFileSpec;
#define kFileURLPrefix "file://"
#define kFileURLPrefixLength (7)
//========================================================================================
class nsFileURL
// This is an escaped string that looks like "file:///foo/bar/mumble%20fish". Since URLs
// are the standard way of doing things in mozilla, this allows a string constructor,
// which just stashes the string with no conversion.
//========================================================================================
{
public:
nsFileURL(const nsFileURL& inURL);
nsFileURL(const std::string& inString);
nsFileURL(const nsUnixFilePath& inPath);
nsFileURL(const nsNativeFileSpec& inPath);
operator const char* () const { return mURL.c_str(); }
// This is the only automatic conversion to const char*
// that is provided, and it allows the
// nsFileURL to be "passed" to NSPR file routines.
operator std::string& () { return mURL; }
// This is the only automatic conversion to string
// that is provided, because a naked string should
// only mean a file URL.
void operator = (const nsFileURL& inURL);
void operator = (const std::string& inString);
void operator = (const nsUnixFilePath& inOther);
void operator = (const nsNativeFileSpec& inOther);
private:
std::string mURL;
}; // class nsFileURL
//========================================================================================
class nsUnixFilePath
// This is a string that looks like "/foo/bar/mumble%20fish". Same as nsFileURL, but
// without the "file:// prefix".
//========================================================================================
{
public:
nsUnixFilePath(const nsUnixFilePath& inPath);
nsUnixFilePath(const std::string& inString);
nsUnixFilePath(const nsFileURL& inURL);
nsUnixFilePath(const nsNativeFileSpec& inPath);
// std::string GetString() const { return mPath; }
// may be needed for implementation reasons,
// but should not provide a conversion constructor.
void operator = (const nsUnixFilePath& inPath);
void operator = (const std::string& inString);
void operator = (const nsFileURL& inURL);
void operator = (const nsNativeFileSpec& inOther);
private:
// Should not be defined (only file URLs are to be treated as strings.
operator std::string& ();
private:
std::string mPath;
}; // class nsUnixFilePath
//========================================================================================
class nsNativeFileSpec
// This is whatever each platform really prefers to describe files as.
//========================================================================================
{
public:
nsNativeFileSpec(const nsUnixFilePath& inPath);
nsNativeFileSpec(const nsFileURL& inURL);
nsNativeFileSpec(const nsNativeFileSpec& inPath);
void operator = (const nsUnixFilePath& inPath);
void operator = (const nsFileURL& inURL);
void operator = (const nsNativeFileSpec& inOther);
#ifdef XP_MAC
// For Macintosh people, this is meant to be useful in its own right as a C++ version
// of the FSSPec class.
nsNativeFileSpec(
short vRefNum,
long parID,
ConstStr255Param name);
nsNativeFileSpec(const FSSpec& inSpec)
: mSpec(inSpec), mError(noErr) {}
operator FSSpec* () { return &mSpec; }
operator const FSSpec* const () { return &mSpec; }
operator FSSpec& () { return mSpec; }
operator const FSSpec& () const { return mSpec; }
bool Valid() const { return mError == noErr; }
OSErr Error() const { return mError; }
void MakeUnique(ConstStr255Param inSuggestedLeafName);
StringPtr GetLeafPName() { return mSpec.name; }
ConstStr255Param GetLeafPName() const { return mSpec.name; }
#else
bool Valid() const { return TRUE; } // Fixme.
#endif
#if DEBUG
friend ostream& operator << (ostream& s, const nsNativeFileSpec& spec);
#endif
string GetLeafName() const;
void SetLeafName(const std::string& inLeafName);
bool Exists() const;
void MakeUnique();
void MakeUnique(const std::string& inSuggestedLeafName);
private:
friend class nsUnixFilePath;
#ifdef XP_MAC
FSSpec mSpec;
OSErr mError;
#elif defined(XP_UNIX) || defined(XP_WIN)
std::string mPath;
#endif
}; // class nsNativeFileSpec
#ifdef XP_UNIX
//========================================================================================
// UNIX nsUnixFilePath implementation
//========================================================================================
//----------------------------------------------------------------------------------------
inline nsUnixFilePath::nsUnixFilePath(const nsNativeFileSpec& inOther)
//----------------------------------------------------------------------------------------
: mPath((std::string&)inOther)
{
}
//----------------------------------------------------------------------------------------
inline void nsUnixFilePath::operator = (const nsNativeFileSpec& inOther)
//----------------------------------------------------------------------------------------
{
mPath = (std::string&)inOther;
}
#endif // XP_UNIX
//----------------------------------------------------------------------------------------
inline void nsUnixFilePath::operator = (const std::string& inString)
//----------------------------------------------------------------------------------------
{
mPath = inString;
}
//========================================================================================
// COMMON nsNativeFileSpec implementation
//========================================================================================
//----------------------------------------------------------------------------------------
inline nsNativeFileSpec::nsNativeFileSpec(const nsFileURL& inURL)
//----------------------------------------------------------------------------------------
{
*this = nsUnixFilePath(inURL); // convert to unix path first
}
//----------------------------------------------------------------------------------------
inline void nsNativeFileSpec::operator = (const nsFileURL& inURL)
//----------------------------------------------------------------------------------------
{
*this = nsUnixFilePath(inURL); // convert to unix path first
}
//========================================================================================
// UNIX & WIN nsNativeFileSpec implementation
//========================================================================================
#ifdef XP_UNIX
//----------------------------------------------------------------------------------------
inline nsNativeFileSpec::nsNativeFileSpec(const nsUnixFilePath& inPath)
//----------------------------------------------------------------------------------------
: mPath((std::string&)inPath)
{
}
#endif // XP_UNIX
#ifdef XP_UNIX
//----------------------------------------------------------------------------------------
inline void nsNativeFileSpec::operator = (const nsUnixFilePath& inPath)
//----------------------------------------------------------------------------------------
{
mPath = (std::string&)inPath;
}
#endif //XP_UNIX
#if defined(XP_UNIX) || defined(XP_WIN)
//----------------------------------------------------------------------------------------
inline nsNativeFileSpec::nsNativeFileSpec(const nsNativeFileSpec& inSpec)
//----------------------------------------------------------------------------------------
: mPath((std::string&)inSpec)
{
}
#endif //XP_UNIX
#if defined(XP_UNIX) || defined(XP_WIN)
//----------------------------------------------------------------------------------------
inline void nsNativeFileSpec::operator = (const nsNativeFileSpec& inSpec)
//----------------------------------------------------------------------------------------
{
mPath = (std::string&)inSpec;
}
#endif //XP_UNIX
#if (defined(XP_UNIX) || defined(XP_WIN)) && DEBUG
//----------------------------------------------------------------------------------------
inline ostream& operator << (ostream& s, const nsNativeFileSpec& spec)
//----------------------------------------------------------------------------------------
{
return (s << (std::string&)spec.mPath);
}
#endif // DEBUG && XP_UNIX
#endif // _FILESPEC_H_

Двоичные данные
modules/files/macbuild/files.mcp Normal file

Двоичный файл не отображается.

Просмотреть файл

@ -0,0 +1,19 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* 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 "MacPrefix_debug.h"

Просмотреть файл

@ -0,0 +1,19 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* 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 "MacPrefix_debug.h"

Просмотреть файл

@ -0,0 +1,48 @@
#!gmake
#
# 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.
#//------------------------------------------------------------------------
#//
#// Makefile to build the MODULES\FILES tree
#//
#//------------------------------------------------------------------------
#//------------------------------------------------------------------------
#//
#// Specify the depth of the current directory relative to the
#// root of NS
#//
#//------------------------------------------------------------------------
DEPTH=..\..
#//------------------------------------------------------------------------
#//
#// Specify any "command" targets. (ie. DIRS, INSTALL_FILES, ...)
#// (these must come before the common makefiles are included)
#//
#// DIRS - There are subdirectories to process
#//
#//------------------------------------------------------------------------
DIRS=include src
#//------------------------------------------------------------------------
#//
#// Include the common makefile rules
#//
#//------------------------------------------------------------------------
include <$(DEPTH)\config\rules.mak>

Просмотреть файл

@ -0,0 +1,289 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* 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"
#include "prtypes.h"
#if DEBUG
#include <ostream>
#endif
#include <strstream>
//========================================================================================
namespace nsFileSpecHelpers
//========================================================================================
{
enum
{ kMaxFilenameLength = 31 // should work on Macintosh, Unix, and Win32.
, kMaxAltDigitLength = 5
, kMaxAltNameLength = (kMaxFilenameLength - (kMaxAltDigitLength + 1))
};
void LeafReplace(
std::string& ioPath,
char inSeparator,
const std::string& inLeafName);
std::string GetLeaf(const std::string& inPath, char inSeparator);
}
//----------------------------------------------------------------------------------------
void nsFileSpecHelpers::LeafReplace(
std::string& ioPath,
char inSeparator,
const std::string& inLeafName)
//----------------------------------------------------------------------------------------
{
// Find the existing leaf name
std::string::size_type lastSeparator = ioPath.rfind(inSeparator);
std::string::size_type myLength = ioPath.length();
if (lastSeparator < myLength)
ioPath = ioPath.substr(0, lastSeparator + 1) + inLeafName;
} // nsNativeFileSpec::SetLeafName
//----------------------------------------------------------------------------------------
std::string nsFileSpecHelpers::GetLeaf(const std::string& inPath, char inSeparator)
//----------------------------------------------------------------------------------------
{
std::string::size_type lastSeparator = inPath.rfind(inSeparator);
std::string::size_type myLength = inPath.length();
if (lastSeparator < myLength)
return inPath.substr(1 + lastSeparator, myLength - lastSeparator - 1);
return inPath;
} // nsNativeFileSpec::GetLeafName
#ifdef XP_MAC
#include "nsFileSpecMac.cpp" // Macintosh-specific implementations
#elif defined(XP_WIN)
#include "nsFileSpecWin.cpp" // Windows-specific implementations
#elif defined(XP_UNIX)
#include "nsFileSpecUnix.cpp" // Windows-specific implementations
#endif
//========================================================================================
// nsFileURL implementation
//========================================================================================
//----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const std::string& inString)
//----------------------------------------------------------------------------------------
: mURL(inString)
{
XP_ASSERT(mURL.substr(0, kFileURLPrefixLength) == kFileURLPrefix);
}
//----------------------------------------------------------------------------------------
void nsFileURL::operator = (const std::string& inString)
//----------------------------------------------------------------------------------------
{
mURL = inString;
XP_ASSERT(mURL.substr(0, kFileURLPrefixLength) == kFileURLPrefix);
}
//----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsFileURL& inOther)
//----------------------------------------------------------------------------------------
: mURL(inOther.mURL)
{
}
//----------------------------------------------------------------------------------------
void nsFileURL::operator = (const nsFileURL& inOther)
//----------------------------------------------------------------------------------------
{
mURL = inOther.mURL;
}
//----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsUnixFilePath& inOther)
//----------------------------------------------------------------------------------------
{
mURL = kFileURLPrefix + ((string&)inOther);
}
//----------------------------------------------------------------------------------------
void nsFileURL::operator = (const nsUnixFilePath& inOther)
//----------------------------------------------------------------------------------------
{
mURL = kFileURLPrefix + ((string&)inOther);
}
#if 0
//----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsNativeFilePath& inOther)
//----------------------------------------------------------------------------------------
{
mURL = kFileURLPrefix + (std::string&)nsUnixFilePath(inOther);
}
#endif
#if 0
//----------------------------------------------------------------------------------------
void nsFileURL::operator = (const nsNativeFilePath& inOther)
//----------------------------------------------------------------------------------------
{
mURL = kFileURLPrefix + (std::string&)nsUnixFilePath(inOther);
}
#endif
//----------------------------------------------------------------------------------------
nsFileURL::nsFileURL(const nsNativeFileSpec& inOther)
//----------------------------------------------------------------------------------------
{
mURL = kFileURLPrefix + (std::string&)nsUnixFilePath(inOther);
}
//----------------------------------------------------------------------------------------
void nsFileURL::operator = (const nsNativeFileSpec& inOther)
//----------------------------------------------------------------------------------------
{
mURL = kFileURLPrefix + (std::string&)nsUnixFilePath(inOther);
}
//========================================================================================
// nsUnixFilePath implementation
//========================================================================================
//----------------------------------------------------------------------------------------
nsUnixFilePath::nsUnixFilePath(const std::string& inString)
//----------------------------------------------------------------------------------------
: mPath(inString)
{
XP_ASSERT(mPath.substr(0, kFileURLPrefixLength) != kFileURLPrefix);
}
//----------------------------------------------------------------------------------------
nsUnixFilePath::nsUnixFilePath(const nsFileURL& inOther)
//----------------------------------------------------------------------------------------
: mPath(((string&)inOther).substr(
kFileURLPrefixLength, ((string&)inOther).length() - kFileURLPrefixLength))
{
}
//----------------------------------------------------------------------------------------
void nsUnixFilePath::operator = (const nsFileURL& inOther)
//----------------------------------------------------------------------------------------
{
mPath = ((string&)inOther).substr(
kFileURLPrefixLength, ((string&)inOther).length() - kFileURLPrefixLength);
}
//========================================================================================
// nsNativeFileSpec implementation
//========================================================================================
//----------------------------------------------------------------------------------------
void nsNativeFileSpec::MakeUnique(const std::string& inSuggestedLeafName)
//----------------------------------------------------------------------------------------
{
if (inSuggestedLeafName.length() > 0)
SetLeafName(inSuggestedLeafName);
MakeUnique();
} // nsNativeFileSpec::MakeUnique
//----------------------------------------------------------------------------------------
void nsNativeFileSpec::MakeUnique()
//----------------------------------------------------------------------------------------
{
if (!Exists())
return;
static short index = 0;
std::string altName = GetLeafName();
std::string::size_type lastDot = altName.rfind('.');
std::string suffix;
if (lastDot < altName.length() - 1)
{
suffix = altName.substr(lastDot + 1, altName.length() - lastDot + 1);
altName = altName.substr(0, lastDot);
}
const std::string::size_type kMaxRootLength
= nsFileSpecHelpers::kMaxAltNameLength - suffix.length() - 1;
if (altName.length() > kMaxRootLength)
altName = altName.substr(0, kMaxRootLength);
while (Exists())
{
// start with "Picture-2.jpg" after "Picture.jpg" exists
if ( ++index > 999 ) // something's very wrong
return;
char buf[nsFileSpecHelpers::kMaxFilenameLength + 1];
ostrstream newName(buf, nsFileSpecHelpers::kMaxFilenameLength);
newName << altName.c_str() << "-" << index << suffix.c_str() << ends;
SetLeafName(newName.str()); // or: SetLeafName(buf)
}
} // nsNativeFileSpec::MakeUnique
#if 0
//========================================================================================
// nsNativeFilePath implementation
//========================================================================================
//----------------------------------------------------------------------------------------
nsNativeFilePath::nsNativeFilePath(const std::string& inString)
//----------------------------------------------------------------------------------------
: mPath(inString)
{
XP_ASSERT(mPath.substr(0, kFileURLPrefixLength) != kFileURLPrefix);
}
//----------------------------------------------------------------------------------------
nsNativeFilePath::nsNativeFilePath(const nsFileURL& inOther)
//----------------------------------------------------------------------------------------
: mPath(((string&)inOther).substr(
kFileURLPrefixLength, ((string&)inOther).length() - kFileURLPrefixLength))
{
}
//----------------------------------------------------------------------------------------
void nsNativeFilePath::operator = (const nsFileURL& inOther)
//----------------------------------------------------------------------------------------
{
mPath = ((string&)inOther).substr(
kFileURLPrefixLength, ((string&)inOther).length() - kFileURLPrefixLength);
}
#ifdef XP_UNIX
//----------------------------------------------------------------------------------------
nsNativeFilePath::nsNativeFilePath(const nsNativeFilePath& inOther)
//----------------------------------------------------------------------------------------
{
mPath = (std::string&)inOther;
}
//----------------------------------------------------------------------------------------
void nsNativeFilePath::operator = (const nsNativeFilePath& inOther)
//----------------------------------------------------------------------------------------
{
mPath = (std::string&)inOther;
}
//----------------------------------------------------------------------------------------
nsNativeFilePath::nsNativeFilePath(const nsNativeFileSpec& inOther)
//----------------------------------------------------------------------------------------
{
mPath = (std::string&)inOther;
}
//----------------------------------------------------------------------------------------
void nsNativeFilePath::operator = (const nsNativeFileSpec& inOther)
//----------------------------------------------------------------------------------------
{
mPath = (std::string&)inOther;
}
#endif // XP_UNIX
#endif // 0

Просмотреть файл

@ -0,0 +1,518 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* 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.
*/
// This file is included by nsFile.cp, and includes the Macintosh-specific
// implementations.
#include "FullPath.h"
#include "FileCopy.h"
#include "net.h"
#include <Aliases.h>
#include <PLStringFuncs.h>
#include <Folders.h>
const unsigned char* kAliasHavenFolderName = "\pnsAliasHaven";
//========================================================================================
namespace MacFileHelpers
//========================================================================================
{
void SwapSlashColon(char * s);
OSErr FSSpecFromFullUnixPath(
const char * unixPath,
FSSpec& outSpec,
Boolean resolveAlias);
char* MacPathFromUnixPath(const char* unixPath);
char* EncodeMacPath(
char* inPath, // NOT const - gets clobbered
Boolean prependSlash,
Boolean doEscape );
OSErr FSSpecFromPathname(
const char* inPathNamePtr,
FSSpec& outSpec);
char* PathNameFromFSSpec(
const FSSpec& inSpec,
Boolean wantLeafName );
OSErr CreateFolderInFolder(
short refNum, // Parent directory/volume
long dirID,
ConstStr255Param folderName, // Name of the new folder
short& outRefNum, // Volume of the created folder
long& outDirID); //
// Some routines to support an "alias haven" directory. Aliases in this directory
// are never resolved. There is a ResolveAlias here that respects that. This is
// to support attaching of aliases in mail.
void EnsureAliasHaven();
void SetNoResolve(Boolean inResolve);
Boolean IsAliasSafe(const FSSpec& inSpec);
OSErr MakeAliasSafe(FSSpec& inOutSpec);
OSErr ResolveAliasFile(FSSpec& inOutSpec, Boolean& wasAliased);
Boolean sNoResolve = false;
long sAliasHavenDirID = 0;
short sAliasHavenVRefNum = 0;
} // namespace MacFileHelpers
//-----------------------------------
void MacFileHelpers::SwapSlashColon(char * s)
//-----------------------------------
{
while ( *s != 0)
{
if (*s == '/')
*s++ = ':';
else if (*s == ':')
*s++ = '/';
else
*s++;
}
} // MacFileHelpers::SwapSlashColon
//-----------------------------------
char* MacFileHelpers::EncodeMacPath(
char* inPath, // NOT const, gets clobbered
Boolean prependSlash,
Boolean doEscape )
// Transforms Macintosh style path into Unix one
// Method: Swap ':' and '/', hex escape the result
//-----------------------------------
{
if (inPath == NULL)
return NULL;
int pathSize = strlen(inPath);
// XP code sometimes chokes if there's a final slash in the unix path.
// Since correct mac paths to folders and volumes will end in ':', strip this
// first.
char* c = inPath + pathSize - 1;
if (*c == ':')
{
*c = 0;
pathSize--;
}
char * newPath = NULL;
char * finalPath = NULL;
if (prependSlash)
{
newPath = new char(pathSize + 2);
newPath[0] = ':'; // It will be converted to '/'
memcpy(&newPath[1], inPath, pathSize + 1);
}
else
{
newPath = new char(pathSize + 1);
strcpy(newPath, inPath);
}
if (newPath)
{
SwapSlashColon( newPath );
if (doEscape)
{
finalPath = NET_Escape(newPath, URL_PATH);
delete [] newPath;
}
else
finalPath = newPath;
}
delete [] inPath;
return finalPath;
} // MacFileHelpers::EncodeMacPath
//----------------------------------------------------------------------------------------
inline void MacFileHelpers::SetNoResolve(Boolean inResolve)
//----------------------------------------------------------------------------------------
{
sNoResolve = inResolve;
} // MacFileHelpers::SetNoResolve
//----------------------------------------------------------------------------------------
OSErr MacFileHelpers::MakeAliasSafe(FSSpec& inOutSpec)
// Pass in the spec of an alias. This copies the file to the safe haven folder, and
// returns the spec of the copy to the caller
//----------------------------------------------------------------------------------------
{
EnsureAliasHaven();
nsNativeFileSpec dstDirSpec(sAliasHavenVRefNum, sAliasHavenDirID, "\p");
// Make sure its name is unique
nsNativeFileSpec havenSpec(sAliasHavenVRefNum, sAliasHavenDirID, "\pG'day");
if (havenSpec.Valid())
havenSpec.MakeUnique(inOutSpec.name);
// Copy the file into the haven directory
if (havenSpec.Valid())
{
OSErr err = ::FSpFileCopy(
&inOutSpec,
dstDirSpec,
havenSpec.GetLeafPName(),
nil, 0, true);
// Return the spec of the copy to the caller.
if (err != noErr)
return err;
inOutSpec = havenSpec;
}
return noErr;
} // MacFileHelpers::MakeAliasSafe
//-----------------------------------
char* MacFileHelpers::MacPathFromUnixPath(const char* unixPath)
//-----------------------------------
{
// Relying on the fact that the unix path is always longer than the mac path:
size_t len = strlen(unixPath);
char* result = new char(len + 2); // ... but allow for the initial colon in a partial name
if (result)
{
char* dst = result;
const char* src = unixPath;
if (*src == '/') // ¥ full path
src++;
else if (strchr(src, '/')) // ¥ partial path, and not just a leaf name
*dst++ = ':';
strcpy(dst, src);
NET_UnEscape(dst); // Hex Decode
MacFileHelpers::SwapSlashColon(dst);
}
return result;
} // MacFileHelpers::MacPathFromUnixPath
//-----------------------------------
OSErr MacFileHelpers::FSSpecFromPathname(const char* inPathNamePtr, FSSpec& outSpec)
// FSSpecFromPathname reverses PathNameFromFSSpec.
// It returns a FSSpec given a c string which is a mac pathname.
//-----------------------------------
{
OSErr err;
// Simplify this routine to use FSMakeFSSpec if length < 255. Otherwise use the MoreFiles
// routine FSpLocationFromFullPath, which allocates memory, to handle longer pathnames.
if (strlen(inPathNamePtr) < 255)
{
Str255 path;
int pos = 0;
while ( (path[++pos] = *inPathNamePtr++) != 0 )
;
path[0] = pos-1; // save the length of the string (pos is the next open spot)
err = ::FSMakeFSSpec(0, 0, path, &outSpec);
}
else
err = FSpLocationFromFullPath(strlen(inPathNamePtr), inPathNamePtr, &outSpec);
return err;
}
//----------------------------------------------------------------------------------------
OSErr MacFileHelpers::CreateFolderInFolder(
short refNum, // Parent directory/volume
long dirID,
ConstStr255Param folderName, // Name of the new folder
short& outRefNum, // Volume of the created folder
long& outDirID) //
// Creates a folder named 'folderName' inside a folder.
// The errors returned are same as PBDirCreate
//----------------------------------------------------------------------------------------
{
HFileParam hpb;
hpb.ioVRefNum = refNum;
hpb.ioDirID = dirID;
hpb.ioNamePtr = (StringPtr)&folderName;
OSErr err = PBDirCreateSync((HParmBlkPtr)&hpb);
if (err == noErr)
{
outRefNum = hpb.ioVRefNum;
outDirID = hpb.ioDirID;
}
else
{
outRefNum = 0;
outDirID = 0;
}
return err;
} // MacFileHelpers::CreateFolderInFolder
//----------------------------------------------------------------------------------------
void MacFileHelpers::EnsureAliasHaven()
//----------------------------------------------------------------------------------------
{
// Alias Haven is a directory in which we never resolve aliases.
if (sAliasHavenVRefNum != 0)
return;
FSSpec temp;
if (FindFolder(0, kTemporaryFolderType, true, & temp.vRefNum, &temp.parID) == noErr)
{
CreateFolderInFolder(
temp.vRefNum, // Parent directory/volume
temp.parID,
kAliasHavenFolderName, // Name of the new folder
sAliasHavenVRefNum, // Volume of the created folder
sAliasHavenDirID);
}
} // MacFileHelpers::EnsureAliasHaven
//----------------------------------------------------------------------------------------
Boolean MacFileHelpers::IsAliasSafe(const FSSpec& inSpec)
// Returns true if the alias is in the alias haven directory, or if alias resolution
// has been turned off.
//----------------------------------------------------------------------------------------
{
return sNoResolve
|| (inSpec.parID == sAliasHavenDirID && inSpec.vRefNum == sAliasHavenVRefNum);
} // MacFileHelpers::IsAliasSafe
//----------------------------------------------------------------------------------------
OSErr MacFileHelpers::ResolveAliasFile(FSSpec& inOutSpec, Boolean& wasAliased)
//----------------------------------------------------------------------------------------
{
wasAliased = false;
if (IsAliasSafe(inOutSpec))
return noErr;
Boolean dummy;
return ::ResolveAliasFile(&inOutSpec, TRUE, &dummy, &wasAliased);
} // MacFileHelpers::ResolveAliasFile
//-----------------------------------
OSErr MacFileHelpers::FSSpecFromFullUnixPath(
const char * unixPath,
FSSpec& outSpec,
Boolean resolveAlias)
// File spec from URL. Reverses GetURLFromFileSpec
// Its input is only the <path> part of the URL
// JRM 97/01/08 changed this so that if it's a partial path (doesn't start with '/'),
// then it is combined with inOutSpec's vRefNum and parID to form a new spec.
//-----------------------------------
{
if (unixPath == NULL)
return badFidErr;
char* macPath = MacPathFromUnixPath(unixPath);
if (!macPath)
return memFullErr;
OSErr err = noErr;
XP_ASSERT(*unixPath == '/' /*full path*/);
err = FSSpecFromPathname(macPath, outSpec);
if (err == fnfErr)
err = noErr;
Boolean dummy;
if (err == noErr && resolveAlias) // Added
err = MacFileHelpers::ResolveAliasFile(outSpec, dummy);
delete [] macPath;
Assert_(err==noErr||err==fnfErr||err==dirNFErr||err==nsvErr);
return err;
} // MacFileHelpers::FSSpecFromLocalUnixPath
//-----------------------------------
char* MacFileHelpers::PathNameFromFSSpec( const FSSpec& inSpec, Boolean wantLeafName )
// Returns a full pathname to the given file
// Returned value is allocated with new [], and must be freed with delete []
// This is taken from FSpGetFullPath in MoreFiles, except that we need to tolerate
// fnfErr.
//-----------------------------------
{
char* result = nil;
OSErr err = noErr;
short fullPathLength = 0;
Handle fullPath = NULL;
FSSpec tempSpec = inSpec;
if ( tempSpec.parID == fsRtParID )
{
/* The object is a volume */
/* Add a colon to make it a full pathname */
tempSpec.name[++tempSpec.name[0]] = ':';
/* We're done */
err = PtrToHand(&tempSpec.name[1], &fullPath, tempSpec.name[0]);
}
else
{
/* The object isn't a volume */
CInfoPBRec pb = { 0 };
Str63 dummyFileName;
PLstrcpy(dummyFileName, "\pG'day!");
/* Is the object a file or a directory? */
pb.dirInfo.ioNamePtr = (! tempSpec.name[0]) ? (StringPtr)dummyFileName : tempSpec.name;
pb.dirInfo.ioVRefNum = tempSpec.vRefNum;
pb.dirInfo.ioDrDirID = tempSpec.parID;
pb.dirInfo.ioFDirIndex = 0;
err = PBGetCatInfoSync(&pb);
if ( err == noErr || err == fnfErr)
{
// if the object is a directory, append a colon so full pathname ends with colon
// Beware of the "illegal spec" case that Netscape uses (empty name string). In
// this case, we don't want the colon.
if ( err == noErr && tempSpec.name[0] && (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 )
{
++tempSpec.name[0];
tempSpec.name[tempSpec.name[0]] = ':';
}
/* Put the object name in first */
err = PtrToHand(&tempSpec.name[1], &fullPath, tempSpec.name[0]);
if ( err == noErr )
{
/* Get the ancestor directory names */
pb.dirInfo.ioNamePtr = tempSpec.name;
pb.dirInfo.ioVRefNum = tempSpec.vRefNum;
pb.dirInfo.ioDrParID = tempSpec.parID;
do /* loop until we have an error or find the root directory */
{
pb.dirInfo.ioFDirIndex = -1;
pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
err = PBGetCatInfoSync(&pb);
if ( err == noErr )
{
/* Append colon to directory name */
++tempSpec.name[0];
tempSpec.name[tempSpec.name[0]] = ':';
/* Add directory name to beginning of fullPath */
(void) Munger(fullPath, 0, NULL, 0, &tempSpec.name[1], tempSpec.name[0]);
err = MemError();
}
} while ( err == noErr && pb.dirInfo.ioDrDirID != fsRtDirID );
}
}
}
if ( err != noErr && err != fnfErr)
goto Clean;
fullPathLength = GetHandleSize(fullPath);
err = noErr;
int allocSize = 1 + fullPathLength;
// We only want the leaf name if it's the root directory or wantLeafName is true.
if (inSpec.parID != fsRtParID && !wantLeafName)
allocSize -= inSpec.name[0];
result = new char(allocSize);
if (!result)
goto Clean;
memcpy(result, *fullPath, allocSize - 1);
result[ allocSize - 1 ] = 0;
Clean:
if (fullPath)
DisposeHandle(fullPath);
Assert_(result); // OOPS! very bad.
return result;
} // MacFileHelpers::PathNameFromFSSpec
//========================================================================================
// Macintosh nsNativeFileSpec implementation
//========================================================================================
//----------------------------------------------------------------------------------------
nsNativeFileSpec::nsNativeFileSpec(
short vRefNum,
long parID,
ConstStr255Param name)
//----------------------------------------------------------------------------------------
{
mError = ::FSMakeFSSpec(vRefNum, parID, name, &mSpec);
if (mError == fnfErr)
mError = noErr;
}
//----------------------------------------------------------------------------------------
nsNativeFileSpec::nsNativeFileSpec(const nsUnixFilePath& inPath)
//----------------------------------------------------------------------------------------
{
*this = inPath;
}
#if DEBUG
//----------------------------------------------------------------------------------------
ostream& operator << (ostream& s, const nsNativeFileSpec& spec)
//----------------------------------------------------------------------------------------
{
s << spec.mSpec.vRefNum << ", " << spec.mSpec.parID << ", \"";
for (int i = 1; i <= spec.mSpec.name[0]; i++)
s.put(spec.mSpec.name[i]);
return s << "\"";
} // ostream& operator << (ostream&, const nsNativeFileSpec&)
#endif // DEBUG
//----------------------------------------------------------------------------------------
void nsNativeFileSpec::operator = (const nsUnixFilePath& inPath)
//----------------------------------------------------------------------------------------
{
std::string& str = (std::string&)inPath;
const char* cstr = str.c_str();
mError = MacFileHelpers::FSSpecFromFullUnixPath(cstr, mSpec, true);
} // nsNativeFileSpec::operator =
//----------------------------------------------------------------------------------------
bool nsNativeFileSpec::Exists() const
//----------------------------------------------------------------------------------------
{
FSSpec temp;
return ::FSMakeFSSpec(mSpec.vRefNum, mSpec.parID, mSpec.name, &temp) == noErr;
} // nsNativeFileSpec::operator =
//----------------------------------------------------------------------------------------
void nsNativeFileSpec::SetLeafName(const std::string& inLeafName)
//----------------------------------------------------------------------------------------
{
*mSpec.name = inLeafName.length();
memcpy(mSpec.name + 1, inLeafName.c_str(), 1 + *mSpec.name);
} // nsNativeFileSpec::SetLeafName
//----------------------------------------------------------------------------------------
std::string nsNativeFileSpec::GetLeafName() const
//----------------------------------------------------------------------------------------
{
char leaf[64];
memcpy(leaf, &mSpec.name[1], mSpec.name[0]);
leaf[mSpec.name[0]] = '\0';
return std::string(leaf);
} // nsNativeFileSpec::GetLeafName
//========================================================================================
// Macintosh nsUnixFilePath implementation
//========================================================================================
//----------------------------------------------------------------------------------------
nsUnixFilePath::nsUnixFilePath(const nsNativeFileSpec& inPath)
//----------------------------------------------------------------------------------------
{
*this = inPath;
}
//----------------------------------------------------------------------------------------
void nsUnixFilePath::operator = (const nsNativeFileSpec& inPath)
//----------------------------------------------------------------------------------------
{
char * path = MacFileHelpers::PathNameFromFSSpec( inPath.mSpec, TRUE );
char * unixPath = MacFileHelpers::EncodeMacPath(path, true, true);
mPath = unixPath;
} // nsUnixFilePath::operator =
//========================================================================================
// Macintosh nsFileURL implementation
//========================================================================================

Просмотреть файл

@ -0,0 +1,35 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* 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.
*/
// This file is included by nsFile.cpp, and includes the Unix-specific
// implementations.
//----------------------------------------------------------------------------------------
void nsNativeFileSpec::SetLeafName(const std::string& inLeafName)
//----------------------------------------------------------------------------------------
{
nsFileHelpers::LeafReplace(mPath, '/', inLeafName);
} // nsNativeFileSpec::SetLeafName
//----------------------------------------------------------------------------------------
std::string nsNativeFileSpec::GetLeafName() const
//----------------------------------------------------------------------------------------
{
return nsFileHelpers::GetLeaf(mPath, '/');
} // nsNativeFileSpec::GetLeafName

Просмотреть файл

@ -0,0 +1,57 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* 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.
*/
// This file is included by nsFile.cp, and includes the Windows-specific
// implementations.
//----------------------------------------------------------------------------------------
nsNativeFileSpec::nsNativeFileSpec(const nsUnixFilePath& inPath)
//----------------------------------------------------------------------------------------
{
*this = inPath;
}
//----------------------------------------------------------------------------------------
void nsNativeFileSpec::operator = (const nsUnixFilePath& inPath)
//----------------------------------------------------------------------------------------
{
// Convert '/' to '\'
std::string& str = (std::string&)inPath;
for (std::string::size_type i = 0; i < str.length(); i++)
{
char c = str[i];
if (c == '/')
c = '\\';
mPath.append(&c, 1);
}
} // nsNativeFileSpec::operator =
//----------------------------------------------------------------------------------------
void nsNativeFileSpec::SetLeafName(const std::string& inLeafName)
//----------------------------------------------------------------------------------------
{
nsFileHelpers::LeafReplace(mPath, '\\', inLeafName);
} // nsNativeFileSpec::SetLeafName
//----------------------------------------------------------------------------------------
std::string nsNativeFileSpec::GetLeafName() const
//----------------------------------------------------------------------------------------
{
return nsFileHelpers::GetLeaf(mPath, '\\');
} // nsNativeFileSpec::GetLeafName

Просмотреть файл

@ -0,0 +1,26 @@
#include "nsFileSpec.h"
#include <iostream>
//----------------------------------------------------------------------------------------
void main()
//----------------------------------------------------------------------------------------
{
nsFileURL fileURL("file:///Development/MPW/MPW%20Shell");
cout << "File URL initialized to: \"" << (string&)fileURL << "\""<< endl;
nsUnixFilePath filePath(fileURL);
cout << "As a unix path: \"" << (string&)filePath << "\""<< endl;
nsNativeFileSpec fileSpec(fileURL);
cout << "As a file spec: " << fileSpec << endl;
fileSpec.MakeUnique();
cout << "Unique file spec: " << fileSpec << endl;
fileURL = fileSpec;
cout << "File URL assigned from spec: \"" << (string&)fileURL << "\""<< endl;
filePath = "/Development/MPW/SysErrs.err";
cout << "File path reassigned to: \"" << (string&)filePath << "\""<< endl;
cout << flush; // ?
fileSpec = filePath;
cout << "File spec reassigned to: " << fileSpec << endl;
fileSpec.MakeUnique();
cout << "File spec made unique: " << fileSpec << endl;
}