/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// First checked in on 98/11/20 by John R. McMullen in the wrong directory.
// Checked in again 98/12/04.
// Polished version 98/12/08.
// Completely rewritten to integrate with nsIInputStream and nsIOutputStream (the
// xpcom stream objects.
//========================================================================================
//
// Classes defined:
//
// nsInputStream, nsOutputStream
// These are the lightweight STATICALLY LINKED wrappers for
// the xpcom objects nsIInputStream and nsIOutputstream.
// Possible uses:
// If you are implementing a function that accepts one of these xpcom
// streams, just make one of these little jobbies on the stack, and
// the handy << or >> notation can be yours.
//
// nsInputFileStream, nsOutputFileStream
// These are the STATICALLY LINKED wrappers for the file-related
// versions of the above.
// nsIOFileStream
// An input and output file stream attached to the same file.
//
// This suite provide the following services:
//
// 1. Encapsulates all platform-specific file details, so that file i/o
// can be done correctly without any platform #ifdefs
//
// 2. Uses NSPR file services (NOT ansi file I/O), in order to get best
// native performance. This performance difference is especially large on
// macintosh.
//
// 3. Allows all the power of the ansi stream syntax.
//
// Basic example:
//
// nsFileSpec myPath("/Development/iotest.txt");
//
// nsOutputFileStream testStream(myPath);
// testStream << "Hello World" << nsEndl;
//
// 4. Requires streams to be constructed using typesafe nsFileSpec specifier
// (not the notorious and bug prone const char*), namely nsFileSpec. See
// nsFileSpec.h for more details.
//
// 5. Fixes a bug that have been there for a long time, and
// is inevitable if you use NSPR alone:
//
// The problem on platforms (Macintosh) in which a path does not fully
// specify a file, because two volumes can have the same name.
//
// Not yet provided:
//
// Endian-awareness for reading and writing crossplatform binary files. At this
// time there seems to be no demand for this.
//
//========================================================================================
#ifndef _FILESTREAM_H_
#define _FILESTREAM_H_
#include "xpcomobsolete.h"
#include "nsStringFwd.h"
#ifdef XP_MAC
#include "pprio.h" // To get PR_ImportFile
#else
#include "prio.h"
#endif
#include "nsCOMPtr.h"
#include "nsIFileStream.h"
// Defined elsewhere
//========================================================================================
// Compiler-specific macros, as needed
//========================================================================================
#define NS_USING_NAMESPACE
#endif
#ifdef NS_USING_NAMESPACE
#define NS_NAMESPACE_PROTOTYPE
#define NS_NAMESPACE_END
#else
#define NS_NAMESPACE_PROTOTYPE static
#define NS_NAMESPACE struct
#define NS_NAMESPACE_END ;
#endif // NS_USING_NAMESPACE
// PR_STDOUT and PR_STDIN are fatal on Macintosh. So for console i/o, we must use the std
// stream stuff instead. However, we have to require that cout and cin are passed in
// to the constructor because in the current build, there is a copy in the base.shlb,
// and another in the caller's file. Passing it in as a parameter ensures that the
// caller and this library are using the same global object. Groan.
//
// Unix currently does not support iostreams at all. Their compilers do not support
// ANSI C++, or even ARM C++.
//
// Windows supports them, but only if you turn on the -GX compile flag, to support
// exceptions.
// Catch 22.
#define NS_USE_PR_STDIO
#endif
#ifdef NS_USE_PR_STDIO
#define CONSOLE_IN 0
#define CONSOLE_OUT 0
#else
#include <iostream>
#endif
//=========================== End Compiler-specific macros ===============================
//========================================================================================
// This is a convenience class, for use on the STACK ("new" junkies: get detoxed first).
// Given a COM-style stream, this allows you to use the >> operators. It also acquires and
// reference counts its stream.
// Please read the comments at the top of this file
//========================================================================================
{
{}
virtual ~nsInputStream();
{
return mInputStream;
}
char get();
{
if (mInputStream) {
return mInputStream->Close();
}
return NS_OK;
}
// Input streamers. Add more as needed (int&, unsigned int& etc). (but you have to
// add delegators to the derived classes, too, because these operators don't inherit).
// Support manipulators
{
}
// These certainly need to be overridden, they give the best shot we can at detecting
// eof in a simple nsIInputStream.
{
}
{
return mEOF;
}
// private and unimplemented to disallow copies and assigns
// DATA
}; // class nsInputStream
//========================================================================================
// This is a convenience class, for use on the STACK ("new" junkies, get detoxed first).
// Given a COM-style stream, this allows you to use the << operators. It also acquires and
// reference counts its stream.
// Please read the comments at the top of this file
//========================================================================================
{
nsOutputStream() {}
{}
{
return mOutputStream;
}
{
if (mOutputStream)
return mOutputStream->Close();
return NS_OK;
}
void put(char c);
// Output streamers. Add more as needed (but you have to add delegators to the derived
// classes, too, because these operators don't inherit).
// Support manipulators
{
}
// private and unimplemented to disallow copies and assigns
// DATA
}; // class nsOutputStream
//========================================================================================
// Common (virtual) base class for remembering errors on demand
//========================================================================================
{
{
}
{
}
{
return mResult;
}
// DATA
}; // class nsErrorProne
//========================================================================================
// Because COM does not allow us to write functions which return a boolean value etc,
// this class is here to take care of the tedious "declare variable then call with
// the address of the variable" chores.
//========================================================================================
{
{
}
virtual ~nsFileClient() {}
void open(
const nsFileSpec& inFile,
int nsprMode,
{
if (mFile)
}
{
if (mFile)
return result;
}
{
}
{
}
// DATA
}; // class nsFileClient
//========================================================================================
// Because COM does not allow us to write functions which return a boolean value etc,
// this class is here to take care of the tedious "declare variable then call with
// the address of the variable" chores.
//========================================================================================
{
{
}
{
}
{
}
{
if (mStore)
}
{
if (mStore)
return result;
}
{
if (mStore)
return result;
}
{
if (mStore)
}
// private and unimplemented to disallow copies and assigns
// DATA
}; // class nsRandomAccessStoreClient
//========================================================================================
// Please read the comments at the top of this file
//========================================================================================
{
{
}
// Result always null-terminated.
// Check eof() before each call.
// CAUTION: false result only indicates line was truncated
// to fit buffer, or an error occurred (OTHER THAN eof).
// Input streamers. Unfortunately, they don't inherit!
{
}
{
return nsRandomAccessStoreClient::get_at_eof();
}
{
}
// private and unimplemented to disallow copies and assigns
}; // class nsRandomAccessInputStream
//========================================================================================
//========================================================================================
{
nsInputStringStream(const char* stringToRead);
// Input streamers. Unfortunately, they don't inherit!
// private and unimplemented to disallow copies and assigns
}; // class nsInputStringStream
//========================================================================================
// Please read the comments at the top of this file
//========================================================================================
{
{
}
const nsFileSpec& inFile,
int nsprMode = kDefaultMode,
void Open(
const nsFileSpec& inFile,
int nsprMode = kDefaultMode,
{
if (mFile)
}
// Input streamers. Unfortunately, they don't inherit!
// private and unimplemented to disallow copies and assigns
// DATA
}; // class nsInputFileStream
//========================================================================================
// Please read the comments at the top of this file
//========================================================================================
{
{
}
// Output streamers. Unfortunately, they don't inherit!
{
}
// private and unimplemented to disallow copies and assigns
}; // class nsRandomAccessOutputStream
//========================================================================================
// Please read the comments at the top of this file
//========================================================================================
{
nsOutputFileStream() {}
{
}
const nsFileSpec& inFile,
int nsprMode = kDefaultMode,
{
&stream,
return;
}
// Output streamers. Unfortunately, they don't inherit!
// private and unimplemented to disallow copies and assigns
// DATA
}; // class nsOutputFileStream
//========================================================================================
// Please read the comments at the top of this file
//========================================================================================
{
{
}
const nsFileSpec& inFile,
int nsprMode = kDefaultMode,
{
&stream,
return;
}
{
// Doesn't matter which of the two we close:
// they're hooked up to the same file.
return nsInputFileStream::close();
}
// Output streamers. Unfortunately, they don't inherit!
// Input streamers. Unfortunately, they don't inherit!
// private and unimplemented to disallow copies and assigns
// DATA
}; // class nsIOFileStream
//========================================================================================
// Manipulators
//========================================================================================
//========================================================================================
#endif /* _FILESTREAM_H_ */