2022-06-04 15:00:13 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
Copyright (c) 2006-2020 Chung, Hyung-Hwan. All rights reserved.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Hawk-Sed.hpp>
|
|
|
|
#include "sed-prv.h"
|
2025-10-01 21:17:51 +09:00
|
|
|
#include "hawk-prv.h"
|
2022-06-04 15:00:13 +00:00
|
|
|
|
|
|
|
/////////////////////////////////
|
|
|
|
HAWK_BEGIN_NAMESPACE(HAWK)
|
|
|
|
/////////////////////////////////
|
|
|
|
|
|
|
|
struct xtn_t
|
|
|
|
{
|
|
|
|
Sed* sed;
|
|
|
|
};
|
|
|
|
|
|
|
|
#if defined(HAWK_HAVE_INLINE)
|
|
|
|
static HAWK_INLINE xtn_t* GET_XTN(hawk_sed_t* sed) { return (xtn_t*)((hawk_uint8_t*)hawk_sed_getxtn(sed) - HAWK_SIZEOF(xtn_t)); }
|
|
|
|
#else
|
|
|
|
#define GET_XTN(sed) ((xtn_t*)((hawk_uint8_t*)hawk_sed_getxtn(sed) - HAWK_SIZEOF(xtn_t)))
|
|
|
|
#endif
|
|
|
|
|
2025-06-08 21:51:11 +09:00
|
|
|
Sed::Sed (Mmgr* mmgr): Mmged(mmgr), sed(HAWK_NULL), dflerrstr(HAWK_NULL)
|
2022-06-04 15:00:13 +00:00
|
|
|
{
|
2025-10-01 21:17:51 +09:00
|
|
|
HAWK_MEMSET (&this->errinf, 0, HAWK_SIZEOF(this->errinf));
|
|
|
|
this->errinf.num = HAWK_ENOERR;
|
2022-06-04 15:00:13 +00:00
|
|
|
this->_cmgr = hawk_get_cmgr_by_id(HAWK_CMGR_UTF8);
|
|
|
|
}
|
|
|
|
|
|
|
|
hawk_cmgr_t* Sed::getCmgr () const
|
|
|
|
{
|
|
|
|
return this->sed? hawk_sed_getcmgr(this->sed): this->_cmgr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sed::setCmgr (hawk_cmgr_t* cmgr)
|
|
|
|
{
|
|
|
|
if (this->sed) hawk_sed_setcmgr(this->sed, cmgr);
|
|
|
|
this->_cmgr = cmgr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Sed::open ()
|
|
|
|
{
|
2025-10-01 21:17:51 +09:00
|
|
|
this->sed = hawk_sed_open(this->getMmgr(), HAWK_SIZEOF(xtn_t), this->getCmgr(), &this->errinf);
|
|
|
|
if (HAWK_UNLIKELY(!this->sed)) return -1;
|
2022-06-04 15:00:13 +00:00
|
|
|
|
|
|
|
this->sed->_instsize += HAWK_SIZEOF(xtn_t);
|
|
|
|
|
|
|
|
xtn_t* xtn = GET_XTN(this->sed);
|
|
|
|
xtn->sed = this;
|
|
|
|
|
|
|
|
this->dflerrstr = hawk_sed_geterrstr(this->sed);
|
|
|
|
// TODO: revive this too when hawk_sed_seterrstr is revived()
|
|
|
|
// hawk_sed_seterrstr (this->sed, Sed::xerrstr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sed::close ()
|
|
|
|
{
|
|
|
|
if (this->sed)
|
|
|
|
{
|
2025-10-01 21:17:51 +09:00
|
|
|
hawk_sed_close(this->sed);
|
2022-06-04 15:00:13 +00:00
|
|
|
this->sed = HAWK_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Sed::compile (Stream& sstream)
|
|
|
|
{
|
2025-10-01 13:55:02 +09:00
|
|
|
HAWK_ASSERT(this->sed != HAWK_NULL);
|
2022-06-04 15:00:13 +00:00
|
|
|
this->sstream = &sstream;
|
2025-10-01 21:17:51 +09:00
|
|
|
int n = hawk_sed_comp(this->sed, Sed::sin);
|
|
|
|
if (n <= -1) this->retrieveError();
|
|
|
|
return n;
|
2022-06-04 15:00:13 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 05:33:46 +00:00
|
|
|
int Sed::execute (Stream& istream, Stream& ostream)
|
2022-06-04 15:00:13 +00:00
|
|
|
{
|
2025-10-01 13:55:02 +09:00
|
|
|
HAWK_ASSERT(this->sed != HAWK_NULL);
|
2022-06-09 05:33:46 +00:00
|
|
|
this->istream = &istream;
|
|
|
|
this->ostream = &ostream;
|
2025-10-01 21:17:51 +09:00
|
|
|
int n = hawk_sed_exec(this->sed, Sed::xin, Sed::xout);
|
|
|
|
if (n <= -1) this->retrieveError();
|
|
|
|
return n;
|
2022-06-04 15:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sed::halt ()
|
|
|
|
{
|
2025-10-01 13:55:02 +09:00
|
|
|
HAWK_ASSERT(this->sed != HAWK_NULL);
|
2025-10-01 21:17:51 +09:00
|
|
|
hawk_sed_halt(this->sed);
|
2022-06-04 15:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Sed::isHalt () const
|
|
|
|
{
|
2025-10-01 13:55:02 +09:00
|
|
|
HAWK_ASSERT(this->sed != HAWK_NULL);
|
2022-06-04 15:00:13 +00:00
|
|
|
return hawk_sed_ishalt(this->sed);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Sed::getTrait () const
|
|
|
|
{
|
2025-10-01 13:55:02 +09:00
|
|
|
HAWK_ASSERT(this->sed != HAWK_NULL);
|
2022-06-04 15:00:13 +00:00
|
|
|
int val;
|
2025-10-01 21:17:51 +09:00
|
|
|
hawk_sed_getopt(this->sed, HAWK_SED_TRAIT, &val);
|
2022-06-04 15:00:13 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sed::setTrait (int trait)
|
|
|
|
{
|
2025-10-01 13:55:02 +09:00
|
|
|
HAWK_ASSERT(this->sed != HAWK_NULL);
|
|
|
|
hawk_sed_setopt(this->sed, HAWK_SED_TRAIT, &trait);
|
2022-06-04 15:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const hawk_ooch_t* Sed::getErrorMessage () const
|
|
|
|
{
|
2025-10-01 21:17:51 +09:00
|
|
|
return this->errinf.msg;
|
2022-06-04 15:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const hawk_uch_t* Sed::getErrorMessageU () const
|
|
|
|
{
|
2025-10-01 21:17:51 +09:00
|
|
|
#if defined(HAWK_OOCH_IS_UCH)
|
|
|
|
return this->errinf.msg;
|
|
|
|
#else
|
|
|
|
hawk_oow_t wcslen, mbslen;
|
|
|
|
wcslen = HAWK_COUNTOF(this->xerrmsg);
|
|
|
|
hawk_conv_bcstr_to_ucstr_with_cmgr(this->errinf.msg, &mbslen, this->xerrmsg, &wcslen, this->getCmgr(), 1);
|
|
|
|
return this->xerrmsg;
|
|
|
|
#endif
|
2022-06-04 15:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const hawk_bch_t* Sed::getErrorMessageB () const
|
|
|
|
{
|
2025-10-01 21:17:51 +09:00
|
|
|
#if defined(HAWK_OOCH_IS_UCH)
|
|
|
|
hawk_oow_t wcslen, mbslen;
|
|
|
|
mbslen = HAWK_COUNTOF(this->xerrmsg);
|
|
|
|
hawk_conv_ucstr_to_bcstr_with_cmgr(this->errinf.msg, &wcslen, this->xerrmsg, &mbslen, this->getCmgr());
|
|
|
|
return this->xerrmsg;
|
|
|
|
#else
|
|
|
|
return this->errinf.msg;
|
|
|
|
#endif
|
2022-06-04 15:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hawk_loc_t Sed::getErrorLocation () const
|
|
|
|
{
|
2025-10-01 21:17:51 +09:00
|
|
|
return this->errinf.loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hawk_uch_t* Sed::getErrorLocationFileU () const
|
|
|
|
{
|
|
|
|
#if defined(HAWK_OOCH_IS_UCH)
|
|
|
|
return this->errinf.loc.file;
|
|
|
|
#else
|
|
|
|
if (!this->errinf.loc.file) return HAWK_NULL;
|
|
|
|
hawk_oow_t wcslen, mbslen;
|
|
|
|
wcslen = HAWK_COUNTOF(this->xerrlocfile);
|
|
|
|
hawk_conv_bcstr_to_ucstr_with_cmgr(this->errinf.loc.file, &mbslen, this->xerrlocfile, &wcslen, this->getCmgr(), 1);
|
|
|
|
return this->xerrlocfile;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
const hawk_bch_t* Sed::getErrorLocationFileB () const
|
|
|
|
{
|
|
|
|
#if defined(HAWK_OOCH_IS_UCH)
|
|
|
|
if (!this->errinf.loc.file) return HAWK_NULL;
|
|
|
|
hawk_oow_t wcslen, mbslen;
|
|
|
|
mbslen = HAWK_COUNTOF(this->xerrlocfile);
|
|
|
|
hawk_conv_ucstr_to_bcstr_with_cmgr(this->errinf.loc.file, &wcslen, this->xerrlocfile, &mbslen, this->getCmgr());
|
|
|
|
return this->xerrlocfile;
|
|
|
|
#else
|
|
|
|
return this->errinf.loc.file;
|
|
|
|
#endif
|
2022-06-04 15:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hawk_errnum_t Sed::getErrorNumber () const
|
|
|
|
{
|
2025-10-01 21:17:51 +09:00
|
|
|
return this->errinf.num;
|
2022-06-04 15:00:13 +00:00
|
|
|
}
|
|
|
|
|
2025-10-01 21:17:51 +09:00
|
|
|
void Sed::setError (hawk_errnum_t code, const hawk_oocs_t* args, const hawk_loc_t* loc)
|
2022-06-04 15:00:13 +00:00
|
|
|
{
|
2025-10-01 21:17:51 +09:00
|
|
|
if (this->sed)
|
|
|
|
{
|
|
|
|
hawk_sed_seterror(this->sed, loc, code, args);
|
|
|
|
this->retrieveError();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HAWK_MEMSET(&this->errinf, 0, HAWK_SIZEOF(this->errinf));
|
|
|
|
this->errinf.num = code;
|
|
|
|
if (loc) this->errinf.loc = *loc;
|
|
|
|
hawk_copy_oocstr(this->errinf.msg, HAWK_COUNTOF(this->errinf.msg), hawk_dfl_errstr(code));
|
|
|
|
}
|
2022-06-04 15:00:13 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 05:33:46 +00:00
|
|
|
void Sed::formatError (hawk_errnum_t code, const hawk_loc_t* loc, const hawk_bch_t* fmt, ...)
|
|
|
|
{
|
2025-10-01 21:17:51 +09:00
|
|
|
if (this->sed)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
hawk_sed_seterrbvfmt(this->sed, loc, code, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
this->retrieveError();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HAWK_MEMSET(&this->errinf, 0, HAWK_SIZEOF(this->errinf));
|
|
|
|
this->errinf.num = code;
|
|
|
|
if (loc) this->errinf.loc = *loc;
|
|
|
|
hawk_copy_oocstr(this->errinf.msg, HAWK_COUNTOF(this->errinf.msg), hawk_dfl_errstr(code));
|
|
|
|
}
|
2022-06-09 05:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sed::formatError (hawk_errnum_t code, const hawk_loc_t* loc, const hawk_uch_t* fmt, ...)
|
|
|
|
{
|
2025-10-01 21:17:51 +09:00
|
|
|
if (this->sed)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
hawk_sed_seterruvfmt(this->sed, loc, code, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
this->retrieveError();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HAWK_MEMSET(&this->errinf, 0, HAWK_SIZEOF(this->errinf));
|
|
|
|
this->errinf.num = code;
|
|
|
|
if (loc) this->errinf.loc = *loc;
|
|
|
|
hawk_copy_oocstr(this->errinf.msg, HAWK_COUNTOF(this->errinf.msg), hawk_dfl_errstr(code));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sed::clearError ()
|
|
|
|
{
|
|
|
|
HAWK_MEMSET(&this->errinf, 0, HAWK_SIZEOF(this->errinf));
|
|
|
|
this->errinf.num = HAWK_ENOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sed::retrieveError()
|
|
|
|
{
|
|
|
|
if (this->sed == HAWK_NULL)
|
|
|
|
{
|
|
|
|
this->clearError();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hawk_sed_geterrinf(this->sed, &this->errinf);
|
|
|
|
}
|
2022-06-09 05:33:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-04 15:00:13 +00:00
|
|
|
const hawk_ooch_t* Sed::getCompileId () const
|
|
|
|
{
|
|
|
|
return hawk_sed_getcompid(this->sed);
|
|
|
|
}
|
|
|
|
|
|
|
|
const hawk_ooch_t* Sed::setCompileId (const hawk_ooch_t* id)
|
|
|
|
{
|
|
|
|
return hawk_sed_setcompid(this->sed, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
hawk_oow_t Sed::getConsoleLine ()
|
|
|
|
{
|
2025-10-01 13:55:02 +09:00
|
|
|
HAWK_ASSERT(this->sed != HAWK_NULL);
|
2022-06-04 15:00:13 +00:00
|
|
|
return hawk_sed_getlinenum(this->sed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sed::setConsoleLine (hawk_oow_t num)
|
|
|
|
{
|
2025-10-01 13:55:02 +09:00
|
|
|
HAWK_ASSERT(this->sed != HAWK_NULL);
|
2022-06-04 15:00:13 +00:00
|
|
|
hawk_sed_setlinenum (this->sed, num);
|
|
|
|
}
|
|
|
|
|
|
|
|
hawk_ooi_t Sed::sin (hawk_sed_t* s, hawk_sed_io_cmd_t cmd, hawk_sed_io_arg_t* arg, hawk_ooch_t* buf, hawk_oow_t len)
|
|
|
|
{
|
|
|
|
xtn_t* xtn = GET_XTN(s);
|
|
|
|
|
|
|
|
Stream::Data iodata (xtn->sed, Stream::READ, arg);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case HAWK_SED_IO_OPEN:
|
|
|
|
return xtn->sed->sstream->open(iodata);
|
|
|
|
case HAWK_SED_IO_CLOSE:
|
|
|
|
return xtn->sed->sstream->close(iodata);
|
|
|
|
case HAWK_SED_IO_READ:
|
|
|
|
return xtn->sed->sstream->read(iodata, buf, len);
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hawk_ooi_t Sed::xin (hawk_sed_t* s, hawk_sed_io_cmd_t cmd, hawk_sed_io_arg_t* arg, hawk_ooch_t* buf, hawk_oow_t len)
|
|
|
|
{
|
|
|
|
xtn_t* xtn = GET_XTN(s);
|
|
|
|
|
|
|
|
Stream::Data iodata (xtn->sed, Stream::READ, arg);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case HAWK_SED_IO_OPEN:
|
2022-06-09 05:33:46 +00:00
|
|
|
return xtn->sed->istream->open(iodata);
|
2022-06-04 15:00:13 +00:00
|
|
|
case HAWK_SED_IO_CLOSE:
|
2022-06-09 05:33:46 +00:00
|
|
|
return xtn->sed->istream->close(iodata);
|
2022-06-04 15:00:13 +00:00
|
|
|
case HAWK_SED_IO_READ:
|
2022-06-09 05:33:46 +00:00
|
|
|
return xtn->sed->istream->read(iodata, buf, len);
|
2022-06-04 15:00:13 +00:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hawk_ooi_t Sed::xout (hawk_sed_t* s, hawk_sed_io_cmd_t cmd, hawk_sed_io_arg_t* arg, hawk_ooch_t* dat, hawk_oow_t len)
|
|
|
|
{
|
|
|
|
xtn_t* xtn = GET_XTN(s);
|
|
|
|
|
|
|
|
Stream::Data iodata (xtn->sed, Stream::WRITE, arg);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case HAWK_SED_IO_OPEN:
|
2022-06-09 05:33:46 +00:00
|
|
|
return xtn->sed->ostream->open(iodata);
|
2022-06-04 15:00:13 +00:00
|
|
|
case HAWK_SED_IO_CLOSE:
|
2022-06-09 05:33:46 +00:00
|
|
|
return xtn->sed->ostream->close(iodata);
|
2022-06-04 15:00:13 +00:00
|
|
|
case HAWK_SED_IO_WRITE:
|
2022-06-09 05:33:46 +00:00
|
|
|
return xtn->sed->ostream->write(iodata, dat, len);
|
2022-06-04 15:00:13 +00:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const hawk_ooch_t* Sed::getErrorString (hawk_errnum_t num) const
|
|
|
|
{
|
2025-10-01 13:55:02 +09:00
|
|
|
HAWK_ASSERT(this->dflerrstr != HAWK_NULL);
|
2022-06-04 15:00:13 +00:00
|
|
|
return this->dflerrstr(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
const hawk_ooch_t* Sed::xerrstr (hawk_sed_t* s, hawk_errnum_t num)
|
|
|
|
{
|
|
|
|
xtn_t* xtn = GET_XTN(s);
|
|
|
|
return xtn->sed->getErrorString(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////
|
|
|
|
HAWK_END_NAMESPACE(HAWK)
|
|
|
|
/////////////////////////////////
|