diff --git a/mozilla/base/macbuild/TestFiles.mcp b/mozilla/base/macbuild/TestFiles.mcp new file mode 100644 index 00000000000..55c19824b48 Binary files /dev/null and b/mozilla/base/macbuild/TestFiles.mcp differ diff --git a/mozilla/base/macbuild/files.prefix b/mozilla/base/macbuild/files.prefix new file mode 100644 index 00000000000..e5eda421547 --- /dev/null +++ b/mozilla/base/macbuild/files.prefix @@ -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" diff --git a/mozilla/base/macbuild/filesDebug.prefix b/mozilla/base/macbuild/filesDebug.prefix new file mode 100644 index 00000000000..e5eda421547 --- /dev/null +++ b/mozilla/base/macbuild/filesDebug.prefix @@ -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" diff --git a/mozilla/base/public/nsEscape.h b/mozilla/base/public/nsEscape.h new file mode 100644 index 00000000000..8cd14adb710 --- /dev/null +++ b/mozilla/base/public/nsEscape.h @@ -0,0 +1,60 @@ +/* -*- 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/12/03 by John R. McMullen, derived from net.h/mkparse.c. */ + +#ifndef _ESCAPE_H_ +#define _ESCAPE_H_ + +#include "PRTypes.h" + +/* valid mask values for NET_Escape() and NET_EscapedSize(). */ +typedef enum { + url_XAlphas = (1<<0) +, url_XPAlphas = (1<<1) +, url_Path = (1<<2) +} nsEscapeMask; + +#ifdef __cplusplus +extern "C" { +#endif +char * nsEscape(const char * str, nsEscapeMask mask); + /* Caller must use delete [] on the result */ +char * nsUnescape(char * str); + /* decode % escaped hex codes into character values, + * modifies the parameter, returns the same buffer + */ + +char * nsEscapeCount(const char * str, PRInt32 len, nsEscapeMask mask, PRInt32* out_len); + /* Like nsEscape, but if out_len is non-null, return result string length + * in *out_len, and uses len instead of NUL termination. + * Caller must use delete [] on the result. + */ +PRInt32 nsUnescapeCount (char * str); + /* decode % escaped hex codes into character values, + * modifies the parameter buffer, returns the length of the result + * (result may contain \0's). + */ + +#ifdef __cplusplus +} +#endif + + + +#endif // _ESCAPE_H_ diff --git a/mozilla/base/public/nsFileSpec.h b/mozilla/base/public/nsFileSpec.h new file mode 100644 index 00000000000..2c12feda22d --- /dev/null +++ b/mozilla/base/public/nsFileSpec.h @@ -0,0 +1,306 @@ +/* -*- 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. Checked in again 98/12/04. + +#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. +// 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 +#include "nsDebug.h" +#ifdef XP_MAC +#include +#endif + +//======================================================================================== +// Here are the allowable ways to describe a file. +//======================================================================================== + +class nsUnixFilePath; // This can be passed to NSPR file I/O routines. +class nsFileURL; +class nsNativeFileSpec; + +#define kFileURLPrefix "file://" +#define kFileURLPrefixLength (7) + +//======================================================================================== +class nsNativeFileSpec +// This is whatever each platform really prefers to describe files as. +//======================================================================================== +{ + public: + nsNativeFileSpec(); + nsNativeFileSpec(const std::string& inString); + nsNativeFileSpec(const nsUnixFilePath& inPath); + nsNativeFileSpec(const nsFileURL& inURL); + nsNativeFileSpec(const nsNativeFileSpec& inPath); + + void operator = (const std::string& 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 + +//======================================================================================== +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 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. + +// std::string GetString() const { return mPath; } + // may be needed for implementation reasons, + // but should not provide a conversion constructor. + + void operator = (const nsFileURL& inURL); + void operator = (const std::string& inString); + void operator = (const nsUnixFilePath& inOther); + void operator = (const nsNativeFileSpec& inOther); + +#ifdef XP_MAC + // Accessor to allow quick assignment to a mNativeFileSpec + const nsNativeFileSpec& GetNativeSpec() const { return mNativeFileSpec; } +#endif + private: + + std::string mURL; +#ifdef XP_MAC + // Since the path on the macintosh does not uniquely specify a file (volumes + // can have the same name), stash the secret nsNativeFileSpec, too. + nsNativeFileSpec mNativeFileSpec; +#endif +}; // 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); + + + operator const char* () const { return mPath.c_str(); } + // This is the only automatic conversion to const char* + // that is provided, and it allows the + // path to be "passed" to NSPR file routines. + + void operator = (const nsUnixFilePath& inPath); + void operator = (const std::string& inString); + void operator = (const nsFileURL& inURL); + void operator = (const nsNativeFileSpec& inOther); + +#ifdef XP_MAC + public: + // Accessor to allow quick assignment to a mNativeFileSpec + const nsNativeFileSpec& GetNativeSpec() const { return mNativeFileSpec; } +#endif + + private: + // Should not be defined (only file URLs are to be treated as strings. + operator std::string& (); + private: + + std::string mPath; +#ifdef XP_MAC + // Since the path on the macintosh does not uniquely specify a file (volumes + // can have the same name), stash the secret nsNativeFileSpec, too. + nsNativeFileSpec mNativeFileSpec; +#endif +}; // class nsUnixFilePath + +#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 + +//======================================================================================== +// 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 nsNativeFileSpec::nsNativeFileSpec(const std::string& inString) +//---------------------------------------------------------------------------------------- +: mPath(inString) +{ +} +#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) +//---------------------------------------------------------------------------------------- +inline nsNativeFileSpec::operator = (const std::string& inString) +//---------------------------------------------------------------------------------------- +{ + mPath = inString; +} +#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_ diff --git a/mozilla/base/public/nsFileStream.h b/mozilla/base/public/nsFileStream.h new file mode 100644 index 00000000000..df0851d6eb4 --- /dev/null +++ b/mozilla/base/public/nsFileStream.h @@ -0,0 +1,737 @@ +/* -*- 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. Checked in again 98/12/04. + +#ifndef _STREAM_H_ +#define _STREAM_H_ + +#include +#include "prio.h" +#include "nsFileSpec.h" + +#ifdef __MWERKS__ +#ifdef MSIPL_WCHART +#define NS_USING_WIDE_CHAR +#endif +#ifdef MSIPL_EXPLICIT_FUNC_TEMPLATE_ARG +#define NS_EXPLICIT_FUNC_TEMPLATE_ARG +#endif +#endif + +//======================================================================================== +template +class nsFileBufferT +//======================================================================================== +: public basic_streambuf +{ + typedef codecvt_base::result result; + +public: + typedef charT char_type; + typedef typename traits::pos_type pos_type; + typedef typename traits::off_type off_type; + typedef typename traits::int_type int_type; + typedef traits traits_type; + typedef typename traits::state_type state_type; + + typedef nsFileBufferT filebuf_type; + typedef codecvt ofacet_type; + typedef codecvt ifacet_type; + + nsFileBufferT(); + nsFileBufferT(PRFileDesc* pfile_arg); + virtual ~nsFileBufferT(); + bool is_open() const; + filebuf_type* open( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode); + filebuf_type* close(); + +protected: + virtual int_type overflow(int_type c=traits::eof()); + virtual int_type pbackfail(int_type c=traits::eof()); + virtual int_type underflow(); + virtual pos_type seekoff( + off_type off, ios_base::seekdir way, + ios_base::openmode which=ios_base::in|ios_base::out); + virtual pos_type seekpos(pos_type sp, + ios_base::openmode which=ios_base::in|ios_base::out); + virtual basic_streambuf* setbuf(char_type* s, streamsize n); + virtual int sync(); + virtual int_type uflow(); + virtual void imbue(const locale& loc); + virtual streamsize showmanyc(); + virtual streamsize xsgetn(char_type* s, streamsize n); + virtual streamsize xsputn(const char_type* s, streamsize n); + +private: + PRFileDesc* mFileDesc; + ios_base::openmode mode_; +}; // class nsFileBufferT + +//======================================================================================== +template +class nsInputFileStreamT +//======================================================================================== +: public basic_istream +{ + typedef nsFileBufferT filebuf_type; + +public: + typedef charT char_type; + typedef typename traits::pos_type pos_type; + typedef typename traits::off_type off_type; + typedef typename traits::int_type int_type; + typedef traits traits_type; + + nsInputFileStreamT(); + explicit nsInputFileStreamT( + const nsUnixFilePath& inFile, + ios_base::openmode mode=ios_base::in, + PRIntn accessMode = 0x00400); + + virtual ~nsInputFileStreamT(); + + filebuf_type* rdbuf() const; + inline bool is_open(); + inline void open( + const nsUnixFilePath& inFile, + ios_base::openmode mode=ios_base::in, + PRIntn accessMode = 0x00400); + inline void close(); + +private: + filebuf_type mBuffer; +}; // class nsInputFileStreamT + +//======================================================================================== +template +class nsOutputFileStreamT +//======================================================================================== +: public basic_ostream +{ + typedef nsFileBufferT filebuf_type; + +public: + typedef charT char_type; + typedef typename traits::pos_type pos_type; + typedef typename traits::off_type off_type; + typedef typename traits::int_type int_type; + typedef traits traits_type; + + nsOutputFileStreamT(); + explicit nsOutputFileStreamT( + const nsUnixFilePath& inFile, + ios_base::openmode mode = ios_base::out|ios_base::trunc, + PRIntn accessMode = 0x00200); + + virtual ~nsOutputFileStreamT(); + + filebuf_type* rdbuf() const; + inline bool is_open(); + inline void open( + const nsUnixFilePath& inFile, + ios_base::openmode mode = ios_base::out|ios_base::trunc, + PRIntn accessMode = 0x00200); + inline void close(); + +private: + filebuf_type mBuffer; +}; // class nsOutputFileStreamT + +//======================================================================================== +// Implementation of nsFileBufferT +//======================================================================================== + +//---------------------------------------------------------------------------------------- +template +inline nsFileBufferT::nsFileBufferT() + : basic_streambuf(), mFileDesc(NULL) +//---------------------------------------------------------------------------------------- +{ +} + +//---------------------------------------------------------------------------------------- +template +inline nsFileBufferT::nsFileBufferT(PRFileDesc* pfarg) + : basic_streambuf(), mFileDesc(pfarg) +//---------------------------------------------------------------------------------------- +{ +} + +//---------------------------------------------------------------------------------------- +template +inline nsFileBufferT::~nsFileBufferT() +//---------------------------------------------------------------------------------------- +{ + close(); +} + +//---------------------------------------------------------------------------------------- +template +inline bool +nsFileBufferT::is_open() const +//---------------------------------------------------------------------------------------- +{ + READ_LOCK(_mutex); + return bool(mFileDesc); // in case it is typedefed to int +} + +//---------------------------------------------------------------------------------------- +template +nsFileBufferT::filebuf_type* +nsFileBufferT::open( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode + ) +//---------------------------------------------------------------------------------------- +{ + if (mFileDesc) + return 0; + const ios_base::openmode valid_modes[]= + { + ios_base::out, + ios_base::out | ios_base::app, + ios_base::out | ios_base::trunc, + ios_base::in, + ios_base::in | ios_base::out, + ios_base::in | ios_base::out | ios_base::trunc, +// ios_base::out | ios_base::binary, +// ios_base::out | ios_base::app | ios_base::binary, +// ios_base::out | ios_base::trunc | ios_base::binary, +// ios_base::in | ios_base::binary, +// ios_base::in | ios_base::out | ios_base::binary, +// ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary, + 0 + }; + const int nspr_modes[]={ + PR_WRONLY | PR_CREATE_FILE, + PR_WRONLY | PR_CREATE_FILE | PR_APPEND, + PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, + PR_RDONLY, + PR_RDONLY | PR_APPEND, + PR_RDWR | PR_CREATE_FILE, + PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE, +// "wb", +// "ab", +// "wb", +// "rb", +// "r+b", +// "w+b", + 0 }; + int ind=0; + while (valid_modes[ind] && valid_modes[ind] != (mode&~ios_base::ate)) + ++ind; + if (!nspr_modes[ind]) + return 0; + + WRITE_LOCK(_mutex); + mode_ = mode; + if ((mFileDesc = PR_Open(inFile, nspr_modes[ind], accessMode)) != 0) + if (mode&ios_base::ate && PR_Seek(mFileDesc, 0, PR_SEEK_END) >= 0) + close(); + else + return this; + return 0; +} // nsFileBufferT::open + +//---------------------------------------------------------------------------------------- +template +nsFileBufferT::filebuf_type* nsFileBufferT::close() +//---------------------------------------------------------------------------------------- +{ + if (mFileDesc==PR_STDIN || mFileDesc==PR_STDOUT || mFileDesc==PR_STDERR) + return this; + WRITE_LOCK(_mutex); + return (mFileDesc && PR_Close(mFileDesc) == PR_SUCCESS) ? mFileDesc = 0, this : 0; +} + +//---------------------------------------------------------------------------------------- +template +inline int +nsFileBufferT:: sync() +//---------------------------------------------------------------------------------------- +{ + return (mFileDesc ? (int)PR_Sync(mFileDesc) : 0); +} + +//---------------------------------------------------------------------------------------- +template +inline basic_streambuf* +nsFileBufferT::setbuf(char_type*, streamsize) +//---------------------------------------------------------------------------------------- +{ + return (!mFileDesc) ? 0 : this; +} + +//---------------------------------------------------------------------------------------- +template +nsFileBufferT::int_type nsFileBufferT::overflow(int_type c) +//---------------------------------------------------------------------------------------- +{ +#ifdef NS_EXPLICIT_FUNC_TEMPLATE_ARG + const ofacet_type& ft=use_facet(loc); +#else + const ofacet_type& ft=use_facet(loc, (ofacet_type*)0); +#endif + char_type ch = traits_type::to_char_type(c); + if (!mFileDesc) + return traits_type::eof(); + if (traits_type::eq_int_type(c, traits::eof())) + return traits_type::not_eof(c); + if (ft.always_noconv()) + { + PRInt32 bytesWrit1 = PR_Write(mFileDesc, &ch, sizeof(ch)); + return bytesWrit1 < sizeof(ch) ? traits::eof() : c; + } + { // <- sic! + state_type fst; + const char_type* end; + char buf[4]; + char* ebuf; + result conv; + if ((conv=ft.out(fst, &ch, &ch+1, end, buf, buf+3, ebuf))== + codecvt_base::noconv) + { + PRInt32 bytesWrit2 = PR_Write(mFileDesc, &ch, sizeof(ch)); + return bytesWrit2 < sizeof(ch) ? traits::eof() : c; + } + if ((conv==codecvt_base::partial)||(conv==codecvt_base::error)) + return traits::eof(); + *ebuf=0; + PRInt32 bytesWrit3 = strlen(buf); + return PR_Write(mFileDesc, buf, bytesWrit3) < bytesWrit3 ? traits_type::eof() : c; + } +} + +//---------------------------------------------------------------------------------------- +template +inline nsFileBufferT::int_type nsFileBufferT::underflow() +//---------------------------------------------------------------------------------------- +{ + if (!mFileDesc) + return traits_type::eof(); + char_type s; + PRInt32 request = 1; + if (1 != PR_Read(mFileDesc, &s, request)) + return traits_type::eof(); + PR_Seek(mFileDesc, -1, PR_SEEK_CUR); + return (int_type)s; +} + +//---------------------------------------------------------------------------------------- +template +streamsize nsFileBufferT::xsputn(const char_type* s, streamsize n) +//---------------------------------------------------------------------------------------- +{ +#ifdef NS_EXPLICIT_FUNC_TEMPLATE_ARG + const ofacet_type& ft=use_facet(loc); +#else + const ofacet_type& ft=use_facet(loc, (ofacet_type*)0); +#endif + if (!mFileDesc || !n) + return 0; + if (ft.always_noconv()) + { + PRInt32 bytesWrit1 = PR_Write(mFileDesc, s, sizeof(char) * size_t(n)); + return bytesWrit1 < 0 ? 0 : (streamsize)bytesWrit1; + } + { // <- sic! + state_type fst; + const char_type* end; + char buf[8]; + char* ebuf; + result conv; +#ifdef NS_EXPLICIT_FUNC_TEMPLATE_ARG + if ((conv=use_facet(loc). + out(fst, s, s+n, end, buf, buf+7, ebuf))==codecvt_base::noconv) +#else + if ((conv=use_facet(loc, (ofacet_type*)0). + out(fst, s, s+n, end, buf, buf+7, ebuf))==codecvt_base::noconv) +#endif + return (streamsize)PR_Write(mFileDesc, s, sizeof(char) * size_t(n)); + if ((conv==codecvt_base::partial) ||(conv==codecvt_base::error)) + return 0; + *ebuf=0; + PRInt32 bytesWrit2 = strlen(buf); + bytesWrit2 = PR_Write(mFileDesc, buf, bytesWrit2); + return bytesWrit2 < 0 ? 0 : streamsize(bytesWrit2 / sizeof(char_type)); + } +} + +//---------------------------------------------------------------------------------------- +template +inline nsFileBufferT::int_type + nsFileBufferT::pbackfail(int_type c) +//---------------------------------------------------------------------------------------- +{ + if (!mFileDesc) + return traits_type::eof(); + if (PR_Seek(mFileDesc, -1, PR_SEEK_CUR) < 0) + return traits_type::eof(); + return (traits::eq_int_type(c, traits_type::eof())) ? traits::not_eof(c) : c; +} + +//---------------------------------------------------------------------------------------- +template +inline nsFileBufferT::int_type nsFileBufferT::uflow() +//---------------------------------------------------------------------------------------- +{ + if (!mFileDesc) + return traits_type::eof(); + char_type s; + if (1 != PR_Read(mFileDesc, &s, 1)) // attempt to read 1 byte, confirm 1 byte + return traits_type::eof(); + return (int_type)s; +} + +//---------------------------------------------------------------------------------------- +template +inline streamsize nsFileBufferT::xsgetn(char_type* s, streamsize n) +//---------------------------------------------------------------------------------------- +{ + return mFileDesc ? (streamsize)PR_Read(mFileDesc, s, sizeof(char) * size_t(n)) : 0; +} + +//---------------------------------------------------------------------------------------- +template +inline void nsFileBufferT::imbue(const locale& loc_arg) +//---------------------------------------------------------------------------------------- +{ + loc = loc_arg; +} + +template +inline streamsize +nsFileBufferT::showmanyc() +{ + return (streamsize)PR_Available(mFileDesc); +} + +//---------------------------------------------------------------------------------------- +template +nsFileBufferT::pos_type nsFileBufferT::seekoff( + off_type off, + ios_base::seekdir way, + ios_base::openmode /* which */) +//---------------------------------------------------------------------------------------- +{ + if (!mFileDesc || ((way&ios_base::beg) && off<0) || ((way&ios_base::end) && off > 0)) + return pos_type(-1); + PRSeekWhence poseek = PR_SEEK_CUR; + switch (way) + { + case ios_base::beg : poseek= PR_SEEK_SET; + break; + case ios_base::end : poseek= PR_SEEK_END; + break; + } + PRInt32 position = PR_Seek(mFileDesc, off, poseek); + if (position < 0) + return pos_type(-1); + return pos_type(position); +} + +//---------------------------------------------------------------------------------------- +template +nsFileBufferT::pos_type +nsFileBufferT::seekpos(pos_type sp, ios_base::openmode) +//---------------------------------------------------------------------------------------- +{ + if (!mFileDesc || sp==pos_type(-1)) + return -1; + PRInt32 position = PR_Seek(mFileDesc, sp.offset(), PR_SEEK_SET); + if (position < 0) + return pos_type(-1); + return position; +} + +//======================================================================================== +// Implementation of nsInputFileStreamT +//======================================================================================== + +//---------------------------------------------------------------------------------------- +template +inline nsInputFileStreamT::nsInputFileStreamT() + : basic_istream(&mBuffer) +//---------------------------------------------------------------------------------------- +{ + // already inited +} + +//---------------------------------------------------------------------------------------- +template +inline nsInputFileStreamT::nsInputFileStreamT( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode) +//---------------------------------------------------------------------------------------- + : basic_istream(&mBuffer) +{ + // already inited + if (!mBuffer.open(inFile, openmode(mode|in), accessMode)) + setstate(failbit); +} + +//---------------------------------------------------------------------------------------- +template +inline nsInputFileStreamT::~nsInputFileStreamT() +//---------------------------------------------------------------------------------------- +{ +} + +//---------------------------------------------------------------------------------------- +template +inline nsInputFileStreamT::filebuf_type* +nsInputFileStreamT::rdbuf() const +//---------------------------------------------------------------------------------------- +{ + return (filebuf_type*)&mBuffer; +} + +//---------------------------------------------------------------------------------------- +template +inline bool +nsInputFileStreamT:: is_open() +//---------------------------------------------------------------------------------------- +{ + return mBuffer.is_open(); +} + +//---------------------------------------------------------------------------------------- +template +inline void +nsInputFileStreamT::open( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode) +//---------------------------------------------------------------------------------------- +{ + if (!mBuffer.open(inFile, openmode(mode|in), accessMode)) + setstate(failbit); +} + +//---------------------------------------------------------------------------------------- +template +inline void nsInputFileStreamT::close() +//---------------------------------------------------------------------------------------- +{ + if (!mBuffer.close()) + setstate(failbit); +} + +//======================================================================================== +// Implementation of nsOutputFileStreamT +//======================================================================================== + +//---------------------------------------------------------------------------------------- +template +inline nsOutputFileStreamT::nsOutputFileStreamT() + : basic_ostream(&mBuffer) +//---------------------------------------------------------------------------------------- +{ + // already inited +} + +//---------------------------------------------------------------------------------------- +template +nsOutputFileStreamT::nsOutputFileStreamT( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode) +//---------------------------------------------------------------------------------------- +: basic_ostream(&mBuffer) +{ + // already inited + if (!mBuffer.open(inFile, openmode(mode|out), accessMode)) + setstate(failbit); +} + +//---------------------------------------------------------------------------------------- +template +inline nsOutputFileStreamT::~nsOutputFileStreamT() +//---------------------------------------------------------------------------------------- +{ +} + +//---------------------------------------------------------------------------------------- +template +inline nsOutputFileStreamT::filebuf_type* +nsOutputFileStreamT::rdbuf() const +//---------------------------------------------------------------------------------------- +{ + return (filebuf_type*)&mBuffer; +} + +//---------------------------------------------------------------------------------------- +template +inline bool nsOutputFileStreamT:: is_open() +//---------------------------------------------------------------------------------------- +{ + return mBuffer.is_open(); +} + +//---------------------------------------------------------------------------------------- +template +inline void nsOutputFileStreamT::open( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode) +//---------------------------------------------------------------------------------------- +{ + if (!mBuffer.open(inFile, openmode(mode | out), accessMode)) + setstate(failbit); +} + +//---------------------------------------------------------------------------------------- +template +inline void nsOutputFileStreamT:: close() +//---------------------------------------------------------------------------------------- +{ + if (!mBuffer.close()) + setstate(failbit); +} + +//======================================================================================== +template +class nsIOFileStreamT : public basic_iostream +//======================================================================================== +{ + typedef nsFileBufferT filebuf_type; + +public: + typedef charT char_type; + typedef typename traits::pos_type pos_type; + typedef typename traits::off_type off_type; + typedef typename traits::int_type int_type; + typedef traits traits_type; + + nsIOFileStreamT(); + explicit nsIOFileStreamT( + const nsUnixFilePath& inFile, + ios_base::openmode mode = ios_base::in|ios_base::out, + PRIntn accessMode = 0x00600); + + virtual ~nsIOFileStreamT(); + + filebuf_type* rdbuf() const; + inline bool is_open(); + inline void open( + const nsUnixFilePath& inFile, + ios_base::openmode mode = ios_base::in|ios_base::out, + PRIntn accessMode = 0x00600); + inline void close(); + +private: + filebuf_type mBuffer; +}; // class nsIOFileStreamT + +//======================================================================================== +// Implementation of nsIOFileStream +//======================================================================================== + +//---------------------------------------------------------------------------------------- +template +inline nsIOFileStreamT::nsIOFileStreamT() +//---------------------------------------------------------------------------------------- + : mBuffer(), basic_iostream(&mBuffer) +{ + // already inited +} + +//---------------------------------------------------------------------------------------- +template +inline nsIOFileStreamT::nsIOFileStreamT( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode) +//---------------------------------------------------------------------------------------- + : mBuffer(), basic_iostream(&mBuffer) +{ + // already inited + if (!mBuffer.open(inFile, mode, accessMode)) + setstate(failbit); +} + +template +inline nsIOFileStreamT::~nsIOFileStreamT() +{ +} + +//---------------------------------------------------------------------------------------- +template +inline nsIOFileStreamT::filebuf_type* +nsIOFileStreamT::rdbuf() const +//---------------------------------------------------------------------------------------- +{ + return (filebuf_type*)&mBuffer; +} + +//---------------------------------------------------------------------------------------- +template +inline bool +nsIOFileStreamT::is_open() +//---------------------------------------------------------------------------------------- +{ + return mBuffer.is_open(); +} + +//---------------------------------------------------------------------------------------- +template +inline void +nsIOFileStreamT::open( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode) +//---------------------------------------------------------------------------------------- +{ + if (!mBuffer.open(inFile, mode, accessMode)) + setstate(failbit); +} + +//---------------------------------------------------------------------------------------- +template +inline void +nsIOFileStreamT::close() +//---------------------------------------------------------------------------------------- +{ + if (!mBuffer.close()) + setstate(failbit); +} + +//======================================================================================== +// Specializations of the stream templates +//======================================================================================== + +typedef nsFileBufferT > nsFileBuffer; +typedef nsInputFileStreamT > nsInputFileStream; +typedef nsOutputFileStreamT > nsOutputFileStream; +typedef nsIOFileStreamT > nsIOFileStream; + +#ifdef NS_USING_WIDE_CHAR +typedef nsFileBufferT > nsWideFileBuffer; +typedef nsInputFileStreamT > nsWideInputFileStream; +typedef nsOutputFileStreamT > nsWideOutputFileStream; +typedef nsIOFileStreamT > nsWideIOFileStream; +#endif // NS_USING_WIDE_CHAR + +#endif /* _STREAM_H_ */ + diff --git a/mozilla/base/src/nsEscape.cpp b/mozilla/base/src/nsEscape.cpp new file mode 100644 index 00000000000..ef13de19e0d --- /dev/null +++ b/mozilla/base/src/nsEscape.cpp @@ -0,0 +1,152 @@ +/* -*- 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/12/03 by John R. McMullen, derived from net.h/mkparse.c. + +#include "nsEscape.h" + +#include "plstr.h" + +const int netCharType[256] = +/* Bit 0 xalpha -- the alphas +** Bit 1 xpalpha -- as xalpha but +** converts spaces to plus and plus to %20 +** Bit 3 ... path -- as xalphas but doesn't escape '/' +*/ + /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ + { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 1x */ + 0,0,0,0,0,0,0,0,0,0,7,4,0,7,7,4, /* 2x !"#$%&'()*+,-./ */ + 7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0, /* 3x 0123456789:;<=>? */ + 0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 4x @ABCDEFGHIJKLMNO */ + /* bits for '@' changed from 7 to 0 so '@' can be escaped */ + /* in usernames and passwords in publishing. */ + 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7, /* 5X PQRSTUVWXYZ[\]^_ */ + 0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 6x `abcdefghijklmno */ + 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0, /* 7X pqrstuvwxyz{\}~ DEL */ + 0, }; + +/* decode % escaped hex codes into character values + */ +#define UNHEX(C) \ + ((C >= '0' && C <= '9') ? C - '0' : \ + ((C >= 'A' && C <= 'F') ? C - 'A' + 10 : \ + ((C >= 'a' && C <= 'f') ? C - 'a' + 10 : 0))) + + +#define IS_OK(C) (netCharType[((unsigned int) (C))] & (mask)) +#define HEX_ESCAPE '%' + +//---------------------------------------------------------------------------------------- +char* nsEscape(const char * str, nsEscapeMask mask) +//---------------------------------------------------------------------------------------- +{ + if(!str) + return NULL; + return nsEscapeCount(str, (PRInt32)PL_strlen(str), mask, NULL); +} + +//---------------------------------------------------------------------------------------- +char* nsEscapeCount(const char * str, PRInt32 len, nsEscapeMask mask, PRInt32 * out_len) +//---------------------------------------------------------------------------------------- +{ + int32 i, extra = 0; + char *hexChars = "0123456789ABCDEF"; + + if(!str) + return(0); + + register const unsigned char* src = (unsigned char *) str; + for (i = 0; i < len; i++) + { + if (!IS_OK(src[i])) + extra+=2; /* the escape, plus an extra byte for each nibble */ + } + + char* result = new char[len + extra + 1]; + if (!result) + return(0); + + register unsigned char* dst = (unsigned char *) result; + for (i = 0; i < len; i++) + { + unsigned char c = src[i]; + if (IS_OK(c)) + { + *dst++ = c; + } + else if (mask == url_XPAlphas && c == ' ') + { + *dst++ = '+'; /* convert spaces to pluses */ + } + else + { + *dst++ = HEX_ESCAPE; + *dst++ = hexChars[c >> 4]; /* high nibble */ + *dst++ = hexChars[c & 0x0f]; /* low nibble */ + } + } + + *dst = '\0'; /* tack on eos */ + if(out_len) + *out_len = dst - (unsigned char *) result; + return result; +} + +//---------------------------------------------------------------------------------------- +char* nsUnescape(char * str) +//---------------------------------------------------------------------------------------- +{ + nsUnescapeCount(str); + return str; +} + +//---------------------------------------------------------------------------------------- +PRInt32 nsUnescapeCount(char * str) +//---------------------------------------------------------------------------------------- +{ + register char *src = str; + register char *dst = str; + + while (*src) + if (*src != HEX_ESCAPE) + { + *dst++ = *src++; + } + else + { + src++; /* walk over escape */ + if (*src) + { + *dst = UNHEX(*src) << 4; + src++; + } + if (*src) + { + *dst = (*dst + UNHEX(*src)); + src++; + } + dst++; + } + + *dst = 0; + + return (int)(dst - str); + +} /* NET_UnEscapeCnt */ + diff --git a/mozilla/base/src/unix/nsFileSpecUnix.cpp b/mozilla/base/src/unix/nsFileSpecUnix.cpp new file mode 100644 index 00000000000..8680b1fec90 --- /dev/null +++ b/mozilla/base/src/unix/nsFileSpecUnix.cpp @@ -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 + diff --git a/mozilla/base/src/windows/nsFileSpecWin.cpp b/mozilla/base/src/windows/nsFileSpecWin.cpp new file mode 100644 index 00000000000..4192593dbbb --- /dev/null +++ b/mozilla/base/src/windows/nsFileSpecWin.cpp @@ -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 + diff --git a/mozilla/xpcom/io/nsEscape.cpp b/mozilla/xpcom/io/nsEscape.cpp new file mode 100644 index 00000000000..ef13de19e0d --- /dev/null +++ b/mozilla/xpcom/io/nsEscape.cpp @@ -0,0 +1,152 @@ +/* -*- 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/12/03 by John R. McMullen, derived from net.h/mkparse.c. + +#include "nsEscape.h" + +#include "plstr.h" + +const int netCharType[256] = +/* Bit 0 xalpha -- the alphas +** Bit 1 xpalpha -- as xalpha but +** converts spaces to plus and plus to %20 +** Bit 3 ... path -- as xalphas but doesn't escape '/' +*/ + /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ + { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 1x */ + 0,0,0,0,0,0,0,0,0,0,7,4,0,7,7,4, /* 2x !"#$%&'()*+,-./ */ + 7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0, /* 3x 0123456789:;<=>? */ + 0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 4x @ABCDEFGHIJKLMNO */ + /* bits for '@' changed from 7 to 0 so '@' can be escaped */ + /* in usernames and passwords in publishing. */ + 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7, /* 5X PQRSTUVWXYZ[\]^_ */ + 0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 6x `abcdefghijklmno */ + 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0, /* 7X pqrstuvwxyz{\}~ DEL */ + 0, }; + +/* decode % escaped hex codes into character values + */ +#define UNHEX(C) \ + ((C >= '0' && C <= '9') ? C - '0' : \ + ((C >= 'A' && C <= 'F') ? C - 'A' + 10 : \ + ((C >= 'a' && C <= 'f') ? C - 'a' + 10 : 0))) + + +#define IS_OK(C) (netCharType[((unsigned int) (C))] & (mask)) +#define HEX_ESCAPE '%' + +//---------------------------------------------------------------------------------------- +char* nsEscape(const char * str, nsEscapeMask mask) +//---------------------------------------------------------------------------------------- +{ + if(!str) + return NULL; + return nsEscapeCount(str, (PRInt32)PL_strlen(str), mask, NULL); +} + +//---------------------------------------------------------------------------------------- +char* nsEscapeCount(const char * str, PRInt32 len, nsEscapeMask mask, PRInt32 * out_len) +//---------------------------------------------------------------------------------------- +{ + int32 i, extra = 0; + char *hexChars = "0123456789ABCDEF"; + + if(!str) + return(0); + + register const unsigned char* src = (unsigned char *) str; + for (i = 0; i < len; i++) + { + if (!IS_OK(src[i])) + extra+=2; /* the escape, plus an extra byte for each nibble */ + } + + char* result = new char[len + extra + 1]; + if (!result) + return(0); + + register unsigned char* dst = (unsigned char *) result; + for (i = 0; i < len; i++) + { + unsigned char c = src[i]; + if (IS_OK(c)) + { + *dst++ = c; + } + else if (mask == url_XPAlphas && c == ' ') + { + *dst++ = '+'; /* convert spaces to pluses */ + } + else + { + *dst++ = HEX_ESCAPE; + *dst++ = hexChars[c >> 4]; /* high nibble */ + *dst++ = hexChars[c & 0x0f]; /* low nibble */ + } + } + + *dst = '\0'; /* tack on eos */ + if(out_len) + *out_len = dst - (unsigned char *) result; + return result; +} + +//---------------------------------------------------------------------------------------- +char* nsUnescape(char * str) +//---------------------------------------------------------------------------------------- +{ + nsUnescapeCount(str); + return str; +} + +//---------------------------------------------------------------------------------------- +PRInt32 nsUnescapeCount(char * str) +//---------------------------------------------------------------------------------------- +{ + register char *src = str; + register char *dst = str; + + while (*src) + if (*src != HEX_ESCAPE) + { + *dst++ = *src++; + } + else + { + src++; /* walk over escape */ + if (*src) + { + *dst = UNHEX(*src) << 4; + src++; + } + if (*src) + { + *dst = (*dst + UNHEX(*src)); + src++; + } + dst++; + } + + *dst = 0; + + return (int)(dst - str); + +} /* NET_UnEscapeCnt */ + diff --git a/mozilla/xpcom/io/nsEscape.h b/mozilla/xpcom/io/nsEscape.h new file mode 100644 index 00000000000..8cd14adb710 --- /dev/null +++ b/mozilla/xpcom/io/nsEscape.h @@ -0,0 +1,60 @@ +/* -*- 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/12/03 by John R. McMullen, derived from net.h/mkparse.c. */ + +#ifndef _ESCAPE_H_ +#define _ESCAPE_H_ + +#include "PRTypes.h" + +/* valid mask values for NET_Escape() and NET_EscapedSize(). */ +typedef enum { + url_XAlphas = (1<<0) +, url_XPAlphas = (1<<1) +, url_Path = (1<<2) +} nsEscapeMask; + +#ifdef __cplusplus +extern "C" { +#endif +char * nsEscape(const char * str, nsEscapeMask mask); + /* Caller must use delete [] on the result */ +char * nsUnescape(char * str); + /* decode % escaped hex codes into character values, + * modifies the parameter, returns the same buffer + */ + +char * nsEscapeCount(const char * str, PRInt32 len, nsEscapeMask mask, PRInt32* out_len); + /* Like nsEscape, but if out_len is non-null, return result string length + * in *out_len, and uses len instead of NUL termination. + * Caller must use delete [] on the result. + */ +PRInt32 nsUnescapeCount (char * str); + /* decode % escaped hex codes into character values, + * modifies the parameter buffer, returns the length of the result + * (result may contain \0's). + */ + +#ifdef __cplusplus +} +#endif + + + +#endif // _ESCAPE_H_ diff --git a/mozilla/xpcom/io/nsFileSpec.h b/mozilla/xpcom/io/nsFileSpec.h new file mode 100644 index 00000000000..2c12feda22d --- /dev/null +++ b/mozilla/xpcom/io/nsFileSpec.h @@ -0,0 +1,306 @@ +/* -*- 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. Checked in again 98/12/04. + +#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. +// 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 +#include "nsDebug.h" +#ifdef XP_MAC +#include +#endif + +//======================================================================================== +// Here are the allowable ways to describe a file. +//======================================================================================== + +class nsUnixFilePath; // This can be passed to NSPR file I/O routines. +class nsFileURL; +class nsNativeFileSpec; + +#define kFileURLPrefix "file://" +#define kFileURLPrefixLength (7) + +//======================================================================================== +class nsNativeFileSpec +// This is whatever each platform really prefers to describe files as. +//======================================================================================== +{ + public: + nsNativeFileSpec(); + nsNativeFileSpec(const std::string& inString); + nsNativeFileSpec(const nsUnixFilePath& inPath); + nsNativeFileSpec(const nsFileURL& inURL); + nsNativeFileSpec(const nsNativeFileSpec& inPath); + + void operator = (const std::string& 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 + +//======================================================================================== +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 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. + +// std::string GetString() const { return mPath; } + // may be needed for implementation reasons, + // but should not provide a conversion constructor. + + void operator = (const nsFileURL& inURL); + void operator = (const std::string& inString); + void operator = (const nsUnixFilePath& inOther); + void operator = (const nsNativeFileSpec& inOther); + +#ifdef XP_MAC + // Accessor to allow quick assignment to a mNativeFileSpec + const nsNativeFileSpec& GetNativeSpec() const { return mNativeFileSpec; } +#endif + private: + + std::string mURL; +#ifdef XP_MAC + // Since the path on the macintosh does not uniquely specify a file (volumes + // can have the same name), stash the secret nsNativeFileSpec, too. + nsNativeFileSpec mNativeFileSpec; +#endif +}; // 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); + + + operator const char* () const { return mPath.c_str(); } + // This is the only automatic conversion to const char* + // that is provided, and it allows the + // path to be "passed" to NSPR file routines. + + void operator = (const nsUnixFilePath& inPath); + void operator = (const std::string& inString); + void operator = (const nsFileURL& inURL); + void operator = (const nsNativeFileSpec& inOther); + +#ifdef XP_MAC + public: + // Accessor to allow quick assignment to a mNativeFileSpec + const nsNativeFileSpec& GetNativeSpec() const { return mNativeFileSpec; } +#endif + + private: + // Should not be defined (only file URLs are to be treated as strings. + operator std::string& (); + private: + + std::string mPath; +#ifdef XP_MAC + // Since the path on the macintosh does not uniquely specify a file (volumes + // can have the same name), stash the secret nsNativeFileSpec, too. + nsNativeFileSpec mNativeFileSpec; +#endif +}; // class nsUnixFilePath + +#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 + +//======================================================================================== +// 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 nsNativeFileSpec::nsNativeFileSpec(const std::string& inString) +//---------------------------------------------------------------------------------------- +: mPath(inString) +{ +} +#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) +//---------------------------------------------------------------------------------------- +inline nsNativeFileSpec::operator = (const std::string& inString) +//---------------------------------------------------------------------------------------- +{ + mPath = inString; +} +#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_ diff --git a/mozilla/xpcom/io/nsFileSpecUnix.cpp b/mozilla/xpcom/io/nsFileSpecUnix.cpp new file mode 100644 index 00000000000..8680b1fec90 --- /dev/null +++ b/mozilla/xpcom/io/nsFileSpecUnix.cpp @@ -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 + diff --git a/mozilla/xpcom/io/nsFileSpecWin.cpp b/mozilla/xpcom/io/nsFileSpecWin.cpp new file mode 100644 index 00000000000..4192593dbbb --- /dev/null +++ b/mozilla/xpcom/io/nsFileSpecWin.cpp @@ -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 + diff --git a/mozilla/xpcom/io/nsFileStream.h b/mozilla/xpcom/io/nsFileStream.h new file mode 100644 index 00000000000..df0851d6eb4 --- /dev/null +++ b/mozilla/xpcom/io/nsFileStream.h @@ -0,0 +1,737 @@ +/* -*- 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. Checked in again 98/12/04. + +#ifndef _STREAM_H_ +#define _STREAM_H_ + +#include +#include "prio.h" +#include "nsFileSpec.h" + +#ifdef __MWERKS__ +#ifdef MSIPL_WCHART +#define NS_USING_WIDE_CHAR +#endif +#ifdef MSIPL_EXPLICIT_FUNC_TEMPLATE_ARG +#define NS_EXPLICIT_FUNC_TEMPLATE_ARG +#endif +#endif + +//======================================================================================== +template +class nsFileBufferT +//======================================================================================== +: public basic_streambuf +{ + typedef codecvt_base::result result; + +public: + typedef charT char_type; + typedef typename traits::pos_type pos_type; + typedef typename traits::off_type off_type; + typedef typename traits::int_type int_type; + typedef traits traits_type; + typedef typename traits::state_type state_type; + + typedef nsFileBufferT filebuf_type; + typedef codecvt ofacet_type; + typedef codecvt ifacet_type; + + nsFileBufferT(); + nsFileBufferT(PRFileDesc* pfile_arg); + virtual ~nsFileBufferT(); + bool is_open() const; + filebuf_type* open( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode); + filebuf_type* close(); + +protected: + virtual int_type overflow(int_type c=traits::eof()); + virtual int_type pbackfail(int_type c=traits::eof()); + virtual int_type underflow(); + virtual pos_type seekoff( + off_type off, ios_base::seekdir way, + ios_base::openmode which=ios_base::in|ios_base::out); + virtual pos_type seekpos(pos_type sp, + ios_base::openmode which=ios_base::in|ios_base::out); + virtual basic_streambuf* setbuf(char_type* s, streamsize n); + virtual int sync(); + virtual int_type uflow(); + virtual void imbue(const locale& loc); + virtual streamsize showmanyc(); + virtual streamsize xsgetn(char_type* s, streamsize n); + virtual streamsize xsputn(const char_type* s, streamsize n); + +private: + PRFileDesc* mFileDesc; + ios_base::openmode mode_; +}; // class nsFileBufferT + +//======================================================================================== +template +class nsInputFileStreamT +//======================================================================================== +: public basic_istream +{ + typedef nsFileBufferT filebuf_type; + +public: + typedef charT char_type; + typedef typename traits::pos_type pos_type; + typedef typename traits::off_type off_type; + typedef typename traits::int_type int_type; + typedef traits traits_type; + + nsInputFileStreamT(); + explicit nsInputFileStreamT( + const nsUnixFilePath& inFile, + ios_base::openmode mode=ios_base::in, + PRIntn accessMode = 0x00400); + + virtual ~nsInputFileStreamT(); + + filebuf_type* rdbuf() const; + inline bool is_open(); + inline void open( + const nsUnixFilePath& inFile, + ios_base::openmode mode=ios_base::in, + PRIntn accessMode = 0x00400); + inline void close(); + +private: + filebuf_type mBuffer; +}; // class nsInputFileStreamT + +//======================================================================================== +template +class nsOutputFileStreamT +//======================================================================================== +: public basic_ostream +{ + typedef nsFileBufferT filebuf_type; + +public: + typedef charT char_type; + typedef typename traits::pos_type pos_type; + typedef typename traits::off_type off_type; + typedef typename traits::int_type int_type; + typedef traits traits_type; + + nsOutputFileStreamT(); + explicit nsOutputFileStreamT( + const nsUnixFilePath& inFile, + ios_base::openmode mode = ios_base::out|ios_base::trunc, + PRIntn accessMode = 0x00200); + + virtual ~nsOutputFileStreamT(); + + filebuf_type* rdbuf() const; + inline bool is_open(); + inline void open( + const nsUnixFilePath& inFile, + ios_base::openmode mode = ios_base::out|ios_base::trunc, + PRIntn accessMode = 0x00200); + inline void close(); + +private: + filebuf_type mBuffer; +}; // class nsOutputFileStreamT + +//======================================================================================== +// Implementation of nsFileBufferT +//======================================================================================== + +//---------------------------------------------------------------------------------------- +template +inline nsFileBufferT::nsFileBufferT() + : basic_streambuf(), mFileDesc(NULL) +//---------------------------------------------------------------------------------------- +{ +} + +//---------------------------------------------------------------------------------------- +template +inline nsFileBufferT::nsFileBufferT(PRFileDesc* pfarg) + : basic_streambuf(), mFileDesc(pfarg) +//---------------------------------------------------------------------------------------- +{ +} + +//---------------------------------------------------------------------------------------- +template +inline nsFileBufferT::~nsFileBufferT() +//---------------------------------------------------------------------------------------- +{ + close(); +} + +//---------------------------------------------------------------------------------------- +template +inline bool +nsFileBufferT::is_open() const +//---------------------------------------------------------------------------------------- +{ + READ_LOCK(_mutex); + return bool(mFileDesc); // in case it is typedefed to int +} + +//---------------------------------------------------------------------------------------- +template +nsFileBufferT::filebuf_type* +nsFileBufferT::open( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode + ) +//---------------------------------------------------------------------------------------- +{ + if (mFileDesc) + return 0; + const ios_base::openmode valid_modes[]= + { + ios_base::out, + ios_base::out | ios_base::app, + ios_base::out | ios_base::trunc, + ios_base::in, + ios_base::in | ios_base::out, + ios_base::in | ios_base::out | ios_base::trunc, +// ios_base::out | ios_base::binary, +// ios_base::out | ios_base::app | ios_base::binary, +// ios_base::out | ios_base::trunc | ios_base::binary, +// ios_base::in | ios_base::binary, +// ios_base::in | ios_base::out | ios_base::binary, +// ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary, + 0 + }; + const int nspr_modes[]={ + PR_WRONLY | PR_CREATE_FILE, + PR_WRONLY | PR_CREATE_FILE | PR_APPEND, + PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, + PR_RDONLY, + PR_RDONLY | PR_APPEND, + PR_RDWR | PR_CREATE_FILE, + PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE, +// "wb", +// "ab", +// "wb", +// "rb", +// "r+b", +// "w+b", + 0 }; + int ind=0; + while (valid_modes[ind] && valid_modes[ind] != (mode&~ios_base::ate)) + ++ind; + if (!nspr_modes[ind]) + return 0; + + WRITE_LOCK(_mutex); + mode_ = mode; + if ((mFileDesc = PR_Open(inFile, nspr_modes[ind], accessMode)) != 0) + if (mode&ios_base::ate && PR_Seek(mFileDesc, 0, PR_SEEK_END) >= 0) + close(); + else + return this; + return 0; +} // nsFileBufferT::open + +//---------------------------------------------------------------------------------------- +template +nsFileBufferT::filebuf_type* nsFileBufferT::close() +//---------------------------------------------------------------------------------------- +{ + if (mFileDesc==PR_STDIN || mFileDesc==PR_STDOUT || mFileDesc==PR_STDERR) + return this; + WRITE_LOCK(_mutex); + return (mFileDesc && PR_Close(mFileDesc) == PR_SUCCESS) ? mFileDesc = 0, this : 0; +} + +//---------------------------------------------------------------------------------------- +template +inline int +nsFileBufferT:: sync() +//---------------------------------------------------------------------------------------- +{ + return (mFileDesc ? (int)PR_Sync(mFileDesc) : 0); +} + +//---------------------------------------------------------------------------------------- +template +inline basic_streambuf* +nsFileBufferT::setbuf(char_type*, streamsize) +//---------------------------------------------------------------------------------------- +{ + return (!mFileDesc) ? 0 : this; +} + +//---------------------------------------------------------------------------------------- +template +nsFileBufferT::int_type nsFileBufferT::overflow(int_type c) +//---------------------------------------------------------------------------------------- +{ +#ifdef NS_EXPLICIT_FUNC_TEMPLATE_ARG + const ofacet_type& ft=use_facet(loc); +#else + const ofacet_type& ft=use_facet(loc, (ofacet_type*)0); +#endif + char_type ch = traits_type::to_char_type(c); + if (!mFileDesc) + return traits_type::eof(); + if (traits_type::eq_int_type(c, traits::eof())) + return traits_type::not_eof(c); + if (ft.always_noconv()) + { + PRInt32 bytesWrit1 = PR_Write(mFileDesc, &ch, sizeof(ch)); + return bytesWrit1 < sizeof(ch) ? traits::eof() : c; + } + { // <- sic! + state_type fst; + const char_type* end; + char buf[4]; + char* ebuf; + result conv; + if ((conv=ft.out(fst, &ch, &ch+1, end, buf, buf+3, ebuf))== + codecvt_base::noconv) + { + PRInt32 bytesWrit2 = PR_Write(mFileDesc, &ch, sizeof(ch)); + return bytesWrit2 < sizeof(ch) ? traits::eof() : c; + } + if ((conv==codecvt_base::partial)||(conv==codecvt_base::error)) + return traits::eof(); + *ebuf=0; + PRInt32 bytesWrit3 = strlen(buf); + return PR_Write(mFileDesc, buf, bytesWrit3) < bytesWrit3 ? traits_type::eof() : c; + } +} + +//---------------------------------------------------------------------------------------- +template +inline nsFileBufferT::int_type nsFileBufferT::underflow() +//---------------------------------------------------------------------------------------- +{ + if (!mFileDesc) + return traits_type::eof(); + char_type s; + PRInt32 request = 1; + if (1 != PR_Read(mFileDesc, &s, request)) + return traits_type::eof(); + PR_Seek(mFileDesc, -1, PR_SEEK_CUR); + return (int_type)s; +} + +//---------------------------------------------------------------------------------------- +template +streamsize nsFileBufferT::xsputn(const char_type* s, streamsize n) +//---------------------------------------------------------------------------------------- +{ +#ifdef NS_EXPLICIT_FUNC_TEMPLATE_ARG + const ofacet_type& ft=use_facet(loc); +#else + const ofacet_type& ft=use_facet(loc, (ofacet_type*)0); +#endif + if (!mFileDesc || !n) + return 0; + if (ft.always_noconv()) + { + PRInt32 bytesWrit1 = PR_Write(mFileDesc, s, sizeof(char) * size_t(n)); + return bytesWrit1 < 0 ? 0 : (streamsize)bytesWrit1; + } + { // <- sic! + state_type fst; + const char_type* end; + char buf[8]; + char* ebuf; + result conv; +#ifdef NS_EXPLICIT_FUNC_TEMPLATE_ARG + if ((conv=use_facet(loc). + out(fst, s, s+n, end, buf, buf+7, ebuf))==codecvt_base::noconv) +#else + if ((conv=use_facet(loc, (ofacet_type*)0). + out(fst, s, s+n, end, buf, buf+7, ebuf))==codecvt_base::noconv) +#endif + return (streamsize)PR_Write(mFileDesc, s, sizeof(char) * size_t(n)); + if ((conv==codecvt_base::partial) ||(conv==codecvt_base::error)) + return 0; + *ebuf=0; + PRInt32 bytesWrit2 = strlen(buf); + bytesWrit2 = PR_Write(mFileDesc, buf, bytesWrit2); + return bytesWrit2 < 0 ? 0 : streamsize(bytesWrit2 / sizeof(char_type)); + } +} + +//---------------------------------------------------------------------------------------- +template +inline nsFileBufferT::int_type + nsFileBufferT::pbackfail(int_type c) +//---------------------------------------------------------------------------------------- +{ + if (!mFileDesc) + return traits_type::eof(); + if (PR_Seek(mFileDesc, -1, PR_SEEK_CUR) < 0) + return traits_type::eof(); + return (traits::eq_int_type(c, traits_type::eof())) ? traits::not_eof(c) : c; +} + +//---------------------------------------------------------------------------------------- +template +inline nsFileBufferT::int_type nsFileBufferT::uflow() +//---------------------------------------------------------------------------------------- +{ + if (!mFileDesc) + return traits_type::eof(); + char_type s; + if (1 != PR_Read(mFileDesc, &s, 1)) // attempt to read 1 byte, confirm 1 byte + return traits_type::eof(); + return (int_type)s; +} + +//---------------------------------------------------------------------------------------- +template +inline streamsize nsFileBufferT::xsgetn(char_type* s, streamsize n) +//---------------------------------------------------------------------------------------- +{ + return mFileDesc ? (streamsize)PR_Read(mFileDesc, s, sizeof(char) * size_t(n)) : 0; +} + +//---------------------------------------------------------------------------------------- +template +inline void nsFileBufferT::imbue(const locale& loc_arg) +//---------------------------------------------------------------------------------------- +{ + loc = loc_arg; +} + +template +inline streamsize +nsFileBufferT::showmanyc() +{ + return (streamsize)PR_Available(mFileDesc); +} + +//---------------------------------------------------------------------------------------- +template +nsFileBufferT::pos_type nsFileBufferT::seekoff( + off_type off, + ios_base::seekdir way, + ios_base::openmode /* which */) +//---------------------------------------------------------------------------------------- +{ + if (!mFileDesc || ((way&ios_base::beg) && off<0) || ((way&ios_base::end) && off > 0)) + return pos_type(-1); + PRSeekWhence poseek = PR_SEEK_CUR; + switch (way) + { + case ios_base::beg : poseek= PR_SEEK_SET; + break; + case ios_base::end : poseek= PR_SEEK_END; + break; + } + PRInt32 position = PR_Seek(mFileDesc, off, poseek); + if (position < 0) + return pos_type(-1); + return pos_type(position); +} + +//---------------------------------------------------------------------------------------- +template +nsFileBufferT::pos_type +nsFileBufferT::seekpos(pos_type sp, ios_base::openmode) +//---------------------------------------------------------------------------------------- +{ + if (!mFileDesc || sp==pos_type(-1)) + return -1; + PRInt32 position = PR_Seek(mFileDesc, sp.offset(), PR_SEEK_SET); + if (position < 0) + return pos_type(-1); + return position; +} + +//======================================================================================== +// Implementation of nsInputFileStreamT +//======================================================================================== + +//---------------------------------------------------------------------------------------- +template +inline nsInputFileStreamT::nsInputFileStreamT() + : basic_istream(&mBuffer) +//---------------------------------------------------------------------------------------- +{ + // already inited +} + +//---------------------------------------------------------------------------------------- +template +inline nsInputFileStreamT::nsInputFileStreamT( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode) +//---------------------------------------------------------------------------------------- + : basic_istream(&mBuffer) +{ + // already inited + if (!mBuffer.open(inFile, openmode(mode|in), accessMode)) + setstate(failbit); +} + +//---------------------------------------------------------------------------------------- +template +inline nsInputFileStreamT::~nsInputFileStreamT() +//---------------------------------------------------------------------------------------- +{ +} + +//---------------------------------------------------------------------------------------- +template +inline nsInputFileStreamT::filebuf_type* +nsInputFileStreamT::rdbuf() const +//---------------------------------------------------------------------------------------- +{ + return (filebuf_type*)&mBuffer; +} + +//---------------------------------------------------------------------------------------- +template +inline bool +nsInputFileStreamT:: is_open() +//---------------------------------------------------------------------------------------- +{ + return mBuffer.is_open(); +} + +//---------------------------------------------------------------------------------------- +template +inline void +nsInputFileStreamT::open( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode) +//---------------------------------------------------------------------------------------- +{ + if (!mBuffer.open(inFile, openmode(mode|in), accessMode)) + setstate(failbit); +} + +//---------------------------------------------------------------------------------------- +template +inline void nsInputFileStreamT::close() +//---------------------------------------------------------------------------------------- +{ + if (!mBuffer.close()) + setstate(failbit); +} + +//======================================================================================== +// Implementation of nsOutputFileStreamT +//======================================================================================== + +//---------------------------------------------------------------------------------------- +template +inline nsOutputFileStreamT::nsOutputFileStreamT() + : basic_ostream(&mBuffer) +//---------------------------------------------------------------------------------------- +{ + // already inited +} + +//---------------------------------------------------------------------------------------- +template +nsOutputFileStreamT::nsOutputFileStreamT( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode) +//---------------------------------------------------------------------------------------- +: basic_ostream(&mBuffer) +{ + // already inited + if (!mBuffer.open(inFile, openmode(mode|out), accessMode)) + setstate(failbit); +} + +//---------------------------------------------------------------------------------------- +template +inline nsOutputFileStreamT::~nsOutputFileStreamT() +//---------------------------------------------------------------------------------------- +{ +} + +//---------------------------------------------------------------------------------------- +template +inline nsOutputFileStreamT::filebuf_type* +nsOutputFileStreamT::rdbuf() const +//---------------------------------------------------------------------------------------- +{ + return (filebuf_type*)&mBuffer; +} + +//---------------------------------------------------------------------------------------- +template +inline bool nsOutputFileStreamT:: is_open() +//---------------------------------------------------------------------------------------- +{ + return mBuffer.is_open(); +} + +//---------------------------------------------------------------------------------------- +template +inline void nsOutputFileStreamT::open( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode) +//---------------------------------------------------------------------------------------- +{ + if (!mBuffer.open(inFile, openmode(mode | out), accessMode)) + setstate(failbit); +} + +//---------------------------------------------------------------------------------------- +template +inline void nsOutputFileStreamT:: close() +//---------------------------------------------------------------------------------------- +{ + if (!mBuffer.close()) + setstate(failbit); +} + +//======================================================================================== +template +class nsIOFileStreamT : public basic_iostream +//======================================================================================== +{ + typedef nsFileBufferT filebuf_type; + +public: + typedef charT char_type; + typedef typename traits::pos_type pos_type; + typedef typename traits::off_type off_type; + typedef typename traits::int_type int_type; + typedef traits traits_type; + + nsIOFileStreamT(); + explicit nsIOFileStreamT( + const nsUnixFilePath& inFile, + ios_base::openmode mode = ios_base::in|ios_base::out, + PRIntn accessMode = 0x00600); + + virtual ~nsIOFileStreamT(); + + filebuf_type* rdbuf() const; + inline bool is_open(); + inline void open( + const nsUnixFilePath& inFile, + ios_base::openmode mode = ios_base::in|ios_base::out, + PRIntn accessMode = 0x00600); + inline void close(); + +private: + filebuf_type mBuffer; +}; // class nsIOFileStreamT + +//======================================================================================== +// Implementation of nsIOFileStream +//======================================================================================== + +//---------------------------------------------------------------------------------------- +template +inline nsIOFileStreamT::nsIOFileStreamT() +//---------------------------------------------------------------------------------------- + : mBuffer(), basic_iostream(&mBuffer) +{ + // already inited +} + +//---------------------------------------------------------------------------------------- +template +inline nsIOFileStreamT::nsIOFileStreamT( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode) +//---------------------------------------------------------------------------------------- + : mBuffer(), basic_iostream(&mBuffer) +{ + // already inited + if (!mBuffer.open(inFile, mode, accessMode)) + setstate(failbit); +} + +template +inline nsIOFileStreamT::~nsIOFileStreamT() +{ +} + +//---------------------------------------------------------------------------------------- +template +inline nsIOFileStreamT::filebuf_type* +nsIOFileStreamT::rdbuf() const +//---------------------------------------------------------------------------------------- +{ + return (filebuf_type*)&mBuffer; +} + +//---------------------------------------------------------------------------------------- +template +inline bool +nsIOFileStreamT::is_open() +//---------------------------------------------------------------------------------------- +{ + return mBuffer.is_open(); +} + +//---------------------------------------------------------------------------------------- +template +inline void +nsIOFileStreamT::open( + const nsUnixFilePath& inFile, + ios_base::openmode mode, + PRIntn accessMode) +//---------------------------------------------------------------------------------------- +{ + if (!mBuffer.open(inFile, mode, accessMode)) + setstate(failbit); +} + +//---------------------------------------------------------------------------------------- +template +inline void +nsIOFileStreamT::close() +//---------------------------------------------------------------------------------------- +{ + if (!mBuffer.close()) + setstate(failbit); +} + +//======================================================================================== +// Specializations of the stream templates +//======================================================================================== + +typedef nsFileBufferT > nsFileBuffer; +typedef nsInputFileStreamT > nsInputFileStream; +typedef nsOutputFileStreamT > nsOutputFileStream; +typedef nsIOFileStreamT > nsIOFileStream; + +#ifdef NS_USING_WIDE_CHAR +typedef nsFileBufferT > nsWideFileBuffer; +typedef nsInputFileStreamT > nsWideInputFileStream; +typedef nsOutputFileStreamT > nsWideOutputFileStream; +typedef nsIOFileStreamT > nsWideIOFileStream; +#endif // NS_USING_WIDE_CHAR + +#endif /* _STREAM_H_ */ +