simplified HawkStd::SourceFile functions

This commit is contained in:
hyung-hwan 2021-08-17 19:26:41 +00:00
parent 460f3b218c
commit ccc32d8118
5 changed files with 39 additions and 79 deletions

View File

@ -1668,8 +1668,8 @@ Hawk::Run* Hawk::parse (Source& in, Source& out)
source_writer = (&out == &Source::NONE)? HAWK_NULL: &out;
hawk_sio_cbs_t sio;
sio.in = readSource;
sio.out = (source_writer == HAWK_NULL)? HAWK_NULL: writeSource;
sio.in = Hawk::readSource;
sio.out = (source_writer == HAWK_NULL)? HAWK_NULL: Hawk::writeSource;
int n = hawk_parse(this->hawk, &sio);
if (n <= -1)

View File

@ -1240,54 +1240,20 @@ void* HawkStd::modgetsym (void* handle, const hawk_ooch_t* name)
return s;
}
HawkStd::SourceFile::~SourceFile ()
{
if (this->_hawk)
{
HAWK_ASSERT (this->name != HAWK_NULL);
hawk_freemem (this->_hawk, this->name);
}
}
int HawkStd::SourceFile::open (Data& io)
{
hawk_sio_t* sio;
if (io.isMaster())
{
bool hawk_set_here = false;
hawk_ooch_t* xpath;
// open the main source file.
if (!this->name)
{
this->_hawk = (hawk_t*)io;
if (this->_type == NAME_UCH)
{
#if defined(HAWK_OOCH_IS_UCH)
this->name = hawk_dupucstr(this->_hawk, (hawk_uch_t*)this->_name, HAWK_NULL);
#else
this->name = hawk_duputobcstr(this->_hawk, (hawk_uch_t*)this->_name, HAWK_NULL);
#endif
}
else
{
HAWK_ASSERT (this->_type == NAME_BCH);
#if defined(HAWK_OOCH_IS_UCH)
this->name = hawk_dupbtoucstr(this->_hawk, (hawk_bch_t*)this->_name, HAWK_NULL, 0);
#else
this->name = hawk_dupbcstr(this->_hawk, (hawk_bch_t*)this->_name, HAWK_NULL);
#endif
}
if (!this->name)
{
this->_hawk = HAWK_NULL;
return -1;
}
xpath = (this->_type == NAME_UCH)?
hawk_addsionamewithuchars((hawk_t*)io, (const hawk_uch_t*)this->_name, hawk_count_ucstr((const hawk_uch_t*)this->_name)):
hawk_addsionamewithbchars((hawk_t*)io, (const hawk_bch_t*)this->_name, hawk_count_bcstr((const hawk_bch_t*)this->_name));
if (HAWK_UNLIKELY(!xpath)) return -1;
hawk_set_here = true;
}
if (this->name[0] == HAWK_T('-') && this->name[1] == HAWK_T('\0'))
if (xpath[0] == HAWK_T('-') && xpath[1] == HAWK_T('\0'))
{
if (io.getMode() == READ)
sio = open_sio_std(
@ -1298,38 +1264,22 @@ int HawkStd::SourceFile::open (Data& io)
io, HAWK_NULL, HAWK_SIO_STDOUT,
HAWK_SIO_WRITE | HAWK_SIO_CREATE |
HAWK_SIO_TRUNCATE | HAWK_SIO_IGNOREECERR | HAWK_SIO_LINEBREAK);
if (sio == HAWK_NULL)
{
if (hawk_set_here)
{
hawk_freemem (this->_hawk, this->name);
this->_hawk = HAWK_NULL;
}
return -1;
}
if (sio == HAWK_NULL) return -1;
}
else
{
sio = open_sio (
io, HAWK_NULL, this->name,
sio = open_sio(
io, HAWK_NULL, xpath,
(io.getMode() == READ?
(HAWK_SIO_READ | HAWK_SIO_IGNOREECERR | HAWK_SIO_KEEPPATH):
(HAWK_SIO_WRITE | HAWK_SIO_CREATE | HAWK_SIO_TRUNCATE | HAWK_SIO_IGNOREECERR))
);
if (sio == HAWK_NULL)
{
if (hawk_set_here)
{
hawk_freemem (this->_hawk, this->name);
this->_hawk = HAWK_NULL;
}
return -1;
}
if (sio == HAWK_NULL) return -1;
}
if (this->cmgr) hawk_sio_setcmgr (sio, this->cmgr);
io.setName (this->name);
io.setPath (this->name);
io.setName (xpath);
io.setPath (xpath); // let parser use this path, especially upon an error
}
else
{
@ -1526,7 +1476,7 @@ int HawkStd::SourceString::open (Data& io)
io.setPath (xpath);
io.setHandle (sio);
//if (this->cmgr) hawk_sio_setcmgr (sio, this->cmgr);
if (this->cmgr) hawk_sio_setcmgr (sio, this->cmgr);
}
return 1;
@ -1534,7 +1484,19 @@ int HawkStd::SourceString::open (Data& io)
int HawkStd::SourceString::close (Data& io)
{
if (!io.isMaster()) hawk_sio_close ((hawk_sio_t*)io.getHandle());
if (io.isMaster())
{
HAWK_ASSERT (this->_hawk != HAWK_NULL);
HAWK_ASSERT (this->str != HAWK_NULL);
hawk_freemem (this->_hawk, this->str);
this->str = HAWK_NULL;
this->ptr = HAWK_NULL;
this->_hawk = HAWK_NULL;
}
else
{
hawk_sio_close ((hawk_sio_t*)io.getHandle());
}
return 0;
}
@ -1543,12 +1505,12 @@ hawk_ooi_t HawkStd::SourceString::read (Data& io, hawk_ooch_t* buf, hawk_oow_t l
if (io.isMaster())
{
hawk_oow_t n = 0;
while (*ptr != HAWK_T('\0') && n < len) buf[n++] = *ptr++;
while (*this->ptr != '\0' && n < len) buf[n++] = *this->ptr++;
return n;
}
else
{
return hawk_sio_getoochars ((hawk_sio_t*)io.getHandle(), buf, len);
return hawk_sio_getoochars((hawk_sio_t*)io.getHandle(), buf, len);
}
}
@ -1562,7 +1524,7 @@ hawk_ooi_t HawkStd::SourceString::write (Data& io, const hawk_ooch_t* buf, hawk_
{
// in fact, this block will never be reached as
// there is no included file concept for deparsing
return hawk_sio_putoochars ((hawk_sio_t*)io.getHandle(), buf, len);
return hawk_sio_putoochars((hawk_sio_t*)io.getHandle(), buf, len);
}
}

View File

@ -65,17 +65,17 @@ public:
class HAWK_EXPORT SourceFile: public Source
{
public:
SourceFile (const hawk_uch_t* name, hawk_cmgr_t* cmgr = HAWK_NULL): _type(NAME_UCH), _name(name), _hawk(HAWK_NULL), name(HAWK_NULL), cmgr (cmgr)
SourceFile (const hawk_uch_t* name, hawk_cmgr_t* cmgr = HAWK_NULL): _type(NAME_UCH), _name(name), cmgr(cmgr)
{
dir.ptr = HAWK_NULL; dir.len = 0;
}
SourceFile (const hawk_bch_t* name, hawk_cmgr_t* cmgr = HAWK_NULL): _type(NAME_BCH), _name(name), _hawk(HAWK_NULL), name(HAWK_NULL), cmgr (cmgr)
SourceFile (const hawk_bch_t* name, hawk_cmgr_t* cmgr = HAWK_NULL): _type(NAME_BCH), _name(name), cmgr(cmgr)
{
dir.ptr = HAWK_NULL; dir.len = 0;
}
~SourceFile ();
~SourceFile () {};
int open (Data& io);
int close (Data& io);
@ -89,9 +89,6 @@ public:
NAME_BCH
} _type;
const void* _name;
hawk_t* _hawk;
hawk_ooch_t* name;
hawk_oocs_t dir;
hawk_cmgr_t* cmgr;
};
@ -103,8 +100,8 @@ public:
class HAWK_EXPORT SourceString: public Source
{
public:
SourceString (const hawk_uch_t* str): _type(STR_UCH), _str(str), _hawk(HAWK_NULL), str(HAWK_NULL), ptr(HAWK_NULL) {}
SourceString (const hawk_bch_t* str): _type(STR_BCH), _str(str), _hawk(HAWK_NULL), str(HAWK_NULL), ptr(HAWK_NULL) {}
SourceString (const hawk_uch_t* str, hawk_cmgr_t* cmgr = HAWK_NULL): _type(STR_UCH), _str(str), _hawk(HAWK_NULL), str(HAWK_NULL), ptr(HAWK_NULL), cmgr(cmgr) {}
SourceString (const hawk_bch_t* str, hawk_cmgr_t* cmgr = HAWK_NULL): _type(STR_BCH), _str(str), _hawk(HAWK_NULL), str(HAWK_NULL), ptr(HAWK_NULL), cmgr(cmgr) {}
~SourceString ();
int open (Data& io);
@ -123,6 +120,7 @@ public:
hawk_ooch_t* str;
const hawk_ooch_t* ptr;
hawk_cmgr_t* cmgr; // for reading included files
};
HawkStd (Mmgr* mmgr = HAWK_NULL): Hawk(mmgr), cmgrtab_inited(false), stdmod_up(false), console_cmgr(HAWK_NULL)

View File

@ -1266,7 +1266,7 @@ static int parse_progunit (hawk_t* hawk)
hawk->parse.id.block = PARSE_PATTERN;
eloc = hawk->tok.loc;
ptn = parse_expr_withdc (hawk, &eloc);
ptn = parse_expr_withdc(hawk, &eloc);
if (ptn == HAWK_NULL) return -1;
HAWK_ASSERT (ptn->next == HAWK_NULL);

View File

@ -1110,7 +1110,7 @@ static int open_parsestd (hawk_t* hawk, hawk_sio_arg_t* arg, xtn_t* xtn, hawk_oo
else
{
path = hawk_addsionamewithbchars(hawk, psin->u.fileb.path, hawk_count_bcstr(psin->u.fileb.path));
if (!path) return -1;
if (HAWK_UNLIKELY(!path)) return -1;
tmp = open_sio(hawk, path, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR | HAWK_SIO_KEEPPATH);
}
if (tmp == HAWK_NULL) return -1;