1998-12-08 02:15:50 +00:00
|
|
|
/* -*- 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.
|
|
|
|
*/
|
|
|
|
|
1998-12-08 22:45:42 +00:00
|
|
|
// 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.
|
1998-12-08 02:15:50 +00:00
|
|
|
|
1998-12-08 22:45:42 +00:00
|
|
|
//========================================================================================
|
|
|
|
//
|
|
|
|
// Classes defined:
|
|
|
|
//
|
|
|
|
// single-byte char:
|
|
|
|
//
|
1998-12-18 23:06:54 +00:00
|
|
|
// nsInputFileStream, nsOutputFileStream
|
1998-12-08 22:45:42 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
1998-12-18 23:06:54 +00:00
|
|
|
// 3. Allows all the power of the ansi stream syntax.
|
1998-12-08 22:45:42 +00:00
|
|
|
//
|
|
|
|
// Basic example:
|
|
|
|
//
|
|
|
|
// nsFilePath myPath("/Development/iotest.txt");
|
|
|
|
//
|
|
|
|
// nsOutputFileStream testStream(myPath);
|
1998-12-18 23:06:54 +00:00
|
|
|
// testStream << "Hello World" << nsEndl;
|
1998-12-08 22:45:42 +00:00
|
|
|
//
|
|
|
|
// 4. Requires streams to be constructed using typesafe nsFilePath specifier
|
|
|
|
// (not the notorious and bug prone const char*), namely nsFilePath. See
|
|
|
|
// nsFileSpec.h for more details.
|
|
|
|
//
|
1998-12-15 22:45:09 +00:00
|
|
|
// 5. Fixes a bug that have been there for a long time, and
|
|
|
|
// is inevitable if you use NSPR alone:
|
1998-12-08 22:45:42 +00:00
|
|
|
//
|
|
|
|
// The problem on platforms (Macintosh) in which a path does not fully
|
1998-12-15 22:45:09 +00:00
|
|
|
// specify a file, because two volumes can have the same name.
|
1998-12-08 22:45:42 +00:00
|
|
|
//
|
|
|
|
// 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_
|
1998-12-08 02:15:50 +00:00
|
|
|
|
1998-12-12 01:25:32 +00:00
|
|
|
#include "nscore.h"
|
1998-12-11 01:42:04 +00:00
|
|
|
|
1998-12-08 22:45:42 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#include "pprio.h" // To get PR_ImportFile
|
|
|
|
#else
|
1998-12-08 02:15:50 +00:00
|
|
|
#include "prio.h"
|
1998-12-08 22:45:42 +00:00
|
|
|
#endif
|
1998-12-08 02:15:50 +00:00
|
|
|
#include "nsFileSpec.h"
|
|
|
|
|
1998-12-08 22:45:42 +00:00
|
|
|
//========================================================================================
|
|
|
|
// Compiler-specific macros, as needed
|
|
|
|
//========================================================================================
|
1998-12-11 01:42:04 +00:00
|
|
|
#if !defined(NS_USING_NAMESPACE) && (defined(__MWERKS__) || defined(XP_PC))
|
1998-12-09 05:09:28 +00:00
|
|
|
#define NS_USING_NAMESPACE
|
|
|
|
#endif
|
|
|
|
|
1998-12-15 22:45:09 +00:00
|
|
|
#if !defined(NS_USING_STL) && (defined(__MWERKS__) || defined(XP_PC))
|
|
|
|
#define NS_USING_STL
|
|
|
|
#endif
|
|
|
|
|
1998-12-09 05:09:28 +00:00
|
|
|
#ifdef NS_USING_NAMESPACE
|
1998-12-15 22:45:09 +00:00
|
|
|
|
1998-12-09 05:09:28 +00:00
|
|
|
#define NS_NAMESPACE_PROTOTYPE
|
|
|
|
#define NS_NAMESPACE namespace
|
|
|
|
#define NS_NAMESPACE_END
|
1998-12-15 22:45:09 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define NS_NAMESPACE_PROTOTYPE static
|
|
|
|
#define NS_NAMESPACE struct
|
|
|
|
#define NS_NAMESPACE_END ;
|
1998-12-11 18:17:22 +00:00
|
|
|
|
1998-12-15 22:45:09 +00:00
|
|
|
#endif // NS_USING_NAMESPACE
|
|
|
|
|
1998-12-29 00:24:32 +00:00
|
|
|
#ifndef XP_MAC
|
|
|
|
// 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
|
|
|
|
class istream;
|
|
|
|
class ostream;
|
|
|
|
#define CONSOLE_IN 0
|
|
|
|
#define CONSOLE_OUT 0
|
|
|
|
#else
|
1998-12-18 23:06:54 +00:00
|
|
|
#include <iostream>
|
|
|
|
using std::istream;
|
|
|
|
using std::ostream;
|
1998-12-29 00:24:32 +00:00
|
|
|
#define CONSOLE_IN &std::cin
|
|
|
|
#define CONSOLE_OUT &std::cout
|
1998-12-08 02:15:50 +00:00
|
|
|
#endif
|
1998-12-15 22:45:09 +00:00
|
|
|
|
1998-12-11 18:17:22 +00:00
|
|
|
//=========================== End Compiler-specific macros ===============================
|
1998-12-08 22:45:42 +00:00
|
|
|
|
|
|
|
//========================================================================================
|
1998-12-18 23:06:54 +00:00
|
|
|
class NS_BASE nsBasicFileStream
|
1998-12-08 02:15:50 +00:00
|
|
|
//========================================================================================
|
|
|
|
{
|
|
|
|
public:
|
1998-12-18 23:06:54 +00:00
|
|
|
nsBasicFileStream();
|
|
|
|
nsBasicFileStream(PRFileDesc* desc, int nsprMode);
|
|
|
|
nsBasicFileStream(
|
|
|
|
const nsFilePath& inFile,
|
|
|
|
int nsprMode,
|
|
|
|
PRIntn accessMode);
|
|
|
|
virtual ~nsBasicFileStream();
|
1998-12-08 22:45:42 +00:00
|
|
|
|
|
|
|
|
1998-12-18 23:06:54 +00:00
|
|
|
inline bool is_open() const { return mFileDesc != 0; }
|
|
|
|
void open(
|
1998-12-08 22:45:42 +00:00
|
|
|
const nsFilePath& inFile,
|
1998-12-18 23:06:54 +00:00
|
|
|
int nsprMode,
|
1998-12-08 22:45:42 +00:00
|
|
|
PRIntn accessMode);
|
1998-12-18 23:06:54 +00:00
|
|
|
void close();
|
|
|
|
PRIntn tell() const;
|
|
|
|
void seek(PRInt32 offset) { seek(PR_SEEK_SET, offset); }
|
|
|
|
void seek(PRSeekWhence whence, PRInt32 offset);
|
|
|
|
bool eof() const { return mEOF; }
|
|
|
|
bool failed() const { return mFailed; }
|
|
|
|
// call PR_GetError() for details
|
|
|
|
|
1998-12-08 02:15:50 +00:00
|
|
|
protected:
|
1998-12-18 23:06:54 +00:00
|
|
|
PRFileDesc* mFileDesc;
|
|
|
|
int mNSPRMode;
|
|
|
|
bool mFailed;
|
|
|
|
bool mEOF;
|
|
|
|
}; // class nsBasicFileStream
|
1998-12-08 02:15:50 +00:00
|
|
|
|
|
|
|
//========================================================================================
|
1998-12-18 23:06:54 +00:00
|
|
|
class NS_BASE nsInputFileStream
|
1998-12-08 02:15:50 +00:00
|
|
|
//========================================================================================
|
1998-12-18 23:06:54 +00:00
|
|
|
: public nsBasicFileStream
|
1998-12-08 02:15:50 +00:00
|
|
|
{
|
|
|
|
public:
|
1998-12-18 23:06:54 +00:00
|
|
|
enum { kDefaultMode = PR_RDONLY };
|
1998-12-29 00:24:32 +00:00
|
|
|
nsInputFileStream(istream* stream = CONSOLE_IN);
|
1998-12-18 23:06:54 +00:00
|
|
|
nsInputFileStream(
|
1998-12-08 22:45:42 +00:00
|
|
|
const nsFilePath& inFile,
|
1998-12-18 23:06:54 +00:00
|
|
|
int nsprMode = kDefaultMode,
|
1998-12-29 00:24:32 +00:00
|
|
|
PRIntn accessMode = 00700) // <- OCTAL
|
1998-12-18 23:06:54 +00:00
|
|
|
: nsBasicFileStream(inFile, nsprMode, accessMode)
|
|
|
|
, mStdStream(0) {}
|
|
|
|
|
|
|
|
nsInputFileStream& operator >> (nsInputFileStream& (*pf)(nsInputFileStream&))
|
|
|
|
{
|
|
|
|
return pf(*this);
|
|
|
|
}
|
|
|
|
void get(char& c);
|
|
|
|
PRInt32 read(void* s, PRInt32 n);
|
|
|
|
bool readline(char* s, PRInt32 n);
|
|
|
|
// Result always null-terminated
|
|
|
|
// false result indicates line was truncated
|
|
|
|
// to fit buffer, or an error occurred.
|
|
|
|
|
1998-12-29 00:24:32 +00:00
|
|
|
// Input streamers. Add more as needed
|
1998-12-18 23:06:54 +00:00
|
|
|
nsInputFileStream& operator >> (char& ch);
|
|
|
|
|
|
|
|
void open(
|
|
|
|
const nsFilePath& inFile,
|
|
|
|
int nsprMode = kDefaultMode,
|
1998-12-29 00:24:32 +00:00
|
|
|
PRIntn accessMode = 00700) // <- OCTAL
|
1998-12-18 23:06:54 +00:00
|
|
|
{
|
|
|
|
nsBasicFileStream::open(inFile, nsprMode, accessMode);
|
|
|
|
}
|
|
|
|
private:
|
1998-12-08 22:45:42 +00:00
|
|
|
|
1998-12-18 23:06:54 +00:00
|
|
|
nsInputFileStream& operator >> (char* buf); // TOO DANGEROUS. DON'T DEFINE.
|
1998-12-08 22:45:42 +00:00
|
|
|
|
1998-12-18 23:06:54 +00:00
|
|
|
protected:
|
1998-12-08 02:15:50 +00:00
|
|
|
|
1998-12-18 23:06:54 +00:00
|
|
|
istream* mStdStream;
|
|
|
|
}; // class nsInputFileStream
|
1998-12-08 02:15:50 +00:00
|
|
|
|
|
|
|
//========================================================================================
|
1998-12-18 23:06:54 +00:00
|
|
|
class NS_BASE nsOutputFileStream
|
1998-12-08 02:15:50 +00:00
|
|
|
//========================================================================================
|
1998-12-18 23:06:54 +00:00
|
|
|
: public nsBasicFileStream
|
1998-12-08 02:15:50 +00:00
|
|
|
{
|
|
|
|
public:
|
1998-12-18 23:06:54 +00:00
|
|
|
enum { kDefaultMode = (PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE) };
|
|
|
|
|
1998-12-29 00:24:32 +00:00
|
|
|
nsOutputFileStream(ostream* stream = CONSOLE_OUT);
|
1998-12-18 23:06:54 +00:00
|
|
|
nsOutputFileStream(
|
1998-12-08 22:45:42 +00:00
|
|
|
const nsFilePath& inFile,
|
1998-12-18 23:06:54 +00:00
|
|
|
int nsprMode = kDefaultMode,
|
1998-12-29 00:24:32 +00:00
|
|
|
PRIntn accessMode = 00700) // <- OCTAL
|
1998-12-18 23:06:54 +00:00
|
|
|
: nsBasicFileStream(inFile, nsprMode, accessMode)
|
|
|
|
, mStdStream(0) {}
|
1998-12-08 02:15:50 +00:00
|
|
|
|
1998-12-18 23:06:54 +00:00
|
|
|
ostream* GetStandardStream() const { return mStdStream; }
|
1998-12-08 22:45:42 +00:00
|
|
|
inline void open(
|
|
|
|
const nsFilePath& inFile,
|
1998-12-18 23:06:54 +00:00
|
|
|
int nsprMode = kDefaultMode,
|
1998-12-29 00:24:32 +00:00
|
|
|
PRIntn accessMode = 00700) // <- OCTAL
|
1998-12-18 23:06:54 +00:00
|
|
|
{
|
|
|
|
nsBasicFileStream::open(inFile, nsprMode, accessMode);
|
|
|
|
}
|
|
|
|
nsOutputFileStream& operator << (nsOutputFileStream& (*pf)(nsOutputFileStream&))
|
|
|
|
{
|
|
|
|
return pf(*this);
|
|
|
|
}
|
|
|
|
void put(char c);
|
|
|
|
PRInt32 write(const void* s, PRInt32 n);
|
|
|
|
void flush();
|
|
|
|
|
|
|
|
// Output streamers. Add more as needed
|
|
|
|
nsOutputFileStream& operator << (const char* buf);
|
|
|
|
nsOutputFileStream& operator << (char ch);
|
|
|
|
nsOutputFileStream& operator << (short val);
|
|
|
|
nsOutputFileStream& operator << (unsigned short val);
|
|
|
|
nsOutputFileStream& operator << (long val);
|
|
|
|
nsOutputFileStream& operator << (unsigned long val);
|
1998-12-08 02:15:50 +00:00
|
|
|
|
1998-12-18 23:06:54 +00:00
|
|
|
protected:
|
1998-12-29 00:24:32 +00:00
|
|
|
|
1998-12-18 23:06:54 +00:00
|
|
|
ostream* mStdStream;
|
|
|
|
}; // class nsOutputFileStream
|
1998-12-08 02:15:50 +00:00
|
|
|
|
|
|
|
//========================================================================================
|
1998-12-18 23:06:54 +00:00
|
|
|
// Manipulators
|
1998-12-08 02:15:50 +00:00
|
|
|
//========================================================================================
|
1998-12-18 23:30:44 +00:00
|
|
|
NS_BASE nsOutputFileStream& nsEndl(nsOutputFileStream& os);
|
1998-12-18 23:06:54 +00:00
|
|
|
|
1998-12-15 22:45:09 +00:00
|
|
|
|
1998-12-08 22:45:42 +00:00
|
|
|
#endif /* _FILESTREAM_H_ */
|
1998-12-08 02:15:50 +00:00
|
|
|
|