- renamed Sed::IOStream to Sed::Stream

- renamed StdSed::StdStream to StdSed::FileStream
- added StdSed::StringStream
This commit is contained in:
hyung-hwan 2009-12-19 21:06:28 +00:00
parent de7082d0d0
commit 92cbbbcec1
6 changed files with 192 additions and 131 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: Sed.hpp 318 2009-12-18 12:34:42Z hyunghwan.chung $
* $Id: Sed.hpp 319 2009-12-19 03:06:28Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -56,10 +56,10 @@ public:
typedef qse_sed_depth_t depth_t;
///
/// The IOStream class is a base class for I/O operation during
/// The Stream class is a base class for I/O operation during
/// execution.
///
class IOStream: public Types
class Stream: public Types
{
public:
enum Mode
@ -116,8 +116,8 @@ public:
io_arg_t* arg;
};
IOStream () {}
virtual ~IOStream () {}
Stream () {}
virtual ~Stream () {}
virtual int open (Data& io) = 0;
virtual int close (Data& io) = 0;
@ -125,8 +125,8 @@ public:
virtual ssize_t write (Data& io, const char_t* buf, size_t len) = 0;
private:
IOStream (const IOStream&);
IOStream& operator= (const IOStream&);
Stream (const Stream&);
Stream& operator= (const Stream&);
};
///
@ -179,7 +179,7 @@ public:
/// streams defined through I/O handlers
/// @return 0 on success, -1 on failure
///
int execute (IOStream& iostream);
int execute (Stream& iostream);
///
/// The getOption() function gets the current options.
@ -278,7 +278,7 @@ protected:
/// default error formatting string getter
errstr_t dflerrstr;
/// I/O stream to read data from and write output to.
IOStream* iostream;
Stream* iostream;
private:

View File

