From 0bf106532dacf96dbf5086d5ae05e77360d73978 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sun, 19 May 2024 15:12:47 +0900 Subject: [PATCH] input handling experiment --- lib/hcl.h | 14 ++++++++++++-- lib/prim.c | 25 ++++++++++--------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/hcl.h b/lib/hcl.h index 18ac7af..385c22f 100644 --- a/lib/hcl.h +++ b/lib/hcl.h @@ -1263,10 +1263,16 @@ struct hcl_io_cciarg_t */ void* handle; + /** + * [OUT] set this to non-zero in HCL_IO_OPEN if the handler fills the buffer with bytes. + * the caller issues HCL_IO_READ_BYTES if it's set to non-zero, expecting bytes. + * otherwise it issues HCL_IO_READ expecting characters. + */ + int is_bytes; + /** * [OUT] place data here for #HCL_IO_READ or #HCL_IO_READ_BYTES */ - int is_bytes; /* set this to non-zero if the handler fills the buffer with bytes */ union { hcl_ooch_t c[HCL_CCI_BUF_LEN]; @@ -1317,6 +1323,11 @@ struct hcl_io_udiarg_t */ void* handle; + /** + * [OUT] indicates if HCL_IO_READ_BYTES is implemented + */ + int is_bytes; + /** * [OUT] place data in c for #HCL_IO_READ and in d for #HCL_IO_READ_BYTES */ @@ -1335,7 +1346,6 @@ struct hcl_io_udiarg_t * Internal use only. Don't touch these. */ hcl_oow_t pos; - int is_byte; }; typedef struct hcl_io_udoarg_t hcl_io_udoarg_t; diff --git a/lib/prim.c b/lib/prim.c index e6d56d0..1cb5119 100644 --- a/lib/prim.c +++ b/lib/prim.c @@ -224,14 +224,7 @@ static int get_udi_char (hcl_t* hcl, hcl_ooch_t* ch) hcl->io.udi_arg.xlen = 0; if (hcl->io.udi_rdr(hcl, HCL_IO_READ, &hcl->io.udi_arg) <= -1) return -1; if (hcl->io.udi_arg.xlen <= 0) return 0; /* EOF */ - hcl->io.udi_arg.is_byte = 0; - } - - - if (hcl->io.udi_arg.is_byte) - { - /* TODO: set error */ - return -1; + hcl->io.udi_arg.is_bytes = 0; } *ch = hcl->io.udi_arg.buf.c[hcl->io.udi_arg.pos++]; @@ -240,19 +233,21 @@ static int get_udi_char (hcl_t* hcl, hcl_ooch_t* ch) static int get_udi_byte (hcl_t* hcl, hcl_uint8_t* bt) { +#if defined(HCL_OOCH_IS_UCH) + if (!hcl->io.udi_arg.is_bytes) + { + hcl_seterrbfmt (hcl, HCL_EPERM, "prohibited byte-oriented input"); + return -1; + } +#endif + if (hcl->io.udi_arg.pos >= hcl->io.udi_arg.xlen) { hcl->io.udi_arg.pos = 0; hcl->io.udi_arg.xlen = 0; if (hcl->io.udi_rdr(hcl, HCL_IO_READ_BYTES, &hcl->io.udi_arg) <= -1) return -1; if (hcl->io.udi_arg.xlen <= 0) return 0; /* EOF */ - hcl->io.udi_arg.is_byte = 1; - } - - if (!hcl->io.udi_arg.is_byte) - { - /* TODO: set error */ - return -1; + hcl->io.udi_arg.is_bytes = 1; } *bt = hcl->io.udi_arg.buf.b[hcl->io.udi_arg.pos++];