- renamed Sed::IOStream to Sed::Stream
- renamed StdSed::StdStream to StdSed::FileStream - added StdSed::StringStream
This commit is contained in:
@ -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)
|
||||
{
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user