From ca1e82816d859fa9194be06c2b7feefc72559e2c Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sun, 18 Feb 2024 18:21:24 +0900 Subject: [PATCH] almost finished the byte cci handler. yet to test more --- lib/fmt.c | 4 ++-- lib/hcl.h | 10 ++++++++-- lib/read.c | 28 ++++++++++++++++++---------- pas/hcl.pas | 2 +- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/fmt.c b/lib/fmt.c index a3c6ff6..3a727cb 100644 --- a/lib/fmt.c +++ b/lib/fmt.c @@ -1867,7 +1867,7 @@ static int print_bcs (hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) if (HCL_UNLIKELY(!hcl->io.udo_wrtr)) { - hcl_seterrmsg (hcl, HCL_EINVAL, "no user-defined output handler"); + hcl_seterrbmsg (hcl, HCL_EINVAL, "no user-defined output handler"); return -1; } @@ -1895,7 +1895,7 @@ static int print_ucs (hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len) if (HCL_UNLIKELY(!hcl->io.udo_wrtr)) { - hcl_seterrmsg (hcl, HCL_EINVAL, "no user-defined output handler"); + hcl_seterrbmsg (hcl, HCL_EINVAL, "no user-defined output handler"); return -1; } diff --git a/lib/hcl.h b/lib/hcl.h index 5d08aab..e42fba4 100644 --- a/lib/hcl.h +++ b/lib/hcl.h @@ -1208,6 +1208,12 @@ struct hcl_lxc_t }; typedef struct hcl_lxc_t hcl_lxc_t; + +/*#define HCL_CCI_BUF_LEN (1)*/ +#if !defined(HCL_CCI_BUF_LEN) +#define HCL_CCI_BUF_LEN (2048) +#endif + typedef struct hcl_io_cciarg_t hcl_io_cciarg_t; struct hcl_io_cciarg_t { @@ -1232,8 +1238,8 @@ struct hcl_io_cciarg_t int is_bytes; /* set this to non-zero if the handler fills the buffer with bytes */ union { - hcl_ooch_t c[2048]; /* TODO: resize this if necessary */ - hcl_uint8_t b[2048 * HCL_SIZEOF(hcl_ooch_t)]; /* TODO: resize this if necessary */ + hcl_ooch_t c[HCL_CCI_BUF_LEN]; + hcl_uint8_t b[HCL_CCI_BUF_LEN * HCL_SIZEOF(hcl_ooch_t)]; } buf; /** diff --git a/lib/read.c b/lib/read.c index b4f8882..6be17e5 100644 --- a/lib/read.c +++ b/lib/read.c @@ -2968,12 +2968,16 @@ static int feed_from_includee (hcl_t* hcl) curinp->b.pos = 0; curinp->b.len = curinp->xlen; +//printf ("curinp->xlen = %d\n", (int)curinp->xlen); } avail = curinp->b.len - curinp->b.pos; /* available in the read buffer */ saved_rsd_len = curinp->rsd.len; - if (curinp->rsd.len > 0) +//printf ("saved_rsd_len = %d avail=%d\n", (int)saved_rsd_len, (int)avail); + if (saved_rsd_len > 0) { + /* there is data in the residue buffer. use the residue buffer to + * locate a proper multi-byte sequence */ hcl_oow_t cpl; /* number of bytes to copy to the residue buffer */ HCL_ASSERT (hcl, curinp->b.pos == 0); cpl = HCL_COUNTOF(curinp->rsd.buf) - curinp->rsd.len; @@ -2985,7 +2989,9 @@ static int feed_from_includee (hcl_t* hcl) curinp->b.pos += cpl; /* advance this because the bytes moved to the residue buffer */ } inplen = curinp->rsd.len; - n = cmgr->bctouc(curinp->rsd.buf, inplen, &c); + n = cmgr->bctouc(&curinp->rsd.buf[0], inplen, &c); +//printf ("residue -> inplen = %d cpl = %d avail=%d\n", (int)inplen, (int)cpl, (int)avail); + if (n > 0 && n <= inplen) curinp->b.pos -= curinp->rsd.len - saved_rsd_len; } else { @@ -2998,19 +3004,20 @@ static int feed_from_includee (hcl_t* hcl) hcl_seterrbfmt (hcl, HCL_EECERR, "invalid byte sequence in %js", curinp->name); return -1; } - if (n > inplen) + if (n > inplen) /* incomplete sequence */ { - /* incomplete sequence */ - HCL_ASSERT (hcl, avail < HCL_COUNTOF(curinp->rsd.buf)); - - /* TODO: wrong */ - HCL_MEMCPY (curinp->rsd.buf, &curinp->buf.b[curinp->b.pos], avail); - curinp->rsd.len = avail; - curinp->b.pos = curinp->b.len; + hcl_oow_t cpl; + HCL_ASSERT (hcl, curinp->rsd.len < HCL_COUNTOF(curinp->rsd.buf)); + cpl = HCL_COUNTOF(curinp->rsd.buf) - curinp->rsd.len; + if (cpl > avail) cpl = avail; + HCL_MEMCPY(&curinp->rsd.buf[curinp->rsd.len], &curinp->buf.b[curinp->b.pos], cpl); + curinp->rsd.len += cpl; + curinp->b.pos += cpl; goto start_over; } /* how much taken from the read buffer as input */ + HCL_ASSERT (hcl, n >= saved_rsd_len); taken = n - saved_rsd_len; } else @@ -3044,6 +3051,7 @@ static int feed_from_includee (hcl_t* hcl) } #endif +//hcl_logbfmt(hcl, HCL_LOG_STDERR, "[%jc]\n", c); x = feed_char(hcl, c); if (x <= -1) return -1; if (x >= 1) diff --git a/pas/hcl.pas b/pas/hcl.pas index a88d76f..dbdff5f 100644 --- a/pas/hcl.pas +++ b/pas/hcl.pas @@ -47,7 +47,7 @@ type name: pwidechar; handle: pointer; is_bytes: integer; - buf: array[0..2047] of widechar; + buf: array[0..(2048 - 1)] of widechar; xlen: System.SizeUint; includer: CciArgPtr; end;