refactored moo_seterrbfmt() and moo_seterrufmt() with moo_bfmt_outv() and moo_ufmt_outv()

This commit is contained in:
hyunghwan.chung 2019-05-28 08:55:13 +00:00
parent fe847a89a5
commit 3f274d74b7
11 changed files with 219 additions and 165 deletions

View File

@ -392,7 +392,6 @@ extend MyObject
[ self testBigintDiv: 76733673740671314025981152630586699414203 divisor: 12682136550675277273 count: 1 ],
[ self testBigintDiv: 265804060782114895959697138188904455994 divisor: 713053462628379038341886861235 count: 1],
## =========================
[
| k |

View File

@ -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;

View File

@ -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);

View File

@ -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
* -------------------------------------------------------------------------- */

View File

@ -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 */
/* ========================================================================= */

View File

@ -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
);

View File

@ -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);

View File

@ -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" */

View File

@ -10,9 +10,20 @@ AM_CPPFLAGS = \
-I$(includedir)
AM_LDFLAGS = -L$(abs_builddir)/../lib -L$(libdir)
LDADD = -lmoo $(PTHREAD_LIBS)
LDADD = $(PTHREAD_LIBS)
bin_PROGRAMS = t-001 t-002 t-003 t-004
bin_PROGRAMS = t-001 t-002 t-003
t_001_SOURCES = t-001.c
t_002_SOURCES = t-002.c
t_002_LDADD = -lmoo $(LDADD)
t_002_DEPENDENCIES = ../lib/libmoo.la
t_003_SOURCES = t-003.c
t_003_LDADD = -lmoo $(LDADD)
t_003_DEPENDENCIES = ../lib/libmoo.la
t_004_SOURCES = t-004.c
t_004_LDADD = -lmoox $(LDADD)
t_004_DEPENDENCIES = ../lib/libmoox.la

View File

