Simplify the console case, so that Windows does not think it has to compile with exception handling.

git-svn-id: svn://10.0.0.236/trunk@16914 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
mcmullen%netscape.com 1998-12-29 00:24:32 +00:00
parent f8bbc3023a
commit 4bbbe1f26f
8 changed files with 160 additions and 144 deletions

View File

@ -100,12 +100,34 @@
#endif // NS_USING_NAMESPACE
#ifdef NS_USING_STL
#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
#include <iostream>
using std::istream;
using std::ostream;
#else
#include <iostream.h>
#define CONSOLE_IN &std::cin
#define CONSOLE_OUT &std::cout
#endif
//=========================== End Compiler-specific macros ===============================
@ -151,15 +173,11 @@ class NS_BASE nsInputFileStream
{
public:
enum { kDefaultMode = PR_RDONLY };
nsInputFileStream()
: nsBasicFileStream()
, mStdStream(0) {}
nsInputFileStream(
istream& stream);
nsInputFileStream(istream* stream = CONSOLE_IN);
nsInputFileStream(
const nsFilePath& inFile,
int nsprMode = kDefaultMode,
PRIntn accessMode = 0x00400)
PRIntn accessMode = 00700) // <- OCTAL
: nsBasicFileStream(inFile, nsprMode, accessMode)
, mStdStream(0) {}
@ -174,13 +192,13 @@ public:
// false result indicates line was truncated
// to fit buffer, or an error occurred.
// Output streamers. Add more as needed
// Input streamers. Add more as needed
nsInputFileStream& operator >> (char& ch);
void open(
const nsFilePath& inFile,
int nsprMode = kDefaultMode,
PRIntn accessMode = 0x00400)
PRIntn accessMode = 00700) // <- OCTAL
{
nsBasicFileStream::open(inFile, nsprMode, accessMode);
}
@ -201,15 +219,11 @@ class NS_BASE nsOutputFileStream
public:
enum { kDefaultMode = (PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE) };
nsOutputFileStream()
: nsBasicFileStream()
, mStdStream(0) {}
nsOutputFileStream(
ostream& stream);
nsOutputFileStream(ostream* stream = CONSOLE_OUT);
nsOutputFileStream(
const nsFilePath& inFile,
int nsprMode = kDefaultMode,
PRIntn accessMode = 0x00200)
PRIntn accessMode = 00700) // <- OCTAL
: nsBasicFileStream(inFile, nsprMode, accessMode)
, mStdStream(0) {}
@ -217,7 +231,7 @@ public:
inline void open(
const nsFilePath& inFile,
int nsprMode = kDefaultMode,
PRIntn accessMode = 0x00200)
PRIntn accessMode = 00700) // <- OCTAL
{
nsBasicFileStream::open(inFile, nsprMode, accessMode);
}
@ -238,6 +252,7 @@ public:
nsOutputFileStream& operator << (unsigned long val);
protected:
ostream* mStdStream;
}; // class nsOutputFileStream

View File

@ -19,27 +19,12 @@
#include "nsFileSpec.h"
#include "nsFileStream.h"
#include "nsDebug.h"
#include "prtypes.h"
#ifdef NS_USING_STL
#include <strstream>
#else
#include <ostream.h>
#include <strstream.h>
#endif
#ifdef NS_USING_NAMESPACE
using std::ends;
using std::ostrstream;
#endif
#include <string.h>
#include "nsDebug.h"
#include <stdio.h>
//========================================================================================
NS_NAMESPACE nsFileSpecHelpers
@ -394,10 +379,9 @@ void nsNativeFileSpec::MakeUnique()
for (short index = 1; index < 1000 && Exists(); index++)
{
// start with "Picture-1.jpg" after "Picture.jpg" exists
char buf[nsFileSpecHelpers::kMaxFilenameLength + 1];
ostrstream newName(buf, nsFileSpecHelpers::kMaxFilenameLength);
newName << leafName << "-" << index << suffix << '\0'; // should be: << std::ends;
SetLeafName(newName.str()); // or: SetLeafName(buf)
char newName[nsFileSpecHelpers::kMaxFilenameLength + 1];
sprintf(newName, "%s-%d%s", leafName, index, suffix);
SetLeafName(newName);
}
if (*suffix)
delete [] suffix;

