added hcl_copy_ucstr_to_bcstr(), hcl_copy_bcstr_to_ucstr(), hcl_errnum_to_errbcstr(), hcl_errnum_to_errucstr()
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
hyung-hwan 2023-10-29 20:26:48 +09:00
parent dc01f41773
commit e5dc214a59
4 changed files with 181 additions and 96 deletions

167
lib/err.c
View File

@ -63,7 +63,7 @@ static hcl_ooch_t errstr_24[] = {'b','y','t','e','-','c','o','d','e',' ','f','u'
static hcl_ooch_t errstr_25[] = {'d','i','c','t','i','o','n','a','r','y',' ','f','u','l','l','\0'};
static hcl_ooch_t errstr_26[] = {'p','r','o','c','e','s','s','o','r',' ','f','u','l','l','\0'};
static hcl_ooch_t errstr_27[] = {'n','o',' ','m','o','r','e',' ','i','n','p','u','t','\0'};
static hcl_ooch_t errstr_27[] = {'n','o',' ','m','o','r','e',' ','i','n','p','u','t','\0'};
static hcl_ooch_t errstr_28[] = {'t','o','o',' ','m','a','n','y',' ','i','t','e','m','s','\0'};
static hcl_ooch_t errstr_29[] = {'d','i','v','i','d','e',' ','b','y',' ','z','e','r','o','\0'};
@ -93,7 +93,7 @@ static hcl_ooch_t* errstr[] =
};
static char* synerrstr[] =
static char* synerrstr[] =
{
"no error",
"internal error",
@ -163,7 +163,7 @@ static char* synerrstr[] =
"empty m-list"
};
/* --------------------------------------------------------------------------
/* --------------------------------------------------------------------------
* ERROR NUMBER TO STRING CONVERSION
* -------------------------------------------------------------------------- */
const hcl_ooch_t* hcl_errnum_to_errstr (hcl_errnum_t errnum)
@ -172,13 +172,38 @@ const hcl_ooch_t* hcl_errnum_to_errstr (hcl_errnum_t errnum)
return (errnum >= 0 && errnum < HCL_COUNTOF(errstr))? errstr[errnum]: e_unknown;
}
const hcl_bch_t* hcl_errnum_to_errbcstr (hcl_errnum_t errnum, hcl_bch_t* buf, hcl_oow_t len)
{
static hcl_bch_t e_unknown[] = {'u','n','k','n','o','w','n',' ','e','r','r','o','r','\0'};
/* it's ok to copy without conversion because the messages above are simple acsii text */
#if defined(HCL_OOCH_IS_BCH)
hcl_copy_bcstr(buf, len, (errnum >= 0 && errnum < HCL_COUNTOF(errstr))? errstr[errnum]: e_unknown);
#else
hcl_copy_ucstr_to_bcstr(buf, len, (errnum >= 0 && errnum < HCL_COUNTOF(errstr))? errstr[errnum]: e_unknown);
#endif
return buf;
}
const hcl_uch_t* hcl_errnum_to_errucstr (hcl_errnum_t errnum, hcl_uch_t* buf, hcl_oow_t len)
{
static hcl_uch_t e_unknown[] = {'u','n','k','n','o','w','n',' ','e','r','r','o','r','\0'};
/* it's ok to copy without conversion because the messages above are simple acsii text */
#if defined(HCL_OOCH_IS_BCH)
hcl_copy_bcstr_to_ucstr(buf, len, (errnum >= 0 && errnum < HCL_COUNTOF(errstr))? errstr[errnum]: e_unknown);
#else
hcl_copy_ucstr(buf, len, (errnum >= 0 && errnum < HCL_COUNTOF(errstr))? errstr[errnum]: e_unknown);
#endif
return buf;
}
static const hcl_bch_t* synerr_to_errstr (hcl_synerrnum_t errnum)
{
static hcl_bch_t e_unknown[] = "unknown error";
return (errnum >= 0 && errnum < HCL_COUNTOF(synerrstr))? synerrstr[errnum]: e_unknown;
}
/* --------------------------------------------------------------------------
/* --------------------------------------------------------------------------
* ERROR NUMBER/MESSAGE HANDLING
* -------------------------------------------------------------------------- */
@ -238,8 +263,8 @@ const hcl_ooch_t* hcl_backuperrmsg (hcl_t* hcl)
void hcl_seterrnum (hcl_t* hcl, hcl_errnum_t errnum)
{
if (hcl->shuterr) return;
hcl->errnum = errnum;
hcl->errmsg.len = 0;
hcl->errnum = errnum;
hcl->errmsg.len = 0;
}
void hcl_seterrbmsg (hcl_t* hcl, hcl_errnum_t errnum, const hcl_bch_t* errmsg)
@ -405,11 +430,11 @@ void hcl_seterrbfmtwithsyserr (hcl_t* hcl, int syserr_type, int syserr_code, con
va_list ap;
if (hcl->shuterr) return;
if (hcl->vmprim.syserrstrb)
{
errnum = hcl->vmprim.syserrstrb(hcl, syserr_type, syserr_code, hcl->errmsg.tmpbuf.bch, HCL_COUNTOF(hcl->errmsg.tmpbuf.bch));
va_start (ap, fmt);
hcl_seterrbfmtv (hcl, errnum, fmt, ap);
va_end (ap);
@ -462,7 +487,7 @@ void hcl_seterrufmtwithsyserr (hcl_t* hcl, int syserr_type, int syserr_code, con
va_list ap;
if (hcl->shuterr) return;
if (hcl->vmprim.syserrstrb)
{
errnum = hcl->vmprim.syserrstrb(hcl, syserr_type, syserr_code, hcl->errmsg.tmpbuf.bch, HCL_COUNTOF(hcl->errmsg.tmpbuf.bch));
@ -534,7 +559,7 @@ void hcl_setsynerrbfmt (hcl_t* hcl, hcl_synerrnum_t num, const hcl_ioloc_t* loc,
if (hcl->shuterr) return;
if (msgfmt)
if (msgfmt)
{
va_list ap;
int i, selen;
@ -548,73 +573,14 @@ void hcl_setsynerrbfmt (hcl_t* hcl, hcl_synerrnum_t num, const hcl_ioloc_t* loc,
for (i = 0; i < selen; i++) hcl->errmsg.buf[i] = syntax_error[i];
hcl->errmsg.buf[HCL_COUNTOF(hcl->errmsg.buf) - 1] = '\0';
}
else
{
hcl_seterrbfmt (hcl, HCL_ESYNERR, "%hs%hs", syntax_error, synerr_to_errstr(num));
}
hcl->c->synerr.num = num;
/* The SCO compiler complains of this ternary operation saying:
* error: operands have incompatible types: op ":"
* it seems to complain of type mismatch between *loc and
* hcl->c->tok.loc due to 'const' prefixed to loc. */
/*hcl->c->synerr.loc = loc? *loc: hcl->c->tok.loc;*/
if (loc)
{
hcl->c->synerr.loc = *loc;
}
else
{
hcl->c->synerr.loc = hcl->c->tok.loc;
}
if (tgt)
{
hcl_oow_t n;
n = hcl_copy_oochars_to_oocstr(hcl->c->synerr.tgt.val, HCL_COUNTOF(hcl->c->synerr.tgt.val), tgt->ptr, tgt->len);
if (n < tgt->len)
{
hcl->c->synerr.tgt.val[n - 1] = '.';
hcl->c->synerr.tgt.val[n - 2] = '.';
hcl->c->synerr.tgt.val[n - 3] = '.';
}
hcl->c->synerr.tgt.len = n;
}
else
{
hcl->c->synerr.tgt.val[0] = '\0';
hcl->c->synerr.tgt.len = 0;
}
}
void hcl_setsynerrufmt (hcl_t* hcl, hcl_synerrnum_t num, const hcl_ioloc_t* loc, const hcl_oocs_t* tgt, const hcl_uch_t* msgfmt, ...)
{
static hcl_bch_t syntax_error[] = "syntax error - ";
if (hcl->shuterr) return;
if (msgfmt)
{
va_list ap;
int i, selen;
va_start (ap, msgfmt);
hcl_seterrufmtv (hcl, HCL_ESYNERR, msgfmt, ap);
va_end (ap);
selen = HCL_COUNTOF(syntax_error) - 1;
HCL_MEMMOVE (&hcl->errmsg.buf[selen], &hcl->errmsg.buf[0], HCL_SIZEOF(hcl->errmsg.buf[0]) * (HCL_COUNTOF(hcl->errmsg.buf) - selen));
for (i = 0; i < selen; i++) hcl->errmsg.buf[i] = syntax_error[i];
hcl->errmsg.buf[HCL_COUNTOF(hcl->errmsg.buf) - 1] = '\0';
}
else
{
hcl_seterrbfmt (hcl, HCL_ESYNERR, "%hs%hs", syntax_error, synerr_to_errstr(num));
}
hcl->c->synerr.num = num;
/* The SCO compiler complains of this ternary operation saying:
* error: operands have incompatible types: op ":"
* error: operands have incompatible types: op ":"
* it seems to complain of type mismatch between *loc and
* hcl->c->tok.loc due to 'const' prefixed to loc. */
/*hcl->c->synerr.loc = loc? *loc: hcl->c->tok.loc;*/
@ -639,7 +605,66 @@ void hcl_setsynerrufmt (hcl_t* hcl, hcl_synerrnum_t num, const hcl_ioloc_t* loc,
}
hcl->c->synerr.tgt.len = n;
}
else
else
{
hcl->c->synerr.tgt.val[0] = '\0';
hcl->c->synerr.tgt.len = 0;
}
}
void hcl_setsynerrufmt (hcl_t* hcl, hcl_synerrnum_t num, const hcl_ioloc_t* loc, const hcl_oocs_t* tgt, const hcl_uch_t* msgfmt, ...)
{
static hcl_bch_t syntax_error[] = "syntax error - ";
if (hcl->shuterr) return;
if (msgfmt)
{
va_list ap;
int i, selen;
va_start (ap, msgfmt);
hcl_seterrufmtv (hcl, HCL_ESYNERR, msgfmt, ap);
va_end (ap);
selen = HCL_COUNTOF(syntax_error) - 1;
HCL_MEMMOVE (&hcl->errmsg.buf[selen], &hcl->errmsg.buf[0], HCL_SIZEOF(hcl->errmsg.buf[0]) * (HCL_COUNTOF(hcl->errmsg.buf) - selen));
for (i = 0; i < selen; i++) hcl->errmsg.buf[i] = syntax_error[i];
hcl->errmsg.buf[HCL_COUNTOF(hcl->errmsg.buf) - 1] = '\0';
}
else
{
hcl_seterrbfmt (hcl, HCL_ESYNERR, "%hs%hs", syntax_error, synerr_to_errstr(num));
}
hcl->c->synerr.num = num;
/* The SCO compiler complains of this ternary operation saying:
* error: operands have incompatible types: op ":"
* it seems to complain of type mismatch between *loc and
* hcl->c->tok.loc due to 'const' prefixed to loc. */
/*hcl->c->synerr.loc = loc? *loc: hcl->c->tok.loc;*/
if (loc)
{
hcl->c->synerr.loc = *loc;
}
else
{
hcl->c->synerr.loc = hcl->c->tok.loc;
}
if (tgt)
{
hcl_oow_t n;
n = hcl_copy_oochars_to_oocstr(hcl->c->synerr.tgt.val, HCL_COUNTOF(hcl->c->synerr.tgt.val), tgt->ptr, tgt->len);
if (n < tgt->len)
{
hcl->c->synerr.tgt.val[n - 1] = '.';
hcl->c->synerr.tgt.val[n - 2] = '.';
hcl->c->synerr.tgt.val[n - 3] = '.';
}
hcl->c->synerr.tgt.len = n;
}
else
{
hcl->c->synerr.tgt.val[0] = '\0';
hcl->c->synerr.tgt.len = 0;

View File

@ -452,6 +452,18 @@ HCL_EXPORT void hcl_copy_uchars_to_bchars (
hcl_oow_t len
);
HCL_EXPORT hcl_oow_t hcl_copy_bcstr_to_ucstr (
hcl_uch_t* dst,
hcl_oow_t len,
const hcl_bch_t* src
);
HCL_EXPORT hcl_oow_t hcl_copy_ucstr_to_bcstr (
hcl_bch_t* dst,
hcl_oow_t len,
const hcl_uch_t* src
);
HCL_EXPORT hcl_oow_t hcl_copy_uchars_to_ucstr_unlimited (
hcl_uch_t* dst,
const hcl_uch_t* src,
@ -628,7 +640,7 @@ HCL_EXPORT hcl_oow_t hcl_count_bcstr (
#define HCL_BYTE_TO_BCSTR_LOWERCASE (1 << 8)
hcl_oow_t hcl_byte_to_bcstr (
hcl_uint8_t byte,
hcl_uint8_t byte,
hcl_bch_t* buf,
hcl_oow_t size,
int flagged_radix,
@ -644,7 +656,7 @@ HCL_EXPORT int hcl_conv_bcstr_to_ucstr_with_cmgr (
hcl_cmgr_t* cmgr,
int all
);
HCL_EXPORT int hcl_conv_bchars_to_uchars_with_cmgr (
const hcl_bch_t* bcs,
hcl_oow_t* bcslen,
@ -660,7 +672,7 @@ HCL_EXPORT int hcl_conv_ucstr_to_bcstr_with_cmgr (
hcl_bch_t* bcs,
hcl_oow_t* bcslen,
hcl_cmgr_t* cmgr
);
);
HCL_EXPORT int hcl_conv_uchars_to_bchars_with_cmgr (
const hcl_uch_t* ucs,
@ -683,7 +695,7 @@ HCL_EXPORT hcl_cmgr_t* hcl_get_utf8_cmgr (
);
/**
* The hcl_conv_uchars_to_utf8() function converts a unicode character string \a ucs
* The hcl_conv_uchars_to_utf8() function converts a unicode character string \a ucs
* to a UTF8 string and writes it into the buffer pointed to by \a bcs, but
* not more than \a bcslen bytes including the terminating null.
*
@ -732,7 +744,7 @@ HCL_EXPORT int hcl_conv_uchars_to_utf8 (
* n = hcl_conv_utf8_to_uchars (bcs, &bcslen, ucs, &ucslen);
* if (n <= -1) { invalid/incomplenete sequence or buffer to small }
* \endcode
*
*
* The resulting \a ucslen can still be greater than 0 even if the return
* value is negative. The value indiates the number of characters converted
* before the error has occurred.
@ -785,19 +797,19 @@ HCL_EXPORT int hcl_ucwidth (
* TIME CALCULATION WITH OVERFLOW/UNDERFLOW DETECTION
* ========================================================================= */
/**
/**
* The hcl_add_ntime() function adds two time structures pointed to by \a x and \a y
* and stores the result in the structure pointed to by \a z. If it detects overflow/
* underflow, it stores the largest/least possible value respectively.
* You may use the HCL_ADD_NTIME() macro if overflow/underflow check isn't needed.
*/
HCL_EXPORT void hcl_add_ntime (
hcl_ntime_t* z,
hcl_ntime_t* z,
const hcl_ntime_t* x,
const hcl_ntime_t* y
);
/**
/**
* The hcl_sub_ntime() function subtracts the time value \a y from the time value \a x
* and stores the result in the structure pointed to by \a z. If it detects overflow/
* underflow, it stores the largest/least possible value respectively.
@ -856,9 +868,9 @@ static HCL_INLINE hcl_uint32_t hcl_bswap32 (hcl_uint32_t x)
);
return x;
#else
return ((x >> 24)) |
((x >> 8) & ((hcl_uint32_t)0xff << 8)) |
((x << 8) & ((hcl_uint32_t)0xff << 16)) |
return ((x >> 24)) |
((x >> 8) & ((hcl_uint32_t)0xff << 8)) |
((x << 8) & ((hcl_uint32_t)0xff << 16)) |
((x << 24));
#endif
}
@ -876,13 +888,13 @@ static HCL_INLINE hcl_uint64_t hcl_bswap64 (hcl_uint64_t x)
__asm__ /*volatile*/ ("rev %0, %0" : "+r"(x));
return x;
#else
return ((x >> 56)) |
((x >> 40) & ((hcl_uint64_t)0xff << 8)) |
((x >> 24) & ((hcl_uint64_t)0xff << 16)) |
((x >> 8) & ((hcl_uint64_t)0xff << 24)) |
((x << 8) & ((hcl_uint64_t)0xff << 32)) |
((x << 24) & ((hcl_uint64_t)0xff << 40)) |
((x << 40) & ((hcl_uint64_t)0xff << 48)) |
return ((x >> 56)) |
((x >> 40) & ((hcl_uint64_t)0xff << 8)) |
((x >> 24) & ((hcl_uint64_t)0xff << 16)) |
((x >> 8) & ((hcl_uint64_t)0xff << 24)) |
((x << 8) & ((hcl_uint64_t)0xff << 32)) |
((x << 24) & ((hcl_uint64_t)0xff << 40)) |
((x << 40) & ((hcl_uint64_t)0xff << 48)) |
((x << 56));
#endif
}
@ -894,7 +906,7 @@ static HCL_INLINE hcl_uint128_t hcl_bswap128 (hcl_uint128_t x)
#if defined(HCL_HAVE_BUILTIN_BSWAP128)
return __builtin_bswap128(x);
#else
return ((x >> 120)) |
return ((x >> 120)) |
((x >> 104) & ((hcl_uint128_t)0xff << 8)) |
((x >> 88) & ((hcl_uint128_t)0xff << 16)) |
((x >> 72) & ((hcl_uint128_t)0xff << 24)) |
@ -919,7 +931,7 @@ static HCL_INLINE hcl_uint128_t hcl_bswap128 (hcl_uint128_t x)
#if defined(HCL_HAVE_UINT16_T)
# if defined(HCL_HAVE_BUILTIN_BSWAP16)
# define hcl_bswap16(x) ((hcl_uint16_t)__builtin_bswap16((hcl_uint16_t)(x)))
# else
# else
# define hcl_bswap16(x) ((hcl_uint16_t)(((hcl_uint16_t)(x)) << 8) | (((hcl_uint16_t)(x)) >> 8))
# endif
#endif
@ -927,7 +939,7 @@ static HCL_INLINE hcl_uint128_t hcl_bswap128 (hcl_uint128_t x)
#if defined(HCL_HAVE_UINT32_T)
# if defined(HCL_HAVE_BUILTIN_BSWAP32)
# define hcl_bswap32(x) ((hcl_uint32_t)__builtin_bswap32((hcl_uint32_t)(x)))
# else
# else
# define hcl_bswap32(x) ((hcl_uint32_t)(((((hcl_uint32_t)(x)) >> 24)) | \
((((hcl_uint32_t)(x)) >> 8) & ((hcl_uint32_t)0xff << 8)) | \
((((hcl_uint32_t)(x)) << 8) & ((hcl_uint32_t)0xff << 16)) | \
@ -938,7 +950,7 @@ static HCL_INLINE hcl_uint128_t hcl_bswap128 (hcl_uint128_t x)
#if defined(HCL_HAVE_UINT64_T)
# if defined(HCL_HAVE_BUILTIN_BSWAP64)
# define hcl_bswap64(x) ((hcl_uint64_t)__builtin_bswap64((hcl_uint64_t)(x)))
# else
# else
# define hcl_bswap64(x) ((hcl_uint64_t)(((((hcl_uint64_t)(x)) >> 56)) | \
((((hcl_uint64_t)(x)) >> 40) & ((hcl_uint64_t)0xff << 8)) | \
((((hcl_uint64_t)(x)) >> 24) & ((hcl_uint64_t)0xff << 16)) | \
@ -953,7 +965,7 @@ static HCL_INLINE hcl_uint128_t hcl_bswap128 (hcl_uint128_t x)
#if defined(HCL_HAVE_UINT128_T)
# if defined(HCL_HAVE_BUILTIN_BSWAP128)
# define hcl_bswap128(x) ((hcl_uint128_t)__builtin_bswap128((hcl_uint128_t)(x)))
# else
# else
# define hcl_bswap128(x) ((hcl_uint128_t)(((((hcl_uint128_t)(x)) >> 120)) | \
((((hcl_uint128_t)(x)) >> 104) & ((hcl_uint128_t)0xff << 8)) | \
((((hcl_uint128_t)(x)) >> 88) & ((hcl_uint128_t)0xff << 16)) | \
@ -1098,7 +1110,7 @@ static HCL_INLINE int hcl_get_pos_of_msb_set_pow2 (hcl_oow_t x)
: "=r"(n) /* output */
: "r"(x) /* input */
);
return (int)(HCL_OOW_BITS - n - 1);
return (int)(HCL_OOW_BITS - n - 1);
/* TODO: PPC - use cntlz, cntlzw, cntlzd, SPARC - use lzcnt, MIPS clz */
#else
int pos = 0;
@ -1132,7 +1144,7 @@ static HCL_INLINE int hcl_get_pos_of_msb_set (hcl_oow_t x)
: "=r"(n) /* output */
: "r"(x) /* input */
);
return (int)(HCL_OOW_BITS - n - 1);
return (int)(HCL_OOW_BITS - n - 1);
/* TODO: PPC - use cntlz, cntlzw, cntlzd, SPARC - use lzcnt, MIPS clz */
#else
int pos = 0;

View File

@ -2119,6 +2119,18 @@ HCL_EXPORT const hcl_ooch_t* hcl_errnum_to_errstr (
hcl_errnum_t errnum
);
HCL_EXPORT const hcl_bch_t* hcl_errnum_to_errbcstr (
hcl_errnum_t errnum,
hcl_bch_t* buf,
hcl_oow_t len
);
HCL_EXPORT const hcl_uch_t* hcl_errnum_to_errucstr (
hcl_errnum_t errnum,
hcl_uch_t* buf,
hcl_oow_t len
);
/**
* The hcl_getoption() function gets the value of an option
* specified by \a id into the buffer pointed to by \a value.

View File

@ -229,6 +229,42 @@ void hcl_copy_uchars_to_bchars (hcl_bch_t* dst, const hcl_uch_t* src, hcl_oow_t
for (i = 0; i < len; i++) dst[i] = src[i];
}
hcl_oow_t hcl_copy_bcstr_to_ucstr (hcl_uch_t* dst, hcl_oow_t len, const hcl_bch_t* src)
{
/* copy without conversions.
* the code is the same as hcl_copy_bcstr() except type of src */
hcl_uch_t* p, * p2;
p = dst; p2 = dst + len - 1;
while (p < p2)
{
if (*src == '\0') break;
*p++ = *src++;
}
if (len > 0) *p = '\0';
return p - dst;
}
hcl_oow_t hcl_copy_ucstr_to_bcstr (hcl_bch_t* dst, hcl_oow_t len, const hcl_uch_t* src)
{
/* copy without conversions */
hcl_bch_t* p, * p2;
p = dst; p2 = dst + len - 1;
while (p < p2)
{
if (*src == '\0') break;
*p++ = *src++;
}
if (len > 0) *p = '\0';
return p - dst;
}
hcl_oow_t hcl_copy_uchars_to_ucstr (hcl_uch_t* dst, hcl_oow_t dlen, const hcl_uch_t* src, hcl_oow_t slen)
{
hcl_oow_t i;