enhanced formatting functions to accept hcl as the first parameter in order to extend hcl_fmttobcstr() adn hcl_fmttoucstr() to handle %O and %J
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
85
lib/print.c
85
lib/print.c
@ -121,12 +121,12 @@ static struct
|
||||
{ 11, { '#','<','I','N','S','T','A','N','C','E','>' } }
|
||||
};
|
||||
|
||||
static HCL_INLINE int print_single_char (hcl_fmtout_t* fmtout, hcl_ooch_t ch)
|
||||
static HCL_INLINE int print_single_char (hcl_t* hcl, hcl_fmtout_t* fmtout, hcl_ooch_t ch)
|
||||
{
|
||||
hcl_oochu_t chu = (hcl_oochu_t)ch;
|
||||
if (chu == '\\' || chu == '\"')
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, "\\%jc", chu) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "\\%jc", chu) <= -1) return -1;
|
||||
}
|
||||
#if defined(HCL_OOCH_IS_UCH)
|
||||
else if (chu < ' ')
|
||||
@ -180,31 +180,30 @@ static HCL_INLINE int print_single_char (hcl_fmtout_t* fmtout, hcl_ooch_t ch)
|
||||
#if (HCL_SIZEOF_OOCH_T >= 2)
|
||||
if (chu >= 0x100u)
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, "\\u%04X", chu) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "\\u%04X", chu) <= -1) return -1;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, "\\x%02X", chu) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "\\x%02X", chu) <= -1) return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, "\\%jc", escaped) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "\\%jc", escaped) <= -1) return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, "%jc", ch) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "%jc", ch) <= -1) return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int hcl_fmt_object_ (hcl_fmtout_t* fmtout, hcl_oop_t obj)
|
||||
int hcl_fmt_object (hcl_t* hcl, hcl_fmtout_t* fmtout, hcl_oop_t obj)
|
||||
{
|
||||
hcl_t* hcl = (hcl_t*)fmtout->ctx;
|
||||
hcl_oop_t cur;
|
||||
print_stack_t ps;
|
||||
int brand;
|
||||
@ -254,23 +253,23 @@ next:
|
||||
switch ((brand = HCL_BRANDOF(hcl, obj)))
|
||||
{
|
||||
case HCL_BRAND_SMOOI:
|
||||
if (hcl_bfmt_out(fmtout, "%zd", HCL_OOP_TO_SMOOI(obj)) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "%zd", HCL_OOP_TO_SMOOI(obj)) <= -1) return -1;
|
||||
goto done;
|
||||
|
||||
case HCL_BRAND_SMPTR:
|
||||
if (hcl_bfmt_out(fmtout, "#p%zX", (hcl_oow_t)HCL_OOP_TO_SMPTR(obj)) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "#p%zX", (hcl_oow_t)HCL_OOP_TO_SMPTR(obj)) <= -1) return -1;
|
||||
goto done;
|
||||
|
||||
case HCL_BRAND_ERROR:
|
||||
if (hcl_bfmt_out(fmtout, "#e%zd", (hcl_ooi_t)HCL_OOP_TO_ERROR(obj)) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "#e%zd", (hcl_ooi_t)HCL_OOP_TO_ERROR(obj)) <= -1) return -1;
|
||||
goto done;
|
||||
|
||||
case HCL_BRAND_CHARACTER:
|
||||
{
|
||||
hcl_ooch_t ch = HCL_OOP_TO_CHAR(obj);
|
||||
if (hcl_bfmt_out(fmtout, "\'") <= -1 ||
|
||||
print_single_char(fmtout, ch) <= -1 ||
|
||||
hcl_bfmt_out(fmtout, "\'") <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "\'") <= -1 ||
|
||||
print_single_char(hcl, fmtout, ch) <= -1 ||
|
||||
hcl_bfmt_out(hcl, fmtout, "\'") <= -1) return -1;
|
||||
goto done;
|
||||
}
|
||||
|
||||
@ -302,7 +301,7 @@ next:
|
||||
if (!tmp) return -1;
|
||||
|
||||
HCL_ASSERT (hcl, (hcl_oop_t)tmp == hcl->_nil);
|
||||
if (hcl_bfmt_out(fmtout, "%.*js", hcl->inttostr.xbuf.len, hcl->inttostr.xbuf.ptr) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "%.*js", hcl->inttostr.xbuf.len, hcl->inttostr.xbuf.ptr) <= -1) return -1;
|
||||
break;
|
||||
}
|
||||
|
||||
@ -317,11 +316,11 @@ next:
|
||||
{
|
||||
if (scale == 0)
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, "0.") <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "0.") <= -1) return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, "0.%0*d", scale, 0) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "0.%0*d", scale, 0) <= -1) return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -339,13 +338,13 @@ next:
|
||||
{
|
||||
if (scale == len)
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, "%.*js0.%.*js",
|
||||
if (hcl_bfmt_out(hcl, fmtout, "%.*js0.%.*js",
|
||||
adj, hcl->inttostr.xbuf.ptr,
|
||||
len, &hcl->inttostr.xbuf.ptr[adj]) <= -1) return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, "%.*js0.%0*d%.*js",
|
||||
if (hcl_bfmt_out(hcl, fmtout, "%.*js0.%0*d%.*js",
|
||||
adj, hcl->inttostr.xbuf.ptr,
|
||||
scale - len, 0,
|
||||
len, &hcl->inttostr.xbuf.ptr[adj]) <= -1) return -1;
|
||||
@ -355,7 +354,7 @@ next:
|
||||
{
|
||||
hcl_ooi_t ndigits;
|
||||
ndigits = hcl->inttostr.xbuf.len - scale;
|
||||
if (hcl_bfmt_out(fmtout, "%.*js.%.*js", ndigits, hcl->inttostr.xbuf.ptr, scale, &hcl->inttostr.xbuf.ptr[ndigits]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "%.*js.%.*js", ndigits, hcl->inttostr.xbuf.ptr, scale, &hcl->inttostr.xbuf.ptr[ndigits]) <= -1) return -1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -385,7 +384,7 @@ next:
|
||||
/* Any needs for special action if SYNT(obj) is true?
|
||||
* I simply treat the syntax symbol as a normal symbol
|
||||
* for printing currently. */
|
||||
if (hcl_bfmt_out(fmtout, "%.*js", HCL_OBJ_GET_SIZE(obj), HCL_OBJ_GET_CHAR_SLOT(obj)) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "%.*js", HCL_OBJ_GET_SIZE(obj), HCL_OBJ_GET_CHAR_SLOT(obj)) <= -1) return -1;
|
||||
break;
|
||||
|
||||
case HCL_BRAND_STRING:
|
||||
@ -406,17 +405,17 @@ next:
|
||||
|
||||
if (escape)
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, "\"") <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "\"") <= -1) return -1;
|
||||
for (i = 0; i < HCL_OBJ_GET_SIZE(obj); i++)
|
||||
{
|
||||
ch = ((hcl_oop_char_t)obj)->slot[i];
|
||||
if (print_single_char(fmtout, ch) <= -1) return -1;
|
||||
if (print_single_char(hcl, fmtout, ch) <= -1) return -1;
|
||||
}
|
||||
if (hcl_bfmt_out(fmtout, "\"") <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "\"") <= -1) return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, "\"%.*js\"", HCL_OBJ_GET_SIZE(obj), HCL_OBJ_GET_CHAR_SLOT(obj)) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "\"%.*js\"", HCL_OBJ_GET_SIZE(obj), HCL_OBJ_GET_CHAR_SLOT(obj)) <= -1) return -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -429,7 +428,7 @@ next:
|
||||
* request to output in the json format */
|
||||
|
||||
concode = HCL_OBJ_GET_FLAGS_SYNCODE(obj);
|
||||
if (hcl_bfmt_out(fmtout, opening_parens[concode][0]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, opening_parens[concode][0]) <= -1) return -1;
|
||||
cur = obj;
|
||||
|
||||
/* TODO: for MLIST, print : after the first element.
|
||||
@ -466,7 +465,7 @@ next:
|
||||
if (!HCL_OOP_IS_POINTER(cur) || HCL_OBJ_GET_FLAGS_BRAND(cur) != HCL_BRAND_CONS)
|
||||
{
|
||||
/* The CDR part does not point to a pair. */
|
||||
if (hcl_bfmt_out(fmtout, " . ") <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, " . ") <= -1) return -1;
|
||||
|
||||
/* Push NIL so that the HCL_IS_NIL(hcl,p) test in
|
||||
* the 'if' statement above breaks the loop
|
||||
@ -483,11 +482,11 @@ next:
|
||||
}
|
||||
|
||||
/* The CDR part points to a pair. proceed to it */
|
||||
if (hcl_bfmt_out(fmtout, breakers[0][0]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, breakers[0][0]) <= -1) return -1;
|
||||
}
|
||||
while (1);
|
||||
|
||||
if (hcl_bfmt_out(fmtout, closing_parens[concode][0]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, closing_parens[concode][0]) <= -1) return -1;
|
||||
break;
|
||||
}
|
||||
|
||||
@ -495,11 +494,11 @@ next:
|
||||
{
|
||||
hcl_oow_t arridx;
|
||||
|
||||
if (hcl_bfmt_out(fmtout, opening_parens[HCL_CONCODE_ARRAY][json]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, opening_parens[HCL_CONCODE_ARRAY][json]) <= -1) return -1;
|
||||
|
||||
if (HCL_OBJ_GET_SIZE(obj) <= 0)
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, closing_parens[HCL_CONCODE_ARRAY][json]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, closing_parens[HCL_CONCODE_ARRAY][json]) <= -1) return -1;
|
||||
break;
|
||||
}
|
||||
arridx = 0;
|
||||
@ -527,7 +526,7 @@ next:
|
||||
obj = ((hcl_oop_oop_t)obj)->slot[arridx];
|
||||
if (arridx > 0)
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, breakers[0][json]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, breakers[0][json]) <= -1) return -1;
|
||||
}
|
||||
/* Jump to the 'next' label so that the object
|
||||
* pointed to by 'obj' is printed. Once it
|
||||
@ -547,16 +546,16 @@ next:
|
||||
case HCL_BRAND_BYTE_ARRAY:
|
||||
{
|
||||
hcl_oow_t i;
|
||||
if (hcl_bfmt_out(fmtout, opening_parens[HCL_CONCODE_BYTEARRAY][json]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, opening_parens[HCL_CONCODE_BYTEARRAY][json]) <= -1) return -1;
|
||||
if (HCL_OBJ_GET_SIZE(obj) > 0)
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, "%d", ((hcl_oop_byte_t)obj)->slot[0]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "%d", ((hcl_oop_byte_t)obj)->slot[0]) <= -1) return -1;
|
||||
for (i = 1; i < HCL_OBJ_GET_SIZE(obj); i++)
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, "%hs%d", breakers[0][json], ((hcl_oop_byte_t)obj)->slot[i]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "%hs%d", breakers[0][json], ((hcl_oop_byte_t)obj)->slot[i]) <= -1) return -1;
|
||||
}
|
||||
}
|
||||
if (hcl_bfmt_out(fmtout, closing_parens[HCL_CONCODE_BYTEARRAY][json]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, closing_parens[HCL_CONCODE_BYTEARRAY][json]) <= -1) return -1;
|
||||
break;
|
||||
}
|
||||
|
||||
@ -565,13 +564,13 @@ next:
|
||||
hcl_oow_t bucidx, bucsize, buctally;
|
||||
hcl_oop_dic_t dic;
|
||||
|
||||
if (hcl_bfmt_out(fmtout, opening_parens[HCL_CONCODE_DIC][json]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, opening_parens[HCL_CONCODE_DIC][json]) <= -1) return -1;
|
||||
|
||||
dic = (hcl_oop_dic_t)obj;
|
||||
HCL_ASSERT (hcl, HCL_OOP_IS_SMOOI(dic->tally));
|
||||
if (HCL_OOP_TO_SMOOI(dic->tally) <= 0)
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, closing_parens[HCL_CONCODE_DIC][json]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, closing_parens[HCL_CONCODE_DIC][json]) <= -1) return -1;
|
||||
break;
|
||||
}
|
||||
bucidx = 0;
|
||||
@ -597,7 +596,7 @@ next:
|
||||
if (bucidx >= bucsize)
|
||||
{
|
||||
/* done. scanned the entire bucket */
|
||||
if (hcl_bfmt_out(fmtout, closing_parens[HCL_CONCODE_DIC][json]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, closing_parens[HCL_CONCODE_DIC][json]) <= -1) return -1;
|
||||
break;
|
||||
}
|
||||
|
||||
@ -638,7 +637,7 @@ next:
|
||||
|
||||
if (buctally > 0)
|
||||
{
|
||||
if (hcl_bfmt_out(fmtout, breakers[buctally & 1][json]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, breakers[buctally & 1][json]) <= -1) return -1;
|
||||
}
|
||||
|
||||
/* Jump to the 'next' label so that the object
|
||||
@ -709,7 +708,7 @@ next:
|
||||
return -1;
|
||||
|
||||
print_word:
|
||||
if (hcl_bfmt_out(fmtout, "%.*js", word[word_index].len, word[word_index].ptr) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, "%.*js", word[word_index].len, word[word_index].ptr) <= -1) return -1;
|
||||
break;
|
||||
}
|
||||
|
||||
@ -727,14 +726,14 @@ done:
|
||||
goto resume_array;
|
||||
|
||||
case PRINT_STACK_ARRAY_END:
|
||||
if (hcl_bfmt_out(fmtout, closing_parens[HCL_CONCODE_ARRAY][json]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, closing_parens[HCL_CONCODE_ARRAY][json]) <= -1) return -1;
|
||||
break;
|
||||
|
||||
case PRINT_STACK_DIC:
|
||||
goto resume_dic;
|
||||
|
||||
case PRINT_STACK_DIC_END:
|
||||
if (hcl_bfmt_out(fmtout, closing_parens[HCL_CONCODE_DIC][json]) <= -1) return -1;
|
||||
if (hcl_bfmt_out(hcl, fmtout, closing_parens[HCL_CONCODE_DIC][json]) <= -1) return -1;
|
||||
break;
|
||||
|
||||
default:
|
||||
|
Reference in New Issue
Block a user