added qse_sed_geterrstr() and qse_sed_seterrstr()

This commit is contained in:
hyung-hwan 2009-05-28 07:55:17 +00:00
parent 83a6e0d3b0
commit 16f0580d75
4 changed files with 72 additions and 85 deletions

View File

@ -26,12 +26,7 @@ QSE_BEGIN_NAMESPACE(QSE)
int Sed::open () throw () int Sed::open () throw ()
{ {
sed = qse_sed_open (this, QSE_SIZEOF(Sed*)); sed = qse_sed_open (this, QSE_SIZEOF(Sed*));
if (sed == QSE_NULL) if (sed == QSE_NULL) return -1;
{
// TODO: set error...
return -1;
}
*(Sed**)QSE_XTN(sed) = this; *(Sed**)QSE_XTN(sed) = this;
return 0; return 0;
} }
@ -73,12 +68,12 @@ Sed::size_t Sed::getErrorLine () const
return (sed == QSE_NULL)? 0: qse_sed_geterrlin (sed); return (sed == QSE_NULL)? 0: qse_sed_geterrlin (sed);
} }
int Sed::getErrorNumber () const Sed::errnum_t Sed::getErrorNumber () const
{ {
return (sed == QSE_NULL)? 0: qse_sed_geterrnum (sed); return (sed == QSE_NULL)? QSE_SED_ENOERR: qse_sed_geterrnum (sed);
} }
Sed::ssize_t Sed::xin (sed_t* s, sed_io_cmd_t cmd, sed_io_arg_t* arg) throw () Sed::ssize_t Sed::xin (sed_t* s, io_cmd_t cmd, io_arg_t* arg) throw ()
{ {
Sed* sed = *(Sed**)QSE_XTN(s); Sed* sed = *(Sed**)QSE_XTN(s);
@ -132,7 +127,7 @@ Sed::ssize_t Sed::xin (sed_t* s, sed_io_cmd_t cmd, sed_io_arg_t* arg) throw ()
} }
} }
Sed::ssize_t Sed::xout (sed_t* s, sed_io_cmd_t cmd, sed_io_arg_t* arg) throw () Sed::ssize_t Sed::xout (sed_t* s, io_cmd_t cmd, io_arg_t* arg) throw ()
{ {
Sed* sed = *(Sed**)QSE_XTN(s); Sed* sed = *(Sed**)QSE_XTN(s);

View File

@ -18,73 +18,17 @@
#include "sed.h" #include "sed.h"
static const qse_char_t* geterrstr (int errnum) qse_sed_errstr_t qse_sed_geterrstr (qse_sed_t* sed)
{ {
static const qse_char_t* errstr[] = return sed->errstr;
{
QSE_T("no error"),
QSE_T("out of memory"),
QSE_T("command '${0}' not recognized"),
QSE_T("command code missing"),
QSE_T("command '${0}' incomplete"),
QSE_T("regular expression '${0}' incomplete"),
QSE_T("failed to compile regular expression '${0}'"),
QSE_T("failed to match regular expression"),
QSE_T("address 1 prohibited for '${0}'"),
QSE_T("address 2 prohibited"),
QSE_T("invalid step address"),
QSE_T("a new line expected"),
QSE_T("a backslash expected"),
QSE_T("a backslash used as a delimiter"),
QSE_T("garbage after a backslash"),
QSE_T("a semicolon expected"),
QSE_T("empty label name"),
QSE_T("duplicate label name '${0}'"),
QSE_T("label '${0}' not found"),
QSE_T("empty file name"),
QSE_T("illegal file name"),
QSE_T("strings in translation set not the same length"),
QSE_T("group brackets not balanced"),
QSE_T("group nesting too deep"),
QSE_T("multiple occurrence specifier"),
QSE_T("occurrence specifier is zero"),
QSE_T("occurrence specifier too large"),
QSE_T("io error with file '${0}'"),
QSE_T("error returned by user io handler")
};
return (errnum >= 0 && errnum < QSE_COUNTOF(errstr))?
errstr[errnum]: QSE_T("unknown error");
} }
#if 0 void qse_sed_seterrstr (qse_sed_t* sed, qse_sed_errstr_t errstr)
const qse_char_t* qse_sed_geterrstr (qse_sed_t* sed, qse_sed_errnum_t num)
{ {
if (sed != QSE_NULL && sed->errstr = errstr;
sed->errstr[num] != QSE_NULL) return sed->errstr[num];
return geterrstr (num);
} }
int qse_sed_seterrstr ( qse_sed_errnum_t qse_sed_geterrnum (qse_sed_t* sed)
qse_sed_t* sed, qse_sed_errnum_t num, const qse_char_t* str)
{
qse_char_t* dup;
if (str == QSE_NULL) dup = QSE_NULL;
else
{
dup = QSE_AWK_STRDUP (sed, str);
if (dup == QSE_NULL) return -1;
}
if (sed->errstr[num] != QSE_NULL)
QSE_AWK_FREE (sed, sed->errstr[num]);
else sed->errstr[num] = dup;
return 0;
}
#endif
int qse_sed_geterrnum (qse_sed_t* sed)
{ {
return sed->errnum; return sed->errnum;
} }
@ -96,14 +40,12 @@ qse_size_t qse_sed_geterrlin (qse_sed_t* sed)
const qse_char_t* qse_sed_geterrmsg (qse_sed_t* sed) const qse_char_t* qse_sed_geterrmsg (qse_sed_t* sed)
{ {
if (sed->errmsg[0] == QSE_T('\0')) return (sed->errmsg[0] == QSE_T('\0'))?
/*return qse_sed_geterrstr (sed, sed->errnum);*/ qse_sed_geterrstr(sed)(sed,sed->errnum): sed->errmsg;
return geterrstr (sed->errnum);
return sed->errmsg;
} }
void qse_sed_geterror ( void qse_sed_geterror (
qse_sed_t* sed, int* errnum, qse_sed_t* sed, qse_sed_errnum_t* errnum,
qse_size_t* errlin, const qse_char_t** errmsg) qse_size_t* errlin, const qse_char_t** errmsg)
{ {
if (errnum != QSE_NULL) *errnum = sed->errnum; if (errnum != QSE_NULL) *errnum = sed->errnum;
@ -111,21 +53,21 @@ void qse_sed_geterror (
if (errmsg != QSE_NULL) if (errmsg != QSE_NULL)
{ {
*errmsg = (sed->errmsg[0] == QSE_T('\0'))? *errmsg = (sed->errmsg[0] == QSE_T('\0'))?
/*qse_sed_geterrstr (sed, sed->errnum):*/ qse_sed_geterrstr(sed)(sed,sed->errnum):
geterrstr (sed->errnum):
sed->errmsg; sed->errmsg;
} }
} }
void qse_sed_seterrnum (qse_sed_t* sed, int errnum) void qse_sed_seterrnum (qse_sed_t* sed, qse_sed_errnum_t errnum)
{ {
sed->errnum = errnum; sed->errnum = errnum;
sed->errlin = 0; sed->errlin = 0;
sed->errmsg[0] = QSE_T('\0'); sed->errmsg[0] = QSE_T('\0');
} }
void qse_sed_seterrmsg (qse_sed_t* sed, void qse_sed_seterrmsg (
int errnum, qse_size_t errlin, const qse_char_t* errmsg) qse_sed_t* sed, qse_sed_errnum_t errnum,
qse_size_t errlin, const qse_char_t* errmsg)
{ {
sed->errnum = errnum; sed->errnum = errnum;
sed->errlin = errlin; sed->errlin = errlin;
@ -133,7 +75,7 @@ void qse_sed_seterrmsg (qse_sed_t* sed,
} }
void qse_sed_seterror ( void qse_sed_seterror (
qse_sed_t* sed, int errnum, qse_sed_t* sed, qse_sed_errnum_t errnum,
qse_size_t errlin, const qse_cstr_t* errarg) qse_size_t errlin, const qse_cstr_t* errarg)
{ {
const qse_char_t* errfmt; const qse_char_t* errfmt;
@ -141,7 +83,7 @@ void qse_sed_seterror (
sed->errnum = errnum; sed->errnum = errnum;
sed->errlin = errlin; sed->errlin = errlin;
errfmt = /*qse_sed_geterrstr (sed, errnum);*/ geterrstr (sed->errnum); errfmt = qse_sed_geterrstr(sed)(sed,sed->errnum);
QSE_ASSERT (errfmt != QSE_NULL); QSE_ASSERT (errfmt != QSE_NULL);
qse_strxfncpy (sed->errmsg, QSE_COUNTOF(sed->errmsg), errfmt, errarg); qse_strxfncpy (sed->errmsg, QSE_COUNTOF(sed->errmsg), errfmt, errarg);
} }

View File

@ -37,6 +37,45 @@ do { \
qse_sed_seterror (sed, num, line, &__qse__err__arg__); \ qse_sed_seterror (sed, num, line, &__qse__err__arg__); \
} while (0) } while (0)
static const qse_char_t* dflerrstr (qse_sed_t* sed, qse_sed_errnum_t errnum)
{
static const qse_char_t* errstr[] =
{
QSE_T("no error"),
QSE_T("out of memory"),
QSE_T("command '${0}' not recognized"),
QSE_T("command code missing"),
QSE_T("command '${0}' incomplete"),
QSE_T("regular expression '${0}' incomplete"),
QSE_T("failed to compile regular expression '${0}'"),
QSE_T("failed to match regular expression"),
QSE_T("address 1 prohibited for '${0}'"),
QSE_T("address 2 prohibited"),
QSE_T("address 2 missing or invalid"),
QSE_T("a new line expected"),
QSE_T("a backslash expected"),
QSE_T("a backslash used as a delimiter"),
QSE_T("garbage after a backslash"),
QSE_T("a semicolon expected"),
QSE_T("empty label name"),
QSE_T("duplicate label name '${0}'"),
QSE_T("label '${0}' not found"),
QSE_T("empty file name"),
QSE_T("illegal file name"),
QSE_T("strings in translation set not the same length"),
QSE_T("group brackets not balanced"),
QSE_T("group nesting too deep"),
QSE_T("multiple occurrence specifier"),
QSE_T("occurrence specifier is zero"),
QSE_T("occurrence specifier too large"),
QSE_T("io error with file '${0}'"),
QSE_T("error returned by user io handler")
};
return (errnum >= 0 && errnum < QSE_COUNTOF(errstr))?
errstr[errnum]: QSE_T("unknown error");
}
qse_sed_t* qse_sed_open (qse_mmgr_t* mmgr, qse_size_t xtn) qse_sed_t* qse_sed_open (qse_mmgr_t* mmgr, qse_size_t xtn)
{ {
qse_sed_t* sed; qse_sed_t* sed;
@ -73,6 +112,7 @@ static qse_sed_t* qse_sed_init (qse_sed_t* sed, qse_mmgr_t* mmgr)
{ {
QSE_MEMSET (sed, 0, QSE_SIZEOF(*sed)); QSE_MEMSET (sed, 0, QSE_SIZEOF(*sed));
sed->mmgr = mmgr; sed->mmgr = mmgr;
sed->errstr = dflerrstr;
if (qse_str_init (&sed->tmp.rex, mmgr, 0) == QSE_NULL) if (qse_str_init (&sed->tmp.rex, mmgr, 0) == QSE_NULL)
{ {
@ -1221,12 +1261,21 @@ int qse_sed_comp (qse_sed_t* sed, const qse_char_t* sptr, qse_size_t slen)
return -1; return -1;
} }
if (delim == QSE_T('~')) if (delim == QSE_T(','))
{
if (cmd->a2.type == QSE_SED_ADR_NONE)
{
SETERR0 (sed, QSE_SED_EA2MOI, sed->src.lnum);
free_address(sed, cmd);
return -1;
}
}
else if (delim == QSE_T('~'))
{ {
if (cmd->a1.type != QSE_SED_ADR_LINE || if (cmd->a1.type != QSE_SED_ADR_LINE ||
cmd->a2.type != QSE_SED_ADR_LINE) cmd->a2.type != QSE_SED_ADR_LINE)
{ {
SETERR0 (sed, QSE_SED_EASTEP, sed->src.lnum); SETERR0 (sed, QSE_SED_EA2MOI, sed->src.lnum);
free_address(sed, cmd); free_address(sed, cmd);
return -1; return -1;
} }

View File

@ -33,6 +33,7 @@ struct qse_sed_t
{ {
QSE_DEFINE_COMMON_FIELDS (sed) QSE_DEFINE_COMMON_FIELDS (sed)
qse_sed_errstr_t errstr; /**< error string getter */
qse_sed_errnum_t errnum; /**< stores an error number */ qse_sed_errnum_t errnum; /**< stores an error number */
qse_char_t errmsg[128]; /**< error message holder */ qse_char_t errmsg[128]; /**< error message holder */
qse_size_t errlin; /**< no of the line where an error occurred */ qse_size_t errlin; /**< no of the line where an error occurred */