1998-12-08 22:43:57 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// First checked in on 98/12/08 by John R. McMullen.
|
|
|
|
// Since nsFileStream.h is entirely templates, common code (such as open())
|
1998-12-15 22:45:09 +00:00
|
|
|
// which does not actually depend on the charT, can be placed here.
|
|
|
|
|
1998-12-08 22:43:57 +00:00
|
|
|
#include "nsFileStream.h"
|
|
|
|
|
1999-02-25 20:49:47 +00:00
|
|
|
|
1998-12-18 23:06:54 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
1998-12-08 22:43:57 +00:00
|
|
|
|
1998-12-18 23:06:54 +00:00
|
|
|
//========================================================================================
|
1999-02-25 20:49:47 +00:00
|
|
|
// nsInputStream
|
1998-12-18 23:06:54 +00:00
|
|
|
//========================================================================================
|
|
|
|
|
1998-12-08 22:43:57 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
nsInputStream::~nsInputStream()
|
1998-12-08 22:43:57 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1998-12-18 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
char nsInputStream::get()
|
1998-12-18 23:06:54 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-02-25 20:49:47 +00:00
|
|
|
char c;
|
|
|
|
read(&c, sizeof(c));
|
|
|
|
return c;
|
1998-12-18 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
static void TidyEndOfLine(char*& cp)
|
|
|
|
// Assumes that cp is pointing at \n or \r. Nulls out the character, checks for
|
|
|
|
// a second terminator (of the opposite persuasion), and returns cp pointing past the
|
|
|
|
// entire eol construct (one or two characters).
|
1998-12-18 23:06:54 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-02-25 20:49:47 +00:00
|
|
|
char ch = *cp;
|
|
|
|
*cp++ = '\0'; // terminate at the newline, then skip past it
|
|
|
|
if ((ch == '\n' && *cp == '\r') || (ch == '\r' && *cp == '\n'))
|
|
|
|
cp++; // possibly a pair.
|
1998-12-18 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
nsInputStream& nsInputStream::operator >> (char& c)
|
1998-12-18 23:06:54 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-02-25 20:49:47 +00:00
|
|
|
c = get();
|
1998-12-18 23:06:54 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//========================================================================================
|
1999-02-25 20:49:47 +00:00
|
|
|
// nsOutputStream
|
1998-12-18 23:06:54 +00:00
|
|
|
//========================================================================================
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
nsOutputStream::~nsOutputStream()
|
1998-12-18 23:06:54 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
void nsOutputStream::put(char c)
|
1998-12-18 23:06:54 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
write(&c, sizeof(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
void nsOutputStream::flush()
|
1998-12-18 23:06:54 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
nsOutputStream& nsOutputStream::operator << (char c)
|
1998-12-18 23:06:54 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
put(c);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
nsOutputStream& nsOutputStream::operator << (const char* s)
|
1998-12-18 23:06:54 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
write(s, strlen(s));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
nsOutputStream& nsOutputStream::operator << (short val)
|
1998-12-18 23:06:54 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
char buf[30];
|
|
|
|
sprintf(buf, "%d", val);
|
1999-02-25 20:49:47 +00:00
|
|
|
return (*this << buf);
|
1998-12-18 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
nsOutputStream& nsOutputStream::operator << (unsigned short val)
|
1998-12-18 23:06:54 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
char buf[30];
|
|
|
|
sprintf(buf, "%ud", val);
|
1999-02-25 20:49:47 +00:00
|
|
|
return (*this << buf);
|
1998-12-18 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
nsOutputStream& nsOutputStream::operator << (long val)
|
1998-12-18 23:06:54 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
char buf[30];
|
|
|
|
sprintf(buf, "%ld", val);
|
1999-02-25 20:49:47 +00:00
|
|
|
return (*this << buf);
|
1998-12-18 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
nsOutputStream& nsOutputStream::operator << (unsigned long val)
|
1998-12-18 23:06:54 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
char buf[30];
|
|
|
|
sprintf(buf, "%uld", val);
|
1999-02-25 20:49:47 +00:00
|
|
|
return (*this << buf);
|
1998-12-18 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
1999-02-25 20:49:47 +00:00
|
|
|
//========================================================================================
|
|
|
|
// nsInputFileStream
|
|
|
|
//========================================================================================
|
|
|
|
|
1998-12-18 23:06:54 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
PRBool nsInputFileStream::readline(char* s, PRInt32 n)
|
|
|
|
// This will truncate if the buffer is too small. Result will always be null-terminated.
|
1998-12-18 23:06:54 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-02-25 20:49:47 +00:00
|
|
|
PRBool bufferLargeEnough = PR_TRUE; // result
|
|
|
|
if (!s || !n)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
PRIntn position = tell();
|
|
|
|
if (position < 0)
|
|
|
|
return PR_FALSE;
|
|
|
|
PRInt32 bytesRead = read(s, n - 1);
|
|
|
|
if (failed())
|
|
|
|
return PR_FALSE;
|
|
|
|
s[bytesRead] = '\0'; // always terminate at the end of the buffer
|
|
|
|
char* tp = strpbrk(s, "\n\r");
|
|
|
|
if (tp)
|
1998-12-18 23:06:54 +00:00
|
|
|
{
|
1999-02-25 20:49:47 +00:00
|
|
|
TidyEndOfLine(tp);
|
|
|
|
bytesRead = (tp - s);
|
1998-12-18 23:06:54 +00:00
|
|
|
}
|
1999-02-25 20:49:47 +00:00
|
|
|
else if (!eof())
|
|
|
|
bufferLargeEnough = PR_FALSE;
|
|
|
|
position += bytesRead;
|
|
|
|
seek(position);
|
|
|
|
return bufferLargeEnough;
|
|
|
|
} // nsInputStream::readline
|
1999-01-26 23:49:33 +00:00
|
|
|
|
|
|
|
//========================================================================================
|
|
|
|
// nsOutputFileStream
|
|
|
|
//========================================================================================
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
void nsOutputFileStream::flush()
|
1999-01-26 23:49:33 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-02-25 20:49:47 +00:00
|
|
|
if (mFileOutputStream)
|
|
|
|
mFileOutputStream->Flush();
|
1999-01-26 23:49:33 +00:00
|
|
|
}
|
1998-12-18 23:06:54 +00:00
|
|
|
|
|
|
|
//========================================================================================
|
|
|
|
// Manipulators
|
|
|
|
//========================================================================================
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
nsOutputStream& nsEndl(nsOutputStream& os)
|
1998-12-18 23:06:54 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-02-25 20:49:47 +00:00
|
|
|
os.put('\n');
|
|
|
|
os.flush();
|
|
|
|
return os;
|
|
|
|
} // nsEndl
|