migrated more common functions to gem.c
This commit is contained in:
parent
96c87440f0
commit
62211e8934
427
hawk/lib/gem.c
427
hawk/lib/gem.c
@ -48,7 +48,7 @@ void* hawk_gem_reallocmem (hawk_gem_t* gem, void* ptr, hawk_oow_t size)
|
|||||||
return nptr;
|
return nptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------------ */
|
/* ------------------------------------------------------------------------ */
|
||||||
|
|
||||||
hawk_uch_t* hawk_gem_dupucstr (hawk_gem_t* gem, const hawk_uch_t* ucs, hawk_oow_t* _ucslen)
|
hawk_uch_t* hawk_gem_dupucstr (hawk_gem_t* gem, const hawk_uch_t* ucs, hawk_oow_t* _ucslen)
|
||||||
{
|
{
|
||||||
@ -166,3 +166,428 @@ hawk_bch_t* hawk_gem_dupbcstrarr (hawk_gem_t* gem, const hawk_bch_t* str[], hawk
|
|||||||
if (len) *len = capa;
|
if (len) *len = capa;
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
int hawk_gem_convbtouchars (hawk_gem_t* gem, const hawk_bch_t* bcs, hawk_oow_t* bcslen, hawk_uch_t* ucs, hawk_oow_t* ucslen, int all)
|
||||||
|
{
|
||||||
|
/* length bound */
|
||||||
|
int n;
|
||||||
|
n = hawk_conv_bchars_to_uchars_with_cmgr(bcs, bcslen, ucs, ucslen, gem->cmgr, all);
|
||||||
|
/* -1: illegal character, -2: buffer too small, -3: incomplete sequence */
|
||||||
|
if (n <= -1) hawk_gem_seterrnum (gem, HAWK_NULL, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
int hawk_gem_convutobchars (hawk_gem_t* gem, const hawk_uch_t* ucs, hawk_oow_t* ucslen, hawk_bch_t* bcs, hawk_oow_t* bcslen)
|
||||||
|
{
|
||||||
|
/* length bound */
|
||||||
|
int n;
|
||||||
|
n = hawk_conv_uchars_to_bchars_with_cmgr(ucs, ucslen, bcs, bcslen, gem->cmgr);
|
||||||
|
if (n <= -1) hawk_gem_seterrnum (gem, HAWK_NULL, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
int hawk_gem_convbtoucstr (hawk_gem_t* gem, const hawk_bch_t* bcs, hawk_oow_t* bcslen, hawk_uch_t* ucs, hawk_oow_t* ucslen, int all)
|
||||||
|
{
|
||||||
|
/* null-terminated. */
|
||||||
|
int n;
|
||||||
|
n = hawk_conv_bcstr_to_ucstr_with_cmgr(bcs, bcslen, ucs, ucslen, gem->cmgr, all);
|
||||||
|
if (n <= -1) hawk_gem_seterrnum (gem, HAWK_NULL, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
int hawk_gem_convutobcstr (hawk_gem_t* gem, const hawk_uch_t* ucs, hawk_oow_t* ucslen, hawk_bch_t* bcs, hawk_oow_t* bcslen)
|
||||||
|
{
|
||||||
|
/* null-terminated */
|
||||||
|
int n;
|
||||||
|
n = hawk_conv_ucstr_to_bcstr_with_cmgr(ucs, ucslen, bcs, bcslen, gem->cmgr);
|
||||||
|
if (n <= -1) hawk_gem_seterrnum (gem, HAWK_NULL, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
hawk_uch_t* hawk_gem_dupbtouchars (hawk_gem_t* gem, const hawk_bch_t* bcs, hawk_oow_t _bcslen, hawk_oow_t* _ucslen, int all)
|
||||||
|
{
|
||||||
|
hawk_oow_t bcslen, ucslen;
|
||||||
|
hawk_uch_t* ucs;
|
||||||
|
|
||||||
|
bcslen = _bcslen;
|
||||||
|
if (hawk_gem_convbtouchars(gem, bcs, &bcslen, HAWK_NULL, &ucslen, all) <= -1) return HAWK_NULL;
|
||||||
|
|
||||||
|
ucs = hawk_gem_allocmem(gem, HAWK_SIZEOF(*ucs) * (ucslen + 1));
|
||||||
|
if (!ucs) return HAWK_NULL;
|
||||||
|
|
||||||
|
bcslen= _bcslen;
|
||||||
|
hawk_gem_convbtouchars (gem, bcs, &bcslen, ucs, &ucslen, all);
|
||||||
|
ucs[ucslen] = '\0';
|
||||||
|
|
||||||
|
if (_ucslen) *_ucslen = ucslen;
|
||||||
|
return ucs;
|
||||||
|
}
|
||||||
|
|
||||||
|
hawk_bch_t* hawk_gem_duputobchars (hawk_gem_t* gem, const hawk_uch_t* ucs, hawk_oow_t _ucslen, hawk_oow_t* _bcslen)
|
||||||
|
{
|
||||||
|
hawk_oow_t bcslen, ucslen;
|
||||||
|
hawk_bch_t* bcs;
|
||||||
|
|
||||||
|
ucslen = _ucslen;
|
||||||
|
if (hawk_gem_convutobchars(gem, ucs, &ucslen, HAWK_NULL, &bcslen) <= -1) return HAWK_NULL;
|
||||||
|
|
||||||
|
bcs = hawk_gem_allocmem(gem, HAWK_SIZEOF(*bcs) * (bcslen + 1));
|
||||||
|
if (!bcs) return HAWK_NULL;
|
||||||
|
|
||||||
|
ucslen = _ucslen;
|
||||||
|
hawk_gem_convutobchars (gem, ucs, &ucslen, bcs, &bcslen);
|
||||||
|
bcs[bcslen] = '\0';
|
||||||
|
|
||||||
|
if (_bcslen) *_bcslen = bcslen;
|
||||||
|
return bcs;
|
||||||
|
}
|
||||||
|
|
||||||
|
hawk_uch_t* hawk_gem_dupb2touchars (hawk_gem_t* gem, const hawk_bch_t* bcs1, hawk_oow_t bcslen1, const hawk_bch_t* bcs2, hawk_oow_t bcslen2, hawk_oow_t* ucslen, int all)
|
||||||
|
{
|
||||||
|
hawk_oow_t inlen, outlen1, outlen2;
|
||||||
|
hawk_uch_t* ptr;
|
||||||
|
|
||||||
|
inlen = bcslen1;
|
||||||
|
if (hawk_gem_convbtouchars(gem, bcs1, &inlen, HAWK_NULL, &outlen1, all) <= -1) return HAWK_NULL;
|
||||||
|
|
||||||
|
inlen = bcslen2;
|
||||||
|
if (hawk_gem_convbtouchars(gem, bcs2, &inlen, HAWK_NULL, &outlen2, all) <= -1) return HAWK_NULL;
|
||||||
|
|
||||||
|
ptr = (hawk_uch_t*)hawk_gem_allocmem(gem, (outlen1 + outlen2 + 1) * HAWK_SIZEOF(*ptr));
|
||||||
|
if (!ptr) return HAWK_NULL;
|
||||||
|
|
||||||
|
inlen = bcslen1;
|
||||||
|
hawk_gem_convbtouchars (gem, bcs1, &inlen, &ptr[0], &outlen1, all);
|
||||||
|
|
||||||
|
inlen = bcslen2;
|
||||||
|
hawk_gem_convbtouchars (gem, bcs2, &inlen, &ptr[outlen1], &outlen2, all);
|
||||||
|
|
||||||
|
/* hawk_convbtouchars() doesn't null-terminate the target.
|
||||||
|
* but in hawk_dupbtouchars(), i allocate space. so i don't mind
|
||||||
|
* null-terminating it with 1 extra character overhead */
|
||||||
|
ptr[outlen1 + outlen2] = '\0';
|
||||||
|
if (ucslen) *ucslen = outlen1 + outlen2;
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
hawk_bch_t* hawk_gem_dupu2tobchars (hawk_gem_t* gem, const hawk_uch_t* ucs1, hawk_oow_t ucslen1, const hawk_uch_t* ucs2, hawk_oow_t ucslen2, hawk_oow_t* bcslen)
|
||||||
|
{
|
||||||
|
hawk_oow_t inlen, outlen1, outlen2;
|
||||||
|
hawk_bch_t* ptr;
|
||||||
|
|
||||||
|
inlen = ucslen1;
|
||||||
|
if (hawk_gem_convutobchars(gem, ucs1, &inlen, HAWK_NULL, &outlen1) <= -1) return HAWK_NULL;
|
||||||
|
|
||||||
|
inlen = ucslen2;
|
||||||
|
if (hawk_gem_convutobchars(gem, ucs2, &inlen, HAWK_NULL, &outlen2) <= -1) return HAWK_NULL;
|
||||||
|
|
||||||
|
ptr = (hawk_bch_t*)hawk_gem_allocmem(gem, (outlen1 + outlen2 + 1) * HAWK_SIZEOF(*ptr));
|
||||||
|
if (!ptr) return HAWK_NULL;
|
||||||
|
|
||||||
|
inlen = ucslen1;
|
||||||
|
hawk_gem_convutobchars (gem, ucs1, &inlen, &ptr[0], &outlen1);
|
||||||
|
|
||||||
|
inlen = ucslen2;
|
||||||
|
hawk_gem_convutobchars (gem, ucs2, &inlen, &ptr[outlen1], &outlen2);
|
||||||
|
|
||||||
|
ptr[outlen1 + outlen2] = '\0';
|
||||||
|
if (bcslen) *bcslen = outlen1 + outlen2;
|
||||||
|
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
hawk_uch_t* hawk_gem_dupbtoucstr (hawk_gem_t* gem, const hawk_bch_t* bcs, hawk_oow_t* _ucslen, int all)
|
||||||
|
{
|
||||||
|
hawk_oow_t bcslen, ucslen;
|
||||||
|
hawk_uch_t* ucs;
|
||||||
|
|
||||||
|
if (hawk_gem_convbtoucstr(gem, bcs, &bcslen, HAWK_NULL, &ucslen, all) <= -1) return HAWK_NULL;
|
||||||
|
ucslen = ucslen + 1; /* for terminating null */
|
||||||
|
|
||||||
|
ucs = hawk_gem_allocmem(gem, HAWK_SIZEOF(*ucs) * ucslen);
|
||||||
|
if (!ucs) return HAWK_NULL;
|
||||||
|
|
||||||
|
hawk_gem_convbtoucstr (gem, bcs, &bcslen, ucs, &ucslen, all);
|
||||||
|
if (_ucslen) *_ucslen = ucslen;
|
||||||
|
return ucs;
|
||||||
|
}
|
||||||
|
|
||||||
|
hawk_bch_t* hawk_gem_duputobcstr (hawk_gem_t* gem, const hawk_uch_t* ucs, hawk_oow_t* _bcslen)
|
||||||
|
{
|
||||||
|
hawk_oow_t bcslen, ucslen;
|
||||||
|
hawk_bch_t* bcs;
|
||||||
|
|
||||||
|
if (hawk_gem_convutobcstr(gem, ucs, &ucslen, HAWK_NULL, &bcslen) <= -1) return HAWK_NULL;
|
||||||
|
bcslen = bcslen + 1; /* for terminating null */
|
||||||
|
|
||||||
|
bcs = hawk_gem_allocmem(gem, HAWK_SIZEOF(*bcs) * bcslen);
|
||||||
|
if (!bcs) return HAWK_NULL;
|
||||||
|
|
||||||
|
hawk_gem_convutobcstr (gem, ucs, &ucslen, bcs, &bcslen);
|
||||||
|
if (_bcslen) *_bcslen = bcslen;
|
||||||
|
return bcs;
|
||||||
|
}
|
||||||
|
|
||||||
|
hawk_uch_t* hawk_gem_dupbtoucharswithcmgr (hawk_gem_t* gem, const hawk_bch_t* bcs, hawk_oow_t _bcslen, hawk_oow_t* _ucslen, hawk_cmgr_t* cmgr, int all)
|
||||||
|
{
|
||||||
|
hawk_oow_t bcslen, ucslen;
|
||||||
|
hawk_uch_t* ucs;
|
||||||
|
int n;
|
||||||
|
|
||||||
|
bcslen = _bcslen;
|
||||||
|
n = hawk_conv_bchars_to_uchars_with_cmgr(bcs, &bcslen, HAWK_NULL, &ucslen, cmgr, all);
|
||||||
|
if (n <= -1)
|
||||||
|
{
|
||||||
|
/* -1: illegal character, -2: buffer too small, -3: incomplete sequence */
|
||||||
|
hawk_gem_seterrnum (gem, HAWK_NULL, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR);
|
||||||
|
return HAWK_NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ucs = hawk_gem_allocmem(gem, HAWK_SIZEOF(*ucs) * (ucslen + 1));
|
||||||
|
if (!ucs) return HAWK_NULL;
|
||||||
|
|
||||||
|
bcslen= _bcslen;
|
||||||
|
hawk_conv_bchars_to_uchars_with_cmgr(bcs, &bcslen, ucs, &ucslen, cmgr, all);
|
||||||
|
ucs[ucslen] = '\0';
|
||||||
|
|
||||||
|
if (_ucslen) *_ucslen = ucslen;
|
||||||
|
return ucs;
|
||||||
|
}
|
||||||
|
|
||||||
|
hawk_bch_t* hawk_gem_duputobcharswithcmgr (hawk_gem_t* gem, const hawk_uch_t* ucs, hawk_oow_t _ucslen, hawk_oow_t* _bcslen, hawk_cmgr_t* cmgr)
|
||||||
|
{
|
||||||
|
hawk_oow_t bcslen, ucslen;
|
||||||
|
hawk_bch_t* bcs;
|
||||||
|
int n;
|
||||||
|
|
||||||
|
ucslen = _ucslen;
|
||||||
|
n = hawk_conv_uchars_to_bchars_with_cmgr(ucs, &ucslen, HAWK_NULL, &bcslen, cmgr);
|
||||||
|
if (n <= -1)
|
||||||
|
{
|
||||||
|
/* -1: illegal character, -2: buffer too small, -3: incomplete sequence */
|
||||||
|
hawk_gem_seterrnum (gem, HAWK_NULL, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR);
|
||||||
|
return HAWK_NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
bcs = hawk_gem_allocmem(gem, HAWK_SIZEOF(*bcs) * (bcslen + 1));
|
||||||
|
if (!bcs) return HAWK_NULL;
|
||||||
|
|
||||||
|
ucslen = _ucslen;
|
||||||
|
hawk_conv_uchars_to_bchars_with_cmgr (ucs, &ucslen, bcs, &bcslen, cmgr);
|
||||||
|
bcs[bcslen] = '\0';
|
||||||
|
|
||||||
|
if (_bcslen) *_bcslen = bcslen;
|
||||||
|
return bcs;
|
||||||
|
}
|
||||||
|
|
||||||
|
hawk_bch_t* hawk_gem_dupucstrarrtobcstr (hawk_gem_t* gem, const hawk_uch_t* ucs[], hawk_oow_t* bcslen)
|
||||||
|
{
|
||||||
|
hawk_oow_t wl, ml, capa, pos, i;
|
||||||
|
hawk_bch_t* bcs;
|
||||||
|
|
||||||
|
for (capa = 0, i = 0; ucs[i]; i++)
|
||||||
|
{
|
||||||
|
if (hawk_gem_convutobcstr(gem, ucs[i], &wl, HAWK_NULL, &ml) <= -1) return HAWK_NULL;
|
||||||
|
capa += ml;
|
||||||
|
}
|
||||||
|
|
||||||
|
bcs = (hawk_bch_t*)hawk_gem_allocmem(gem, (capa + 1) * HAWK_SIZEOF(*bcs));
|
||||||
|
if (!bcs) return HAWK_NULL;
|
||||||
|
|
||||||
|
for (pos = 0, i = 0; ucs[i]; i++)
|
||||||
|
{
|
||||||
|
ml = capa - pos + 1;
|
||||||
|
hawk_gem_convutobcstr (gem, ucs[i], &wl, &bcs[pos], &ml);
|
||||||
|
pos += ml;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bcslen) *bcslen = capa;
|
||||||
|
return bcs;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
struct fmt_uch_buf_t
|
||||||
|
{
|
||||||
|
hawk_gem_t* gem;
|
||||||
|
hawk_uch_t* ptr;
|
||||||
|
hawk_oow_t len;
|
||||||
|
hawk_oow_t capa;
|
||||||
|
};
|
||||||
|
typedef struct fmt_uch_buf_t fmt_uch_buf_t;
|
||||||
|
|
||||||
|
static int fmt_put_bchars_to_uch_buf (hawk_fmtout_t* fmtout, const hawk_bch_t* ptr, hawk_oow_t len)
|
||||||
|
{
|
||||||
|
fmt_uch_buf_t* b = (fmt_uch_buf_t*)fmtout->ctx;
|
||||||
|
hawk_oow_t bcslen, ucslen;
|
||||||
|
int n;
|
||||||
|
|
||||||
|
bcslen = b->capa - b->len;
|
||||||
|
ucslen = len;
|
||||||
|
n = hawk_conv_bchars_to_uchars_with_cmgr(ptr, &bcslen, &b->ptr[b->len], &ucslen, b->gem->cmgr, 1);
|
||||||
|
if (n <= -1)
|
||||||
|
{
|
||||||
|
if (n == -2)
|
||||||
|
{
|
||||||
|
return 0; /* buffer full. stop */
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hawk_gem_seterrnum (b->gem, HAWK_NULL, HAWK_EECERR);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1; /* success. carry on */
|
||||||
|
}
|
||||||
|
|
||||||
|
static int fmt_put_uchars_to_uch_buf (hawk_fmtout_t* fmtout, const hawk_uch_t* ptr, hawk_oow_t len)
|
||||||
|
{
|
||||||
|
fmt_uch_buf_t* b = (fmt_uch_buf_t*)fmtout->ctx;
|
||||||
|
hawk_oow_t n;
|
||||||
|
|
||||||
|
/* this function null-terminates the destination. so give the restored buffer size */
|
||||||
|
n = hawk_copy_uchars_to_ucstr(&b->ptr[b->len], b->capa - b->len + 1, ptr, len);
|
||||||
|
b->len += n;
|
||||||
|
if (n < len)
|
||||||
|
{
|
||||||
|
hawk_gem_seterrnum (b->gem, HAWK_NULL, HAWK_EBUFFULL);
|
||||||
|
return 0; /* stop. insufficient buffer */
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1; /* success */
|
||||||
|
}
|
||||||
|
|
||||||
|
hawk_oow_t hawk_gem_vfmttoucstr (hawk_gem_t* gem, hawk_uch_t* buf, hawk_oow_t bufsz, const hawk_uch_t* fmt, va_list ap)
|
||||||
|
{
|
||||||
|
hawk_fmtout_t fo;
|
||||||
|
fmt_uch_buf_t fb;
|
||||||
|
|
||||||
|
if (bufsz <= 0) return 0;
|
||||||
|
|
||||||
|
HAWK_MEMSET (&fo, 0, HAWK_SIZEOF(fo));
|
||||||
|
fo.mmgr = gem->mmgr;
|
||||||
|
fo.putbchars = fmt_put_bchars_to_uch_buf;
|
||||||
|
fo.putuchars = fmt_put_uchars_to_uch_buf;
|
||||||
|
fo.ctx = &fb;
|
||||||
|
|
||||||
|
HAWK_MEMSET (&fb, 0, HAWK_SIZEOF(fb));
|
||||||
|
fb.gem = gem;
|
||||||
|
fb.ptr = buf;
|
||||||
|
fb.capa = bufsz - 1;
|
||||||
|
|
||||||
|
if (hawk_ufmt_outv(&fo, fmt, ap) <= -1) return -1;
|
||||||
|
|
||||||
|
buf[fb.len] = '\0';
|
||||||
|
return fb.len;
|
||||||
|
}
|
||||||
|
|
||||||
|
hawk_oow_t hawk_gem_fmttoucstr (hawk_gem_t* gem, hawk_uch_t* buf, hawk_oow_t bufsz, const hawk_uch_t* fmt, ...)
|
||||||
|
{
|
||||||
|
hawk_oow_t x;
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
|
va_start (ap, fmt);
|
||||||
|
x = hawk_gem_vfmttoucstr(gem, buf, bufsz, fmt, ap);
|
||||||
|
va_end (ap);
|
||||||
|
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
struct fmt_bch_buf_t
|
||||||
|
{
|
||||||
|
hawk_gem_t* gem;
|
||||||
|
hawk_bch_t* ptr;
|
||||||
|
hawk_oow_t len;
|
||||||
|
hawk_oow_t capa;
|
||||||
|
};
|
||||||
|
typedef struct fmt_bch_buf_t fmt_bch_buf_t;
|
||||||
|
|
||||||
|
|
||||||
|
static int fmt_put_bchars_to_bch_buf (hawk_fmtout_t* fmtout, const hawk_bch_t* ptr, hawk_oow_t len)
|
||||||
|
{
|
||||||
|
fmt_bch_buf_t* b = (fmt_bch_buf_t*)fmtout->ctx;
|
||||||
|
hawk_oow_t n;
|
||||||
|
|
||||||
|
/* this function null-terminates the destination. so give the restored buffer size */
|
||||||
|
n = hawk_copy_bchars_to_bcstr(&b->ptr[b->len], b->capa - b->len + 1, ptr, len);
|
||||||
|
b->len += n;
|
||||||
|
if (n < len)
|
||||||
|
{
|
||||||
|
hawk_gem_seterrnum (b->gem, HAWK_NULL, HAWK_EBUFFULL);
|
||||||
|
return 0; /* stop. insufficient buffer */
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1; /* success */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int fmt_put_uchars_to_bch_buf (hawk_fmtout_t* fmtout, const hawk_uch_t* ptr, hawk_oow_t len)
|
||||||
|
{
|
||||||
|
fmt_bch_buf_t* b = (fmt_bch_buf_t*)fmtout->ctx;
|
||||||
|
hawk_oow_t bcslen, ucslen;
|
||||||
|
int n;
|
||||||
|
|
||||||
|
bcslen = b->capa - b->len;
|
||||||
|
ucslen = len;
|
||||||
|
n = hawk_conv_uchars_to_bchars_with_cmgr(ptr, &ucslen, &b->ptr[b->len], &bcslen, b->gem->cmgr);
|
||||||
|
b->len += bcslen;
|
||||||
|
if (n <= -1)
|
||||||
|
{
|
||||||
|
if (n == -2)
|
||||||
|
{
|
||||||
|
return 0; /* buffer full. stop */
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hawk_gem_seterrnum (b->gem, HAWK_NULL, HAWK_EECERR);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1; /* success. carry on */
|
||||||
|
}
|
||||||
|
|
||||||
|
hawk_oow_t hawk_gem_vfmttobcstr (hawk_gem_t* gem, hawk_bch_t* buf, hawk_oow_t bufsz, const hawk_bch_t* fmt, va_list ap)
|
||||||
|
{
|
||||||
|
hawk_fmtout_t fo;
|
||||||
|
fmt_bch_buf_t fb;
|
||||||
|
|
||||||
|
if (bufsz <= 0) return 0;
|
||||||
|
|
||||||
|
HAWK_MEMSET (&fo, 0, HAWK_SIZEOF(fo));
|
||||||
|
fo.mmgr = gem->mmgr;
|
||||||
|
fo.putbchars = fmt_put_bchars_to_bch_buf;
|
||||||
|
fo.putuchars = fmt_put_uchars_to_bch_buf;
|
||||||
|
fo.ctx = &fb;
|
||||||
|
|
||||||
|
HAWK_MEMSET (&fb, 0, HAWK_SIZEOF(fb));
|
||||||
|
fb.gem = gem;
|
||||||
|
fb.ptr = buf;
|
||||||
|
fb.capa = bufsz - 1;
|
||||||
|
|
||||||
|
if (hawk_bfmt_outv(&fo, fmt, ap) <= -1) return -1;
|
||||||
|
|
||||||
|
buf[fb.len] = '\0';
|
||||||
|
return fb.len;
|
||||||
|
}
|
||||||
|
|
||||||
|
hawk_oow_t hawk_gem_fmttobcstr (hawk_gem_t* gem, hawk_bch_t* buf, hawk_oow_t bufsz, const hawk_bch_t* fmt, ...)
|
||||||
|
{
|
||||||
|
hawk_oow_t x;
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
|
va_start (ap, fmt);
|
||||||
|
x = hawk_gem_vfmttobcstr(gem, buf, bufsz, fmt, ap);
|
||||||
|
va_end (ap);
|
||||||
|
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -115,6 +115,149 @@ HAWK_EXPORT hawk_bch_t* hawk_gem_dupbcstrarr (
|
|||||||
# define hawk_gem_dupoocstrarr hawk_gem_dupbcstrarr
|
# define hawk_gem_dupoocstrarr hawk_gem_dupbcstrarr
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
HAWK_EXPORT int hawk_gem_convbtouchars (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
const hawk_bch_t* bcs,
|
||||||
|
hawk_oow_t* bcslen,
|
||||||
|
hawk_uch_t* ucs,
|
||||||
|
hawk_oow_t* ucslen,
|
||||||
|
int all
|
||||||
|
);
|
||||||
|
|
||||||
|
HAWK_EXPORT int hawk_gem_convutobchars (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
const hawk_uch_t* ucs,
|
||||||
|
hawk_oow_t* ucslen,
|
||||||
|
hawk_bch_t* bcs,
|
||||||
|
hawk_oow_t* bcslen
|
||||||
|
);
|
||||||
|
|
||||||
|
HAWK_EXPORT int hawk_gem_convbtoucstr (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
const hawk_bch_t* bcs,
|
||||||
|
hawk_oow_t* bcslen,
|
||||||
|
hawk_uch_t* ucs,
|
||||||
|
hawk_oow_t* ucslen,
|
||||||
|
int all
|
||||||
|
);
|
||||||
|
|
||||||
|
HAWK_EXPORT int hawk_gem_convutobcstr (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
const hawk_uch_t* ucs,
|
||||||
|
hawk_oow_t* ucslen,
|
||||||
|
hawk_bch_t* bcs,
|
||||||
|
hawk_oow_t* bcslen
|
||||||
|
);
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
|
||||||
|
HAWK_EXPORT hawk_uch_t* hawk_gem_dupbtouchars (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
const hawk_bch_t* bcs,
|
||||||
|
hawk_oow_t bcslen,
|
||||||
|
hawk_oow_t* ucslen,
|
||||||
|
int all
|
||||||
|
);
|
||||||
|
|
||||||
|
HAWK_EXPORT hawk_bch_t* hawk_gem_duputobchars (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
const hawk_uch_t* ucs,
|
||||||
|
hawk_oow_t ucslen,
|
||||||
|
hawk_oow_t* bcslen
|
||||||
|
);
|
||||||
|
|
||||||
|
HAWK_EXPORT hawk_uch_t* hawk_gem_dupb2touchars (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
const hawk_bch_t* bcs1,
|
||||||
|
hawk_oow_t bcslen1,
|
||||||
|
const hawk_bch_t* bcs2,
|
||||||
|
hawk_oow_t bcslen2,
|
||||||
|
hawk_oow_t* ucslen,
|
||||||
|
int all
|
||||||
|
);
|
||||||
|
|
||||||
|
HAWK_EXPORT hawk_bch_t* hawk_gem_dupu2tobchars (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
const hawk_uch_t* ucs1,
|
||||||
|
hawk_oow_t ucslen1,
|
||||||
|
const hawk_uch_t* ucs2,
|
||||||
|
hawk_oow_t ucslen2,
|
||||||
|
hawk_oow_t* bcslen
|
||||||
|
);
|
||||||
|
|
||||||
|
HAWK_EXPORT hawk_uch_t* hawk_gem_dupbtoucstr (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
const hawk_bch_t* bcs,
|
||||||
|
hawk_oow_t* ucslen,
|
||||||
|
int all
|
||||||
|
);
|
||||||
|
|
||||||
|
HAWK_EXPORT hawk_bch_t* hawk_gem_duputobcstr (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
const hawk_uch_t* ucs,
|
||||||
|
hawk_oow_t* bcslen
|
||||||
|
);
|
||||||
|
|
||||||
|
HAWK_EXPORT hawk_uch_t* hawk_gem_dupbtoucharswithcmgr (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
const hawk_bch_t* bcs,
|
||||||
|
hawk_oow_t _bcslen,
|
||||||
|
hawk_oow_t* _ucslen,
|
||||||
|
hawk_cmgr_t* cmgr,
|
||||||
|
int all
|
||||||
|
);
|
||||||
|
|
||||||
|
HAWK_EXPORT hawk_bch_t* hawk_gem_duputobcharswithcmgr (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
const hawk_uch_t* ucs,
|
||||||
|
hawk_oow_t _ucslen,
|
||||||
|
hawk_oow_t* _bcslen,
|
||||||
|
hawk_cmgr_t* cmgr
|
||||||
|
);
|
||||||
|
|
||||||
|
HAWK_EXPORT hawk_bch_t* hawk_gem_dupucstrarrtobcstr (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
const hawk_uch_t* ucs[],
|
||||||
|
hawk_oow_t* bcslen
|
||||||
|
);
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
hawk_oow_t hawk_gem_vfmttoucstr (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
hawk_uch_t* buf,
|
||||||
|
hawk_oow_t bufsz,
|
||||||
|
const hawk_uch_t* fmt,
|
||||||
|
va_list ap
|
||||||
|
);
|
||||||
|
|
||||||
|
hawk_oow_t hawk_gem_fmttoucstr (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
hawk_uch_t* buf,
|
||||||
|
hawk_oow_t bufsz,
|
||||||
|
const hawk_uch_t* fmt,
|
||||||
|
...
|
||||||
|
);
|
||||||
|
|
||||||
|
hawk_oow_t hawk_gem_vfmttobcstr (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
hawk_bch_t* buf,
|
||||||
|
hawk_oow_t bufsz,
|
||||||
|
const hawk_bch_t* fmt,
|
||||||
|
va_list ap
|
||||||
|
);
|
||||||
|
|
||||||
|
hawk_oow_t hawk_gem_fmttobcstr (
|
||||||
|
hawk_gem_t* gem,
|
||||||
|
hawk_bch_t* buf,
|
||||||
|
hawk_oow_t bufsz,
|
||||||
|
const hawk_bch_t* fmt,
|
||||||
|
...
|
||||||
|
);
|
||||||
|
|
||||||
#if defined(__cplusplus)
|
#if defined(__cplusplus)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
348
hawk/lib/hawk.c
348
hawk/lib/hawk.c
@ -536,348 +536,24 @@ void hawk_pushecb (hawk_t* awk, hawk_ecb_t* ecb)
|
|||||||
awk->ecb = ecb;
|
awk->ecb = ecb;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------ */
|
||||||
|
|
||||||
int hawk_convbtouchars (hawk_t* hawk, const hawk_bch_t* bcs, hawk_oow_t* bcslen, hawk_uch_t* ucs, hawk_oow_t* ucslen, int all)
|
hawk_oow_t hawk_fmttoucstr_ (hawk_t* hawk, hawk_uch_t* buf, hawk_oow_t bufsz, const hawk_uch_t* fmt, ...)
|
||||||
{
|
{
|
||||||
/* length bound */
|
|
||||||
int n;
|
|
||||||
n = hawk_conv_bchars_to_uchars_with_cmgr(bcs, bcslen, ucs, ucslen, hawk_getcmgr(hawk), all);
|
|
||||||
/* -1: illegal character, -2: buffer too small, -3: incomplete sequence */
|
|
||||||
if (n <= -1) hawk_seterrnum (hawk, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR, HAWK_NULL);
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
int hawk_convutobchars (hawk_t* hawk, const hawk_uch_t* ucs, hawk_oow_t* ucslen, hawk_bch_t* bcs, hawk_oow_t* bcslen)
|
|
||||||
{
|
|
||||||
/* length bound */
|
|
||||||
int n;
|
|
||||||
n = hawk_conv_uchars_to_bchars_with_cmgr(ucs, ucslen, bcs, bcslen, hawk_getcmgr(hawk));
|
|
||||||
if (n <= -1) hawk_seterrnum (hawk, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR, HAWK_NULL);
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
int hawk_convbtoucstr (hawk_t* hawk, const hawk_bch_t* bcs, hawk_oow_t* bcslen, hawk_uch_t* ucs, hawk_oow_t* ucslen, int all)
|
|
||||||
{
|
|
||||||
/* null-terminated. */
|
|
||||||
int n;
|
|
||||||
n = hawk_conv_bcstr_to_ucstr_with_cmgr(bcs, bcslen, ucs, ucslen, hawk_getcmgr(hawk), all);
|
|
||||||
if (n <= -1) hawk_seterrnum (hawk, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR, HAWK_NULL);
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
int hawk_convutobcstr (hawk_t* hawk, const hawk_uch_t* ucs, hawk_oow_t* ucslen, hawk_bch_t* bcs, hawk_oow_t* bcslen)
|
|
||||||
{
|
|
||||||
/* null-terminated */
|
|
||||||
int n;
|
|
||||||
n = hawk_conv_ucstr_to_bcstr_with_cmgr(ucs, ucslen, bcs, bcslen, hawk_getcmgr(hawk));
|
|
||||||
if (n <= -1) hawk_seterrnum (hawk, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR, HAWK_NULL);
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------- */
|
|
||||||
|
|
||||||
hawk_uch_t* hawk_dupbtoucharswithheadroom (hawk_t* hawk, hawk_oow_t headroom_bytes, const hawk_bch_t* bcs, hawk_oow_t bcslen, hawk_oow_t* ucslen, int all)
|
|
||||||
{
|
|
||||||
hawk_oow_t inlen, outlen;
|
|
||||||
hawk_uch_t* ptr;
|
|
||||||
|
|
||||||
inlen = bcslen;
|
|
||||||
if (hawk_convbtouchars(hawk, bcs, &inlen, HAWK_NULL, &outlen, all) <= -1)
|
|
||||||
{
|
|
||||||
/* note it's also an error if no full conversion is made in this function */
|
|
||||||
return HAWK_NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ptr = (hawk_uch_t*)hawk_allocmem(hawk, headroom_bytes + ((outlen + 1) * HAWK_SIZEOF(*ptr)));
|
|
||||||
if (!ptr) return HAWK_NULL;
|
|
||||||
|
|
||||||
inlen = bcslen;
|
|
||||||
|
|
||||||
ptr = (hawk_uch_t*)((hawk_oob_t*)ptr + headroom_bytes);
|
|
||||||
hawk_convbtouchars (hawk, bcs, &inlen, ptr, &outlen, all);
|
|
||||||
|
|
||||||
/* hawk_convbtouchars() doesn't null-terminate the target.
|
|
||||||
* but in hawk_dupbtouchars(), i allocate space. so i don't mind
|
|
||||||
* null-terminating it with 1 extra character overhead */
|
|
||||||
ptr[outlen] = '\0';
|
|
||||||
if (ucslen) *ucslen = outlen;
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
hawk_bch_t* hawk_duputobcharswithheadroom (hawk_t* hawk, hawk_oow_t headroom_bytes, const hawk_uch_t* ucs, hawk_oow_t ucslen, hawk_oow_t* bcslen)
|
|
||||||
{
|
|
||||||
hawk_oow_t inlen, outlen;
|
|
||||||
hawk_bch_t* ptr;
|
|
||||||
|
|
||||||
inlen = ucslen;
|
|
||||||
if (hawk_convutobchars(hawk, ucs, &inlen, HAWK_NULL, &outlen) <= -1)
|
|
||||||
{
|
|
||||||
/* note it's also an error if no full conversion is made in this function */
|
|
||||||
return HAWK_NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ptr = (hawk_bch_t*)hawk_allocmem(hawk, headroom_bytes + ((outlen + 1) * HAWK_SIZEOF(*ptr)));
|
|
||||||
if (!ptr) return HAWK_NULL;
|
|
||||||
|
|
||||||
inlen = ucslen;
|
|
||||||
ptr = (hawk_bch_t*)((hawk_oob_t*)ptr + headroom_bytes);
|
|
||||||
hawk_convutobchars (hawk, ucs, &inlen, ptr, &outlen);
|
|
||||||
|
|
||||||
ptr[outlen] = '\0';
|
|
||||||
if (bcslen) *bcslen = outlen;
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------- */
|
|
||||||
|
|
||||||
hawk_uch_t* hawk_dupbtoucstrwithheadroom (hawk_t* hawk, hawk_oow_t headroom_bytes, const hawk_bch_t* bcs, hawk_oow_t* ucslen, int all)
|
|
||||||
{
|
|
||||||
hawk_oow_t inlen, outlen;
|
|
||||||
hawk_uch_t* ptr;
|
|
||||||
|
|
||||||
if (hawk_convbtoucstr(hawk, bcs, &inlen, HAWK_NULL, &outlen, all) <= -1)
|
|
||||||
{
|
|
||||||
/* note it's also an error if no full conversion is made in this function */
|
|
||||||
return HAWK_NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
outlen++;
|
|
||||||
ptr = (hawk_uch_t*)hawk_allocmem(hawk, headroom_bytes + (outlen * HAWK_SIZEOF(hawk_uch_t)));
|
|
||||||
if (!ptr) return HAWK_NULL;
|
|
||||||
|
|
||||||
hawk_convbtoucstr (hawk, bcs, &inlen, ptr, &outlen, all);
|
|
||||||
if (ucslen) *ucslen = outlen;
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
hawk_bch_t* hawk_duputobcstrwithheadroom (hawk_t* hawk, hawk_oow_t headroom_bytes, const hawk_uch_t* ucs, hawk_oow_t* bcslen)
|
|
||||||
{
|
|
||||||
hawk_oow_t inlen, outlen;
|
|
||||||
hawk_bch_t* ptr;
|
|
||||||
|
|
||||||
if (hawk_convutobcstr(hawk, ucs, &inlen, HAWK_NULL, &outlen) <= -1)
|
|
||||||
{
|
|
||||||
/* note it's also an error if no full conversion is made in this function */
|
|
||||||
return HAWK_NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
outlen++;
|
|
||||||
ptr = (hawk_bch_t*)hawk_allocmem(hawk, headroom_bytes + (outlen * HAWK_SIZEOF(hawk_bch_t)));
|
|
||||||
if (!ptr) return HAWK_NULL;
|
|
||||||
|
|
||||||
ptr = (hawk_bch_t*)((hawk_oob_t*)ptr + headroom_bytes);
|
|
||||||
|
|
||||||
hawk_convutobcstr (hawk, ucs, &inlen, ptr, &outlen);
|
|
||||||
if (bcslen) *bcslen = outlen;
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------- */
|
|
||||||
|
|
||||||
hawk_bch_t* hawk_dupucstrarrtobcstr (hawk_t* hawk, const hawk_uch_t* ucs[], hawk_oow_t* bcslen)
|
|
||||||
{
|
|
||||||
hawk_oow_t wl, ml, capa, pos, i;
|
|
||||||
hawk_bch_t* bcs;
|
|
||||||
|
|
||||||
for (capa = 0, i = 0; ucs[i]; i++)
|
|
||||||
{
|
|
||||||
if (hawk_convutobcstr(hawk, ucs[i], &wl, HAWK_NULL, &ml) <= -1) return HAWK_NULL;
|
|
||||||
capa += ml;
|
|
||||||
}
|
|
||||||
|
|
||||||
bcs = (hawk_bch_t*)hawk_allocmem(hawk, (capa + 1) * HAWK_SIZEOF(*bcs));
|
|
||||||
if (!bcs) return HAWK_NULL;
|
|
||||||
|
|
||||||
for (pos = 0, i = 0; ucs[i]; i++)
|
|
||||||
{
|
|
||||||
ml = capa - pos + 1;
|
|
||||||
hawk_convutobcstr (hawk, ucs[i], &wl, &bcs[pos], &ml);
|
|
||||||
pos += ml;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bcslen) *bcslen = capa;
|
|
||||||
return bcs;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------- */
|
|
||||||
|
|
||||||
struct fmt_uch_buf_t
|
|
||||||
{
|
|
||||||
hawk_t* hawk;
|
|
||||||
hawk_uch_t* ptr;
|
|
||||||
hawk_oow_t len;
|
|
||||||
hawk_oow_t capa;
|
|
||||||
};
|
|
||||||
typedef struct fmt_uch_buf_t fmt_uch_buf_t;
|
|
||||||
|
|
||||||
static int fmt_put_bchars_to_uch_buf (hawk_fmtout_t* fmtout, const hawk_bch_t* ptr, hawk_oow_t len)
|
|
||||||
{
|
|
||||||
fmt_uch_buf_t* b = (fmt_uch_buf_t*)fmtout->ctx;
|
|
||||||
hawk_oow_t bcslen, ucslen;
|
|
||||||
int n;
|
|
||||||
|
|
||||||
bcslen = b->capa - b->len;
|
|
||||||
ucslen = len;
|
|
||||||
n = hawk_conv_bchars_to_uchars_with_cmgr(ptr, &bcslen, &b->ptr[b->len], &ucslen, hawk_getcmgr(b->hawk), 1);
|
|
||||||
if (n <= -1)
|
|
||||||
{
|
|
||||||
if (n == -2)
|
|
||||||
{
|
|
||||||
return 0; /* buffer full. stop */
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
hawk_seterrnum (b->hawk, HAWK_EECERR, HAWK_NULL);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1; /* success. carry on */
|
|
||||||
}
|
|
||||||
|
|
||||||
static int fmt_put_uchars_to_uch_buf (hawk_fmtout_t* fmtout, const hawk_uch_t* ptr, hawk_oow_t len)
|
|
||||||
{
|
|
||||||
fmt_uch_buf_t* b = (fmt_uch_buf_t*)fmtout->ctx;
|
|
||||||
hawk_oow_t n;
|
|
||||||
|
|
||||||
/* this function null-terminates the destination. so give the restored buffer size */
|
|
||||||
n = hawk_copy_uchars_to_ucstr(&b->ptr[b->len], b->capa - b->len + 1, ptr, len);
|
|
||||||
b->len += n;
|
|
||||||
if (n < len)
|
|
||||||
{
|
|
||||||
hawk_seterrnum (b->hawk, HAWK_EBUFFULL, HAWK_NULL);
|
|
||||||
return 0; /* stop. insufficient buffer */
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1; /* success */
|
|
||||||
}
|
|
||||||
|
|
||||||
hawk_oow_t hawk_vfmttoucstr (hawk_t* hawk, hawk_uch_t* buf, hawk_oow_t bsz, const hawk_uch_t* fmt, va_list ap)
|
|
||||||
{
|
|
||||||
hawk_fmtout_t fo;
|
|
||||||
fmt_uch_buf_t fb;
|
|
||||||
|
|
||||||
if (bsz <= 0) return 0;
|
|
||||||
|
|
||||||
HAWK_MEMSET (&fo, 0, HAWK_SIZEOF(fo));
|
|
||||||
fo.mmgr = hawk_getmmgr(hawk);
|
|
||||||
fo.putbchars = fmt_put_bchars_to_uch_buf;
|
|
||||||
fo.putuchars = fmt_put_uchars_to_uch_buf;
|
|
||||||
fo.ctx = &fb;
|
|
||||||
|
|
||||||
HAWK_MEMSET (&fb, 0, HAWK_SIZEOF(fb));
|
|
||||||
fb.hawk = hawk;
|
|
||||||
fb.ptr = buf;
|
|
||||||
fb.capa = bsz - 1;
|
|
||||||
|
|
||||||
if (hawk_ufmt_outv(&fo, fmt, ap) <= -1) return -1;
|
|
||||||
|
|
||||||
buf[fb.len] = '\0';
|
|
||||||
return fb.len;
|
|
||||||
}
|
|
||||||
|
|
||||||
hawk_oow_t hawk_fmttoucstr (hawk_t* hawk, hawk_uch_t* buf, hawk_oow_t bufsz, const hawk_uch_t* fmt, ...)
|
|
||||||
{
|
|
||||||
hawk_oow_t x;
|
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
va_start (ap, fmt);
|
|
||||||
x = hawk_vfmttoucstr(hawk, buf, bufsz, fmt, ap);
|
|
||||||
va_end (ap);
|
|
||||||
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------- */
|
|
||||||
|
|
||||||
struct fmt_bch_buf_t
|
|
||||||
{
|
|
||||||
hawk_t* hawk;
|
|
||||||
hawk_bch_t* ptr;
|
|
||||||
hawk_oow_t len;
|
|
||||||
hawk_oow_t capa;
|
|
||||||
};
|
|
||||||
typedef struct fmt_bch_buf_t fmt_bch_buf_t;
|
|
||||||
|
|
||||||
|
|
||||||
static int fmt_put_bchars_to_bch_buf (hawk_fmtout_t* fmtout, const hawk_bch_t* ptr, hawk_oow_t len)
|
|
||||||
{
|
|
||||||
fmt_bch_buf_t* b = (fmt_bch_buf_t*)fmtout->ctx;
|
|
||||||
hawk_oow_t n;
|
hawk_oow_t n;
|
||||||
|
va_start(ap, fmt);
|
||||||
/* this function null-terminates the destination. so give the restored buffer size */
|
n = hawk_gem_vfmttoucstr(hawk_getgem(hawk), buf, bufsz, fmt, ap);
|
||||||
n = hawk_copy_bchars_to_bcstr(&b->ptr[b->len], b->capa - b->len + 1, ptr, len);
|
va_end(ap);
|
||||||
b->len += n;
|
return n;
|
||||||
if (n < len)
|
|
||||||
{
|
|
||||||
hawk_seterrnum (b->hawk, HAWK_EBUFFULL, HAWK_NULL);
|
|
||||||
return 0; /* stop. insufficient buffer */
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1; /* success */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hawk_oow_t hawk_fmttobcstr_ (hawk_t* hawk, hawk_bch_t* buf, hawk_oow_t bufsz, const hawk_bch_t* fmt, ...)
|
||||||
static int fmt_put_uchars_to_bch_buf (hawk_fmtout_t* fmtout, const hawk_uch_t* ptr, hawk_oow_t len)
|
|
||||||
{
|
{
|
||||||
fmt_bch_buf_t* b = (fmt_bch_buf_t*)fmtout->ctx;
|
|
||||||
hawk_oow_t bcslen, ucslen;
|
|
||||||
int n;
|
|
||||||
|
|
||||||
bcslen = b->capa - b->len;
|
|
||||||
ucslen = len;
|
|
||||||
n = hawk_conv_uchars_to_bchars_with_cmgr(ptr, &ucslen, &b->ptr[b->len], &bcslen, hawk_getcmgr(b->hawk));
|
|
||||||
b->len += bcslen;
|
|
||||||
if (n <= -1)
|
|
||||||
{
|
|
||||||
if (n == -2)
|
|
||||||
{
|
|
||||||
return 0; /* buffer full. stop */
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
hawk_seterrnum (b->hawk, HAWK_EECERR, HAWK_NULL);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1; /* success. carry on */
|
|
||||||
}
|
|
||||||
|
|
||||||
hawk_oow_t hawk_vfmttobcstr (hawk_t* hawk, hawk_bch_t* buf, hawk_oow_t bsz, const hawk_bch_t* fmt, va_list ap)
|
|
||||||
{
|
|
||||||
hawk_fmtout_t fo;
|
|
||||||
fmt_bch_buf_t fb;
|
|
||||||
|
|
||||||
if (bsz <= 0) return 0;
|
|
||||||
|
|
||||||
HAWK_MEMSET (&fo, 0, HAWK_SIZEOF(fo));
|
|
||||||
fo.mmgr = hawk_getmmgr(hawk);
|
|
||||||
fo.putbchars = fmt_put_bchars_to_bch_buf;
|
|
||||||
fo.putuchars = fmt_put_uchars_to_bch_buf;
|
|
||||||
fo.ctx = &fb;
|
|
||||||
|
|
||||||
HAWK_MEMSET (&fb, 0, HAWK_SIZEOF(fb));
|
|
||||||
fb.hawk = hawk;
|
|
||||||
fb.ptr = buf;
|
|
||||||
fb.capa = bsz - 1;
|
|
||||||
|
|
||||||
if (hawk_bfmt_outv(&fo, fmt, ap) <= -1) return -1;
|
|
||||||
|
|
||||||
buf[fb.len] = '\0';
|
|
||||||
return fb.len;
|
|
||||||
}
|
|
||||||
|
|
||||||
hawk_oow_t hawk_fmttobcstr (hawk_t* hawk, hawk_bch_t* buf, hawk_oow_t bufsz, const hawk_bch_t* fmt, ...)
|
|
||||||
{
|
|
||||||
hawk_oow_t x;
|
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
hawk_oow_t n;
|
||||||
va_start (ap, fmt);
|
va_start(ap, fmt);
|
||||||
x = hawk_vfmttobcstr(hawk, buf, bufsz, fmt, ap);
|
n = hawk_gem_vfmttobcstr(hawk_getgem(hawk), buf, bufsz, fmt, ap);
|
||||||
va_end (ap);
|
va_end(ap);
|
||||||
|
return n;
|
||||||
return x;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
297
hawk/lib/hawk.h
297
hawk/lib/hawk.h
@ -1888,120 +1888,72 @@ static HAWK_INLINE hawk_bch_t* hawk_dupbcstrarr (hawk_t* hawk, const hawk_bch_t*
|
|||||||
|
|
||||||
/* ----------------------------------------------------------------------- */
|
/* ----------------------------------------------------------------------- */
|
||||||
|
|
||||||
HAWK_EXPORT int hawk_convbtouchars (
|
#if defined(HAWK_HAVE_INLINE)
|
||||||
hawk_t* hawk,
|
static HAWK_INLINE int hawk_convbtouchars (hawk_t* hawk, const hawk_bch_t* bcs, hawk_oow_t* bcslen, hawk_uch_t* ucs, hawk_oow_t* ucslen, int all) { return hawk_gem_convbtouchars(hawk_getgem(hawk), bcs, bcslen, ucs, ucslen, all); }
|
||||||
const hawk_bch_t* bcs,
|
static HAWK_INLINE int hawk_convutobchars (hawk_t* hawk, const hawk_uch_t* ucs, hawk_oow_t* ucslen, hawk_bch_t* bcs, hawk_oow_t* bcslen) { return hawk_gem_convutobchars(hawk_getgem(hawk), ucs, ucslen, bcs, bcslen); }
|
||||||
hawk_oow_t* bcslen,
|
static HAWK_INLINE int hawk_convbtoucstr (hawk_t* hawk, const hawk_bch_t* bcs, hawk_oow_t* bcslen, hawk_uch_t* ucs, hawk_oow_t* ucslen, int all) { return hawk_gem_convbtoucstr(hawk_getgem(hawk), bcs, bcslen, ucs, ucslen, all); }
|
||||||
hawk_uch_t* ucs,
|
static HAWK_INLINE int hawk_convutobcstr (hawk_t* hawk, const hawk_uch_t* ucs, hawk_oow_t* ucslen, hawk_bch_t* bcs, hawk_oow_t* bcslen) { return hawk_gem_convutobcstr(hawk_getgem(hawk), ucs, ucslen, bcs, bcslen); }
|
||||||
hawk_oow_t* ucslen,
|
#else
|
||||||
int all
|
#define hawk_convbtouchars(hawk, bcs, bcslen, ucs, ucslen, all) hawk_gem_convbtouchars(hawk_getgem(hawk), bcs, bcslen, ucs, ucslen, all)
|
||||||
);
|
#define hawk_convutobchars(hawk, ucs, ucslen, bcs, bcslen) hawk_gem_convutobchars(hawk_getgem(hawk), ucs, ucslen, bcs, bcslen)
|
||||||
|
#define hawk_convbtoucstr(hawk, bcs, bcslen, ucs, ucslen, all) hawk_gem_convbtoucstr(hawk_getgem(hawk), bcs, bcslen, ucs, ucslen, all)
|
||||||
HAWK_EXPORT int hawk_convutobchars (
|
#define hawk_convutobcstr(hawk, ucs, ucslen, bcs, bcslen) hawk_gem_convutobcstr(hawk_getgem(hawk), ucs, ucslen, bcs, bcslen)
|
||||||
hawk_t* hawk,
|
#endif
|
||||||
const hawk_uch_t* ucs,
|
|
||||||
hawk_oow_t* ucslen,
|
|
||||||
hawk_bch_t* bcs,
|
|
||||||
hawk_oow_t* bcslen
|
|
||||||
);
|
|
||||||
|
|
||||||
HAWK_EXPORT int hawk_convbtoucstr (
|
|
||||||
hawk_t* hawk,
|
|
||||||
const hawk_bch_t* bcs,
|
|
||||||
hawk_oow_t* bcslen,
|
|
||||||
hawk_uch_t* ucs,
|
|
||||||
hawk_oow_t* ucslen,
|
|
||||||
int all
|
|
||||||
);
|
|
||||||
|
|
||||||
HAWK_EXPORT int hawk_convutobcstr (
|
|
||||||
hawk_t* hawk,
|
|
||||||
const hawk_uch_t* ucs,
|
|
||||||
hawk_oow_t* ucslen,
|
|
||||||
hawk_bch_t* bcs,
|
|
||||||
hawk_oow_t* bcslen
|
|
||||||
);
|
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------- */
|
/* ----------------------------------------------------------------------- */
|
||||||
|
|
||||||
HAWK_EXPORT hawk_uch_t* hawk_dupbtoucharswithheadroom (
|
#if defined(HAWK_HAVE_INLINE)
|
||||||
hawk_t* hawk,
|
static HAWK_INLINE hawk_uch_t* hawk_dupbtouchars (hawk_t* hawk, const hawk_bch_t* bcs, hawk_oow_t _bcslen, hawk_oow_t* _ucslen, int all) { return hawk_gem_dupbtouchars(hawk_getgem(hawk), bcs, _bcslen, _ucslen, all); }
|
||||||
hawk_oow_t headroom_bytes,
|
static HAWK_INLINE hawk_bch_t* hawk_duputobchars (hawk_t* hawk, const hawk_uch_t* ucs, hawk_oow_t _ucslen, hawk_oow_t* _bcslen) { return hawk_gem_duputobchars(hawk_getgem(hawk), ucs, _ucslen, _bcslen); }
|
||||||
const hawk_bch_t* bcs,
|
static HAWK_INLINE hawk_uch_t* hawk_dupb2touchars (hawk_t* hawk, const hawk_bch_t* bcs1, hawk_oow_t bcslen1, const hawk_bch_t* bcs2, hawk_oow_t bcslen2, hawk_oow_t* ucslen, int all) { return hawk_gem_dupb2touchars(hawk_getgem(hawk), bcs1, bcslen1, bcs2, bcslen2, ucslen, all); }
|
||||||
hawk_oow_t bcslen,
|
static HAWK_INLINE hawk_bch_t* hawk_dupu2tobchars (hawk_t* hawk, const hawk_uch_t* ucs1, hawk_oow_t ucslen1, const hawk_uch_t* ucs2, hawk_oow_t ucslen2, hawk_oow_t* bcslen) { return hawk_gem_dupu2tobchars(hawk_getgem(hawk), ucs1, ucslen1, ucs2, ucslen2, bcslen); }
|
||||||
hawk_oow_t* ucslen,
|
static HAWK_INLINE hawk_uch_t* hawk_dupbtoucstr (hawk_t* hawk, const hawk_bch_t* bcs, hawk_oow_t* _ucslen, int all) { return hawk_gem_dupbtoucstr(hawk_getgem(hawk), bcs, _ucslen, all); }
|
||||||
int all
|
static HAWK_INLINE hawk_bch_t* hawk_duputobcstr (hawk_t* hawk, const hawk_uch_t* ucs, hawk_oow_t* _bcslen) { return hawk_gem_duputobcstr(hawk_getgem(hawk), ucs, _bcslen); }
|
||||||
);
|
static HAWK_INLINE hawk_uch_t* hawk_dupbtoucharswithcmgr (hawk_t* hawk, const hawk_bch_t* bcs, hawk_oow_t _bcslen, hawk_oow_t* _ucslen, hawk_cmgr_t* cmgr, int all) { return hawk_gem_dupbtoucharswithcmgr(hawk_getgem(hawk), bcs, _bcslen, _ucslen, cmgr, all); }
|
||||||
|
static HAWK_INLINE hawk_bch_t* hawk_duputobcharswithcmgr (hawk_t* hawk, const hawk_uch_t* ucs, hawk_oow_t _ucslen, hawk_oow_t* _bcslen, hawk_cmgr_t* cmgr) { return hawk_gem_duputobcharswithcmgr(hawk_getgem(hawk), ucs, _ucslen, _bcslen, cmgr); }
|
||||||
HAWK_EXPORT hawk_bch_t* hawk_duputobcharswithheadroom (
|
static HAWK_INLINE hawk_bch_t* hawk_dupucstrarrtobcstr (hawk_t* hawk, const hawk_uch_t* ucsarr[], hawk_oow_t* bcslen) { return hawk_gem_dupucstrarrtobcstr(hawk_getgem(hawk), ucsarr, bcslen); }
|
||||||
hawk_t* hawk,
|
#else
|
||||||
hawk_oow_t headroom_bytes,
|
#define hawk_dupbtouchars(hawk, bcs, _bcslen, _ucslen, all) hawk_gem_dupbtouchars(hawk_getgem(hawk), bcs, _bcslen, _ucslen, all)
|
||||||
const hawk_uch_t* ucs,
|
#define hawk_duputobchars(hawk, ucs, _ucslen, _bcslen) hawk_gem_duputobchars(hawk_getgem(hawk), ucs, _ucslen, _bcslen)
|
||||||
hawk_oow_t ucslen,
|
#define hawk_dupb2touchars(hawk, bcs1, bcslen1, bcs2, bcslen2, ucslen, all) hawk_gem_dupb2touchars(hawk_getgem(hawk), bcs1, bcslen1, bcs2, bcslen2, ucslen, all)
|
||||||
hawk_oow_t* bcslen
|
#define hawk_dupu2tobchars(hawk, ucs1, ucslen1, ucs2, ucslen2, bcslen) hawk_gem_dupu2tobchars(hawk_getgem(hawk), ucs1, ucslen1, ucs2, ucslen2, bcslen)
|
||||||
);
|
#define hawk_dupbtoucstr(hawk, bcs, _ucslen, all) hawk_gem_dupbtoucstr(hawk_getgem(hawk), bcs, _ucslen, all)
|
||||||
|
#define hawk_duputobcstr(hawk, ucs, _bcslen) hawk_gem_duputobcstr(hawk_getgem(hawk), ucs, _bcslen)
|
||||||
HAWK_EXPORT hawk_uch_t* hawk_dupbtoucstrwithheadroom (
|
#define hawk_dupbtoucharswithcmgr(hawk, bcs, _bcslen, _ucslen, cmgr, all) hawk_gem_dupbtoucharswithcmgr(hawk_getgem(hawk), bcs, _bcslen, _ucslen, cmgr, all)
|
||||||
hawk_t* hawk,
|
#define hawk_duputobcharswithcmgr(hawk, ucs, _ucslen, _bcslen, cmgr) hawk_gem_duputobcharswithcmgr(hawk_getgem(hawk), ucs, _ucslen, _bcslen, cmgr)
|
||||||
hawk_oow_t headroom_bytes,
|
#define hawk_dupucstrarrtobcstr(hawk, ucsarr, bcslen) hawk_gem_dupucstrarrtobcstr(hawk_getgem(hawk), ucsarr, bcslen)
|
||||||
const hawk_bch_t* bcs,
|
#endif
|
||||||
hawk_oow_t* ucslen,
|
|
||||||
int all
|
|
||||||
);
|
|
||||||
|
|
||||||
HAWK_EXPORT hawk_bch_t* hawk_duputobcstrwithheadroom (
|
|
||||||
hawk_t* hawk,
|
|
||||||
hawk_oow_t headroom_bytes,
|
|
||||||
const hawk_uch_t* ucs,
|
|
||||||
hawk_oow_t* bcslen
|
|
||||||
);
|
|
||||||
|
|
||||||
#define hawk_dupbtouchars(hawk,bcs,bcslen,ucslen,all) hawk_dupbtoucharswithheadroom(hawk,0,bcs,bcslen,ucslen,all)
|
|
||||||
#define hawk_duputobchars(hawk,ucs,ucslen,bcslen) hawk_duputobcharswithheadroom(hawk,0,ucs,ucslen,bcslen)
|
|
||||||
#define hawk_dupbtoucstr(hawk,bcs,ucslen,all) hawk_dupbtoucstrwithheadroom(hawk,0,bcs,ucslen,all)
|
|
||||||
#define hawk_duputobcstr(hawk,ucs,bcslen) hawk_duputobcstrwithheadroom(hawk,0,ucs,bcslen)
|
|
||||||
|
|
||||||
|
|
||||||
HAWK_EXPORT hawk_bch_t* hawk_dupucstrarrtobcstr (
|
|
||||||
hawk_t* hawk,
|
|
||||||
const hawk_uch_t* ucs[],
|
|
||||||
hawk_oow_t* bcslen
|
|
||||||
);
|
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------- */
|
/* ----------------------------------------------------------------------- */
|
||||||
|
|
||||||
hawk_oow_t hawk_vfmttoucstr (
|
HAWK_EXPORT hawk_oow_t hawk_fmttoucstr_ (
|
||||||
hawk_t* hawk,
|
hawk_t* hawk,
|
||||||
hawk_uch_t* buf,
|
hawk_uch_t* buf,
|
||||||
hawk_oow_t bsz,
|
hawk_oow_t bufsz,
|
||||||
const hawk_uch_t* fmt,
|
|
||||||
va_list ap
|
|
||||||
);
|
|
||||||
|
|
||||||
hawk_oow_t hawk_fmttoucstr (
|
|
||||||
hawk_t* hawk,
|
|
||||||
hawk_uch_t* buf,
|
|
||||||
hawk_oow_t bsz,
|
|
||||||
const hawk_uch_t* fmt,
|
const hawk_uch_t* fmt,
|
||||||
...
|
...
|
||||||
);
|
);
|
||||||
|
|
||||||
hawk_oow_t hawk_vfmttobcstr (
|
HAWK_EXPORT hawk_oow_t hawk_fmttobcstr_ (
|
||||||
hawk_t* hawk,
|
hawk_t* hawk,
|
||||||
hawk_bch_t* buf,
|
hawk_bch_t* buf,
|
||||||
hawk_oow_t bsz,
|
hawk_oow_t bufsz,
|
||||||
const hawk_bch_t* fmt,
|
|
||||||
va_list ap
|
|
||||||
);
|
|
||||||
|
|
||||||
hawk_oow_t hawk_fmttobcstr (
|
|
||||||
hawk_t* hawk,
|
|
||||||
hawk_bch_t* buf,
|
|
||||||
hawk_oow_t bsz,
|
|
||||||
const hawk_bch_t* fmt,
|
const hawk_bch_t* fmt,
|
||||||
...
|
...
|
||||||
);
|
);
|
||||||
|
|
||||||
|
#if defined(HAWK_HAVE_INLINE)
|
||||||
|
static HAWK_INLINE hawk_oow_t hawk_vfmttoucstr (hawk_t* hawk, hawk_uch_t* buf, hawk_oow_t bufsz, const hawk_uch_t* fmt, va_list ap) { return hawk_gem_vfmttoucstr(hawk_getgem(hawk), buf, bufsz, fmt, ap); }
|
||||||
|
static HAWK_INLINE hawk_oow_t hawk_fmttoucstr (hawk_t* hawk, hawk_uch_t* buf, hawk_oow_t bufsz, const hawk_uch_t* fmt, ...) { va_list ap; hawk_oow_t n; va_start(ap, fmt); n = hawk_gem_vfmttoucstr(hawk_getgem(hawk), buf, bufsz, fmt, ap); va_end(ap); return n; }
|
||||||
|
static HAWK_INLINE hawk_oow_t hawk_vfmttobcstr (hawk_t* hawk, hawk_bch_t* buf, hawk_oow_t bufsz, const hawk_bch_t* fmt, va_list ap) { return hawk_gem_vfmttobcstr(hawk_getgem(hawk), buf, bufsz, fmt, ap); }
|
||||||
|
static HAWK_INLINE hawk_oow_t hawk_fmttobcstr (hawk_t* hawk, hawk_bch_t* buf, hawk_oow_t bufsz, const hawk_bch_t* fmt, ...) { va_list ap; hawk_oow_t n; va_start(ap, fmt); n = hawk_gem_vfmttobcstr(hawk_getgem(hawk), buf, bufsz, fmt, ap); va_end(ap); return n; }
|
||||||
|
#else
|
||||||
|
#define hawk_vfmttoucstr(hawk, buf, bufsz, fmt, ap) hawk_gem_vfmttoucstr(hawk_getgem(hawk), buf, bufsz, fmt, ap)
|
||||||
|
#define hawk_fmttoucstr hawk_fmttoucstr_
|
||||||
|
#define hawk_vfmttobcstr(hawk, buf, bufsz, fmt, ap) hawk_gem_vfmttobcstr(hawk_getgem(hawk), buf, bufsz, fmt, ap)
|
||||||
|
#define hawk_fmttobcstr hawk_fmttobcstr_
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(HAWK_OOCH_IS_UCH)
|
#if defined(HAWK_OOCH_IS_UCH)
|
||||||
# define hawk_vfmttooocstr hawk_vfmttoucstr
|
# define hawk_vfmttooocstr hawk_vfmttoucstr
|
||||||
# define hawk_fmttooocstr hawk_fmttoucstr
|
# define hawk_fmttooocstr hawk_fmttoucstr
|
||||||
@ -3281,106 +3233,79 @@ static HAWK_INLINE hawk_bch_t* hawk_rtx_dupbcstrarr (hawk_rtx_t* rtx, const hawk
|
|||||||
|
|
||||||
/* ----------------------------------------------------------------------- */
|
/* ----------------------------------------------------------------------- */
|
||||||
|
|
||||||
HAWK_EXPORT int hawk_rtx_convbtouchars (
|
#if defined(HAWK_HAVE_INLINE)
|
||||||
hawk_rtx_t* rtx,
|
static HAWK_INLINE int hawk_rtx_convbtouchars (hawk_rtx_t* rtx, const hawk_bch_t* bcs, hawk_oow_t* bcslen, hawk_uch_t* ucs, hawk_oow_t* ucslen, int all) { return hawk_gem_convbtouchars(hawk_rtx_getgem(rtx), bcs, bcslen, ucs, ucslen, all); }
|
||||||
const hawk_bch_t* bcs,
|
static HAWK_INLINE int hawk_rtx_convutobchars (hawk_rtx_t* rtx, const hawk_uch_t* ucs, hawk_oow_t* ucslen, hawk_bch_t* bcs, hawk_oow_t* bcslen) { return hawk_gem_convutobchars(hawk_rtx_getgem(rtx), ucs, ucslen, bcs, bcslen); }
|
||||||
hawk_oow_t* bcslen,
|
static HAWK_INLINE int hawk_rtx_convbtoucstr (hawk_rtx_t* rtx, const hawk_bch_t* bcs, hawk_oow_t* bcslen, hawk_uch_t* ucs, hawk_oow_t* ucslen, int all) { return hawk_gem_convbtoucstr(hawk_rtx_getgem(rtx), bcs, bcslen, ucs, ucslen, all); }
|
||||||
hawk_uch_t* ucs,
|
static HAWK_INLINE int hawk_rtx_convutobcstr (hawk_rtx_t* rtx, const hawk_uch_t* ucs, hawk_oow_t* ucslen, hawk_bch_t* bcs, hawk_oow_t* bcslen) { return hawk_gem_convutobcstr(hawk_rtx_getgem(rtx), ucs, ucslen, bcs, bcslen); }
|
||||||
hawk_oow_t* ucslen,
|
#else
|
||||||
int all
|
#define hawk_rtx_convbtouchars(rtx, bcs, bcslen, ucs, ucslen, all) hawk_gem_convbtouchars(hawk_rtx_getgem(rtx), bcs, bcslen, ucs, ucslen, all)
|
||||||
);
|
#define hawk_rtx_convutobchars(rtx, ucs, ucslen, bcs, bcslen) hawk_gem_convutobchars(hawk_rtx_getgem(rtx), ucs, ucslen, bcs, bcslen)
|
||||||
|
#define hawk_rtx_convbtoucstr(rtx, bcs, bcslen, ucs, ucslen, all) hawk_gem_convbtoucstr(hawk_rtx_getgem(rtx), bcs, bcslen, ucs, ucslen, all)
|
||||||
HAWK_EXPORT int hawk_rtx_convutobchars (
|
#define hawk_rtx_convutobcstr(rtx, ucs, ucslen, bcs, bcslen) hawk_gem_convutobcstr(hawk_rtx_getgem(rtx), ucs, ucslen, bcs, bcslen)
|
||||||
hawk_rtx_t* rtx,
|
#endif
|
||||||
const hawk_uch_t* ucs,
|
|
||||||
hawk_oow_t* ucslen,
|
|
||||||
hawk_bch_t* bcs,
|
|
||||||
hawk_oow_t* bcslen
|
|
||||||
);
|
|
||||||
|
|
||||||
HAWK_EXPORT int hawk_rtx_convbtoucstr (
|
|
||||||
hawk_rtx_t* rtx,
|
|
||||||
const hawk_bch_t* bcs,
|
|
||||||
hawk_oow_t* bcslen,
|
|
||||||
hawk_uch_t* ucs,
|
|
||||||
hawk_oow_t* ucslen,
|
|
||||||
int all
|
|
||||||
);
|
|
||||||
|
|
||||||
HAWK_EXPORT int hawk_rtx_convutobcstr (
|
|
||||||
hawk_rtx_t* rtx,
|
|
||||||
const hawk_uch_t* ucs,
|
|
||||||
hawk_oow_t* ucslen,
|
|
||||||
hawk_bch_t* bcs,
|
|
||||||
hawk_oow_t* bcslen
|
|
||||||
);
|
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------- */
|
/* ----------------------------------------------------------------------- */
|
||||||
|
|
||||||
HAWK_EXPORT hawk_uch_t* hawk_rtx_dupbtouchars (
|
#if defined(HAWK_HAVE_INLINE)
|
||||||
|
static HAWK_INLINE hawk_uch_t* hawk_rtx_dupbtouchars (hawk_rtx_t* rtx, const hawk_bch_t* bcs, hawk_oow_t _bcslen, hawk_oow_t* _ucslen, int all) { return hawk_gem_dupbtouchars(hawk_rtx_getgem(rtx), bcs, _bcslen, _ucslen, all); }
|
||||||
|
static HAWK_INLINE hawk_bch_t* hawk_rtx_duputobchars (hawk_rtx_t* rtx, const hawk_uch_t* ucs, hawk_oow_t _ucslen, hawk_oow_t* _bcslen) { return hawk_gem_duputobchars(hawk_rtx_getgem(rtx), ucs, _ucslen, _bcslen); }
|
||||||
|
static HAWK_INLINE hawk_uch_t* hawk_rtx_dupb2touchars (hawk_rtx_t* rtx, const hawk_bch_t* bcs1, hawk_oow_t bcslen1, const hawk_bch_t* bcs2, hawk_oow_t bcslen2, hawk_oow_t* ucslen, int all) { return hawk_gem_dupb2touchars(hawk_rtx_getgem(rtx), bcs1, bcslen1, bcs2, bcslen2, ucslen, all); }
|
||||||
|
static HAWK_INLINE hawk_bch_t* hawk_rtx_dupu2tobchars (hawk_rtx_t* rtx, const hawk_uch_t* ucs1, hawk_oow_t ucslen1, const hawk_uch_t* ucs2, hawk_oow_t ucslen2, hawk_oow_t* bcslen) { return hawk_gem_dupu2tobchars(hawk_rtx_getgem(rtx), ucs1, ucslen1, ucs2, ucslen2, bcslen); }
|
||||||
|
static HAWK_INLINE hawk_uch_t* hawk_rtx_dupbtoucstr (hawk_rtx_t* rtx, const hawk_bch_t* bcs, hawk_oow_t* _ucslen, int all) { return hawk_gem_dupbtoucstr(hawk_rtx_getgem(rtx), bcs, _ucslen, all); }
|
||||||
|
static HAWK_INLINE hawk_bch_t* hawk_rtx_duputobcstr (hawk_rtx_t* rtx, const hawk_uch_t* ucs, hawk_oow_t* _bcslen) { return hawk_gem_duputobcstr(hawk_rtx_getgem(rtx), ucs, _bcslen); }
|
||||||
|
static HAWK_INLINE hawk_uch_t* hawk_rtx_dupbtoucharswithcmgr (hawk_rtx_t* rtx, const hawk_bch_t* bcs, hawk_oow_t _bcslen, hawk_oow_t* _ucslen, hawk_cmgr_t* cmgr, int all) { return hawk_gem_dupbtoucharswithcmgr(hawk_rtx_getgem(rtx), bcs, _bcslen, _ucslen, cmgr, all); }
|
||||||
|
static HAWK_INLINE hawk_bch_t* hawk_rtx_duputobcharswithcmgr (hawk_rtx_t* rtx, const hawk_uch_t* ucs, hawk_oow_t _ucslen, hawk_oow_t* _bcslen, hawk_cmgr_t* cmgr) { return hawk_gem_duputobcharswithcmgr(hawk_rtx_getgem(rtx), ucs, _ucslen, _bcslen, cmgr); }
|
||||||
|
static HAWK_INLINE hawk_bch_t* hawk_rtx_dupucstrarrtobcstr (hawk_rtx_t* rtx, const hawk_uch_t* ucsarr[], hawk_oow_t* bcslen) { return hawk_gem_dupucstrarrtobcstr(hawk_rtx_getgem(rtx), ucsarr, bcslen); }
|
||||||
|
#else
|
||||||
|
#define hawk_rtx_dupbtouchars(rtx, bcs, _bcslen, _ucslen, all) hawk_gem_dupbtouchars(hawk_rtx_getgem(rtx), bcs, _bcslen, _ucslen, all)
|
||||||
|
#define hawk_rtx_duputobchars(rtx, ucs, _ucslen, _bcslen) hawk_gem_duputobchars(hawk_rtx_getgem(rtx), ucs, _ucslen, _bcslen)
|
||||||
|
#define hawk_rtx_dupb2touchars(rtx, bcs1, bcslen1, bcs2, bcslen2, ucslen, all) hawk_gem_dupb2touchars(hawk_rtx_getgem(rtx), bcs1, bcslen1, bcs2, bcslen2, ucslen, all)
|
||||||
|
#define hawk_rtx_dupu2tobchars(rtx, ucs1, ucslen1, ucs2, ucslen2, bcslen) hawk_gem_dupu2tobchars(hawk_rtx_getgem(rtx), ucs1, ucslen1, ucs2, ucslen2, bcslen)
|
||||||
|
#define hawk_rtx_dupbtoucstr(rtx, bcs, _ucslen, all) hawk_gem_dupbtoucstr(hawk_rtx_getgem(rtx), bcs, _ucslen, all)
|
||||||
|
#define hawk_rtx_duputobcstr(rtx, ucs, _bcslen) hawk_gem_duputobcstr(hawk_rtx_getgem(rtx), ucs, _bcslen)
|
||||||
|
#define hawk_rtx_dupbtoucharswithcmgr(rtx, bcs, _bcslen, _ucslen, cmgr, all) hawk_gem_dupbtoucharswithcmgr(hawk_rtx_getgem(rtx), bcs, _bcslen, _ucslen, cmgr, all)
|
||||||
|
#define hawk_rtx_duputobcharswithcmgr(rtx, ucs, _ucslen, _bcslen, cmgr) hawk_gem_duputobcharswithcmgr(hawk_rtx_getgem(rtx), ucs, _ucslen, _bcslen, cmgr)
|
||||||
|
#define hawk_rtx_dupucstrarrtobcstr(rtx, ucsarr, bcslen) hawk_gem_dupucstrarrtobcstr(hawk_rtx_getgem(rtx), ucsarr, bcslen)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
HAWK_EXPORT hawk_oow_t hawk_rtx_fmttoucstr_ (
|
||||||
hawk_rtx_t* rtx,
|
hawk_rtx_t* rtx,
|
||||||
const hawk_bch_t* bcs,
|
hawk_uch_t* buf,
|
||||||
hawk_oow_t bcslen,
|
hawk_oow_t bufsz,
|
||||||
hawk_oow_t* ucslen,
|
const hawk_uch_t* fmt,
|
||||||
int all
|
...
|
||||||
);
|
);
|
||||||
|
|
||||||
HAWK_EXPORT hawk_bch_t* hawk_rtx_duputobchars (
|
HAWK_EXPORT hawk_oow_t hawk_rtx_fmttobcstr_ (
|
||||||
hawk_rtx_t* rtx,
|
hawk_rtx_t* rtx,
|
||||||
const hawk_uch_t* ucs,
|
hawk_bch_t* buf,
|
||||||
hawk_oow_t ucslen,
|
hawk_oow_t bufsz,
|
||||||
hawk_oow_t* bcslen
|
const hawk_bch_t* fmt,
|
||||||
|
...
|
||||||
);
|
);
|
||||||
|
|
||||||
HAWK_EXPORT hawk_uch_t* hawk_rtx_dupb2touchars (
|
#if defined(HAWK_HAVE_INLINE)
|
||||||
hawk_rtx_t* rtx,
|
static HAWK_INLINE hawk_oow_t hawk_rtx_vfmttoucstr (hawk_rtx_t* rtx, hawk_uch_t* buf, hawk_oow_t bufsz, const hawk_uch_t* fmt, va_list ap) { return hawk_gem_vfmttoucstr(hawk_rtx_getgem(rtx), buf, bufsz, fmt, ap); }
|
||||||
const hawk_bch_t* bcs1,
|
static HAWK_INLINE hawk_oow_t hawk_rtx_fmttoucstr (hawk_rtx_t* rtx, hawk_uch_t* buf, hawk_oow_t bufsz, const hawk_uch_t* fmt, ...) { va_list ap; hawk_oow_t n; va_start(ap, fmt); n = hawk_gem_vfmttoucstr(hawk_rtx_getgem(rtx), buf, bufsz, fmt, ap); va_end(ap); return n; }
|
||||||
hawk_oow_t bcslen1,
|
static HAWK_INLINE hawk_oow_t hawk_rtx_vfmttobcstr (hawk_rtx_t* rtx, hawk_bch_t* buf, hawk_oow_t bufsz, const hawk_bch_t* fmt, va_list ap) { return hawk_gem_vfmttobcstr(hawk_rtx_getgem(rtx), buf, bufsz, fmt, ap); }
|
||||||
const hawk_bch_t* bcs2,
|
static HAWK_INLINE hawk_oow_t hawk_rtx_fmttobcstr (hawk_rtx_t* rtx, hawk_bch_t* buf, hawk_oow_t bufsz, const hawk_bch_t* fmt, ...) { va_list ap; hawk_oow_t n; va_start(ap, fmt); n = hawk_gem_vfmttobcstr(hawk_rtx_getgem(rtx), buf, bufsz, fmt, ap); va_end(ap); return n; }
|
||||||
hawk_oow_t bcslen2,
|
#else
|
||||||
hawk_oow_t* ucslen,
|
#define hawk_rtx_vfmttoucstr(rtx, buf, bufsz, fmt, ap) hawk_gem_vfmttoucstr(hawk_rtx_getgem(rtx), buf, bufsz, fmt, ap)
|
||||||
int all
|
#define hawk_rtx_fmttoucstr hawk_rtx_fmttoucstr_
|
||||||
);
|
#define hawk_rtx_vfmttobcstr(rtx, buf, bufsz, fmt, ap) hawk_gem_vfmttobcstr(hawk_rtx_getgem(rtx), buf, bufsz, fmt, ap)
|
||||||
|
#define hawk_rtx_fmttobcstr hawk_rtx_fmttobcstr_
|
||||||
|
#endif
|
||||||
|
|
||||||
HAWK_EXPORT hawk_bch_t* hawk_rtx_dupu2tobchars (
|
#if defined(HAWK_OOCH_IS_UCH)
|
||||||
hawk_rtx_t* rtx,
|
# define hawk_rtx_vfmttooocstr hawk_rtx_vfmttoucstr
|
||||||
const hawk_uch_t* ucs1,
|
# define hawk_rtx_fmttooocstr hawk_rtx_fmttoucstr
|
||||||
hawk_oow_t ucslen1,
|
#else
|
||||||
const hawk_uch_t* ucs2,
|
# define hawk_rtx_vfmttooocstr hawk_rtx_vfmttobcstr
|
||||||
hawk_oow_t ucslen2,
|
# define hawk_rtx_fmttooocstr hawk_rtx_fmttobcstr
|
||||||
hawk_oow_t* bcslen
|
#endif
|
||||||
);
|
|
||||||
|
|
||||||
HAWK_EXPORT hawk_uch_t* hawk_rtx_dupbtoucstr (
|
|
||||||
hawk_rtx_t* rtx,
|
|
||||||
const hawk_bch_t* bcs,
|
|
||||||
hawk_oow_t* ucslen,
|
|
||||||
int all
|
|
||||||
);
|
|
||||||
|
|
||||||
HAWK_EXPORT hawk_bch_t* hawk_rtx_duputobcstr (
|
|
||||||
hawk_rtx_t* rtx,
|
|
||||||
const hawk_uch_t* ucs,
|
|
||||||
hawk_oow_t* bcslen
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
HAWK_EXPORT hawk_uch_t* hawk_rtx_dupbtoucharswithcmgr (
|
|
||||||
hawk_rtx_t* rtx,
|
|
||||||
const hawk_bch_t* bcs,
|
|
||||||
hawk_oow_t _bcslen,
|
|
||||||
hawk_oow_t* _ucslen,
|
|
||||||
hawk_cmgr_t* cmgr,
|
|
||||||
int all
|
|
||||||
);
|
|
||||||
|
|
||||||
HAWK_EXPORT hawk_bch_t* hawk_rtx_duputobcharswithcmgr (
|
|
||||||
hawk_rtx_t* rtx,
|
|
||||||
const hawk_uch_t* ucs,
|
|
||||||
hawk_oow_t _ucslen,
|
|
||||||
hawk_oow_t* _bcslen,
|
|
||||||
hawk_cmgr_t* cmgr
|
|
||||||
);
|
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------- */
|
/* ----------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
227
hawk/lib/run.c
227
hawk/lib/run.c
@ -8760,223 +8760,26 @@ void hawk_rtx_getnrflt (hawk_rtx_t* rtx, hawk_nrflt_t* nrflt)
|
|||||||
*nrflt = rtx->nrflt;
|
*nrflt = rtx->nrflt;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
|
||||||
|
|
||||||
int hawk_rtx_convbtouchars (hawk_rtx_t* rtx, const hawk_bch_t* bcs, hawk_oow_t* bcslen, hawk_uch_t* ucs, hawk_oow_t* ucslen, int all)
|
|
||||||
|
/* ------------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
hawk_oow_t hawk_rtx_fmttoucstr_ (hawk_rtx_t* rtx, hawk_uch_t* buf, hawk_oow_t bufsz, const hawk_uch_t* fmt, ...)
|
||||||
{
|
{
|
||||||
/* length bound */
|
va_list ap;
|
||||||
int n;
|
hawk_oow_t n;
|
||||||
n = hawk_conv_bchars_to_uchars_with_cmgr(bcs, bcslen, ucs, ucslen, hawk_rtx_getcmgr(rtx), all);
|
va_start(ap, fmt);
|
||||||
/* -1: illegal character, -2: buffer too small, -3: incomplete sequence */
|
n = hawk_gem_vfmttoucstr(hawk_rtx_getgem(rtx), buf, bufsz, fmt, ap);
|
||||||
if (n <= -1) hawk_rtx_seterrnum (rtx, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR, HAWK_NULL);
|
va_end(ap);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
int hawk_rtx_convutobchars (hawk_rtx_t* rtx, const hawk_uch_t* ucs, hawk_oow_t* ucslen, hawk_bch_t* bcs, hawk_oow_t* bcslen)
|
hawk_oow_t hawk_rtx_fmttobcstr_ (hawk_rtx_t* rtx, hawk_bch_t* buf, hawk_oow_t bufsz, const hawk_bch_t* fmt, ...)
|
||||||
{
|
{
|
||||||
/* length bound */
|
va_list ap;
|
||||||
int n;
|
hawk_oow_t n;
|
||||||
n = hawk_conv_uchars_to_bchars_with_cmgr(ucs, ucslen, bcs, bcslen, hawk_rtx_getcmgr(rtx));
|
va_start(ap, fmt);
|
||||||
if (n <= -1) hawk_rtx_seterrnum (rtx, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR, HAWK_NULL);
|
n = hawk_gem_vfmttobcstr(hawk_rtx_getgem(rtx), buf, bufsz, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
int hawk_rtx_convbtoucstr (hawk_rtx_t* rtx, const hawk_bch_t* bcs, hawk_oow_t* bcslen, hawk_uch_t* ucs, hawk_oow_t* ucslen, int all)
|
|
||||||
{
|
|
||||||
/* null-terminated. */
|
|
||||||
int n;
|
|
||||||
n = hawk_conv_bcstr_to_ucstr_with_cmgr(bcs, bcslen, ucs, ucslen, hawk_rtx_getcmgr(rtx), all);
|
|
||||||
if (n <= -1) hawk_rtx_seterrnum (rtx, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR, HAWK_NULL);
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
int hawk_rtx_convutobcstr (hawk_rtx_t* rtx, const hawk_uch_t* ucs, hawk_oow_t* ucslen, hawk_bch_t* bcs, hawk_oow_t* bcslen)
|
|
||||||
{
|
|
||||||
/* null-terminated */
|
|
||||||
int n;
|
|
||||||
n = hawk_conv_ucstr_to_bcstr_with_cmgr(ucs, ucslen, bcs, bcslen, hawk_rtx_getcmgr(rtx));
|
|
||||||
if (n <= -1) hawk_rtx_seterrnum (rtx, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR, HAWK_NULL);
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
|
||||||
|
|
||||||
hawk_uch_t* hawk_rtx_dupbtouchars (hawk_rtx_t* rtx, const hawk_bch_t* bcs, hawk_oow_t _bcslen, hawk_oow_t* _ucslen, int all)
|
|
||||||
{
|
|
||||||
hawk_oow_t bcslen, ucslen;
|
|
||||||
hawk_uch_t* ucs;
|
|
||||||
|
|
||||||
bcslen = _bcslen;
|
|
||||||
if (hawk_rtx_convbtouchars(rtx, bcs, &bcslen, HAWK_NULL, &ucslen, all) <= -1) return HAWK_NULL;
|
|
||||||
|
|
||||||
ucs = hawk_rtx_allocmem(rtx, HAWK_SIZEOF(*ucs) * (ucslen + 1));
|
|
||||||
if (!ucs) return HAWK_NULL;
|
|
||||||
|
|
||||||
bcslen= _bcslen;
|
|
||||||
hawk_rtx_convbtouchars (rtx, bcs, &bcslen, ucs, &ucslen, all);
|
|
||||||
ucs[ucslen] = '\0';
|
|
||||||
|
|
||||||
if (_ucslen) *_ucslen = ucslen;
|
|
||||||
return ucs;
|
|
||||||
}
|
|
||||||
|
|
||||||
hawk_bch_t* hawk_rtx_duputobchars (hawk_rtx_t* rtx, const hawk_uch_t* ucs, hawk_oow_t _ucslen, hawk_oow_t* _bcslen)
|
|
||||||
{
|
|
||||||
hawk_oow_t bcslen, ucslen;
|
|
||||||
hawk_bch_t* bcs;
|
|
||||||
|
|
||||||
ucslen = _ucslen;
|
|
||||||
if (hawk_rtx_convutobchars(rtx, ucs, &ucslen, HAWK_NULL, &bcslen) <= -1) return HAWK_NULL;
|
|
||||||
|
|
||||||
bcs = hawk_rtx_allocmem(rtx, HAWK_SIZEOF(*bcs) * (bcslen + 1));
|
|
||||||
if (!bcs) return HAWK_NULL;
|
|
||||||
|
|
||||||
ucslen = _ucslen;
|
|
||||||
hawk_rtx_convutobchars (rtx, ucs, &ucslen, bcs, &bcslen);
|
|
||||||
bcs[bcslen] = '\0';
|
|
||||||
|
|
||||||
if (_bcslen) *_bcslen = bcslen;
|
|
||||||
return bcs;
|
|
||||||
}
|
|
||||||
|
|
||||||
hawk_uch_t* hawk_rtx_dupb2touchars (hawk_rtx_t* rtx, const hawk_bch_t* bcs1, hawk_oow_t bcslen1, const hawk_bch_t* bcs2, hawk_oow_t bcslen2, hawk_oow_t* ucslen, int all)
|
|
||||||
{
|
|
||||||
hawk_oow_t inlen, outlen1, outlen2;
|
|
||||||
hawk_uch_t* ptr;
|
|
||||||
|
|
||||||
inlen = bcslen1;
|
|
||||||
if (hawk_rtx_convbtouchars(rtx, bcs1, &inlen, HAWK_NULL, &outlen1, all) <= -1) return HAWK_NULL;
|
|
||||||
|
|
||||||
inlen = bcslen2;
|
|
||||||
if (hawk_rtx_convbtouchars(rtx, bcs2, &inlen, HAWK_NULL, &outlen2, all) <= -1) return HAWK_NULL;
|
|
||||||
|
|
||||||
ptr = (hawk_uch_t*)hawk_rtx_allocmem(rtx, (outlen1 + outlen2 + 1) * HAWK_SIZEOF(*ptr));
|
|
||||||
if (!ptr) return HAWK_NULL;
|
|
||||||
|
|
||||||
inlen = bcslen1;
|
|
||||||
hawk_rtx_convbtouchars (rtx, bcs1, &inlen, &ptr[0], &outlen1, all);
|
|
||||||
|
|
||||||
inlen = bcslen2;
|
|
||||||
hawk_rtx_convbtouchars (rtx, bcs2, &inlen, &ptr[outlen1], &outlen2, all);
|
|
||||||
|
|
||||||
/* hawk_convbtouchars() doesn't null-terminate the target.
|
|
||||||
* but in hawk_dupbtouchars(), i allocate space. so i don't mind
|
|
||||||
* null-terminating it with 1 extra character overhead */
|
|
||||||
ptr[outlen1 + outlen2] = '\0';
|
|
||||||
if (ucslen) *ucslen = outlen1 + outlen2;
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
hawk_bch_t* hawk_rtx_dupu2tobchars (hawk_rtx_t* rtx, const hawk_uch_t* ucs1, hawk_oow_t ucslen1, const hawk_uch_t* ucs2, hawk_oow_t ucslen2, hawk_oow_t* bcslen)
|
|
||||||
{
|
|
||||||
hawk_oow_t inlen, outlen1, outlen2;
|
|
||||||
hawk_bch_t* ptr;
|
|
||||||
|
|
||||||
inlen = ucslen1;
|
|
||||||
if (hawk_rtx_convutobchars(rtx, ucs1, &inlen, HAWK_NULL, &outlen1) <= -1) return HAWK_NULL;
|
|
||||||
|
|
||||||
inlen = ucslen2;
|
|
||||||
if (hawk_rtx_convutobchars(rtx, ucs2, &inlen, HAWK_NULL, &outlen2) <= -1) return HAWK_NULL;
|
|
||||||
|
|
||||||
ptr = (hawk_bch_t*)hawk_rtx_allocmem(rtx, (outlen1 + outlen2 + 1) * HAWK_SIZEOF(*ptr));
|
|
||||||
if (!ptr) return HAWK_NULL;
|
|
||||||
|
|
||||||
inlen = ucslen1;
|
|
||||||
hawk_rtx_convutobchars (rtx, ucs1, &inlen, &ptr[0], &outlen1);
|
|
||||||
|
|
||||||
inlen = ucslen2;
|
|
||||||
hawk_rtx_convutobchars (rtx, ucs2, &inlen, &ptr[outlen1], &outlen2);
|
|
||||||
|
|
||||||
ptr[outlen1 + outlen2] = '\0';
|
|
||||||
if (bcslen) *bcslen = outlen1 + outlen2;
|
|
||||||
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
hawk_uch_t* hawk_rtx_dupbtoucstr (hawk_rtx_t* rtx, const hawk_bch_t* bcs, hawk_oow_t* _ucslen, int all)
|
|
||||||
{
|
|
||||||
hawk_oow_t bcslen, ucslen;
|
|
||||||
hawk_uch_t* ucs;
|
|
||||||
|
|
||||||
if (hawk_rtx_convbtoucstr(rtx, bcs, &bcslen, HAWK_NULL, &ucslen, all) <= -1) return HAWK_NULL;
|
|
||||||
ucslen = ucslen + 1; /* for terminating null */
|
|
||||||
|
|
||||||
ucs = hawk_rtx_allocmem(rtx, HAWK_SIZEOF(*ucs) * ucslen);
|
|
||||||
if (!ucs) return HAWK_NULL;
|
|
||||||
|
|
||||||
hawk_rtx_convbtoucstr (rtx, bcs, &bcslen, ucs, &ucslen, all);
|
|
||||||
if (_ucslen) *_ucslen = ucslen;
|
|
||||||
return ucs;
|
|
||||||
}
|
|
||||||
|
|
||||||
hawk_bch_t* hawk_rtx_duputobcstr (hawk_rtx_t* rtx, const hawk_uch_t* ucs, hawk_oow_t* _bcslen)
|
|
||||||
{
|
|
||||||
hawk_oow_t bcslen, ucslen;
|
|
||||||
hawk_bch_t* bcs;
|
|
||||||
|
|
||||||
if (hawk_rtx_convutobcstr(rtx, ucs, &ucslen, HAWK_NULL, &bcslen) <= -1) return HAWK_NULL;
|
|
||||||
bcslen = bcslen + 1; /* for terminating null */
|
|
||||||
|
|
||||||
bcs = hawk_rtx_allocmem(rtx, HAWK_SIZEOF(*bcs) * bcslen);
|
|
||||||
if (!bcs) return HAWK_NULL;
|
|
||||||
|
|
||||||
hawk_rtx_convutobcstr (rtx, ucs, &ucslen, bcs, &bcslen);
|
|
||||||
if (_bcslen) *_bcslen = bcslen;
|
|
||||||
return bcs;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
|
||||||
|
|
||||||
hawk_uch_t* hawk_rtx_dupbtoucharswithcmgr (hawk_rtx_t* rtx, const hawk_bch_t* bcs, hawk_oow_t _bcslen, hawk_oow_t* _ucslen, hawk_cmgr_t* cmgr, int all)
|
|
||||||
{
|
|
||||||
hawk_oow_t bcslen, ucslen;
|
|
||||||
hawk_uch_t* ucs;
|
|
||||||
int n;
|
|
||||||
|
|
||||||
bcslen = _bcslen;
|
|
||||||
n = hawk_conv_bchars_to_uchars_with_cmgr(bcs, &bcslen, HAWK_NULL, &ucslen, cmgr, all);
|
|
||||||
if (n <= -1)
|
|
||||||
{
|
|
||||||
/* -1: illegal character, -2: buffer too small, -3: incomplete sequence */
|
|
||||||
hawk_rtx_seterrnum (rtx, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR, HAWK_NULL);
|
|
||||||
return HAWK_NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ucs = hawk_rtx_allocmem(rtx, HAWK_SIZEOF(*ucs) * (ucslen + 1));
|
|
||||||
if (!ucs) return HAWK_NULL;
|
|
||||||
|
|
||||||
bcslen= _bcslen;
|
|
||||||
hawk_conv_bchars_to_uchars_with_cmgr(bcs, &bcslen, ucs, &ucslen, cmgr, all);
|
|
||||||
ucs[ucslen] = '\0';
|
|
||||||
|
|
||||||
if (_ucslen) *_ucslen = ucslen;
|
|
||||||
return ucs;
|
|
||||||
}
|
|
||||||
|
|
||||||
hawk_bch_t* hawk_rtx_duputobcharswithcmgr (hawk_rtx_t* rtx, const hawk_uch_t* ucs, hawk_oow_t _ucslen, hawk_oow_t* _bcslen, hawk_cmgr_t* cmgr)
|
|
||||||
{
|
|
||||||
hawk_oow_t bcslen, ucslen;
|
|
||||||
hawk_bch_t* bcs;
|
|
||||||
int n;
|
|
||||||
|
|
||||||
ucslen = _ucslen;
|
|
||||||
n = hawk_conv_uchars_to_bchars_with_cmgr(ucs, &ucslen, HAWK_NULL, &bcslen, cmgr);
|
|
||||||
if (n <= -1)
|
|
||||||
{
|
|
||||||
/* -1: illegal character, -2: buffer too small, -3: incomplete sequence */
|
|
||||||
hawk_rtx_seterrnum (rtx, (n == -2)? HAWK_EBUFFULL: HAWK_EECERR, HAWK_NULL);
|
|
||||||
return HAWK_NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
bcs = hawk_rtx_allocmem(rtx, HAWK_SIZEOF(*bcs) * (bcslen + 1));
|
|
||||||
if (!bcs) return HAWK_NULL;
|
|
||||||
|
|
||||||
ucslen = _ucslen;
|
|
||||||
hawk_conv_uchars_to_bchars_with_cmgr (ucs, &ucslen, bcs, &bcslen, cmgr);
|
|
||||||
bcs[bcslen] = '\0';
|
|
||||||
|
|
||||||
if (_bcslen) *_bcslen = bcslen;
|
|
||||||
return bcs;
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user