@ -88,7 +88,8 @@ PRE_UNINSTALL = :
POST_UNINSTALL = :
build_triplet = @build@
host_triplet = @host@
bin_PROGRAMS = t-001$(EXEEXT) t-002$(EXEEXT) t-003$(EXEEXT)
bin_PROGRAMS = t-001$(EXEEXT) t-002$(EXEEXT) t-003$(EXEEXT) \
t-004$(EXEEXT)
subdir = t
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/m4/ax_check_sign.m4 \
@ -117,12 +118,11 @@ am__v_lt_0 = --silent
am__v_lt_1 =
am_t_002_OBJECTS = t-002.$(OBJEXT)
t_002_OBJECTS = $(am_t_002_OBJECTS)
t_002_LDADD = $(LDADD)
t_002_DEPENDENCIES = $(am__DEPENDENCIES_1)
am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1)
am_t_003_OBJECTS = t-003.$(OBJEXT)
t_003_OBJECTS = $(am_t_003_OBJECTS)
t_003_LDADD = $(LDADD)
t_003_DEPENDENCIES = $(am__DEPENDENCIES_1)
am_t_004_OBJECTS = t-004.$(OBJEXT)
t_004_OBJECTS = $(am_t_004_OBJECTS)
AM_V_P = $(am__v_P_@AM_V@)
am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
am__v_P_0 = false
@ -157,8 +157,10 @@ AM_V_CCLD = $(am__v_CCLD_@AM_V@)
am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@)
am__v_CCLD_0 = @echo " CCLD " $@;
am__v_CCLD_1 =
SOURCES = $(t_001_SOURCES) $(t_002_SOURCES) $(t_003_SOURCES)
DIST_SOURCES = $(t_001_SOURCES) $(t_002_SOURCES) $(t_003_SOURCES)
SOURCES = $(t_001_SOURCES) $(t_002_SOURCES) $(t_003_SOURCES) \
$(t_004_SOURCES)
DIST_SOURCES = $(t_001_SOURCES) $(t_002_SOURCES) $(t_003_SOURCES) \
$(t_004_SOURCES)
am__can_run_installinfo = \
case $$AM_UPDATE_INFO_DIR in \
n|no|NO) false;; \
@ -348,10 +350,17 @@ AM_CPPFLAGS = \
-I$(includedir)
AM_LDFLAGS = -L$(abs_builddir)/../lib -L$(libdir)
LDADD = -lmoo $(PTHREAD_LIBS)
LDADD = $(PTHREAD_LIBS)
t_001_SOURCES = t-001.c
t_002_SOURCES = t-002.c
t_002_LDADD = -lmoo $(LDADD)
t_002_DEPENDENCIES = ../lib/libmoo.la
t_003_SOURCES = t-003.c
t_003_LDADD = -lmoo $(LDADD)
t_003_DEPENDENCIES = ../lib/libmoo.la
t_004_SOURCES = t-004.c
t_004_LDADD = -lmoox $(LDADD)
t_004_DEPENDENCIES = ../lib/libmoox.la
all: all-am
.SUFFIXES:
@ -447,6 +456,10 @@ t-003$(EXEEXT): $(t_003_OBJECTS) $(t_003_DEPENDENCIES) $(EXTRA_t_003_DEPENDENCIE
@rm -f t-003$(EXEEXT)
$(AM_V_CCLD)$(LINK) $(t_003_OBJECTS) $(t_003_LDADD) $(LIBS)
t-004$(EXEEXT): $(t_004_OBJECTS) $(t_004_DEPENDENCIES) $(EXTRA_t_004_DEPENDENCIES)
@rm -f t-004$(EXEEXT)
$(AM_V_CCLD)$(LINK) $(t_004_OBJECTS) $(t_004_LDADD) $(LIBS)
mostlyclean-compile:
-rm -f *.$(OBJEXT)
@ -456,6 +469,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/t-001.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/t-002.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/t-003.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/t-004.Po@am__quote@
.c.o:
@am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.o$$||'`;\

37
moo/t/t-004.c Normal file
View File

@ -0,0 +1,37 @@
#include <moo-std.h>
#include <moo-utl.h>
#include <stdio.h>
#include "t.h"
int main ()
{
moo_t* moo = MOO_NULL;
moo_uch_t ufmt1[] = { '%', '0', '5', 'd', ' ', '%', '-', '9', 'h', 's', '\0' };
moo_uch_t ufmt2[] = { '%', '0', '5', 'd', ' ', '%', '-', '9', 'h', 's', ' ', '%','O','\0' };
moo = moo_openstd(0, MOO_NULL, MOO_NULL);
if (!moo)
{
fprintf (stderr, "Unable to open moo\n");
return -1;
}
moo_seterrbfmt (moo, MOO_EINVAL, "%d %ld %s %hs", 10, 20L, "moo", "moo");
T_ASSERT1 (moo_comp_oocstr_bcstr(moo_geterrmsg(moo), "10 20 moo moo") == 0, "moo seterrbfmt #1");
moo_logbfmt (moo, MOO_LOG_STDERR, "[%js]\n", moo_geterrmsg(moo));
moo_seterrufmt (moo, MOO_EINVAL, ufmt1, 9923, "moo");
T_ASSERT1 (moo_comp_oocstr_bcstr(moo_geterrmsg(moo), "09923 moo ") == 0, "moo seterrufmt #1");
moo_logbfmt (moo, MOO_LOG_STDERR, "[%js]\n", moo_geterrmsg(moo));
moo_seterrufmt (moo, MOO_EINVAL, ufmt2, 9923, "moo", MOO_SMPTR_TO_OOP(0x12345678));
T_ASSERT1 (moo_comp_oocstr_bcstr(moo_geterrmsg(moo), "09923 moo #\\p12345678") == 0, "moo seterrufmt #1");
moo_logbfmt (moo, MOO_LOG_STDERR, "[%js]\n", moo_geterrmsg(moo));
moo_close (moo);
return 0;
oops:
if (moo) moo_close (moo);
return -1;
}