Files
qse/qse/lib/sed/Sed.cpp

259 lines
4.7 KiB
C++
Raw Normal View History

2009-05-20 23:26:47 +00:00
/*
2009-08-26 21:03:51 +00:00
* $Id: Sed.cpp 269 2009-08-26 03:03:51Z hyunghwan.chung $
2009-05-20 23:26:47 +00:00
*
Copyright 2006-2009 Chung, Hyung-Hwan.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <qse/sed/Sed.hpp>
#include "sed.h"
2009-05-20 23:26:47 +00:00
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
2009-07-16 04:43:31 +00:00
int Sed::open ()
2009-05-20 23:26:47 +00:00
{
sed = qse_sed_open (this, QSE_SIZEOF(Sed*));
if (sed == QSE_NULL) return -1;
*(Sed**)QSE_XTN(sed) = this;
dflerrstr = qse_sed_geterrstr (sed);
qse_sed_seterrstr (sed, xerrstr);
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
{
if (sed != QSE_NULL)
{
qse_sed_close (sed);
sed = QSE_NULL;
}
}
2009-07-16 04:43:31 +00:00
int Sed::compile (const char_t* sptr)
2009-05-27 07:29:47 +00:00
{
QSE_ASSERT (sed != QSE_NULL);
return qse_sed_comp (sed, sptr, qse_strlen(sptr));
}
2009-07-16 04:43:31 +00:00
int Sed::compile (const char_t* sptr, size_t slen)
{
2009-05-26 07:39:18 +00:00
QSE_ASSERT (sed != QSE_NULL);
return qse_sed_comp (sed, sptr, slen);
}
2009-07-16 04:43:31 +00:00
int Sed::execute ()
{
2009-05-26 07:39:18 +00:00
QSE_ASSERT (sed != QSE_NULL);
return qse_sed_exec (sed, xin, xout);
}
2009-07-16 04:43:31 +00:00
int Sed::getOption() const
{
QSE_ASSERT (sed != QSE_NULL);
return qse_sed_getoption (sed);
}
2009-07-16 04:43:31 +00:00
void Sed::setOption (int opt)
{
QSE_ASSERT (sed != QSE_NULL);
qse_sed_setoption (sed, opt);
}
2009-07-16 04:43:31 +00:00
Sed::size_t Sed::getMaxDepth (depth_t id) const
{
QSE_ASSERT (sed != QSE_NULL);
return qse_sed_getmaxdepth (sed, id);
}
2009-07-16 04:43:31 +00:00
void Sed::setMaxDepth (int ids, size_t depth)
{
QSE_ASSERT (sed != QSE_NULL);
qse_sed_setmaxdepth (sed, ids, depth);
}
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;
loc.lin = 0; loc.col = 0;
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
{
return (sed == QSE_NULL)? QSE_SED_ENOERR: qse_sed_geterrnum (sed);
2009-05-28 01:01:33 +00:00
}
2009-08-26 21:03:51 +00:00
void Sed::setError (errnum_t err, const cstr_t* args, const loc_t* loc)
{
QSE_ASSERT (sed != QSE_NULL);
2009-08-26 21:03:51 +00:00
qse_sed_seterror (sed, err, args, loc);
}
2009-07-16 04:43:31 +00:00
Sed::size_t Sed::getConsoleLine ()
{
QSE_ASSERT (sed != QSE_NULL);
return qse_sed_getlinnum (sed);
}
2009-07-16 04:43:31 +00:00
void Sed::setConsoleLine (size_t num)
{
QSE_ASSERT (sed != QSE_NULL);
qse_sed_setlinnum (sed, num);
}
2009-07-16 04:43:31 +00:00
Sed::ssize_t Sed::xin (sed_t* s, io_cmd_t cmd, io_arg_t* arg)
2009-05-26 07:39:18 +00:00
{
Sed* sed = *(Sed**)QSE_XTN(s);
2009-05-28 01:01:33 +00:00
if (arg->path == QSE_NULL)
2009-05-26 07:39:18 +00:00
{
2009-05-28 01:01:33 +00:00
Console io (arg, Console::READ);
try
2009-05-26 07:39:18 +00:00
{
2009-05-28 01:01:33 +00:00
switch (cmd)
{
case QSE_SED_IO_OPEN:
return sed->openConsole (io);
case QSE_SED_IO_CLOSE:
return sed->closeConsole (io);
case QSE_SED_IO_READ:
return sed->readConsole (
io, arg->u.r.buf, arg->u.r.len);
default:
return -1;
}
}
catch (...)
{
return -1;
2009-05-26 07:39:18 +00:00
}
}
2009-05-28 01:01:33 +00:00
else
2009-05-26 07:39:18 +00:00
{
2009-05-28 01:01:33 +00:00
File io (arg, File::READ);
try
{
switch (cmd)
{
case QSE_SED_IO_OPEN:
return sed->openFile (io);
case QSE_SED_IO_CLOSE:
return sed->closeFile (io);
case QSE_SED_IO_READ:
return sed->readFile (
io, arg->u.r.buf, arg->u.r.len);
default:
return -1;
}
}
catch (...)
{
return -1;
}
2009-05-26 07:39:18 +00:00
}
}
2009-07-16 04:43:31 +00:00
Sed::ssize_t Sed::xout (sed_t* s, io_cmd_t cmd, io_arg_t* arg)
2009-05-26 07:39:18 +00:00
{
Sed* sed = *(Sed**)QSE_XTN(s);
2009-05-28 01:01:33 +00:00
if (arg->path == QSE_NULL)
2009-05-26 07:39:18 +00:00
{
2009-05-28 01:01:33 +00:00
Console io (arg, Console::WRITE);
try
{
switch (cmd)
{
case QSE_SED_IO_OPEN:
return sed->openConsole (io);
case QSE_SED_IO_CLOSE:
return sed->closeConsole (io);
case QSE_SED_IO_WRITE:
return sed->writeConsole (
io, arg->u.w.data, arg->u.w.len);
default:
return -1;
}
}
catch (...)
2009-05-26 07:39:18 +00:00
{
2009-05-28 01:01:33 +00:00
return -1;
2009-05-26 07:39:18 +00:00
}
}
2009-05-28 01:01:33 +00:00
else
2009-05-26 07:39:18 +00:00
{
2009-05-28 01:01:33 +00:00
File io (arg, File::WRITE);
try
{
switch (cmd)
{
case QSE_SED_IO_OPEN:
return sed->openFile (io);
case QSE_SED_IO_CLOSE:
return sed->closeFile (io);
case QSE_SED_IO_WRITE:
return sed->writeFile (
io, arg->u.w.data, arg->u.w.len);
default:
return -1;
}
}
catch (...)
{
return -1;
}
2009-05-26 07:39:18 +00:00
}
}
2009-05-20 23:26:47 +00:00
const Sed::char_t* Sed::getErrorString (errnum_t num) const
{
QSE_ASSERT (dflerrstr != QSE_NULL);
return dflerrstr (sed, num);
}
2009-07-16 04:43:31 +00:00
const Sed::char_t* Sed::xerrstr (sed_t* s, errnum_t num)
{
Sed* sed = *(Sed**)QSE_XTN(s);
try
{
return sed->getErrorString (num);
}
catch (...)
{
return sed->dflerrstr (s, num);
}
}
2009-05-20 23:26:47 +00:00
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
2009-05-20 23:26:47 +00:00
/////////////////////////////////