domstream.cpp revision 819aa4f3956291e17dab9b2548dce90d4b195a03
/*
* Phoebe DOM Implementation.
*
* This is a C++ approximation of the W3C DOM model, which follows
* fairly closely the specifications in the various .idl files, copies of
* which are provided for reference. Most important is this one:
*
*
* Authors:
* Bob Jamison
*
* Copyright (C) 2006-2008 Bob Jamison
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* inherited from iostreams, and includes any extra
* functionality that we might need.
*
*/
#include <cstdio>
#include <math.h>
#include <stdarg.h>
#include "domstream.h"
namespace org
{
namespace w3c
{
namespace dom
{
namespace io
{
//#########################################################################
//# U T I L I T Y
//#########################################################################
{
for (;;)
{
if (ch<0)
break;
}
}
//#########################################################################
//# F O R M A T T E D P R I N T I N G
//#########################################################################
static char const *digits = "0123456789abcdefghijklmnopqrstuvwxyz";
{
//### Get the digits
while (arg > 0)
{
}
{
}
buf = "0";
for (int i=0 ; i<pad ; i++)
//### Output the result
{
return -1;
}
return 1;
}
{
//printf("int:%f frac:%f\n", intPart, fracPart);
bool negative = false;
if (val < 0)
{
negative = true;
}
int intDigits = 0;
double scale = 1.0;
{
intDigits++;
scale *= 10.0;
}
double intPart;
if (precision <= 0)
precision = 5;
//### How many pad digits?
if (precision > 0)
else if (flag == '#')
pad--;
//### Signs
if (negative)
else if (flag == '+')
//### Prefix pad
{
while (pad--)
}
//### Integer digits
while (intDigits--)
{
intPart *= 10.0;
double dig;
}
buf = "0";
//### Decimal point
{
}
//### Fractional digits
while (precision--)
{
fracPart *= 10.0;
double dig;
}
//### Left justify if requested
{
while (pad--)
}
//### Output the result
{
return -1;
}
return 1;
}
/**
* Output a string. We veer from the standard a tiny bit.
* Normally, a flag of '#' is undefined for strings. We use
* it as an indicator that the user wants to XML-escape any
* XML entities.
*/
{
if (flags == '#')
{
{
if (ch == '&')
else if (ch == '<')
else if (ch == '>')
else if (ch == '"')
else if (ch == '\'')
else
}
}
else
{
}
return 1;
}
{
if (!len)
{
*ret = 0;
return pos;
}
bool has_sign = false;
int val = 0;
{
has_sign = true;
pos++;
}
{
else
break;
pos++;
}
if (has_sign)
return pos;
}
{
{
//## normal character
if (ch != '%')
{
{
return -1;
}
continue;
}
{
return -1;
}
//## is this %% ?
{
{
return -1;
}
continue;
}
//## flag
char flag = '\0';
{
{
return -1;
}
}
//## width.precision
int width = 0;
int precision = 0;
{
return -1;
}
if (ch == '.')
{
{
return -1;
}
{
return -1;
}
}
//## length
char length = '\0';
{
{
return -1;
}
}
//## data type
switch (ch)
{
case 'f':
case 'g':
{
break;
}
case 'd':
{
long val = 0;
if (length == 'l')
else if (length == 'h')
else
break;
}
case 'x':
{
long val = 0;
if (length == 'l')
else if (length == 'h')
else
break;
}
case 's':
{
break;
}
default:
{
break;
}
}
}
return 1;
}
//#########################################################################
//# B A S I C I N P U T S T R E A M
//#########################################################################
/**
*
*/
{
closed = false;
}
/**
* Returns the number of bytes that can be read (or skipped over) from
* this input stream without blocking by the next caller of a method for
* this input stream.
*/
int BasicInputStream::available()
{
if (closed)
return 0;
}
/**
* Closes this input stream and releases any system resources
* associated with the stream.
*/
void BasicInputStream::close()
{
if (closed)
return;
closed = true;
}
/**
* Reads the next byte of data from the input stream. -1 if EOF
*/
int BasicInputStream::get()
{
if (closed)
return -1;
}
//#########################################################################
//# B A S I C O U T P U T S T R E A M
//#########################################################################
/**
*
*/
{
closed = false;
}
/**
* Closes this output stream and releases any system resources
* associated with this stream.
*/
void BasicOutputStream::close()
{
if (closed)
return;
destination.close();
closed = true;
}
/**
* Flushes this output stream and forces any buffered output
* bytes to be written out.
*/
void BasicOutputStream::flush()
{
if (closed)
return;
destination.flush();
}
/**
* Writes the specified byte to this output stream.
*/
{
if (closed)
return -1;
return -1;
return 1;
}
//#########################################################################
//# B A S I C R E A D E R
//#########################################################################
/**
*
*/
{
source = &sourceReader;
}
/**
* Returns the number of bytes that can be read (or skipped over) from
* this reader without blocking by the next caller of a method for
* this reader.
*/
int BasicReader::available()
{
if (source)
else
return 0;
}
/**
* Closes this reader and releases any system resources
* associated with the reader.
*/
void BasicReader::close()
{
if (source)
}
/**
* Reads the next byte of data from the reader.
*/
int BasicReader::get()
{
if (source)
else
return -1;
}
/**
* Reads a line of data from the reader.
*/
{
while (available() > 0)
{
if (ch == '\n')
break;
}
return str;
}
/**
* Reads a line of data from the reader.
*/
{
while (available() > 0)
{
if (uni_is_space(ch))
break;
}
return str;
}
{
char *end;
return false;
return true;
}
{
char *end;
return false;
return true;
}
{
char *end;
return false;
return true;
}
/**
*
*/
{
if (buf == "true")
val = true;
else
val = false;
return *this;
}
/**
*
*/
{
long ival;
return *this;
}
/**
*
*/
{
unsigned long ival;
return *this;
}
/**
*
*/
{
long ival;
return *this;
}
/**
*
*/
{
unsigned long ival;
return *this;
}
/**
*
*/
{
long ival;
return *this;
}
/**
*
*/
{
unsigned long ival;
return *this;
}
/**
*
*/
{
double ival;
return *this;
}
/**
*
*/
{
double ival;
return *this;
}
//#########################################################################
//# I N P U T S T R E A M R E A D E R
//#########################################################################
{
}
/**
* Close the underlying OutputStream
*/
void InputStreamReader::close()
{
inputStream.close();
}
/**
* Flush the underlying OutputStream
*/
int InputStreamReader::available()
{
return inputStream.available();
}
/**
* Overloaded to receive its bytes from an InputStream
* rather than a Reader
*/
int InputStreamReader::get()
{
//Do we need conversions here?
return ch;
}
//#########################################################################
//# S T D R E A D E R
//#########################################################################
/**
*
*/
{
inputStream = new StdInputStream();
}
/**
*
*/
{
delete inputStream;
}
/**
* Close the underlying OutputStream
*/
{
inputStream->close();
}
/**
* Flush the underlying OutputStream
*/
{
return inputStream->available();
}
/**
* Overloaded to receive its bytes from an InputStream
* rather than a Reader
*/
{
//Do we need conversions here?
return ch;
}
//#########################################################################
//# B A S I C W R I T E R
//#########################################################################
/**
*
*/
{
}
/**
* Closes this writer and releases any system resources
* associated with this writer.
*/
void BasicWriter::close()
{
if (destination)
destination->close();
}
/**
* Flushes this output stream and forces any buffered output
* bytes to be written out.
*/
void BasicWriter::flush()
{
if (destination)
destination->flush();
}
/**
* Writes the specified byte to this output writer.
*/
{
return 1;
return -1;
}
/**
* Provide printf()-like formatting
*/
/*
Writer &BasicWriter::printf(char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
//replace this wish vsnprintf()
vsnprintf(formatBuf, 2047, fmt, args);
va_end(args);
writeString(formatBuf);
return *this;
}
*/
{
return *this;
}
/**
* Writes the specified character to this output writer.
*/
{
return *this;
}
/**
* Writes the specified standard string to this output writer.
*/
{
return *this;
}
/**
*
*/
{
if (val)
writeString("true");
else
writeString("false");
return *this;
}
/**
*
*/
{
char buf[32];
return *this;
}
/**
*
*/
{
char buf[32];
return *this;
}
/**
*
*/
{
char buf[32];
return *this;
}
/**
*
*/
{
char buf[32];
return *this;
}
/**
*
*/
{
char buf[32];
return *this;
}
/**
*
*/
{
char buf[32];
return *this;
}
/**
*
*/
{
char buf[32];
return *this;
}
/**
*
*/
{
char buf[32];
return *this;
}
//#########################################################################
//# O U T P U T S T R E A M W R I T E R
//#########################################################################
{
}
/**
* Close the underlying OutputStream
*/
void OutputStreamWriter::close()
{
flush();
}
/**
* Flush the underlying OutputStream
*/
void OutputStreamWriter::flush()
{
}
/**
* Overloaded to redirect the output chars from the next Writer
* in the chain to an OutputStream instead.
*/
{
//Do we need conversions here?
return -1;
return 1;
}
//#########################################################################
//# S T D W R I T E R
//#########################################################################
/**
*
*/
{
outputStream = new StdOutputStream();
}
/**
*
*/
{
delete outputStream;
}
/**
* Close the underlying OutputStream
*/
{
flush();
outputStream->close();
}
/**
* Flush the underlying OutputStream
*/
{
outputStream->flush();
}
/**
* Overloaded to redirect the output chars from the next Writer
* in the chain to an OutputStream instead.
*/
{
//Do we need conversions here?
return -1;
return 1;
}
//###############################################
//# O P E R A T O R S
//###############################################
//# Normally these would be in the .h, but we
//# just want to be absolutely certain that these
//# are never multiply defined. Easy to maintain,
//# into the .h , and replace the {} with a ;
//###############################################
} //namespace io
} //namespace dom
} //namespace w3c
} //namespace org
//#########################################################################
//# E N D O F F I L E
//#########################################################################