View File

@ -20,11 +20,6 @@
// Since nsFileStream.h is entirely templates, common code (such as open())
// which does not actually depend on the charT, can be placed here.
#ifdef XP_UNIX
// Compile the un-inlined functions in this file only.
#define DEFINING_FILE_STREAM
#endif
#include "nsFileStream.h"
#include <string.h>
@ -34,11 +29,6 @@
#include <Errors.h>
#endif
#ifdef NS_USING_STL
using std::endl;
using std::cout;
#endif
//========================================================================================
// nsBasicFileStream
//========================================================================================
@ -224,12 +214,11 @@ PRIntn nsBasicFileStream::tell() const
//========================================================================================
//----------------------------------------------------------------------------------------
nsInputFileStream::nsInputFileStream(
istream& stream)
nsInputFileStream::nsInputFileStream(istream* stream)
//----------------------------------------------------------------------------------------
#ifdef XP_MAC
#ifndef NS_USE_PR_STDIO
: nsBasicFileStream(0, kDefaultMode)
, mStdStream(&stream)
, mStdStream(stream)
#else
: nsBasicFileStream(PR_STDIN, kDefaultMode)
, mStdStream(0)
@ -279,12 +268,14 @@ bool nsInputFileStream::readline(char* s, PRInt32 n)
PRInt32 nsInputFileStream::read(void* s, PRInt32 n)
//----------------------------------------------------------------------------------------
{
#ifndef NS_USE_PR_STDIO
// Calling PR_Read on stdin is sure suicide on Macintosh.
if (mStdStream)
{
mStdStream->read((char*)s, n);
return n;
}
#endif
if (!mFileDesc || mFailed)
return -1;
PRInt32 bytesRead = PR_Read(mFileDesc, s, n);
@ -308,12 +299,11 @@ nsInputFileStream& nsInputFileStream::operator >> (char& c)
//========================================================================================
//----------------------------------------------------------------------------------------
nsOutputFileStream::nsOutputFileStream(
ostream& stream)
nsOutputFileStream::nsOutputFileStream(ostream* stream)
//----------------------------------------------------------------------------------------
#ifdef XP_MAC
#ifndef NS_USE_PR_STDIO
: nsBasicFileStream(0, kDefaultMode)
, mStdStream(&stream)
, mStdStream(stream)
#else
: nsBasicFileStream(PR_STDOUT, kDefaultMode)
, mStdStream(0)
@ -332,12 +322,14 @@ void nsOutputFileStream::put(char c)
PRInt32 nsOutputFileStream::write(const void* s, PRInt32 n)
//----------------------------------------------------------------------------------------
{
#ifndef NS_USE_PR_STDIO
// Calling PR_Write on stdout is sure suicide.
if (mStdStream)
{
mStdStream->write((const char*)s, n);
return n;
}
#endif
if (!mFileDesc || mFailed)
return -1;
PRInt32 bytesWrit = PR_Write(mFileDesc, s, n);
@ -403,11 +395,13 @@ void nsOutputFileStream::flush()
// Must precede the destructor because both are inline.
//----------------------------------------------------------------------------------------
{
#ifndef NS_USE_PR_STDIO
if (mStdStream)
{
mStdStream->flush();
return;
}
#endif
if (mFileDesc == 0)
return;
bool itFailed = PR_Sync(mFileDesc) != PR_SUCCESS;
@ -426,13 +420,15 @@ void nsOutputFileStream::flush()
nsOutputFileStream& nsEndl(nsOutputFileStream& os)
//----------------------------------------------------------------------------------------
{
#ifndef NS_USE_PR_STDIO
// Calling PR_Write on stdout is sure suicide on Macintosh.
ostream* stream = os.GetStandardStream();
if (stream)
{
*stream << endl;
*stream << std::endl;
return os;
}
#endif
os.put('\n');
os.flush();
return os;

View File

@ -1,18 +1,13 @@
#include "nsFileSpec.h"
#include "nsFileStream.h"
#ifdef NS_USING_STL
using std::endl;
using std::cout;
#endif
NS_NAMESPACE FileTest
NS_NAMESPACE FilesTest
{
NS_NAMESPACE_PROTOTYPE void WriteStuff(nsOutputFileStream& s);
} NS_NAMESPACE_END
//----------------------------------------------------------------------------------------
void FileTest::WriteStuff(nsOutputFileStream& s)
void FilesTest::WriteStuff(nsOutputFileStream& s)
//----------------------------------------------------------------------------------------
{
// Initialize a URL from a string without suffix. Change the path to suit your machine.
@ -54,12 +49,14 @@ void main()
//----------------------------------------------------------------------------------------
{
// Test of nsFileSpec
// Test of console output
nsOutputFileStream nsOut(cout);
nsOutputFileStream nsOut;
nsOut << "WRITING TEST OUTPUT TO cout" << nsEndl << nsEndl;
FileTest::WriteStuff(nsOut);
// Test of nsFileSpec
FilesTest::WriteStuff(nsOut);
nsOut << nsEndl << nsEndl;
// Test of nsOutputFileStream
@ -69,8 +66,16 @@ void main()
{
nsOut << "WRITING IDENTICAL OUTPUT TO " << (const char*)myTextFilePath << nsEndl << nsEndl;
nsOutputFileStream testStream(myTextFilePath);
NS_ASSERTION(testStream.is_open(), "File could not be opened");
FileTest::WriteStuff(testStream);
if (!testStream.is_open())
{
nsOut
<< "ERROR: File "
<< (const char*)myTextFilePath
<< " could not be opened for output"
<< nsEndl;
return;
}
FilesTest::WriteStuff(testStream);
} // <-- Scope closes the stream (and the file).
// Test of nsInputFileStream
@ -78,7 +83,15 @@ void main()
{
nsOut << "READING BACK DATA FROM " << (const char*)myTextFilePath << nsEndl << nsEndl;
nsInputFileStream testStream2(myTextFilePath);
NS_ASSERTION(testStream2.is_open(), "File could not be opened");
if (!testStream2.is_open())
{
nsOut
<< "ERROR: File "
<< (const char*)myTextFilePath
<< " could not be opened for input"
<< nsEndl;
return;
}
char line[1000];
testStream2.seek(0); // check that the seek compiles
@ -87,5 +100,5 @@ void main()
testStream2.readline(line, sizeof(line));
nsOut << line << nsEndl;
}
}
} // <-- Scope closes the stream (and the file).
} // main

View File

@ -19,27 +19,12 @@
#include "nsFileSpec.h"
#include "nsFileStream.h"
#include "nsDebug.h"
#include "prtypes.h"
#ifdef NS_USING_STL
#include <strstream>
#else
#include <ostream.h>
#include <strstream.h>
#endif
#ifdef NS_USING_NAMESPACE
using std::ends;
using std::ostrstream;
#endif
#include <string.h>
#include "nsDebug.h"
#include <stdio.h>
//========================================================================================
NS_NAMESPACE nsFileSpecHelpers
@ -394,10 +379,9 @@ void nsNativeFileSpec::MakeUnique()
for (short index = 1; index < 1000 && Exists(); index++)
{
// start with "Picture-1.jpg" after "Picture.jpg" exists
char buf[nsFileSpecHelpers::kMaxFilenameLength + 1];
ostrstream newName(buf, nsFileSpecHelpers::kMaxFilenameLength);
newName << leafName << "-" << index << suffix << '\0'; // should be: << std::ends;
SetLeafName(newName.str()); // or: SetLeafName(buf)
char newName[nsFileSpecHelpers::kMaxFilenameLength + 1];
sprintf(newName, "%s-%d%s", leafName, index, suffix);
SetLeafName(newName);
}
if (*suffix)
delete [] suffix;

View File

@ -20,11 +20,6 @@
// Since nsFileStream.h is entirely templates, common code (such as open())
// which does not actually depend on the charT, can be placed here.
#ifdef XP_UNIX
// Compile the un-inlined functions in this file only.
#define DEFINING_FILE_STREAM
#endif
#include "nsFileStream.h"
#include <string.h>
@ -34,11 +29,6 @@
#include <Errors.h>
#endif
#ifdef NS_USING_STL
using std::endl;
using std::cout;
#endif
//========================================================================================
// nsBasicFileStream
//========================================================================================
@ -224,12 +214,11 @@ PRIntn nsBasicFileStream::tell() const
//========================================================================================
//----------------------------------------------------------------------------------------
nsInputFileStream::nsInputFileStream(
istream& stream)
nsInputFileStream::nsInputFileStream(istream* stream)
//----------------------------------------------------------------------------------------
#ifdef XP_MAC
#ifndef NS_USE_PR_STDIO
: nsBasicFileStream(0, kDefaultMode)
, mStdStream(&stream)
, mStdStream(stream)
#else
: nsBasicFileStream(PR_STDIN, kDefaultMode)
, mStdStream(0)
@ -279,12 +268,14 @@ bool nsInputFileStream::readline(char* s, PRInt32 n)
PRInt32 nsInputFileStream::read(void* s, PRInt32 n)
//----------------------------------------------------------------------------------------
{
#ifndef NS_USE_PR_STDIO
// Calling PR_Read on stdin is sure suicide on Macintosh.
if (mStdStream)
{
mStdStream->read((char*)s, n);
return n;
}
#endif
if (!mFileDesc || mFailed)
return -1;
PRInt32 bytesRead = PR_Read(mFileDesc, s, n);
@ -308,12 +299,11 @@ nsInputFileStream& nsInputFileStream::operator >> (char& c)
//========================================================================================
//----------------------------------------------------------------------------------------
nsOutputFileStream::nsOutputFileStream(
ostream& stream)
nsOutputFileStream::nsOutputFileStream(ostream* stream)
//----------------------------------------------------------------------------------------
#ifdef XP_MAC
#ifndef NS_USE_PR_STDIO
: nsBasicFileStream(0, kDefaultMode)
, mStdStream(&stream)
, mStdStream(stream)
#else
: nsBasicFileStream(PR_STDOUT, kDefaultMode)
, mStdStream(0)
@ -332,12 +322,14 @@ void nsOutputFileStream::put(char c)
PRInt32 nsOutputFileStream::write(const void* s, PRInt32 n)
//----------------------------------------------------------------------------------------
{
#ifndef NS_USE_PR_STDIO
// Calling PR_Write on stdout is sure suicide.
if (mStdStream)
{
mStdStream->write((const char*)s, n);
return n;
}
#endif
if (!mFileDesc || mFailed)
return -1;
PRInt32 bytesWrit = PR_Write(mFileDesc, s, n);
@ -403,11 +395,13 @@ void nsOutputFileStream::flush()
// Must precede the destructor because both are inline.
//----------------------------------------------------------------------------------------
{
#ifndef NS_USE_PR_STDIO
if (mStdStream)
{
mStdStream->flush();
return;
}
#endif
if (mFileDesc == 0)
return;
bool itFailed = PR_Sync(mFileDesc) != PR_SUCCESS;
@ -426,13 +420,15 @@ void nsOutputFileStream::flush()
nsOutputFileStream& nsEndl(nsOutputFileStream& os)
//----------------------------------------------------------------------------------------
{
#ifndef NS_USE_PR_STDIO
// Calling PR_Write on stdout is sure suicide on Macintosh.
ostream* stream = os.GetStandardStream();
if (stream)
{
*stream << endl;
*stream << std::endl;
return os;
}
#endif
os.put('\n');
os.flush();
return os;

View File

@ -100,12 +100,34 @@
#endif // NS_USING_NAMESPACE
#ifdef NS_USING_STL
#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
#include <iostream>
using std::istream;
using std::ostream;
#else
#include <iostream.h>
#define CONSOLE_IN &std::cin
#define CONSOLE_OUT &std::cout
#endif
//=========================== End Compiler-specific macros ===============================
@ -151,15 +173,11 @@ class NS_BASE nsInputFileStream
{
public:
enum { kDefaultMode = PR_RDONLY };
nsInputFileStream()
: nsBasicFileStream()
, mStdStream(0) {}
nsInputFileStream(
istream& stream);
nsInputFileStream(istream* stream = CONSOLE_IN);
nsInputFileStream(
const nsFilePath& inFile,
int nsprMode = kDefaultMode,
PRIntn accessMode = 0x00400)
PRIntn accessMode = 00700) // <- OCTAL
: nsBasicFileStream(inFile, nsprMode, accessMode)
, mStdStream(0) {}
@ -174,13 +192,13 @@ public:
// false result indicates line was truncated
// to fit buffer, or an error occurred.
// Output streamers. Add more as needed
// Input streamers. Add more as needed
nsInputFileStream& operator >> (char& ch);
void open(
const nsFilePath& inFile,
int nsprMode = kDefaultMode,
PRIntn accessMode = 0x00400)
PRIntn accessMode = 00700) // <- OCTAL
{
nsBasicFileStream::open(inFile, nsprMode, accessMode);
}
@ -201,15 +219,11 @@ class NS_BASE nsOutputFileStream
public:
enum { kDefaultMode = (PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE) };
nsOutputFileStream()
: nsBasicFileStream()
, mStdStream(0) {}
nsOutputFileStream(
ostream& stream);
nsOutputFileStream(ostream* stream = CONSOLE_OUT);
nsOutputFileStream(
const nsFilePath& inFile,
int nsprMode = kDefaultMode,
PRIntn accessMode = 0x00200)
PRIntn accessMode = 00700) // <- OCTAL
: nsBasicFileStream(inFile, nsprMode, accessMode)
, mStdStream(0) {}
@ -217,7 +231,7 @@ public:
inline void open(
const nsFilePath& inFile,
int nsprMode = kDefaultMode,
PRIntn accessMode = 0x00200)
PRIntn accessMode = 00700) // <- OCTAL
{
nsBasicFileStream::open(inFile, nsprMode, accessMode);
}
@ -238,6 +252,7 @@ public:
nsOutputFileStream& operator << (unsigned long val);
protected:
ostream* mStdStream;
}; // class nsOutputFileStream

View File

@ -1,18 +1,13 @@
#include "nsFileSpec.h"
#include "nsFileStream.h"
#ifdef NS_USING_STL
using std::endl;
using std::cout;
#endif
NS_NAMESPACE FileTest
NS_NAMESPACE FilesTest
{
NS_NAMESPACE_PROTOTYPE void WriteStuff(nsOutputFileStream& s);
} NS_NAMESPACE_END
//----------------------------------------------------------------------------------------
void FileTest::WriteStuff(nsOutputFileStream& s)
void FilesTest::WriteStuff(nsOutputFileStream& s)
//----------------------------------------------------------------------------------------
{
// Initialize a URL from a string without suffix. Change the path to suit your machine.
@ -54,12 +49,14 @@ void main()
//----------------------------------------------------------------------------------------
{
// Test of nsFileSpec
// Test of console output
nsOutputFileStream nsOut(cout);
nsOutputFileStream nsOut;
nsOut << "WRITING TEST OUTPUT TO cout" << nsEndl << nsEndl;
FileTest::WriteStuff(nsOut);
// Test of nsFileSpec
FilesTest::WriteStuff(nsOut);
nsOut << nsEndl << nsEndl;
// Test of nsOutputFileStream
@ -69,8 +66,16 @@ void main()
{
nsOut << "WRITING IDENTICAL OUTPUT TO " << (const char*)myTextFilePath << nsEndl << nsEndl;
nsOutputFileStream testStream(myTextFilePath);
NS_ASSERTION(testStream.is_open(), "File could not be opened");
FileTest::WriteStuff(testStream);
if (!testStream.is_open())
{
nsOut
<< "ERROR: File "
<< (const char*)myTextFilePath
<< " could not be opened for output"
<< nsEndl;
return;
}
FilesTest::WriteStuff(testStream);
} // <-- Scope closes the stream (and the file).
// Test of nsInputFileStream
@ -78,7 +83,15 @@ void main()
{
nsOut << "READING BACK DATA FROM " << (const char*)myTextFilePath << nsEndl << nsEndl;
nsInputFileStream testStream2(myTextFilePath);
NS_ASSERTION(testStream2.is_open(), "File could not be opened");
if (!testStream2.is_open())
{
nsOut
<< "ERROR: File "
<< (const char*)myTextFilePath
<< " could not be opened for input"
<< nsEndl;
return;
}
char line[1000];
testStream2.seek(0); // check that the seek compiles
@ -87,5 +100,5 @@ void main()
testStream2.readline(line, sizeof(line));
nsOut << line << nsEndl;
}
}
} // <-- Scope closes the stream (and the file).
} // main