Back to index

lightning-sunbird  0.9+nobinonly
nsFileStream.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 //    First checked in on 98/11/20 by John R. McMullen in the wrong directory.
00039 //    Checked in again 98/12/04.
00040 //    Polished version 98/12/08.
00041 //    Completely rewritten to integrate with nsIInputStream and nsIOutputStream (the
00042 //        xpcom stream objects.
00043 
00044 //========================================================================================
00045 //
00046 //  Classes defined:
00047 //
00048 //          nsInputStream, nsOutputStream
00049 //                These are the lightweight STATICALLY LINKED wrappers for
00050 //                the xpcom objects nsIInputStream and nsIOutputstream.
00051 //             Possible uses:
00052 //                If you are implementing a function that accepts one of these xpcom
00053 //                streams, just make one of these little jobbies on the stack, and
00054 //                the handy << or >> notation can be yours.
00055 //                
00056 //          nsInputFileStream, nsOutputFileStream
00057 //                These are the STATICALLY LINKED wrappers for the file-related
00058 //                versions of the above.
00059 //          nsIOFileStream
00060 //                An input and output file stream attached to the same file.
00061 //
00062 //  This suite provide the following services:
00063 //
00064 //      1.  Encapsulates all platform-specific file details, so that file i/o
00065 //          can be done correctly without any platform #ifdefs
00066 //
00067 //      2.  Uses NSPR file services (NOT ansi file I/O), in order to get best
00068 //          native performance.  This performance difference is especially large on
00069 //          macintosh.
00070 //
00071 //      3.  Allows all the power of the ansi stream syntax.
00072 //
00073 //          Basic example:
00074 //
00075 //              nsFileSpec myPath("/Development/iotest.txt");
00076 //
00077 //                 nsOutputFileStream testStream(myPath);
00078 //                          testStream << "Hello World" << nsEndl;
00079 //
00080 //      4.  Requires streams to be constructed using typesafe nsFileSpec specifier
00081 //          (not the notorious and bug prone const char*), namely nsFileSpec.  See
00082 //          nsFileSpec.h for more details.
00083 //
00084 //      5.  Fixes a bug that have been there for a long time, and
00085 //          is inevitable if you use NSPR alone:
00086 //
00087 //               The problem on platforms (Macintosh) in which a path does not fully
00088 //               specify a file, because two volumes can have the same name.
00089 //
00090 //  Not yet provided:
00091 //
00092 //      Endian-awareness for reading and writing crossplatform binary files.  At this
00093 //      time there seems to be no demand for this.
00094 //
00095 //========================================================================================
00096 
00097 #ifndef _FILESTREAM_H_
00098 #define _FILESTREAM_H_
00099 
00100 #include "xpcomobsolete.h"
00101 #include "nsStringFwd.h"
00102 
00103 #ifdef XP_MAC
00104 #include "pprio.h" // To get PR_ImportFile
00105 #else
00106 #include "prio.h"
00107 #endif
00108 
00109 #include "nsCOMPtr.h"
00110 #include "nsIFileStream.h"
00111 
00112 // Defined elsewhere
00113 class nsFileSpec;
00114 class nsIInputStream;
00115 class nsIOutputStream;
00116 class nsIFileSpec;
00117 
00118 //========================================================================================
00119 //                          Compiler-specific macros, as needed
00120 //========================================================================================
00121 #if !defined(NS_USING_NAMESPACE) && (defined(__MWERKS__) || defined(_MSC_VER))
00122 #define NS_USING_NAMESPACE
00123 #endif
00124 
00125 #ifdef NS_USING_NAMESPACE
00126 
00127 #define NS_NAMESPACE_PROTOTYPE
00128 #define NS_NAMESPACE namespace
00129 #define NS_NAMESPACE_END
00130        
00131 #else
00132 
00133 #define NS_NAMESPACE_PROTOTYPE static
00134 #define NS_NAMESPACE struct
00135 #define NS_NAMESPACE_END ;
00136 
00137 #endif // NS_USING_NAMESPACE
00138 
00139 #if !defined(XP_MAC) && !defined(__KCC)
00140 // PR_STDOUT and PR_STDIN are fatal on Macintosh.  So for console i/o, we must use the std
00141 // stream stuff instead.  However, we have to require that cout and cin are passed in
00142 // to the constructor because in the current build, there is a copy in the base.shlb,
00143 // and another in the caller's file.  Passing it in as a parameter ensures that the
00144 // caller and this library are using the same global object.  Groan.
00145 //
00146 // Unix currently does not support iostreams at all.  Their compilers do not support
00147 // ANSI C++, or even ARM C++.
00148 //
00149 // Windows supports them, but only if you turn on the -GX (VC5-VC7.x) or -EHsc (VC8+)
00150 // compile flag, to support exceptions.
00151 
00152 // Catch 22.
00153 #define NS_USE_PR_STDIO
00154 #endif
00155 
00156 #ifdef NS_USE_PR_STDIO
00157 class istream;
00158 class ostream;
00159 #define CONSOLE_IN 0
00160 #define CONSOLE_OUT 0
00161 #else
00162 #include <iostream>
00163 using std::istream;
00164 using std::ostream;
00165 #define CONSOLE_IN &std::cin
00166 #define CONSOLE_OUT &std::cout
00167 #endif
00168 
00169 //=========================== End Compiler-specific macros ===============================
00170 
00171 //========================================================================================
00172 class NS_COM_OBSOLETE nsInputStream
00173 // This is a convenience class, for use on the STACK ("new" junkies: get detoxed first).
00174 // Given a COM-style stream, this allows you to use the >> operators.  It also acquires and
00175 // reference counts its stream.
00176 // Please read the comments at the top of this file
00177 //========================================================================================
00178 {
00179 public:
00180                                       nsInputStream(nsIInputStream* inStream)
00181                                       :   mInputStream(do_QueryInterface(inStream))
00182                                       ,   mEOF(PR_FALSE)
00183                                       {}
00184     virtual                           ~nsInputStream();
00185  
00186     nsCOMPtr<nsIInputStream>          GetIStream() const
00187                                       {
00188                                           return mInputStream;
00189                                       }
00190     PRBool                            eof() const { return get_at_eof(); }
00191     char                              get();
00192     nsresult                          close()
00193                                       {
00194                                    NS_ASSERTION(mInputStream, "mInputStream is null!");
00195                                    if (mInputStream) {
00196                                           return mInputStream->Close();                        
00197                                    }
00198                     return NS_OK;
00199                                       }
00200     PRInt32                           read(void* s, PRInt32 n);
00201 
00202     // Input streamers.  Add more as needed (int&, unsigned int& etc). (but you have to
00203     // add delegators to the derived classes, too, because these operators don't inherit).
00204     nsInputStream&                    operator >> (char& ch);
00205 
00206     // Support manipulators
00207     nsInputStream&                    operator >> (nsInputStream& (*pf)(nsInputStream&))
00208                                       {
00209                                            return pf(*this);
00210                                       }
00211 
00212 protected:
00213 
00214    // These certainly need to be overridden, they give the best shot we can at detecting
00215    // eof in a simple nsIInputStream.
00216    virtual void                       set_at_eof(PRBool atEnd)
00217                                       {
00218                                          mEOF = atEnd;
00219                                       }
00220    virtual PRBool                     get_at_eof() const
00221                                       {
00222                                           return mEOF;
00223                                       }
00224 private:
00225 
00226     nsInputStream&                    operator >> (char* buf); // TOO DANGEROUS. DON'T DEFINE.
00227 
00228     // private and unimplemented to disallow copies and assigns
00229                                       nsInputStream(const nsInputStream& rhs);
00230     nsInputStream&                    operator=(const nsInputStream& rhs);
00231 
00232 // DATA
00233 protected:
00234     nsCOMPtr<nsIInputStream>          mInputStream;
00235     PRBool                            mEOF;
00236 }; // class nsInputStream
00237 
00238 typedef nsInputStream nsBasicInStream; // historic support for this name
00239 
00240 //========================================================================================
00241 class NS_COM_OBSOLETE nsOutputStream
00242 // This is a convenience class, for use on the STACK ("new" junkies, get detoxed first).
00243 // Given a COM-style stream, this allows you to use the << operators.  It also acquires and
00244 // reference counts its stream.
00245 // Please read the comments at the top of this file
00246 //========================================================================================
00247 {
00248 public:
00249                                       nsOutputStream() {}
00250                                       nsOutputStream(nsIOutputStream* inStream)
00251                                       :   mOutputStream(do_QueryInterface(inStream))
00252                                           {}
00253 
00254     virtual                          ~nsOutputStream();
00255 
00256     nsCOMPtr<nsIOutputStream>         GetIStream() const
00257                                       {
00258                                           return mOutputStream;
00259                                       }
00260     nsresult                          close()
00261                                       {
00262                                           if (mOutputStream)
00263                                             return mOutputStream->Close();
00264                                           return NS_OK;
00265                                       }
00266     void                              put(char c);
00267     PRInt32                           write(const void* s, PRInt32 n);
00268     virtual nsresult                  flush();
00269     nsresult                          lastWriteStatus();
00270 
00271     // Output streamers.  Add more as needed (but you have to add delegators to the derived
00272     // classes, too, because these operators don't inherit).
00273     nsOutputStream&                   operator << (const char* buf);
00274     nsOutputStream&                   operator << (char ch);
00275     nsOutputStream&                   operator << (short val);
00276     nsOutputStream&                   operator << (unsigned short val);
00277     nsOutputStream&                   operator << (long val);
00278     nsOutputStream&                   operator << (unsigned long val);
00279     nsOutputStream&                   operator << (int val);
00280     nsOutputStream&                   operator << (unsigned int val);
00281 
00282     // Support manipulators
00283     nsOutputStream&                   operator << (nsOutputStream& (*pf)(nsOutputStream&))
00284                                       {
00285                                            return pf(*this);
00286                                       }
00287 
00288 private:
00289 
00290     // private and unimplemented to disallow copies and assigns
00291                                       nsOutputStream(const nsOutputStream& rhs);
00292     nsOutputStream&                   operator=(const nsOutputStream& rhs);
00293 
00294     nsresult                          mWriteStatus;
00295 
00296 // DATA
00297 protected:
00298     nsCOMPtr<nsIOutputStream>         mOutputStream;
00299 }; // class nsOutputStream
00300 
00301 typedef nsOutputStream nsBasicOutStream; // Historic support for this name
00302 
00303 //========================================================================================
00304 class NS_COM_OBSOLETE nsErrorProne
00305 // Common (virtual) base class for remembering errors on demand
00306 //========================================================================================
00307 {
00308 public:
00309                                       nsErrorProne() // for delayed opening
00310                                       :   mResult(NS_OK)
00311                                       {
00312                                       }
00313     PRBool                            failed() const
00314                                       {
00315                                           return NS_FAILED(mResult);
00316                                       }
00317     nsresult                          error() const
00318                                       {
00319                                           return mResult;
00320                                       }
00321 
00322 // DATA
00323 protected:
00324     nsresult                          mResult;
00325 }; // class nsErrorProne
00326 
00327 //========================================================================================
00328 class NS_COM_OBSOLETE nsFileClient
00329 // Because COM does not allow us to write functions which return a boolean value etc,
00330 // this class is here to take care of the tedious "declare variable then call with
00331 // the address of the variable" chores.
00332 //========================================================================================
00333 :    public virtual nsErrorProne
00334 {
00335 public:
00336                                       nsFileClient(const nsCOMPtr<nsIOpenFile>& inFile)
00337                                       :   mFile(do_QueryInterface(inFile))
00338                                       {
00339                                       }
00340     virtual                           ~nsFileClient() {}
00341 
00342     void                              open(
00343                                           const nsFileSpec& inFile,
00344                                           int nsprMode,
00345                                           PRIntn accessMode)
00346                                       {
00347                                           if (mFile)
00348                                               mResult = mFile->Open(inFile, nsprMode, accessMode);
00349                                       }
00350     PRBool                            is_open() const
00351                                       {
00352                                           PRBool result = PR_FALSE;
00353                                           if (mFile)
00354                                               mFile->GetIsOpen(&result);
00355                                           return result;
00356                                       }
00357     PRBool                            is_file() const
00358                                       {
00359                                           return mFile ? PR_TRUE : PR_FALSE;
00360                                       }
00361 
00362 protected:
00363 
00364                                       nsFileClient() // for delayed opening
00365                                       {
00366                                       }
00367 // DATA
00368 protected:
00369     nsCOMPtr<nsIOpenFile>                 mFile;
00370 }; // class nsFileClient
00371 
00372 //========================================================================================
00373 class NS_COM_OBSOLETE nsRandomAccessStoreClient
00374 // Because COM does not allow us to write functions which return a boolean value etc,
00375 // this class is here to take care of the tedious "declare variable then call with
00376 // the address of the variable" chores.
00377 //========================================================================================
00378 :    public virtual nsErrorProne
00379 {
00380 public:
00381                                       nsRandomAccessStoreClient() // for delayed opening
00382                                       {
00383                                       }
00384                                       nsRandomAccessStoreClient(const nsCOMPtr<nsIRandomAccessStore>& inStore)
00385                                       :   mStore(do_QueryInterface(inStore))
00386                                       {
00387                                       }
00388     virtual                           ~nsRandomAccessStoreClient() {}
00389     
00390     void                              seek(PRInt64 offset)
00391                                       {
00392                                           seek(PR_SEEK_SET, offset);
00393                                       }
00394 
00395     void                              seek(PRSeekWhence whence, PRInt64 offset)
00396                                       {
00397                                           set_at_eof(PR_FALSE);
00398                                           if (mStore)
00399                                               mResult = mStore->Seek(whence, offset);
00400                                       }
00401     PRInt64                           tell()
00402                                       {
00403                                           PRInt64 result;
00404                                           LL_I2L(result, -1);
00405                                           if (mStore)
00406                                               mResult = mStore->Tell(&result);
00407                                           return result;
00408                                       }
00409 
00410 protected:
00411 
00412    virtual PRBool                     get_at_eof() const
00413                                       {
00414                                           PRBool result = PR_TRUE;
00415                                           if (mStore)
00416                                               mStore->GetAtEOF(&result);
00417                                           return result;
00418                                       }
00419 
00420    virtual void                       set_at_eof(PRBool atEnd)
00421                                       {
00422                                           if (mStore)
00423                                               mStore->SetAtEOF(atEnd);
00424                                       }
00425 
00426 private:
00427 
00428     // private and unimplemented to disallow copies and assigns
00429                                       nsRandomAccessStoreClient(const nsRandomAccessStoreClient& rhs);
00430     nsRandomAccessStoreClient&        operator=(const nsRandomAccessStoreClient& rhs);
00431 
00432 // DATA
00433 protected:
00434     nsCOMPtr<nsIRandomAccessStore>    mStore;
00435 }; // class nsRandomAccessStoreClient
00436 
00437 //========================================================================================
00438 class NS_COM_OBSOLETE nsRandomAccessInputStream
00439 // Please read the comments at the top of this file
00440 //========================================================================================
00441 :      public nsRandomAccessStoreClient
00442 ,      public nsInputStream
00443 {
00444 public:
00445                                       nsRandomAccessInputStream(nsIInputStream* inStream)
00446                                       :   nsRandomAccessStoreClient(do_QueryInterface(inStream))
00447                                       ,   nsInputStream(inStream)
00448                                       {
00449                                       }
00450     PRBool                            readline(char* s,  PRInt32 n);
00451                                           // Result always null-terminated.
00452                                           // Check eof() before each call.
00453                                           // CAUTION: false result only indicates line was truncated
00454                                           // to fit buffer, or an error occurred (OTHER THAN eof).
00455 
00456     // Input streamers.  Unfortunately, they don't inherit!
00457     nsInputStream&                    operator >> (char& ch)
00458                                          { return nsInputStream::operator >>(ch); }
00459     nsInputStream&                    operator >> (nsInputStream& (*pf)(nsInputStream&))
00460                                          { return nsInputStream::operator >>(pf); }
00461 
00462 protected:
00463                                       nsRandomAccessInputStream()
00464                                       :  nsInputStream(nsnull)
00465                                       {
00466                                       }
00467 
00468    virtual PRBool                     get_at_eof() const
00469                                       {
00470                                           return nsRandomAccessStoreClient::get_at_eof();
00471                                       }
00472 
00473    virtual void                       set_at_eof(PRBool atEnd)
00474                                       {
00475                                           nsRandomAccessStoreClient::set_at_eof(atEnd);
00476                                       }
00477 
00478 private:
00479 
00480     // private and unimplemented to disallow copies and assigns
00481                                       nsRandomAccessInputStream(const nsRandomAccessInputStream& rhs);
00482     nsRandomAccessInputStream&        operator=(const nsRandomAccessInputStream& rhs);
00483 
00484 }; // class nsRandomAccessInputStream
00485 
00486 //========================================================================================
00487 class NS_COM_OBSOLETE nsInputStringStream
00488 //========================================================================================
00489 : public nsRandomAccessInputStream
00490 {
00491 public:
00492                                       nsInputStringStream(const char* stringToRead);
00493                                       nsInputStringStream(const nsString& stringToRead);
00494 
00495     // Input streamers.  Unfortunately, they don't inherit!
00496     nsInputStream&                    operator >> (char& ch)
00497                                          { return nsInputStream::operator >>(ch); }
00498     nsInputStream&                    operator >> (nsInputStream& (*pf)(nsInputStream&))
00499                                          { return nsInputStream::operator >>(pf); }
00500 
00501 
00502 private:
00503 
00504     // private and unimplemented to disallow copies and assigns
00505                                       nsInputStringStream(const nsInputStringStream& rhs);
00506     nsInputStringStream&              operator=(const nsInputStringStream& rhs);
00507 
00508 
00509 }; // class nsInputStringStream
00510 
00511 //========================================================================================
00512 class NS_COM_OBSOLETE nsInputFileStream
00513 // Please read the comments at the top of this file
00514 //========================================================================================
00515 :      public nsRandomAccessInputStream
00516 ,   public nsFileClient
00517 {
00518 public:
00519        enum  { kDefaultMode = PR_RDONLY };
00520                                       nsInputFileStream(nsIInputStream* inStream)
00521                                       :   nsRandomAccessInputStream(inStream)
00522                                       ,   nsFileClient(do_QueryInterface(inStream))
00523                                       ,   mFileInputStream(do_QueryInterface(inStream))
00524                                       {
00525                                       }
00526                                       nsInputFileStream(
00527                                           const nsFileSpec& inFile,
00528                                           int nsprMode = kDefaultMode,
00529                                           PRIntn accessMode = 00666);
00530                                       nsInputFileStream(nsIFileSpec* inFile);
00531     virtual                           ~nsInputFileStream();
00532 
00533     void                              Open(
00534                                           const nsFileSpec& inFile,
00535                                           int nsprMode = kDefaultMode,
00536                                           PRIntn accessMode = 00666)
00537                                       {
00538                                           if (mFile)
00539                                               mFile->Open(inFile, nsprMode, accessMode);
00540                                       }
00541 
00542     // Input streamers.  Unfortunately, they don't inherit!
00543     nsInputStream&                    operator >> (char& ch)
00544                                          { return nsInputStream::operator >>(ch); }
00545     nsInputStream&                    operator >> (nsInputStream& (*pf)(nsInputStream&))
00546                                          { return nsInputStream::operator >>(pf); }
00547 
00548 protected:
00549     void                              AssignFrom(nsISupports* stream);
00550 
00551 private:
00552 
00553     // private and unimplemented to disallow copies and assigns
00554                                       nsInputFileStream(const nsInputFileStream& rhs);
00555     nsInputFileStream&                operator=(const nsInputFileStream& rhs);
00556 
00557 // DATA
00558 protected:
00559     nsCOMPtr<nsIFileSpecInputStream>      mFileInputStream;
00560 }; // class nsInputFileStream
00561 
00562 //========================================================================================
00563 class NS_COM_OBSOLETE nsRandomAccessOutputStream
00564 // Please read the comments at the top of this file
00565 //========================================================================================
00566 :      public nsRandomAccessStoreClient
00567 ,      public nsOutputStream
00568 {
00569 public:
00570                                       nsRandomAccessOutputStream(nsIOutputStream* inStream)
00571                                       :   nsRandomAccessStoreClient(do_QueryInterface(inStream))
00572                                       ,   nsOutputStream(inStream)
00573                                       {
00574                                       }
00575 
00576     // Output streamers.  Unfortunately, they don't inherit!
00577     nsOutputStream&                   operator << (const char* buf)
00578                                         { return nsOutputStream::operator << (buf); }
00579     nsOutputStream&                   operator << (char ch)
00580                                         { return nsOutputStream::operator << (ch); }
00581     nsOutputStream&                   operator << (short val)
00582                                         { return nsOutputStream::operator << (val); }
00583     nsOutputStream&                   operator << (unsigned short val)
00584                                         { return nsOutputStream::operator << (val); }
00585     nsOutputStream&                   operator << (long val)
00586                                         { return nsOutputStream::operator << (val); }
00587     nsOutputStream&                   operator << (unsigned long val)
00588                                         { return nsOutputStream::operator << (val); }
00589     nsOutputStream&                   operator << (int val)
00590                                         { return nsOutputStream::operator << (val); }
00591     nsOutputStream&                   operator << (unsigned int val)
00592                                         { return nsOutputStream::operator << (val); }
00593     nsOutputStream&                   operator << (nsOutputStream& (*pf)(nsOutputStream&))
00594                                         { return nsOutputStream::operator << (pf); }
00595 
00596 protected:
00597                                       nsRandomAccessOutputStream()
00598                                       :  nsOutputStream(nsnull)
00599                                       {
00600                                       }
00601 
00602 private:
00603 
00604     // private and unimplemented to disallow copies and assigns
00605                                       nsRandomAccessOutputStream(const nsRandomAccessOutputStream& rhs);
00606     nsRandomAccessOutputStream&       operator=(const nsRandomAccessOutputStream& rhs);
00607 
00608 }; // class nsRandomAccessOutputStream
00609 
00610 //========================================================================================
00611 class NS_COM_OBSOLETE nsOutputFileStream
00612 // Please read the comments at the top of this file
00613 //========================================================================================
00614 :      public nsRandomAccessOutputStream
00615 ,      public nsFileClient
00616 {
00617 public:
00618        enum  { kDefaultMode = (PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE) };
00619 
00620                                       nsOutputFileStream() {}
00621                                       nsOutputFileStream(nsIOutputStream* inStream)
00622                                       {
00623                                           AssignFrom(inStream);
00624                                       }
00625                                       nsOutputFileStream(
00626                                            const nsFileSpec& inFile,
00627                                            int nsprMode = kDefaultMode,
00628                                            PRIntn accessMode = 00666) 
00629                                       {
00630                                           nsISupports* stream;
00631                                           if (NS_FAILED(NS_NewIOFileStream(
00632                                               &stream,
00633                                               inFile, nsprMode, accessMode)))
00634                                               return;
00635                                           AssignFrom(stream);
00636                                           NS_RELEASE(stream);
00637                                       }
00638                                       nsOutputFileStream(nsIFileSpec* inFile);
00639     virtual                           ~nsOutputFileStream();
00640  
00641     virtual nsresult                  flush();
00642     virtual void                                   abort();
00643 
00644     // Output streamers.  Unfortunately, they don't inherit!
00645     nsOutputStream&                   operator << (const char* buf)
00646                                         { return nsOutputStream::operator << (buf); }
00647     nsOutputStream&                   operator << (char ch)
00648                                         { return nsOutputStream::operator << (ch); }
00649     nsOutputStream&                   operator << (short val)
00650                                         { return nsOutputStream::operator << (val); }
00651     nsOutputStream&                   operator << (unsigned short val)
00652                                         { return nsOutputStream::operator << (val); }
00653     nsOutputStream&                   operator << (long val)
00654                                         { return nsOutputStream::operator << (val); }
00655     nsOutputStream&                   operator << (unsigned long val)
00656                                         { return nsOutputStream::operator << (val); }
00657     nsOutputStream&                   operator << (int val)
00658                                         { return nsOutputStream::operator << (val); }
00659     nsOutputStream&                   operator << (unsigned int val)
00660                                         { return nsOutputStream::operator << (val); }
00661     nsOutputStream&                   operator << (nsOutputStream& (*pf)(nsOutputStream&))
00662                                         { return nsOutputStream::operator << (pf); }
00663 
00664 protected:
00665     void                              AssignFrom(nsISupports* stream);
00666 
00667 private:
00668     
00669     // private and unimplemented to disallow copies and assigns
00670                                       nsOutputFileStream(const nsOutputFileStream& rhs);
00671     nsOutputFileStream&               operator=(const nsOutputFileStream& rhs);
00672 
00673 // DATA
00674 protected:
00675     nsCOMPtr<nsIFileSpecOutputStream>     mFileOutputStream;
00676 }; // class nsOutputFileStream
00677 
00678 
00679 //========================================================================================
00680 class nsIOFileStream
00681 // Please read the comments at the top of this file
00682 //========================================================================================
00683 :      public nsInputFileStream
00684 ,      public nsOutputStream
00685 {
00686 public:
00687        enum  { kDefaultMode = (PR_RDWR | PR_CREATE_FILE) };
00688 
00689                                       nsIOFileStream(
00690                                           nsIInputStream* inInputStream
00691                                       ,   nsIOutputStream* inOutputStream)
00692                                       :   nsInputFileStream(inInputStream)
00693                                       ,   nsOutputStream(inOutputStream)
00694                                       ,   mFileOutputStream(do_QueryInterface(inOutputStream))
00695                                       {
00696                                       }
00697                                       nsIOFileStream(
00698                                            const nsFileSpec& inFile,
00699                                            int nsprMode = kDefaultMode,
00700                                            PRIntn accessMode = 00666) 
00701                                       :  nsInputFileStream((nsIInputStream*)nsnull)
00702                                       ,  nsOutputStream(nsnull)
00703                                       {
00704                                           nsISupports* stream;
00705                                           if (NS_FAILED(NS_NewIOFileStream(
00706                                               &stream,
00707                                               inFile, nsprMode, accessMode)))
00708                                               return;
00709                                           mFile = do_QueryInterface(stream);
00710                                           mStore = do_QueryInterface(stream);
00711                                           mInputStream = do_QueryInterface(stream);
00712                                           mOutputStream = do_QueryInterface(stream);
00713                                           mFileInputStream = do_QueryInterface(stream);
00714                                           mFileOutputStream = do_QueryInterface(stream);
00715                                           NS_RELEASE(stream);
00716                                       }
00717  
00718     virtual nsresult                  close()
00719                                       {
00720                                           // Doesn't matter which of the two we close:
00721                                           // they're hooked up to the same file.
00722                                           return nsInputFileStream::close();
00723                                       }
00724 
00725      // Output streamers.  Unfortunately, they don't inherit!
00726     nsOutputStream&                   operator << (const char* buf)
00727                                         { return nsOutputStream::operator << (buf); }
00728     nsOutputStream&                   operator << (char ch)
00729                                         { return nsOutputStream::operator << (ch); }
00730     nsOutputStream&                   operator << (short val)
00731                                         { return nsOutputStream::operator << (val); }
00732     nsOutputStream&                   operator << (unsigned short val)
00733                                         { return nsOutputStream::operator << (val); }
00734     nsOutputStream&                   operator << (long val)
00735                                         { return nsOutputStream::operator << (val); }
00736     nsOutputStream&                   operator << (unsigned long val)
00737                                         { return nsOutputStream::operator << (val); }
00738     nsOutputStream&                   operator << (int val)
00739                                         { return nsOutputStream::operator << (val); }
00740     nsOutputStream&                   operator << (unsigned int val)
00741                                         { return nsOutputStream::operator << (val); }
00742     nsOutputStream&                   operator << (nsOutputStream& (*pf)(nsOutputStream&))
00743                                         { return nsOutputStream::operator << (pf); }
00744 
00745     // Input streamers.  Unfortunately, they don't inherit!
00746     nsInputStream&                    operator >> (char& ch)
00747                                          { return nsInputStream::operator >>(ch); }
00748     nsInputStream&                    operator >> (nsInputStream& (*pf)(nsInputStream&))
00749                                          { return nsInputStream::operator >>(pf); }
00750 
00751        virtual nsresult flush() {if (mFileOutputStream) mFileOutputStream->Flush(); return error(); }
00752 
00753 
00754 private:
00755 
00756     // private and unimplemented to disallow copies and assigns
00757                                       nsIOFileStream(const nsIOFileStream& rhs);
00758     nsIOFileStream&                   operator=(const nsIOFileStream& rhs);
00759 
00760     // DATA
00761 protected:
00762     nsCOMPtr<nsIFileSpecOutputStream>     mFileOutputStream;
00763 }; // class nsIOFileStream
00764  
00765 //========================================================================================
00766 //        Manipulators
00767 //========================================================================================
00768 
00769 NS_COM_OBSOLETE nsOutputStream&     nsEndl(nsOutputStream& os); // outputs and FLUSHES.
00770 
00771 //========================================================================================
00772 #endif /* _FILESTREAM_H_ */