@ -1,5 +1,5 @@
/*
* $Id: StdSed.hpp 318 2009-12-18 12:34:42Z hyunghwan.chung $
* $Id: StdSed.hpp 319 2009-12-19 03:06:28Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -23,6 +23,7 @@
#include <qse/sed/Sed.hpp>
#include <qse/cmn/StdMmgr.hpp>
#include <qse/cmn/str.h>
/** @file
* Standard Stream Editor
@ -42,11 +43,11 @@ class StdSed: public Sed
public:
StdSed (Mmgr* mmgr = &StdMmgr::DFL): Sed (mmgr) {}
class StdStream: public IOStream
class FileStream: public Stream
{
public:
StdStream (const char_t* infile = QSE_NULL,
const char_t* outfile = QSE_NULL):
FileStream (const char_t* infile = QSE_NULL,
const char_t* outfile = QSE_NULL):
infile(infile), outfile(outfile)
{
}
@ -60,6 +61,36 @@ public:
const char_t* infile;
const char_t* outfile;
};
class StringStream: public Stream
{
public:
StringStream (const char_t* in);
StringStream (const char_t* in, size_t len);
~StringStream ();
int open (Data& io);
int close (Data& io);
ssize_t read (Data& io, char_t* buf, size_t len);
ssize_t write (Data& io, const char_t* buf, size_t len);
const char_t* getInput (size_t* len = QSE_NULL) const;
const char_t* getOutput (size_t* len = QSE_NULL) const;
protected:
struct
{
const char_t* ptr;
const char_t* end;
const char_t* cur;
} in;
struct
{
bool inited;
qse_str_t buf;
} out;
};
};
/**

View File

@ -1,5 +1,5 @@
/*
* $Id: Sed.cpp 318 2009-12-18 12:34:42Z hyunghwan.chung $
* $Id: Sed.cpp 319 2009-12-19 03:06:28Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -60,7 +60,7 @@ int Sed::compile (const char_t* sptr, size_t slen)
return qse_sed_comp (sed, sptr, slen);
}
int Sed::execute (IOStream& iostream)
int Sed::execute (Stream& iostream)
{
QSE_ASSERT (sed != QSE_NULL);
@ -136,7 +136,7 @@ Sed::ssize_t Sed::xin (
{
Sed* sed = *(Sed**)QSE_XTN(s);
IOStream::Data iodata (sed, IOStream::READ, arg);
Stream::Data iodata (sed, Stream::READ, arg);
try
{
@ -163,7 +163,7 @@ Sed::ssize_t Sed::xout (
{
Sed* sed = *(Sed**)QSE_XTN(s);
IOStream::Data iodata (sed, IOStream::WRITE, arg);
Stream::Data iodata (sed, Stream::WRITE, arg);
try
{

View File

@ -1,5 +1,5 @@
/*
* $Id: StdSed.cpp 318 2009-12-18 12:34:42Z hyunghwan.chung $
* $Id: StdSed.cpp 319 2009-12-19 03:06:28Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -28,7 +28,7 @@
QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
int StdSed::StdStream::open (Data& io)
int StdSed::FileStream::open (Data& io)
{
qse_sio_t* sio;
const char_t* ioname = io.getName();
@ -114,7 +114,7 @@ int StdSed::StdStream::open (Data& io)
return 1;
}
int StdSed::StdStream::close (Data& io)
int StdSed::FileStream::close (Data& io)
{
qse_sio_t* sio = (qse_sio_t*)io.getHandle();
@ -125,7 +125,7 @@ int StdSed::StdStream::close (Data& io)
return 0;
}
StdSed::ssize_t StdSed::StdStream::read (Data& io, char_t* buf, size_t len)
StdSed::ssize_t StdSed::FileStream::read (Data& io, char_t* buf, size_t len)
{
ssize_t n = qse_sio_getsn ((qse_sio_t*)io.getHandle(), buf, len);
@ -146,7 +146,7 @@ StdSed::ssize_t StdSed::StdStream::read (Data& io, char_t* buf, size_t len)
return n;
}
StdSed::ssize_t StdSed::StdStream::write (Data& io, const char_t* buf, size_t len)
StdSed::ssize_t StdSed::FileStream::write (Data& io, const char_t* buf, size_t len)
{
ssize_t n = qse_sio_putsn ((qse_sio_t*)io.getHandle(), buf, len);
@ -167,6 +167,142 @@ StdSed::ssize_t StdSed::StdStream::write (Data& io, const char_t* buf, size_t le
return n;
}
StdSed::StringStream::StringStream (const char_t* in)
{
this->in.ptr = in;
this->in.end = in + qse_strlen(in);
this->out.inited = false;
}
StdSed::StringStream::StringStream (const char_t* in, size_t len)
{
this->in.ptr = in;
this->in.end = in + len;
this->out.inited = false;
}
StdSed::StringStream::~StringStream ()
{
if (out.inited) qse_str_fini (&out.buf);
}
int StdSed::StringStream::open (Data& io)
{
const char_t* ioname = io.getName ();
if (ioname == QSE_NULL)
{
// open a main data stream
if (io.getMode() == READ)
{
in.cur = in.ptr;
io.setHandle ((void*)in.ptr);
}
else
{
if (!out.inited)
{
if (qse_str_init (&out.buf, ((Sed*)io)->getMmgr(), 256) == QSE_NULL)
{
((Sed*)io)->setError (QSE_SED_ENOMEM);
return -1;
}
out.inited = true;
}
qse_str_clear (&out.buf);
io.setHandle (&out.buf);
}
}
else
{
// open files for a r or w command
qse_sio_t* sio;
int mode = (io.getMode() == READ)?
QSE_SIO_READ:
(QSE_SIO_WRITE|QSE_SIO_CREATE|QSE_SIO_TRUNCATE);
sio = qse_sio_open (((Sed*)io)->getMmgr(), 0, ioname, mode);
if (sio == QSE_NULL) return -1;
io.setHandle (sio);
}
return 1;
}
int StdSed::StringStream::close (Data& io)
{
const void* handle = io.getHandle();
if (handle != in.ptr && handle != &out.buf)
qse_sio_close ((qse_sio_t*)handle);
return 0;
}
StdSed::ssize_t StdSed::StringStream::read (Data& io, char_t* buf, size_t len)
{
const void* handle = io.getHandle();
if (len == (size_t)-1) len--; // shrink buffer if too long
if (handle == in.ptr)
{
size_t n = 0;
while (in.cur < in.end && n < len)
buf[n++] = *in.cur++;
return (ssize_t)n;
}
else
{
QSE_ASSERT (handle != &out.buf);
return qse_sio_getsn ((qse_sio_t*)handle, buf, len);
}
}
StdSed::ssize_t StdSed::StringStream::write (Data& io, const char_t* data, size_t len)
{
const void* handle = io.getHandle();
if (len == (qse_size_t)-1) len--; // shrink data if too long
if (handle == &out.buf)
{
if (qse_str_ncat (&out.buf, data, len) == (qse_size_t)-1)
{
((Sed*)io)->setError (QSE_SED_ENOMEM);
return -1;
}
return len;
}
else
{
QSE_ASSERT (handle != in.ptr);
return qse_sio_putsn ((qse_sio_t*)handle, data, len);
}
}
const StdSed::char_t* StdSed::StringStream::getInput (size_t* len) const
{
if (len) *len = in.end - in.ptr;
return in.ptr;
}
const StdSed::char_t* StdSed::StringStream::getOutput (size_t* len) const
{
if (out.inited)
{
if (len) *len = QSE_STR_LEN(&out.buf);
return QSE_STR_PTR(&out.buf);
}
else
{
static char_t empty[] = QSE_T("");
if (len) *len = 0;
return empty;
}
}
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////

View File

@ -54,7 +54,7 @@ int sed_main (int argc, qse_char_t* argv[])
qse_char_t* infile = (argc >= 3)? argv[2]: QSE_NULL;
qse_char_t* outfile = (argc >= 4)? argv[3]: QSE_NULL;
QSE::StdSed::StdStream stream (infile, outfile);
QSE::StdSed::FileStream stream (infile, outfile);
if (sed.execute (stream) == -1)
{

View File

@ -21,8 +21,6 @@
#include <qse/sed/StdSed.hpp>
#include <qse/cmn/main.h>
#include <qse/cmn/sio.h>
#include <string>
#include <iostream>
#ifdef QSE_CHAR_IS_MCHAR
@ -31,116 +29,12 @@
# define xcout std::wcout
#endif
//
// The StringStream class implements a data I/O stream over strings.
//
class StringStream: public QSE::StdSed::IOStream
{
public:
StringStream (const char_t* in) { this->in.ptr = in; }
int open (Data& io)
{
const char_t* ioname = io.getName ();
if (ioname == QSE_NULL)
{
// open a main data stream
if (io.getMode() == READ)
{
in.cur = in.ptr;
io.setHandle ((void*)in.ptr);
}
else
{
out.erase ();
io.setHandle (&out);
}
}
else
{
// open files for a r or w command
qse_sio_t* sio;
int mode = (io.getMode() == READ)?
QSE_SIO_READ:
(QSE_SIO_WRITE|QSE_SIO_CREATE|QSE_SIO_TRUNCATE);
sio = qse_sio_open (((QSE::Sed*)io)->getMmgr(), 0, ioname, mode);
if (sio == QSE_NULL) return -1;
io.setHandle (sio);
}
return 1;
}
int close (Data& io)
{
const void* handle = io.getHandle();
if (handle != in.ptr && handle != &out)
qse_sio_close ((qse_sio_t*)handle);
return 0;
}
ssize_t read (Data& io, char_t* buf, size_t len)
{
const void* handle = io.getHandle();
if (handle == in.ptr)
{
ssize_t n = qse_strxcpy (buf, len, in.cur);
in.cur += n; return n;
}
else
{
QSE_ASSERT (handle != &out);
return qse_sio_getsn ((qse_sio_t*)handle, buf, len);
}
}
ssize_t write (Data& io, const char_t* buf, size_t len)
{
const void* handle = io.getHandle();
if (handle == &out)
{
try
{
out.append (buf, len);
return len;
}
catch (...)
{
((QSE::Sed*)io)->setError (QSE_SED_ENOMEM);
throw;
}
}
else
{
QSE_ASSERT (handle != in.ptr);
return qse_sio_putsn ((qse_sio_t*)handle, buf, len);
}
}
const char_t* getInput () const { return in.ptr; }
const char_t* getOutput () const { return out.c_str (); }
protected:
struct
{
const char_t* ptr;
const char_t* cur;
} in;
std::basic_string<char_t> out;
};
//
// The MySed class simplifies QSE::StdSed by utilizing exception handling.
//
class MySed: protected QSE::StdSed
{
public:
class Error
{
public:
@ -159,7 +53,7 @@ public:
throw Error (getErrorMessage());
}
void execute (IOStream& stream)
void execute (Stream& stream)
{
if (QSE::StdSed::execute (stream) <= -1)
throw Error (getErrorMessage());
@ -174,7 +68,7 @@ int sed_main (int argc, qse_char_t* argv[])
sed.compile (QSE_T("y/ABC/abc/;s/abc/def/g"));
StringStream stream (QSE_T("ABCDEFabcdef"));
QSE::StdSed::StringStream stream (QSE_T("ABCDEFabcdef"));
sed.execute (stream);
xcout << QSE_T("INPUT: ") << stream.getInput() << std::endl;