2009-05-20 23:26:47 +00:00
|
|
|
/*
|
2012-08-16 03:47:55 +00:00
|
|
|
* $Id$
|
2009-05-20 23:26: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-20 23:26:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <qse/sed/Sed.hpp>
|
2016-04-28 15:29:28 +00:00
|
|
|
#include <qse/si/sio.h>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include "../cmn/mem-prv.h"
|
|
|
|
#include "sed-prv.h"
|
2009-05-21 04:44:47 +00:00
|
|
|
|
2009-05-20 23:26:47 +00:00
|
|
|
/////////////////////////////////
|
|
|
|
QSE_BEGIN_NAMESPACE(QSE)
|
|
|
|
/////////////////////////////////
|
|
|
|
|
2019-06-25 01:21:00 +00:00
|
|
|
struct xtn_t
|
|
|
|
{
|
|
|
|
Sed* sed;
|
|
|
|
};
|
|
|
|
|
|
|
|
#if defined(QSE_HAVE_INLINE)
|
|
|
|
static QSE_INLINE xtn_t* GET_XTN(qse_sed_t* sed) { return (xtn_t*)((qse_uint8_t*)qse_sed_getxtn(sed) - QSE_SIZEOF(xtn_t)); }
|
|
|
|
#else
|
|
|
|
#define GET_XTN(sed) ((xtn_t*)((qse_uint8_t*)qse_sed_getxtn(sed) - QSE_SIZEOF(xtn_t)))
|
|
|
|
#endif
|
|
|
|
|
2009-07-16 04:43:31 +00:00
|
|
|
int Sed::open ()
|
2009-05-20 23:26:47 +00:00
|
|
|
{
|
2015-06-12 13:11:50 +00:00
|
|
|
qse_sed_errnum_t errnum;
|
2019-06-25 01:21:00 +00:00
|
|
|
this->sed = qse_sed_open(this->getMmgr(), QSE_SIZEOF(xtn_t), &errnum);
|
2015-06-12 13:11:50 +00:00
|
|
|
if (!this->sed)
|
|
|
|
{
|
|
|
|
this->setError (errnum);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-05-28 23:00:24 +00:00
|
|
|
|
2019-06-25 01:21:00 +00:00
|
|
|
this->sed->_instsize += QSE_SIZEOF(xtn_t);
|
|
|
|
|
|
|
|
xtn_t* xtn = GET_XTN(this->sed);
|
|
|
|
xtn->sed = this;
|
|
|
|
|
|
|
|
dflerrstr = qse_sed_geterrstr(this->sed);
|
2009-05-28 23:00:24 +00:00
|
|
|
qse_sed_seterrstr (sed, xerrstr);
|
|
|
|
|
2009-05-21 04:44:47 +00:00
|
|
|
return 0;
|
2009-05-20 23:26:47 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 04:43:31 +00:00
|
|
|
void Sed::close ()
|
2009-05-20 23:26:47 +00:00
|
|
|
{
|
2015-01-02 06:40:41 +00:00
|
|
|
if (sed)
|
2009-05-20 23:26:47 +00:00
|
|
|
{
|
|
|
|
qse_sed_close (sed);
|
2015-01-02 06:40:41 +00:00
|
|
|
sed = QSE_NULL;
|
2009-05-20 23:26:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
int Sed::compile (Stream& sstream)
|
2009-05-27 07:29:47 +00:00
|
|
|
{
|
|
|
|
QSE_ASSERT (sed != QSE_NULL);
|
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
this->sstream = &sstream;
|
|
|
|
return qse_sed_comp (sed, sin);
|
2009-05-21 04:44:47 +00:00
|
|
|
}
|
|
|
|
|
2009-12-19 21:06:28 +00:00
|
|
|
int Sed::execute (Stream& iostream)
|
2009-05-21 04:44:47 +00:00
|
|
|
{
|
2009-05-26 07:39:18 +00:00
|
|
|
QSE_ASSERT (sed != QSE_NULL);
|
2009-12-19 06:34:42 +00:00
|
|
|
|
|
|
|
this->iostream = &iostream;
|
2009-05-26 07:39:18 +00:00
|
|
|
return qse_sed_exec (sed, xin, xout);
|
|
|
|
}
|
|
|
|
|
2019-11-12 07:55:32 +00:00
|
|
|
void Sed::halt ()
|
2011-09-21 08:04:48 +00:00
|
|
|
{
|
|
|
|
QSE_ASSERT (sed != QSE_NULL);
|
2019-11-12 07:55:32 +00:00
|
|
|
qse_sed_halt (sed);
|
2011-09-21 08:04:48 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 07:55:32 +00:00
|
|
|
bool Sed::isHalt () const
|
2011-09-21 08:04:48 +00:00
|
|
|
{
|
|
|
|
QSE_ASSERT (sed != QSE_NULL);
|
2019-11-12 07:55:32 +00:00
|
|
|
return qse_sed_ishalt (sed);
|
2011-09-21 08:04:48 +00:00
|
|
|
}
|
|
|
|
|
2013-01-13 13:15:03 +00:00
|
|
|
int Sed::getTrait () const
|
2009-05-28 23:00:24 +00:00
|
|
|
{
|
|
|
|
QSE_ASSERT (sed != QSE_NULL);
|
2013-01-13 13:15:03 +00:00
|
|
|
int val;
|
|
|
|
qse_sed_getopt (sed, QSE_SED_TRAIT, &val);
|
|
|
|
return val;
|
2009-05-28 23:00:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-13 13:15:03 +00:00
|
|
|
void Sed::setTrait (int trait)
|
2009-05-28 23:00:24 +00:00
|
|
|
{
|
|
|
|
QSE_ASSERT (sed != QSE_NULL);
|
2013-01-13 13:15:03 +00:00
|
|
|
qse_sed_setopt (sed, QSE_SED_TRAIT, &trait);
|
2009-05-28 23:00:24 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 04:43:31 +00:00
|
|
|
const Sed::char_t* Sed::getErrorMessage () const
|
2009-05-28 01:01:33 +00:00
|
|
|
{
|
|
|
|
return (sed == QSE_NULL)? QSE_T(""): qse_sed_geterrmsg (sed);
|
|
|
|
}
|
|
|
|
|
2009-08-26 21:03:51 +00:00
|
|
|
Sed::loc_t Sed::getErrorLocation () const
|
2009-05-28 01:01:33 +00:00
|
|
|
{
|
2009-08-26 21:03:51 +00:00
|
|
|
if (sed == QSE_NULL)
|
|
|
|
{
|
|
|
|
loc_t loc;
|
2011-10-07 02:01:35 +00:00
|
|
|
loc.line = 0;
|
|
|
|
loc.colm = 0;
|
2009-08-26 21:03:51 +00:00
|
|
|
return loc;
|
|
|
|
}
|
|
|
|
return *qse_sed_geterrloc (sed);
|
2009-05-28 01:01:33 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 04:43:31 +00:00
|
|
|
Sed::errnum_t Sed::getErrorNumber () const
|
2009-05-28 01:01:33 +00:00
|
|
|
{
|
2009-05-28 07:55:17 +00:00
|
|
|
return (sed == QSE_NULL)? QSE_SED_ENOERR: qse_sed_geterrnum (sed);
|
2009-05-28 01:01:33 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 14:30:42 +00:00
|
|
|
void Sed::setError (errnum_t err, const cstr_t* args, const loc_t* loc)
|
2009-05-30 00:21:41 +00:00
|
|
|
{
|
|
|
|
QSE_ASSERT (sed != QSE_NULL);
|
2009-08-26 21:03:51 +00:00
|
|
|
qse_sed_seterror (sed, err, args, loc);
|
2009-05-30 00:21:41 +00:00
|
|
|
}
|
|
|
|
|
2011-10-07 02:01:35 +00:00
|
|
|
const Sed::char_t* Sed::getCompileId () const
|
|
|
|
{
|
|
|
|
return qse_sed_getcompid (this->sed);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Sed::char_t* Sed::setCompileId (const char_t* id)
|
|
|
|
{
|
|
|
|
return qse_sed_setcompid (this->sed, id);
|
|
|
|
}
|
|
|
|
|
2009-07-16 04:43:31 +00:00
|
|
|
Sed::size_t Sed::getConsoleLine ()
|
2009-05-29 07:00:56 +00:00
|
|
|
{
|
|
|
|
QSE_ASSERT (sed != QSE_NULL);
|
2011-10-07 02:01:35 +00:00
|
|
|
return qse_sed_getlinenum (this->sed);
|
2009-05-29 07:00:56 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 04:43:31 +00:00
|
|
|
void Sed::setConsoleLine (size_t num)
|
2009-05-29 07:00:56 +00:00
|
|
|
{
|
|
|
|
QSE_ASSERT (sed != QSE_NULL);
|
2011-10-07 02:01:35 +00:00
|
|
|
qse_sed_setlinenum (this->sed, num);
|
2009-05-29 07:00:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 01:21:00 +00:00
|
|
|
Sed::ssize_t Sed::sin (sed_t* s, io_cmd_t cmd, io_arg_t* arg, char_t* buf, size_t len)
|
2011-10-03 01:25:23 +00:00
|
|
|
{
|
2019-06-25 01:21:00 +00:00
|
|
|
xtn_t* xtn = GET_XTN(s);
|
2011-10-03 01:25:23 +00:00
|
|
|
|
2019-06-25 01:21:00 +00:00
|
|
|
Stream::Data iodata (xtn->sed, Stream::READ, arg);
|
2011-10-03 01:25:23 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case QSE_SED_IO_OPEN:
|
2019-06-25 01:21:00 +00:00
|
|
|
return xtn->sed->sstream->open (iodata);
|
2011-10-03 01:25:23 +00:00
|
|
|
case QSE_SED_IO_CLOSE:
|
2019-06-25 01:21:00 +00:00
|
|
|
return xtn->sed->sstream->close (iodata);
|
2011-10-03 01:25:23 +00:00
|
|
|
case QSE_SED_IO_READ:
|
2019-06-25 01:21:00 +00:00
|
|
|
return xtn->sed->sstream->read (iodata, buf, len);
|
2011-10-03 01:25:23 +00:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 01:21:00 +00:00
|
|
|
Sed::ssize_t Sed::xin (sed_t* s, io_cmd_t cmd, io_arg_t* arg, char_t* buf, size_t len)
|
2009-05-26 07:39:18 +00:00
|
|
|
{
|
2019-06-25 01:21:00 +00:00
|
|
|
xtn_t* xtn = GET_XTN(s);
|
2009-05-26 07:39:18 +00:00
|
|
|
|
2019-06-25 01:21:00 +00:00
|
|
|
Stream::Data iodata (xtn->sed, Stream::READ, arg);
|
2009-05-28 01:01:33 +00:00
|
|
|
|
2009-12-19 06:34:42 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
switch (cmd)
|
2009-05-28 01:01:33 +00:00
|
|
|
{
|
2009-12-19 06:34:42 +00:00
|
|
|
case QSE_SED_IO_OPEN:
|
2019-06-25 01:21:00 +00:00
|
|
|
return xtn->sed->iostream->open (iodata);
|
2009-12-19 06:34:42 +00:00
|
|
|
case QSE_SED_IO_CLOSE:
|
2019-06-25 01:21:00 +00:00
|
|
|
return xtn->sed->iostream->close (iodata);
|
2009-12-19 06:34:42 +00:00
|
|
|
case QSE_SED_IO_READ:
|
2019-06-25 01:21:00 +00:00
|
|
|
return xtn->sed->iostream->read (iodata, buf, len);
|
2009-12-19 06:34:42 +00:00
|
|
|
default:
|
|
|
|
return -1;
|
2009-05-26 07:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-19 06:34:42 +00:00
|
|
|
catch (...)
|
2009-05-26 07:39:18 +00:00
|
|
|
{
|
2009-12-19 06:34:42 +00:00
|
|
|
return -1;
|
2009-05-26 07:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 01:21:00 +00:00
|
|
|
Sed::ssize_t Sed::xout (sed_t* s, io_cmd_t cmd, io_arg_t* arg, char_t* dat, size_t len)
|
2009-05-26 07:39:18 +00:00
|
|
|
{
|
2019-06-25 01:21:00 +00:00
|
|
|
xtn_t* xtn = GET_XTN(s);
|
2009-05-26 07:39:18 +00:00
|
|
|
|
2019-06-25 01:21:00 +00:00
|
|
|
Stream::Data iodata (xtn->sed, Stream::WRITE, arg);
|
2009-05-28 01:01:33 +00:00
|
|
|
|
2009-12-19 06:34:42 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
switch (cmd)
|
2009-05-26 07:39:18 +00:00
|
|
|
{
|
2009-12-19 06:34:42 +00:00
|
|
|
case QSE_SED_IO_OPEN:
|
2019-06-25 01:21:00 +00:00
|
|
|
return xtn->sed->iostream->open (iodata);
|
2009-12-19 06:34:42 +00:00
|
|
|
case QSE_SED_IO_CLOSE:
|
2019-06-25 01:21:00 +00:00
|
|
|
return xtn->sed->iostream->close (iodata);
|
2009-12-19 06:34:42 +00:00
|
|
|
case QSE_SED_IO_WRITE:
|
2019-06-25 01:21:00 +00:00
|
|
|
return xtn->sed->iostream->write (iodata, dat, len);
|
2009-12-19 06:34:42 +00:00
|
|
|
default:
|
|
|
|
return -1;
|
2009-05-26 07:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-19 06:34:42 +00:00
|
|
|
catch (...)
|
2009-05-26 07:39:18 +00:00
|
|
|
{
|
2009-12-19 06:34:42 +00:00
|
|
|
return -1;
|
2009-05-26 07:39:18 +00:00
|
|
|
}
|
2009-05-21 04:44:47 +00:00
|
|
|
}
|
2009-05-20 23:26:47 +00:00
|
|
|
|
2009-06-02 03:34:34 +00:00
|
|
|
const Sed::char_t* Sed::getErrorString (errnum_t num) const
|
2009-05-28 23:00:24 +00:00
|
|
|
{
|
2019-06-25 01:21:00 +00:00
|
|
|
QSE_ASSERT (this->dflerrstr != QSE_NULL);
|
|
|
|
return this->dflerrstr (sed, num);
|
2009-05-28 23:00:24 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 01:21:00 +00:00
|
|
|
const Sed::char_t* Sed::xerrstr (sed_t* s, errnum_t num)
|
2009-05-28 23:00:24 +00:00
|
|
|
{
|
2019-06-25 01:21:00 +00:00
|
|
|
xtn_t* xtn = GET_XTN(s);
|
2009-05-28 23:00:24 +00:00
|
|
|
try
|
|
|
|
{
|
2019-06-25 01:21:00 +00:00
|
|
|
return xtn->sed->getErrorString (num);
|
2009-05-28 23:00:24 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2019-06-25 01:21:00 +00:00
|
|
|
return xtn->sed->dflerrstr (s, num);
|
2009-05-28 23:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-20 23:26:47 +00:00
|
|
|
/////////////////////////////////
|
2009-05-21 04:44:47 +00:00
|
|
|
QSE_END_NAMESPACE(QSE)
|
2009-05-20 23:26:47 +00:00
|
|
|
/////////////////////////////////
|
|
|
|
|