2009-05-21 04:44:47 +00:00
|
|
|
/*
|
2012-08-16 03:47:55 +00:00
|
|
|
* $Id$
|
2009-05-21 04:44:47 +00:00
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2014-11-19 14:42:24 +00:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2009-05-21 04:44:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <qse/sed/StdSed.hpp>
|
2016-04-28 15:29:28 +00:00
|
|
|
#include <qse/si/fio.h>
|
|
|
|
#include <qse/si/sio.h>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include "sed-prv.h"
|
|
|
|
#include "../cmn/mem-prv.h"
|
2009-05-21 04:44:47 +00:00
|
|
|
|
|
|
|
/////////////////////////////////
|
|
|
|
QSE_BEGIN_NAMESPACE(QSE)
|
|
|
|
/////////////////////////////////
|
|
|
|
|
2011-09-20 00:51:02 +00:00
|
|
|
static qse_sio_t* open_sio (StdSed::Stream::Data& io, const qse_char_t* file, int flags)
|
|
|
|
{
|
|
|
|
qse_sio_t* sio;
|
|
|
|
|
2019-06-25 01:21:00 +00:00
|
|
|
sio = qse_sio_open(qse_sed_getmmgr((StdSed::sed_t*)io), 0, file, flags);
|
2011-09-20 00:51:02 +00:00
|
|
|
if (sio == QSE_NULL)
|
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t ea;
|
2014-07-02 12:28:58 +00:00
|
|
|
ea.ptr = (StdSed::char_t*)file;
|
2011-09-20 00:51:02 +00:00
|
|
|
ea.len = qse_strlen (file);
|
2012-12-13 13:07:16 +00:00
|
|
|
((Sed*)io)->setError (QSE_SED_EIOFIL, &ea);
|
2011-09-20 00:51:02 +00:00
|
|
|
}
|
|
|
|
return sio;
|
|
|
|
}
|
|
|
|
|
|
|
|
static qse_sio_t* open_sio_std (StdSed::Stream::Data& io, qse_sio_std_t std, int flags)
|
|
|
|
{
|
|
|
|
qse_sio_t* sio;
|
2014-07-02 12:28:58 +00:00
|
|
|
static const StdSed::char_t* std_names[] =
|
2011-09-20 00:51:02 +00:00
|
|
|
{
|
|
|
|
QSE_T("stdin"),
|
|
|
|
QSE_T("stdout"),
|
|
|
|
QSE_T("stderr"),
|
|
|
|
};
|
|
|
|
|
2019-06-25 01:21:00 +00:00
|
|
|
sio = qse_sio_openstd(qse_sed_getmmgr((StdSed::sed_t*)io), 0, std, flags);
|
2011-09-20 00:51:02 +00:00
|
|
|
if (sio == QSE_NULL)
|
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t ea;
|
2014-07-02 12:28:58 +00:00
|
|
|
ea.ptr = (StdSed::char_t*)std_names[std];
|
2011-09-20 00:51:02 +00:00
|
|
|
ea.len = qse_strlen (std_names[std]);
|
2012-12-13 13:07:16 +00:00
|
|
|
((Sed*)io)->setError (QSE_SED_EIOFIL, &ea);
|
2011-09-20 00:51:02 +00:00
|
|
|
}
|
|
|
|
return sio;
|
|
|
|
}
|
|
|
|
|
2009-12-19 21:06:28 +00:00
|
|
|
int StdSed::FileStream::open (Data& io)
|
2009-05-27 07:29:47 +00:00
|
|
|
{
|
2009-12-19 06:34:42 +00:00
|
|
|
qse_sio_t* sio;
|
|
|
|
const char_t* ioname = io.getName();
|
2011-09-20 00:51:02 +00:00
|
|
|
int oflags;
|
|
|
|
|
|
|
|
if (io.getMode() == READ)
|
|
|
|
oflags = QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR;
|
|
|
|
else
|
2013-10-21 14:53:53 +00:00
|
|
|
oflags = QSE_SIO_WRITE | QSE_SIO_CREATE | QSE_SIO_TRUNCATE | QSE_SIO_IGNOREMBWCERR | QSE_SIO_LINEBREAK;
|
2009-12-19 06:34:42 +00:00
|
|
|
|
|
|
|
if (ioname == QSE_NULL)
|
|
|
|
{
|
|
|
|
//
|
2013-01-13 09:04:54 +00:00
|
|
|
// a normal console is indicated by a null name or a dash
|
2009-12-19 06:34:42 +00:00
|
|
|
//
|
|
|
|
if (io.getMode() == READ)
|
|
|
|
{
|
2013-01-13 09:04:54 +00:00
|
|
|
sio = (infile == QSE_NULL || (infile[0] == QSE_T('-') && infile[1] == QSE_T('\0')))?
|
2011-09-20 00:51:02 +00:00
|
|
|
open_sio_std (io, QSE_SIO_STDIN, oflags):
|
|
|
|
open_sio (io, infile, oflags);
|
2009-12-19 06:34:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-13 09:04:54 +00:00
|
|
|
sio = (outfile == QSE_NULL || (outfile[0] == QSE_T('-') && outfile[1] == QSE_T('\0')))?
|
2011-09-20 00:51:02 +00:00
|
|
|
open_sio_std (io, QSE_SIO_STDOUT, oflags):
|
|
|
|
open_sio (io, outfile, oflags);
|
2009-12-19 06:34:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// if ioname is not empty, it is a file name
|
|
|
|
//
|
2011-09-20 00:51:02 +00:00
|
|
|
sio = open_sio (io, ioname, oflags);
|
2009-12-19 06:34:42 +00:00
|
|
|
}
|
2011-09-20 00:51:02 +00:00
|
|
|
if (sio == QSE_NULL) return -1;
|
2009-12-19 06:34:42 +00:00
|
|
|
|
2012-01-15 15:25:28 +00:00
|
|
|
if (this->cmgr) qse_sio_setcmgr (sio, this->cmgr);
|
2009-12-19 06:34:42 +00:00
|
|
|
io.setHandle (sio);
|
2009-05-27 07:29:47 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-12-19 21:06:28 +00:00
|
|
|
int StdSed::FileStream::close (Data& io)
|
2009-05-27 07:29:47 +00:00
|
|
|
{
|
2011-09-20 00:51:02 +00:00
|
|
|
qse_sio_close ((qse_sio_t*)io.getHandle());
|
2009-05-27 07:29:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-19 21:06:28 +00:00
|
|
|
StdSed::ssize_t StdSed::FileStream::read (Data& io, char_t* buf, size_t len)
|
2009-05-28 01:01:33 +00:00
|
|
|
{
|
2011-12-18 17:40:37 +00:00
|
|
|
ssize_t n = qse_sio_getstrn ((qse_sio_t*)io.getHandle(), buf, len);
|
2009-12-19 06:34:42 +00:00
|
|
|
|
|
|
|
if (n == -1)
|
|
|
|
{
|
|
|
|
if (io.getName() == QSE_NULL && infile != QSE_NULL)
|
|
|
|
{
|
|
|
|
// if writing to outfile, set the error message
|
|
|
|
// explicitly. other cases are handled by
|
|
|
|
// the caller in sed.c.
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t ea;
|
2014-07-02 12:28:58 +00:00
|
|
|
ea.ptr = (char_t*)infile;
|
2009-12-19 06:34:42 +00:00
|
|
|
ea.len = qse_strlen (infile);
|
|
|
|
((Sed*)io)->setError (QSE_SED_EIOFIL, &ea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
2009-05-28 01:01:33 +00:00
|
|
|
}
|
|
|
|
|
2009-12-19 21:06:28 +00:00
|
|
|
StdSed::ssize_t StdSed::FileStream::write (Data& io, const char_t* buf, size_t len)
|
2009-05-27 07:29:47 +00:00
|
|
|
{
|
2011-12-18 17:40:37 +00:00
|
|
|
ssize_t n = qse_sio_putstrn ((qse_sio_t*)io.getHandle(), buf, len);
|
2009-12-19 06:34:42 +00:00
|
|
|
|
|
|
|
if (n == -1)
|
|
|
|
{
|
|
|
|
if (io.getName() == QSE_NULL && outfile != QSE_NULL)
|
|
|
|
{
|
|
|
|
// if writing to outfile, set the error message
|
|
|
|
// explicitly. other cases are handled by
|
|
|
|
// the caller in sed.c.
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t ea;
|
2014-07-02 12:28:58 +00:00
|
|
|
ea.ptr = (char_t*)outfile;
|
2009-12-19 06:34:42 +00:00
|
|
|
ea.len = qse_strlen (outfile);
|
|
|
|
((Sed*)io)->setError (QSE_SED_EIOFIL, &ea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
2009-05-27 07:29:47 +00:00
|
|
|
}
|
|
|
|
|
2009-12-19 21:06:28 +00:00
|
|
|
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)
|
|
|
|
{
|
2011-09-01 09:43:46 +00:00
|
|
|
if (qse_str_init (&out.buf, ((Sed*)io)->getMmgr(), 256) <= -1)
|
2009-12-19 21:06:28 +00:00
|
|
|
{
|
|
|
|
((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);
|
2011-12-18 17:40:37 +00:00
|
|
|
return qse_sio_getstrn ((qse_sio_t*)handle, buf, len);
|
2009-12-19 21:06:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2011-12-18 17:40:37 +00:00
|
|
|
return qse_sio_putstrn ((qse_sio_t*)handle, data, len);
|
2009-12-19 21:06:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
if (len) *len = 0;
|
2009-12-22 06:29:52 +00:00
|
|
|
return QSE_T("");
|
2009-12-19 21:06:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-21 04:44:47 +00:00
|
|
|
/////////////////////////////////
|
|
|
|
QSE_END_NAMESPACE(QSE)
|
|
|
|
/////////////////////////////////
|