added HCL_IO_WRITE_BYTES
All checks were successful
continuous-integration/drone/push Build is passing

implemented it in the print handler
This commit is contained in:
hyung-hwan 2023-06-01 00:30:41 +09:00
parent 8514f0e590
commit a5d96cc6eb
5 changed files with 164 additions and 84 deletions

View File

@ -122,7 +122,7 @@ static void vm_checkbc (hcl_t* hcl, hcl_oob_t bcode)
static void gc_hcl (hcl_t* hcl) static void gc_hcl (hcl_t* hcl)
{ {
xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl); /*xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl);*/
/*if (xtn->sym_errstr) xtn->sym_errstr = hcl_moveoop(hcl, xtn->sym_errstr);*/ /*if (xtn->sym_errstr) xtn->sym_errstr = hcl_moveoop(hcl, xtn->sym_errstr);*/
} }

View File

@ -1790,6 +1790,7 @@ static int print_bcs (hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len)
{ {
hcl_t* hcl = (hcl_t*)fmtout->ctx; hcl_t* hcl = (hcl_t*)fmtout->ctx;
#if 0
#if defined(HCL_OOCH_IS_UCH) #if defined(HCL_OOCH_IS_UCH)
hcl_oow_t ucslen, bcslen; hcl_oow_t ucslen, bcslen;
hcl_ooch_t ucsbuf[64], * ucsptr; hcl_ooch_t ucsbuf[64], * ucsptr;
@ -1834,6 +1835,23 @@ static int print_bcs (hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len)
len -= hcl->io.outarg.xlen; len -= hcl->io.outarg.xlen;
} }
#endif #endif
#else
hcl_bch_t* optr;
optr = (hcl_bch_t*)ptr;
while (len > 0)
{
hcl->io.outarg.ptr = optr;
hcl->io.outarg.len = len;
if (hcl->io.printer(hcl, HCL_IO_WRITE_BYTES, &hcl->io.outarg) <= -1) return -1;
if (hcl->io.outarg.xlen <= 0) return 0; /* end of stream. but not failure */
HCL_ASSERT (hcl, hcl->io.outarg.xlen <= len);
optr += hcl->io.outarg.xlen;
len -= hcl->io.outarg.xlen;
}
#endif
return 1; /* success */ return 1; /* success */
} }

View File

@ -588,6 +588,24 @@ static int print_handler (hcl_t* hcl, hcl_iocmd_t cmd, void* arg)
return 0; return 0;
} }
case HCL_IO_WRITE_BYTES:
{
worker_hcl_xtn_t* xtn = (worker_hcl_xtn_t*)hcl_getxtn(hcl);
hcl_iooutarg_t* outarg = (hcl_iooutarg_t*)arg;
if (hcl_server_proto_feed_reply_bytes(xtn->proto, outarg->ptr, outarg->len, 0) <= -1)
{
/* TODO: change error code and message. propagage the errormessage from proto */
hcl_seterrbfmt (hcl, HCL_EIOERR, "failed to write message via proto");
/* writing failure on the socket is a critical failure.
* execution must get aborted */
hcl_abort (hcl);
return -1;
}
outarg->xlen = outarg->len;
return 0;
}
default: default:
hcl_seterrnum (hcl, HCL_EINTERN); hcl_seterrnum (hcl, HCL_EINTERN);
return -1; return -1;
@ -850,6 +868,24 @@ int hcl_server_proto_feed_reply (hcl_server_proto_t* proto, const hcl_ooch_t* pt
#endif #endif
} }
int hcl_server_proto_feed_reply_bytes (hcl_server_proto_t* proto, const hcl_bch_t* ptr, hcl_oow_t len, int escape)
{
while (len > 0)
{
if (escape && (*ptr == '\\' || *ptr == '\"'))
{
if (proto->reply.len >= HCL_COUNTOF(proto->reply.buf) && write_reply_chunk(proto) <=-1) return -1;
proto->reply.buf[proto->reply.len++] = '\\';
}
if (proto->reply.len >= HCL_COUNTOF(proto->reply.buf) && write_reply_chunk(proto) <=-1) return -1;
proto->reply.buf[proto->reply.len++] = *ptr++;
len--;
}
return 0;
}
int hcl_server_proto_end_reply (hcl_server_proto_t* proto, const hcl_ooch_t* failmsg) int hcl_server_proto_end_reply (hcl_server_proto_t* proto, const hcl_ooch_t* failmsg)
{ {
HCL_ASSERT (proto->hcl, proto->reply.type == HCL_SERVER_PROTO_REPLY_CHUNKED); HCL_ASSERT (proto->hcl, proto->reply.type == HCL_SERVER_PROTO_REPLY_CHUNKED);

View File

@ -1198,6 +1198,7 @@ enum hcl_iocmd_t
HCL_IO_CLOSE, HCL_IO_CLOSE,
HCL_IO_READ, HCL_IO_READ,
HCL_IO_WRITE, HCL_IO_WRITE,
HCL_IO_WRITE_BYTES,
HCL_IO_FLUSH HCL_IO_FLUSH
}; };
typedef enum hcl_iocmd_t hcl_iocmd_t; typedef enum hcl_iocmd_t hcl_iocmd_t;
@ -1301,18 +1302,21 @@ struct hcl_iooutarg_t
void* handle; void* handle;
/** /**
* [IN] the pointer to the beginning of the character string * [IN] the pointer to the beginning of the character/byte string
* to write * to write.
* hcl_ooch_t* for HCL_IO_WRITE
* hcl_bch_t* or hcl_uint8_t* for HCL_IO_WRITE_BYTES
*/ */
hcl_ooch_t* ptr; void* ptr;
/** /**
* [IN] total number of characters to write * [IN] total number of characters/bytes to write
*/ */
hcl_oow_t len; hcl_oow_t len;
/** /**
* [OUT] place the number of characters written here for HCL_IO_WRITE * [OUT] place the number of characters/bytes written here for
* HCL_IO_WRITE or HCL_IO_WRITE_BYTES
*/ */
hcl_oow_t xlen; hcl_oow_t xlen;
}; };

