From ce9cf0d08ed775a19b3cd1dbacb8e44185253f4e Mon Sep 17 00:00:00 2001 From: "mcmullen%netscape.com" Date: Tue, 8 Dec 1998 02:15:50 +0000 Subject: [PATCH] First Checked In. git-svn-id: svn://10.0.0.236/trunk@15952 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/base/macbuild/TestFiles.mcp | Bin 0 -> 81980 bytes mozilla/base/macbuild/files.prefix | 19 + mozilla/base/macbuild/filesDebug.prefix | 19 + mozilla/base/public/nsEscape.h | 60 ++ mozilla/base/public/nsFileSpec.h | 306 +++++++++ mozilla/base/public/nsFileStream.h | 737 +++++++++++++++++++++ mozilla/base/src/nsEscape.cpp | 152 +++++ mozilla/base/src/unix/nsFileSpecUnix.cpp | 35 + mozilla/base/src/windows/nsFileSpecWin.cpp | 57 ++ mozilla/xpcom/io/nsEscape.cpp | 152 +++++ mozilla/xpcom/io/nsEscape.h | 60 ++ mozilla/xpcom/io/nsFileSpec.h | 306 +++++++++ mozilla/xpcom/io/nsFileSpecUnix.cpp | 35 + mozilla/xpcom/io/nsFileSpecWin.cpp | 57 ++ mozilla/xpcom/io/nsFileStream.h | 737 +++++++++++++++++++++ 15 files changed, 2732 insertions(+) create mode 100644 mozilla/base/macbuild/TestFiles.mcp create mode 100644 mozilla/base/macbuild/files.prefix create mode 100644 mozilla/base/macbuild/filesDebug.prefix create mode 100644 mozilla/base/public/nsEscape.h create mode 100644 mozilla/base/public/nsFileSpec.h create mode 100644 mozilla/base/public/nsFileStream.h create mode 100644 mozilla/base/src/nsEscape.cpp create mode 100644 mozilla/base/src/unix/nsFileSpecUnix.cpp create mode 100644 mozilla/base/src/windows/nsFileSpecWin.cpp create mode 100644 mozilla/xpcom/io/nsEscape.cpp create mode 100644 mozilla/xpcom/io/nsEscape.h create mode 100644 mozilla/xpcom/io/nsFileSpec.h create mode 100644 mozilla/xpcom/io/nsFileSpecUnix.cpp create mode 100644 mozilla/xpcom/io/nsFileSpecWin.cpp create mode 100644 mozilla/xpcom/io/nsFileStream.h diff --git a/mozilla/base/macbuild/TestFiles.mcp b/mozilla/base/macbuild/TestFiles.mcp new file mode 100644 index 0000000000000000000000000000000000000000..55c19824b483d429856aa92a6f0656abd887a4c6 GIT binary patch literal 81980 zcmeHQ349z?{h!&SNqW$Ww)CK|ZGoPorL?7lt2vsIHfckX($WI$HrcjIlWf@CKq*(` zkXtA>e?$?y^{E)enUM*1Y)9z`P(-9AA}@(d_|%igr-Dj>XCa>t z>Sq|!sL(~AG7wWMs5~;^wxP&lpbDex)q9X9LH$8YY0vyFoz8Teh4@^5efKt;0efkL2m<3H1;#$$Pwyw z5(p`RV~hhpQ-C3B9B7!QCxB-GAB6rwHgXK`WDrth6*iFlEXpIN0JEP_V77DG{8{tS z*T}`d(~Y~{CZGDBv17+S(8tKFzz2gO`2Q{Bl_2yu@)Yu!rQdw(MBqOH&x*`_>_NH+ z@F7t78^9xgXM>PRSjV9X8YK&n&k=kw^0|UfLr%RTm0W_&VQ9|+;Ew~(M|(yCe+Bq( z(1E}YBCi5LZzVrNegp`8EBPJrBSF)E-$8y9294S9P|zsF zSY<30{7>XdH15ZCHVS4tn*_6+O9eAuCb%7Wvtat$ae{A0zFhD(khcha7 zs9&VAPHff@0iS~n%aF?G1M3wu%C;eI6MPGD_8F<{i`c{!G|KKqzCrMhkZ%#{; zXfkLD2p+)eoly=N3Staw2GP&y-;5QG6(@oEgZ2h79!>^TgVus-L5%TrpcrU9Xc(vs zM1Nwuwu453MuA#EjIj<7bGe53O;FODk*J^r};a%w7#KnA$(}G z(zgd_#g$8;+!Alc+J9)}Ta{G!F&+o8qaB@#wO^$3~87ojb=M zRW~;`V)hCyR@b;_Romu7J6C+HY-z5A!-4Ixo1W$()7CJfoE13Q>O4^adu!LGGcmPE z1w8~Spr-#&gkcv^b6o#%1*Wx(!J`JPE^HavYHFuNLrlLQc#u}8i01Sao5E=;Mm(po z^sy(%!j9F#Un}`KDc>M@OzK%L^|pyzyVM(JUK2|v+M=yk5VStICEC=}kx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}KZNWr}S~T7x{=nMt$16`IHG|2Cn1qcfxW zYvb>awi&(Pr;*$E(Mi}J3Vy=LzqSSYvpO>!{Pkg%UyFUM|82uQj`l3UKGF*p;J)Pz zW#4UIgKtL~HUHfV?l-g^w(SF*N66l!4s3e@V_;;tVjB~FgnYd|@6m^W z&we6Jge@eFwjK2(_^pa +#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_ */ +