added the logf builtin function

This commit is contained in:
hyung-hwan 2018-03-01 09:46:02 +00:00
parent e58d4e30cc
commit 0b460a3f2a
3 changed files with 72 additions and 27 deletions

View File

@ -1053,7 +1053,12 @@ int hcl_outfmtobj (
hcl_outbfmt_t outbfmt hcl_outbfmt_t outbfmt
); );
int hcl_printfmt ( int hcl_printfmtst (
hcl_t* hcl,
hcl_ooi_t nargs
);
int hcl_logfmtst (
hcl_t* hcl, hcl_t* hcl,
hcl_ooi_t nargs hcl_ooi_t nargs
); );

View File

@ -192,7 +192,6 @@ static int put_ooch (hcl_t* hcl, int mask, hcl_ooch_t ch, hcl_oow_t len)
if (hcl->log.len > 0 && hcl->log.last_mask != mask) if (hcl->log.len > 0 && hcl->log.last_mask != mask)
{ {
/* the mask has changed. commit the buffered text */ /* the mask has changed. commit the buffered text */
/* TODO: HANDLE LINE ENDING CONVENTION BETTER... */ /* TODO: HANDLE LINE ENDING CONVENTION BETTER... */
if (hcl->log.ptr[hcl->log.len - 1] != '\n') if (hcl->log.ptr[hcl->log.len - 1] != '\n')
{ {
@ -725,7 +724,7 @@ void hcl_seterrufmtv (hcl_t* hcl, hcl_errnum_t errnum, const hcl_uch_t* fmt, va_
#define PRINT_OOCH(c,n) do { \ #define PRINT_OOCH(c,n) do { \
if (n > 0) { \ if (n > 0) { \
int xx; \ int xx; \
if ((xx = put_prch(hcl, data->mask, c, n)) <= -1) goto oops; \ if ((xx = data->putch(hcl, data->mask, c, n)) <= -1) goto oops; \
if (xx == 0) goto done; \ if (xx == 0) goto done; \
data->count += n; \ data->count += n; \
} \ } \
@ -734,7 +733,7 @@ void hcl_seterrufmtv (hcl_t* hcl, hcl_errnum_t errnum, const hcl_uch_t* fmt, va_
#define PRINT_OOCS(ptr,len) do { \ #define PRINT_OOCS(ptr,len) do { \
if (len > 0) { \ if (len > 0) { \
int xx; \ int xx; \
if ((xx = put_prcs(hcl, data->mask, ptr, len)) <= -1) goto oops; \ if ((xx = data->putcs(hcl, data->mask, ptr, len)) <= -1) goto oops; \
if (xx == 0) goto done; \ if (xx == 0) goto done; \
data->count += len; \ data->count += len; \
} \ } \
@ -751,7 +750,7 @@ void hcl_seterrufmtv (hcl_t* hcl, hcl_errnum_t errnum, const hcl_uch_t* fmt, va_
else { ch = *(fmt); (fmt)++; }\ else { ch = *(fmt); (fmt)++; }\
} while(0) } while(0)
static HCL_INLINE int print_formatted (hcl_t* hcl, hcl_ooi_t nargs, hcl_fmtout_t* data) static HCL_INLINE int print_formatted (hcl_t* hcl, hcl_ooi_t nargs, hcl_fmtout_t* data, hcl_outbfmt_t outbfmt)
{ {
const hcl_ooch_t* fmt, * fmtend; const hcl_ooch_t* fmt, * fmtend;
const hcl_ooch_t* checkpoint, * percent; const hcl_ooch_t* checkpoint, * percent;
@ -772,6 +771,7 @@ static HCL_INLINE int print_formatted (hcl_t* hcl, hcl_ooi_t nargs, hcl_fmtout_t
arg = HCL_STACK_GETARG(hcl, nargs, 0); arg = HCL_STACK_GETARG(hcl, nargs, 0);
if (!HCL_OOP_IS_POINTER(arg) || HCL_OBJ_GET_FLAGS_TYPE(arg) != HCL_OBJ_TYPE_CHAR) if (!HCL_OOP_IS_POINTER(arg) || HCL_OBJ_GET_FLAGS_TYPE(arg) != HCL_OBJ_TYPE_CHAR)
{ {
#if 0
hcl_ooi_t i; hcl_ooi_t i;
/* if the first argument is not a valid formatting string, /* if the first argument is not a valid formatting string,
* print all arguments as objects */ * print all arguments as objects */
@ -782,6 +782,7 @@ static HCL_INLINE int print_formatted (hcl_t* hcl, hcl_ooi_t nargs, hcl_fmtout_t
if (hcl_print(hcl, arg) <= -1) goto oops; if (hcl_print(hcl, arg) <= -1) goto oops;
} }
return 0; return 0;
#endif
} }
fmt = HCL_OBJ_GET_CHAR_SLOT(arg); fmt = HCL_OBJ_GET_CHAR_SLOT(arg);
@ -1000,7 +1001,7 @@ static HCL_INLINE int print_formatted (hcl_t* hcl, hcl_ooi_t nargs, hcl_fmtout_t
case 'O': /* object - ignore precision, width, adjustment */ case 'O': /* object - ignore precision, width, adjustment */
GET_NEXT_ARG_TO (hcl, nargs, &arg_state, arg); GET_NEXT_ARG_TO (hcl, nargs, &arg_state, arg);
if (hcl_outfmtobj(hcl, 0, arg, hcl_proutbfmt) <= -1) goto oops; if (hcl_outfmtobj(hcl, data->mask, arg, outbfmt) <= -1) goto oops;
break; break;
number: number:
@ -1111,10 +1112,34 @@ oops:
return -1; return -1;
} }
int hcl_printfmt (hcl_t* hcl, hcl_ooi_t nargs) int hcl_printfmtst (hcl_t* hcl, hcl_ooi_t nargs)
{ {
hcl_fmtout_t fo; hcl_fmtout_t fo;
HCL_MEMSET (&fo, 0, HCL_SIZEOF(fo)); HCL_MEMSET (&fo, 0, HCL_SIZEOF(fo));
return print_formatted(hcl, nargs, &fo); fo.putch = put_prch;
fo.putcs = put_prcs;
return print_formatted(hcl, nargs, &fo, hcl_proutbfmt);
} }
int hcl_logfmtst (hcl_t* hcl, hcl_ooi_t nargs)
{
hcl_fmtout_t fo;
HCL_MEMSET (&fo, 0, HCL_SIZEOF(fo));
fo.mask = HCL_LOG_FATAL | HCL_LOG_APP;
if (hcl->log.default_type_mask & HCL_LOG_ALL_TYPES)
{
/* if a type is given, it's not untyped any more.
* mask off the UNTYPED bit */
fo.mask &= ~HCL_LOG_UNTYPED;
/* if the default_type_mask has the UNTYPED bit on,
* it'll get turned back on */
fo.mask |= (hcl->log.default_type_mask & HCL_LOG_ALL_TYPES);
}
fo.putch = put_ooch;
fo.putcs = put_oocs;
return print_formatted(hcl, nargs, &fo, hcl_logbfmt);
}

View File

@ -167,6 +167,37 @@ static hcl_pfrc_t pf_log (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
return HCL_PF_SUCCESS; return HCL_PF_SUCCESS;
} }
static hcl_pfrc_t pf_logf (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
{
if (hcl_logfmtst(hcl, nargs) <= -1)
{
HCL_STACK_SETRETTOERRNUM (hcl, nargs);
}
else
{
/* TODO: better return code? */
HCL_STACK_SETRET (hcl, nargs, hcl->_nil);
}
return HCL_PF_SUCCESS;
}
static hcl_pfrc_t pf_printf (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
{
if (hcl_printfmtst(hcl, nargs) <= -1)
{
HCL_STACK_SETRETTOERRNUM (hcl, nargs);
}
else
{
/* TODO: better return code? */
HCL_STACK_SETRET (hcl, nargs, hcl->_nil);
}
return HCL_PF_SUCCESS;
}
static hcl_pfrc_t pf_gc (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs) static hcl_pfrc_t pf_gc (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
{ {
hcl_gc (hcl); hcl_gc (hcl);
@ -377,26 +408,12 @@ static hcl_pfrc_t pf_integer_rem (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
static hcl_pfrc_t pf_printf (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
{
if (hcl_printfmt(hcl, nargs) <= -1)
{
HCL_STACK_SETRETTOERRNUM (hcl, nargs);
}
else
{
/* TODO: better return code? */
HCL_STACK_SETRET (hcl, nargs, hcl->_nil);
}
return HCL_PF_SUCCESS;
}
/* ------------------------------------------------------------------------- */
static pf_t builtin_prims[] = static pf_t builtin_prims[] =
{ {
{ 0, HCL_TYPE_MAX(hcl_oow_t), pf_log, 3, { 'l','o','g' } }, { 0, HCL_TYPE_MAX(hcl_oow_t), pf_log, 3, { 'l','o','g' } },
{ 1, HCL_TYPE_MAX(hcl_oow_t), pf_logf, 4, { 'l','o','g','f' } },
{ 1, HCL_TYPE_MAX(hcl_oow_t), pf_printf, 6, { 'p','r','i','n','t','f' } },
{ 0, 0, pf_gc, 2, { 'g','c' } }, { 0, 0, pf_gc, 2, { 'g','c' } },
{ 1, 1, pf_not, 3, { 'n','o','t' } }, { 1, 1, pf_not, 3, { 'n','o','t' } },
@ -424,8 +441,6 @@ static pf_t builtin_prims[] =
{ 1, HCL_TYPE_MAX(hcl_oow_t), pf_integer_mul, 1, { '*' } }, { 1, HCL_TYPE_MAX(hcl_oow_t), pf_integer_mul, 1, { '*' } },
{ 1, HCL_TYPE_MAX(hcl_oow_t), pf_integer_quo, 1, { '/' } }, { 1, HCL_TYPE_MAX(hcl_oow_t), pf_integer_quo, 1, { '/' } },
{ 2, HCL_TYPE_MAX(hcl_oow_t), pf_integer_rem, 3, { 'm','o','d' } }, { 2, HCL_TYPE_MAX(hcl_oow_t), pf_integer_rem, 3, { 'm','o','d' } },
{ 1, HCL_TYPE_MAX(hcl_oow_t), pf_printf, 6, { 'p','r','i','n','t','f' } },
}; };