refactored moo_seterrbfmt() and moo_seterrufmt() with moo_bfmt_outv() and moo_ufmt_outv()
This commit is contained in:
125
moo/lib/err.c
125
moo/lib/err.c
@ -215,6 +215,131 @@ void moo_seterrnum (moo_t* moo, moo_errnum_t errnum)
|
||||
moo->errmsg.len = 0;
|
||||
}
|
||||
|
||||
|
||||
static int err_bcs (moo_fmtout_t* fmtout, const moo_bch_t* ptr, moo_oow_t len)
|
||||
{
|
||||
moo_t* moo = (moo_t*)fmtout->ctx;
|
||||
moo_oow_t max;
|
||||
|
||||
max = MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len - 1;
|
||||
|
||||
#if defined(MOO_OOCH_IS_UCH)
|
||||
if (max <= 0) return 1;
|
||||
moo_conv_bchars_to_uchars_with_cmgr (ptr, &len, &moo->errmsg.buf[moo->errmsg.len], &max, moo->cmgr, 1);
|
||||
moo->errmsg.len += max;
|
||||
#else
|
||||
if (len > max) len = max;
|
||||
if (len <= 0) return 1;
|
||||
MOO_MEMCPY (&moo->errmsg.buf[moo->errmsg.len], ptr, len * MOO_SIZEOF(*ptr));
|
||||
moo->errmsg.len += len;
|
||||
#endif
|
||||
|
||||
moo->errmsg.buf[moo->errmsg.len] = '\0';
|
||||
|
||||
return 1; /* success */
|
||||
}
|
||||
|
||||
static int err_ucs (moo_fmtout_t* fmtout, const moo_uch_t* ptr, moo_oow_t len)
|
||||
{
|
||||
moo_t* moo = (moo_t*)fmtout->ctx;
|
||||
moo_oow_t max;
|
||||
|
||||
max = MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len - 1;
|
||||
|
||||
#if defined(MOO_OOCH_IS_UCH)
|
||||
if (len > max) len = max;
|
||||
if (len <= 0) return 1;
|
||||
MOO_MEMCPY (&moo->errmsg.buf[moo->errmsg.len], ptr, len * MOO_SIZEOF(*ptr));
|
||||
moo->errmsg.len += len;
|
||||
#else
|
||||
if (max <= 0) return 1;
|
||||
moo_conv_uchars_to_bchars_with_cmgr (ptr, &len, &moo->errmsg.buf[moo->errmsg.len], &max, moo->cmgr);
|
||||
moo->errmsg.len += max;
|
||||
#endif
|
||||
moo->errmsg.buf[moo->errmsg.len] = '\0';
|
||||
return 1; /* success */
|
||||
}
|
||||
|
||||
void moo_seterrbfmt (moo_t* moo, moo_errnum_t errnum, const moo_bch_t* fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
moo_fmtout_t fo;
|
||||
|
||||
if (moo->shuterr) return;
|
||||
moo->errmsg.len = 0;
|
||||
|
||||
MOO_MEMSET (&fo, 0, MOO_SIZEOF(fo));
|
||||
fo.putbcs = err_bcs;
|
||||
fo.putucs = err_ucs;
|
||||
fo.putobj = moo_fmt_object_;
|
||||
fo.ctx = moo;
|
||||
|
||||
va_start (ap, fmt);
|
||||
moo_bfmt_outv (&fo, fmt, ap);
|
||||
va_end (ap);
|
||||
|
||||
moo->errnum = errnum;
|
||||
}
|
||||
|
||||
void moo_seterrufmt (moo_t* moo, moo_errnum_t errnum, const moo_uch_t* fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
moo_fmtout_t fo;
|
||||
|
||||
if (moo->shuterr) return;
|
||||
moo->errmsg.len = 0;
|
||||
|
||||
MOO_MEMSET (&fo, 0, MOO_SIZEOF(fo));
|
||||
fo.putbcs = err_bcs;
|
||||
fo.putucs = err_ucs;
|
||||
fo.putobj = moo_fmt_object_;
|
||||
fo.ctx = moo;
|
||||
|
||||
va_start (ap, fmt);
|
||||
moo_ufmt_outv (&fo, fmt, ap);
|
||||
va_end (ap);
|
||||
|
||||
moo->errnum = errnum;
|
||||
}
|
||||
|
||||
|
||||
void moo_seterrbfmtv (moo_t* moo, moo_errnum_t errnum, const moo_bch_t* fmt, va_list ap)
|
||||
{
|
||||
moo_fmtout_t fo;
|
||||
|
||||
if (moo->shuterr) return;
|
||||
|
||||
moo->errmsg.len = 0;
|
||||
|
||||
MOO_MEMSET (&fo, 0, MOO_SIZEOF(fo));
|
||||
fo.putbcs = err_bcs;
|
||||
fo.putucs = err_ucs;
|
||||
fo.putobj = moo_fmt_object_;
|
||||
fo.ctx = moo;
|
||||
|
||||
moo_bfmt_outv (&fo, fmt, ap);
|
||||
moo->errnum = errnum;
|
||||
}
|
||||
|
||||
void moo_seterrufmtv (moo_t* moo, moo_errnum_t errnum, const moo_uch_t* fmt, va_list ap)
|
||||
{
|
||||
moo_fmtout_t fo;
|
||||
|
||||
if (moo->shuterr) return;
|
||||
|
||||
moo->errmsg.len = 0;
|
||||
|
||||
MOO_MEMSET (&fo, 0, MOO_SIZEOF(fo));
|
||||
fo.putbcs = err_bcs;
|
||||
fo.putucs = err_ucs;
|
||||
fo.putobj = moo_fmt_object_;
|
||||
fo.ctx = moo;
|
||||
|
||||
moo_ufmt_outv (&fo, fmt, ap);
|
||||
moo->errnum = errnum;
|
||||
}
|
||||
|
||||
|
||||
void moo_seterrwithsyserr (moo_t* moo, int syserr_type, int syserr_code)
|
||||
{
|
||||
moo_errnum_t errnum;
|
||||
|
@ -1267,7 +1267,7 @@ int moo_ufmt_outv (moo_fmtout_t* fmtout, const moo_uch_t* fmt, va_list ap)
|
||||
fmt_str = fmtout->fmt_str;
|
||||
fmt_type = fmtout->fmt_type;
|
||||
|
||||
fmtout->fmt_type = MOO_FMTOUT_FMT_TYPE_BCH;
|
||||
fmtout->fmt_type = MOO_FMTOUT_FMT_TYPE_UCH;
|
||||
fmtout->fmt_str = fmt;
|
||||
|
||||
n = fmt_outv(fmtout, ap);
|
||||
@ -1325,7 +1325,7 @@ int moo_ufmt_out (moo_fmtout_t* fmtout, const moo_uch_t* fmt, ...)
|
||||
* OBJECT OUTPUT
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
static int print_object (moo_fmtout_t* fmtout, moo_oop_t oop)
|
||||
int moo_fmt_object_ (moo_fmtout_t* fmtout, moo_oop_t oop)
|
||||
{
|
||||
moo_t* moo = (moo_t*)fmtout->ctx;
|
||||
|
||||
@ -1499,11 +1499,11 @@ static int print_object (moo_fmtout_t* fmtout, moo_oop_t oop)
|
||||
i = 0;
|
||||
if (i < MOO_OBJ_GET_SIZE(oop))
|
||||
{
|
||||
if (print_object(fmtout, MOO_OBJ_GET_OOP_VAL(oop, i)) <= -1) return -1;
|
||||
if (moo_fmt_object_(fmtout, MOO_OBJ_GET_OOP_VAL(oop, i)) <= -1) return -1;
|
||||
for (++i; i < MOO_OBJ_GET_SIZE(oop); i++)
|
||||
{
|
||||
if (moo_bfmt_out(fmtout, " ") <= -1) return -1;
|
||||
if (print_object(fmtout, MOO_OBJ_GET_OOP_VAL(oop, i)) <= -1) return -1;
|
||||
if (moo_fmt_object_(fmtout, MOO_OBJ_GET_OOP_VAL(oop, i)) <= -1) return -1;
|
||||
}
|
||||
}
|
||||
if (moo_bfmt_out(fmtout, ")") <= -1) return -1;
|
||||
@ -1701,7 +1701,7 @@ moo_ooi_t moo_logbfmt (moo_t* moo, moo_bitmask_t mask, const moo_bch_t* fmt, ...
|
||||
fo.mask = mask;
|
||||
fo.putbcs = log_bcs;
|
||||
fo.putucs = log_ucs;
|
||||
fo.putobj = print_object;
|
||||
fo.putobj = moo_fmt_object_;
|
||||
|
||||
va_start (ap, fmt);
|
||||
x = fmt_outv(&fo, ap);
|
||||
@ -1746,7 +1746,7 @@ moo_ooi_t moo_logufmt (moo_t* moo, moo_bitmask_t mask, const moo_uch_t* fmt, ...
|
||||
fo.mask = mask;
|
||||
fo.putbcs = log_bcs;
|
||||
fo.putucs = log_ucs;
|
||||
fo.putobj = print_object;
|
||||
fo.putobj = moo_fmt_object_;
|
||||
|
||||
va_start (ap, fmt);
|
||||
x = fmt_outv(&fo, ap);
|
||||
|
140
moo/lib/fmtout.c
140
moo/lib/fmtout.c
@ -696,146 +696,6 @@ moo_ooi_t moo_logufmt (moo_t* moo, moo_bitmask_t mask, const moo_uch_t* fmt, ...
|
||||
}
|
||||
#endif
|
||||
|
||||
/* --------------------------------------------------------------------------
|
||||
* ERROR MESSAGE FORMATTING
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
static int put_errch (moo_t* moo, moo_bitmask_t mask, moo_ooch_t ch, moo_oow_t len)
|
||||
{
|
||||
moo_oow_t max;
|
||||
|
||||
max = MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len - 1;
|
||||
if (len > max) len = max;
|
||||
|
||||
if (len <= 0) return 1;
|
||||
|
||||
while (len > 0)
|
||||
{
|
||||
moo->errmsg.buf[moo->errmsg.len++] = ch;
|
||||
len--;
|
||||
}
|
||||
moo->errmsg.buf[moo->errmsg.len] = '\0';
|
||||
|
||||
return 1; /* success */
|
||||
}
|
||||
|
||||
static int put_errcs (moo_t* moo, moo_bitmask_t mask, const moo_ooch_t* ptr, moo_oow_t len)
|
||||
{
|
||||
moo_oow_t max;
|
||||
|
||||
max = MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len - 1;
|
||||
if (len > max) len = max;
|
||||
|
||||
if (len <= 0) return 1;
|
||||
|
||||
MOO_MEMCPY (&moo->errmsg.buf[moo->errmsg.len], ptr, len * MOO_SIZEOF(*ptr));
|
||||
moo->errmsg.len += len;
|
||||
moo->errmsg.buf[moo->errmsg.len] = '\0';
|
||||
|
||||
return 1; /* success */
|
||||
}
|
||||
|
||||
|
||||
static moo_ooi_t __errbfmtv (moo_t* moo, moo_bitmask_t mask, const moo_bch_t* fmt, ...);
|
||||
|
||||
static int _errbfmtv (moo_t* moo, const moo_bch_t* fmt, moo_fmtout_data_t* data, va_list ap)
|
||||
{
|
||||
return __logbfmtv (moo, fmt, data, ap, __errbfmtv);
|
||||
}
|
||||
|
||||
static int _errufmtv (moo_t* moo, const moo_uch_t* fmt, moo_fmtout_data_t* data, va_list ap)
|
||||
{
|
||||
return __logufmtv (moo, fmt, data, ap, __errbfmtv);
|
||||
}
|
||||
|
||||
static moo_ooi_t __errbfmtv (moo_t* moo, moo_bitmask_t mask, const moo_bch_t* fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
moo_fmtout_data_t fo;
|
||||
|
||||
fo.mask = 0; /* not used */
|
||||
fo.putch = put_errch;
|
||||
fo.putcs = put_errcs;
|
||||
|
||||
va_start (ap, fmt);
|
||||
_errbfmtv (moo, fmt, &fo, ap);
|
||||
va_end (ap);
|
||||
|
||||
return fo.count;
|
||||
}
|
||||
|
||||
void moo_seterrbfmt (moo_t* moo, moo_errnum_t errnum, const moo_bch_t* fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
moo_fmtout_data_t fo;
|
||||
|
||||
if (moo->shuterr) return;
|
||||
moo->errmsg.len = 0;
|
||||
|
||||
fo.mask = 0; /* not used */
|
||||
fo.putch = put_errch;
|
||||
fo.putcs = put_errcs;
|
||||
|
||||
va_start (ap, fmt);
|
||||
_errbfmtv (moo, fmt, &fo, ap);
|
||||
va_end (ap);
|
||||
|
||||
moo->errnum = errnum;
|
||||
}
|
||||
|
||||
void moo_seterrufmt (moo_t* moo, moo_errnum_t errnum, const moo_uch_t* fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
moo_fmtout_data_t fo;
|
||||
|
||||
if (moo->shuterr) return;
|
||||
moo->errmsg.len = 0;
|
||||
|
||||
fo.mask = 0; /* not used */
|
||||
fo.putch = put_errch;
|
||||
fo.putcs = put_errcs;
|
||||
|
||||
va_start (ap, fmt);
|
||||
_errufmtv (moo, fmt, &fo, ap);
|
||||
va_end (ap);
|
||||
|
||||
moo->errnum = errnum;
|
||||
}
|
||||
|
||||
|
||||
void moo_seterrbfmtv (moo_t* moo, moo_errnum_t errnum, const moo_bch_t* fmt, va_list ap)
|
||||
{
|
||||
moo_fmtout_data_t fo;
|
||||
|
||||
if (moo->shuterr) return;
|
||||
|
||||
moo->errmsg.len = 0;
|
||||
|
||||
fo.mask = 0; /* not used */
|
||||
fo.putch = put_errch;
|
||||
fo.putcs = put_errcs;
|
||||
|
||||
_errbfmtv (moo, fmt, &fo, ap);
|
||||
moo->errnum = errnum;
|
||||
}
|
||||
|
||||
void moo_seterrufmtv (moo_t* moo, moo_errnum_t errnum, const moo_uch_t* fmt, va_list ap)
|
||||
{
|
||||
moo_fmtout_data_t fo;
|
||||
|
||||
if (moo->shuterr) return;
|
||||
|
||||
moo->errmsg.len = 0;
|
||||
|
||||
fo.mask = 0; /* not used */
|
||||
fo.putch = put_errch;
|
||||
fo.putcs = put_errcs;
|
||||
|
||||
_errufmtv (moo, fmt, &fo, ap);
|
||||
moo->errnum = errnum;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------------------
|
||||
* SUPPORT FOR FORMATTED OUTPUT TO BE USED BY BUILTIN PRIMITIVE FUNCTIONS
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
@ -1483,6 +1483,14 @@ moo_oop_t moo_numtostr (
|
||||
);
|
||||
|
||||
|
||||
/* ========================================================================= */
|
||||
/* fmt.c */
|
||||
/* ========================================================================= */
|
||||
int moo_fmt_object_ (
|
||||
moo_fmtout_t* fmtout,
|
||||
moo_oop_t oop
|
||||
);
|
||||
|
||||
/* ========================================================================= */
|
||||
/* fmtout.c */
|
||||
/* ========================================================================= */
|
||||
|
@ -596,7 +596,7 @@ moo_oow_t moo_byte_to_bcstr (
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
MOO_EXPORT int moo_ucwidth (
|
||||
MOO_EXPORT int moo_get_ucwidth (
|
||||
moo_uch_t uc
|
||||
);
|
||||
|
||||
|
@ -3624,12 +3624,12 @@ static void fini_moo (moo_t* moo)
|
||||
xtn_t* xtn;
|
||||
|
||||
MOO_MEMSET(&vmprim, 0, MOO_SIZEOF(vmprim));
|
||||
if (cfg->large_pages)
|
||||
if (cfg && cfg->large_pages)
|
||||
{
|
||||
vmprim.alloc_heap = alloc_heap;
|
||||
vmprim.free_heap = free_heap;
|
||||
}
|
||||
vmprim.log_write = (cfg->log_write? cfg->log_write: log_write);
|
||||
vmprim.log_write = ((cfg && cfg->log_write)? cfg->log_write: log_write);
|
||||
vmprim.syserrstrb = syserrstrb;
|
||||
vmprim.assertfail = assert_fail;
|
||||
vmprim.dl_startup = dl_startup;
|
||||
@ -3646,13 +3646,13 @@ static void fini_moo (moo_t* moo)
|
||||
vmprim.vm_muxwait = vm_muxwait;
|
||||
vmprim.vm_sleep = vm_sleep;
|
||||
|
||||
moo = moo_open(&sys_mmgr, MOO_SIZEOF(xtn_t) + xtnsize, (cfg->cmgr? cfg->cmgr: moo_get_utf8_cmgr()), &vmprim, errinfo);
|
||||
moo = moo_open(&sys_mmgr, MOO_SIZEOF(xtn_t) + xtnsize, ((cfg && cfg->cmgr)? cfg->cmgr: moo_get_utf8_cmgr()), &vmprim, errinfo);
|
||||
if (!moo) return MOO_NULL;
|
||||
|
||||
xtn = GET_XTN(moo);
|
||||
xtn->input_cmgr = cfg->input_cmgr;
|
||||
if (cfg) xtn->input_cmgr = cfg->input_cmgr;
|
||||
if (!xtn->input_cmgr) xtn->input_cmgr = moo_getcmgr(moo);
|
||||
xtn->log_cmgr = cfg->log_cmgr;
|
||||
if (cfg) xtn->log_cmgr = cfg->log_cmgr;
|
||||
if (!xtn->log_cmgr) xtn->log_cmgr = moo_getcmgr(moo);
|
||||
|
||||
chain (moo); /* call chain() before moo_regevtcb() as fini_moo() calls unchain() */
|
||||
@ -3677,7 +3677,7 @@ static void fini_moo (moo_t* moo)
|
||||
moo_setoption (moo, MOO_OPTION_LOG_MASK, &bm);
|
||||
}
|
||||
|
||||
if (handle_cfg_options(moo, cfg) <= -1)
|
||||
if (cfg && handle_cfg_options(moo, cfg) <= -1)
|
||||
{
|
||||
if (errinfo) moo_geterrinf (moo, errinfo);
|
||||
moo_close (moo);
|
||||
|
@ -239,7 +239,7 @@ static int bisearch(moo_uch_t ucs, const struct interval *table, int max)
|
||||
* in ISO 10646.
|
||||
*/
|
||||
|
||||
int moo_ucwidth (moo_uch_t uc)
|
||||
int moo_get_ucwidth (moo_uch_t uc)
|
||||
{
|
||||
/* sorted list of non-overlapping intervals of non-spacing characters */
|
||||
/* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */
|
||||
|
Reference in New Issue
Block a user