View File

@ -3562,10 +3562,12 @@ static HCL_INLINE int close_out_stream (hcl_t* hcl, hcl_iooutarg_t* arg)
static HCL_INLINE int write_out_stream (hcl_t* hcl, hcl_iooutarg_t* arg) static HCL_INLINE int write_out_stream (hcl_t* hcl, hcl_iooutarg_t* arg)
{ {
/*xtn_t* xtn = GET_XTN(hcl);*/ /*xtn_t* xtn = GET_XTN(hcl);*/
const hcl_ooch_t* ptr;
hcl_bch_t bcsbuf[1024]; hcl_bch_t bcsbuf[1024];
hcl_oow_t bcslen, ucslen, donelen; hcl_oow_t bcslen, ucslen, donelen;
int x; int x;
ptr = (const hcl_ooch_t*)arg->ptr;
donelen = 0; donelen = 0;
do do
@ -3573,14 +3575,14 @@ static HCL_INLINE int write_out_stream (hcl_t* hcl, hcl_iooutarg_t* arg)
#if defined(HCL_OOCH_IS_UCH) #if defined(HCL_OOCH_IS_UCH)
bcslen = HCL_COUNTOF(bcsbuf); bcslen = HCL_COUNTOF(bcsbuf);
ucslen = arg->len - donelen; ucslen = arg->len - donelen;
x = hcl_convootobchars(hcl, &arg->ptr[donelen], &ucslen, bcsbuf, &bcslen); x = hcl_convootobchars(hcl, &ptr[donelen], &ucslen, bcsbuf, &bcslen);
if (x <= -1 && ucslen <= 0) return -1; if (x <= -1 && ucslen <= 0) return -1;
#else #else
bcslen = HCL_COUNTOF(bcsbuf); bcslen = HCL_COUNTOF(bcsbuf);
ucslen = arg->len - donelen; ucslen = arg->len - donelen;
if (ucslen > bcslen) ucslen = bcslen; if (ucslen > bcslen) ucslen = bcslen;
else if (ucslen < bcslen) bcslen = ucslen; else if (ucslen < bcslen) bcslen = ucslen;
hcl_copy_bchars (bcsbuf, &arg->ptr[donelen], bcslen); hcl_copy_bchars (bcsbuf, &ptr[donelen], bcslen);
#endif #endif
if (fwrite(bcsbuf, HCL_SIZEOF(bcsbuf[0]), bcslen, (FILE*)arg->handle) < bcslen) if (fwrite(bcsbuf, HCL_SIZEOF(bcsbuf[0]), bcslen, (FILE*)arg->handle) < bcslen)
@ -3597,6 +3599,23 @@ static HCL_INLINE int write_out_stream (hcl_t* hcl, hcl_iooutarg_t* arg)
return 0; return 0;
} }
static HCL_INLINE int write_bytes_out_stream (hcl_t* hcl, hcl_iooutarg_t* arg)
{
/*xtn_t* xtn = GET_XTN(hcl);*/
const hcl_uint8_t* ptr;
ptr = (const hcl_uint8_t*)arg->ptr; // take the buffer as a byte series
if (fwrite(ptr, HCL_SIZEOF(*ptr), arg->len, (FILE*)arg->handle) < arg->len)
{
hcl_seterrnum (hcl, HCL_EIOERR);
return -1;
}
arg->xlen = arg->len;
return 0;
}
static HCL_INLINE int flush_out_stream (hcl_t* hcl, hcl_iooutarg_t* arg) static HCL_INLINE int flush_out_stream (hcl_t* hcl, hcl_iooutarg_t* arg)
{ {
FILE* fp; FILE* fp;
@ -3621,6 +3640,9 @@ static int print_handler (hcl_t* hcl, hcl_iocmd_t cmd, void* arg)
case HCL_IO_WRITE: case HCL_IO_WRITE:
return write_out_stream(hcl, (hcl_iooutarg_t*)arg); return write_out_stream(hcl, (hcl_iooutarg_t*)arg);
case HCL_IO_WRITE_BYTES:
return write_bytes_out_stream(hcl, (hcl_iooutarg_t*)arg);
case HCL_IO_FLUSH: case HCL_IO_FLUSH:
return flush_out_stream(hcl, (hcl_iooutarg_t*)arg); return flush_out_stream(hcl, (hcl_iooutarg_t*)arg);