added qse_awk_rtx_valtostrdupwithcmgr()/qse_awk_rtx_getvalstrwithcmgr()

enhanced str::frommbs()/str::tombs() to support the encoding name as the second parameter
This commit is contained in:
hyung-hwan 2019-06-18 10:51:26 +00:00
parent a04c9b6125
commit 00d0dc786f
3 changed files with 57 additions and 49 deletions

View File

@ -2846,25 +2846,41 @@ QSE_EXPORT int qse_awk_rtx_valtostr (
* \return character pointer to a string converted on success,
* #QSE_NULL on failure
*/
QSE_EXPORT qse_char_t* qse_awk_rtx_valtostrdup (
#if 0
QSE_EXPORT qse_char_t* qse_awk_rtx_valtostrdupwithcmgr (
qse_awk_rtx_t* rtx, /**< runtime context */
const qse_awk_val_t* val, /**< value to convert */
qse_size_t* len /**< result length */
qse_size_t* len, /**< result length */
qse_cmgr_t* cmgr
);
#endif
QSE_EXPORT qse_mchar_t* qse_awk_rtx_valtombsdup (
qse_awk_rtx_t* rtx, /**< runtime context */
const qse_awk_val_t* val, /**< value to convert */
qse_size_t* len /**< result length */
qse_size_t* len, /**< result length */
qse_cmgr_t* cmgr
);
QSE_EXPORT qse_wchar_t* qse_awk_rtx_valtowcsdup (
qse_awk_rtx_t* rtx, /**< runtime context */
const qse_awk_val_t* val, /**< value to convert */
qse_size_t* len /**< result length */
qse_size_t* len, /**< result length */
qse_cmgr_t* cmgr
);
#define qse_awk_rtx_valtombsdup(rtx, val, len) qse_awk_rtx_valtombsdupwithcmgr(rtx, val, len, qse_awk_rtx_getcmgr(rtx))
#define qse_awk_rtx_valtowcsdup(rtx, val, len) qse_awk_rtx_valtowcsdupwithcmgr(rtx, val, len, qse_awk_rtx_getcmgr(rtx))
#if defined(QSE_CHAR_IS_MCHAR)
# define qse_awk_rtx_valtostrdupwithcmgr(rtx, val, len, cmgr) qse_awk_rtx_valtombsdup(rtx, val, len, cmgr)
# define qse_awk_rtx_valtostrdup(rtx, val, len) qse_awk_rtx_valtombsdup(rtx, val, len)
#else
# define qse_awk_rtx_valtostrdupwithcmgr(rtx, val, len, cmgr) qse_awk_rtx_valtowcsdupwithcmgr(rtx, val, len, cmgr)
# define qse_awk_rtx_valtostrdup(rtx, val, len) qse_awk_rtx_valtowcsdup(rtx, val, len)
#endif
/**
* The qse_awk_rtx_getvalstr() function returns a string
* pointer converted from a value \a val. If the value
@ -2873,12 +2889,15 @@ QSE_EXPORT qse_wchar_t* qse_awk_rtx_valtowcsdup (
* qse_awk_rtx_valtostrdup(). The length of the returned
* string is stored into the location pointed to by \a len.
*/
QSE_EXPORT qse_char_t* qse_awk_rtx_getvalstr (
QSE_EXPORT qse_char_t* qse_awk_rtx_getvalstrwithcmgr (
qse_awk_rtx_t* rtx, /**< runtime context */
const qse_awk_val_t* val, /**< value to convert */
qse_size_t* len /**< result length */
qse_size_t* len, /**< result length */
qse_cmgr_t* cmgr
);
#define qse_awk_rtx_getvalstr(rtx,val,len) qse_awk_rtx_getvalstrwithcmgr(rtx, val, len, qse_awk_rtx_getcmgr(rtx))
/**
* The qse_awk_rtx_freevalstr() function frees the memory pointed
* to by \a str if \a val is not of the #QSE_AWK_VAL_STR type.
@ -2899,7 +2918,7 @@ QSE_EXPORT qse_mchar_t* qse_awk_rtx_getvalmbswithcmgr (
qse_cmgr_t* cmgr
);
#define qse_awk_rtx_getvalmbs(rtx,val,len) qse_awk_rtx_getvalmbswithcmgr(rtx, val, len, qse_awk_rtx_getcmgr(rtx))
#define qse_awk_rtx_getvalmbs(rtx, val, len) qse_awk_rtx_getvalmbswithcmgr(rtx, val, len, qse_awk_rtx_getcmgr(rtx))
QSE_EXPORT void qse_awk_rtx_freevalmbs (
qse_awk_rtx_t* rtx, /**< runtime context */

View File

@ -358,7 +358,12 @@ static int fnc_tocharcode (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
static int fnc_frommbs (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
{
/* str::frommbs(B"byte-string" [, "encoding-name"]) */
/* str::frommbs(B"byte-string" [, "encoding-name"])
*
* if you use a supported encoding name, it may look like this:
* a = str::frommbs(B"\xC7\xD1\xB1\xDB", "cp949");
* printf ("%K\n", a);
*/
qse_awk_val_t* a0, * r;
qse_cmgr_t* cmgr = qse_awk_rtx_getcmgr(rtx);
@ -367,10 +372,13 @@ static int fnc_frommbs (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
qse_awk_val_t* a1;
qse_cstr_t enc;
a1 = qse_awk_rtx_getarg(rtx, 1);
enc.ptr = qse_awk_rtx_getvalstr(rtx, a1, &enc.len);
if (!enc.ptr) return -1;
cmgr = (enc.len == qse_strlen(enc.ptr))? qse_findcmgr(enc.ptr): QSE_NULL;
/* if encoding name is an empty string, qse_Findcmgr() returns the default cmgr.
* i don't want that behavior. */
cmgr = (enc.len > 0 && enc.len == qse_strlen(enc.ptr))? qse_findcmgr(enc.ptr): QSE_NULL;
qse_awk_rtx_freevalstr (rtx, a1, enc.ptr);
if (!cmgr)
@ -391,7 +399,7 @@ static int fnc_frommbs (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
default:
{
qse_cstr_t str;
str.ptr = qse_awk_rtx_getvalstr(rtx, a0, &str.len);
str.ptr = qse_awk_rtx_getvalstrwithcmgr(rtx, a0, &str.len, cmgr);
if (!str.ptr) return -1;
r = qse_awk_rtx_makestrvalwithxstr(rtx, &str);
qse_awk_rtx_freevalstr (rtx, a0, str.ptr);
@ -407,7 +415,13 @@ done:
static int fnc_tombs (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
{
/* str::tombs("string", [, "encoding-name"]) */
/* str::tombs("string", [, "encoding-name"])
*
* if you use a supported encoding name, it may look like this:
* a = str::tombs("\uD55C\uAE00", "cp949");
* printf (B"%K\n", a);
*/
qse_awk_val_t* a0, * r;
qse_cmgr_t* cmgr = qse_awk_rtx_getcmgr(rtx);
@ -418,7 +432,9 @@ static int fnc_tombs (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
a1 = qse_awk_rtx_getarg(rtx, 1);
enc.ptr = qse_awk_rtx_getvalstr(rtx, a1, &enc.len);
if (!enc.ptr) return -1;
cmgr = (enc.len == qse_strlen(enc.ptr))? qse_findcmgr(enc.ptr): QSE_NULL;
/* if encoding name is an empty string, qse_Findcmgr() returns the default cmgr.
* i don't want that behavior. */
cmgr = (enc.len > 0 && enc.len == qse_strlen(enc.ptr))? qse_findcmgr(enc.ptr): QSE_NULL;
qse_awk_rtx_freevalstr (rtx, a1, enc.ptr);
if (!cmgr)
@ -490,7 +506,7 @@ static int fnc_tonum (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
qse_awk_int_t base;
if (qse_awk_rtx_valtoint(rtx, a1, &base) <= -1) return -1;
rx = qse_awk_rtx_strtonum (
rx = qse_awk_rtx_strtonum(
rtx,
QSE_AWK_RTX_STRTONUM_MAKE_OPTION(0, base),
((qse_awk_val_str_t*)a0)->val.ptr,

View File

@ -1492,18 +1492,7 @@ int qse_awk_rtx_valtostr (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_awk_rt
return -1;
}
qse_char_t* qse_awk_rtx_valtostrdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
{
qse_awk_rtx_valtostr_out_t out;
out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP;
if (qse_awk_rtx_valtostr(rtx, v, &out) <= -1) return QSE_NULL;
if (len) *len = out.u.cpldup.len;
return out.u.cpldup.ptr;
}
qse_mchar_t* qse_awk_rtx_valtombsdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
qse_mchar_t* qse_awk_rtx_valtombsdupwithcmgr (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len, qse_cmgr_t* cmgr)
{
qse_mchar_t* mbs;
@ -1530,7 +1519,7 @@ qse_mchar_t* qse_awk_rtx_valtombsdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v
mbs = qse_strxdup(str0, len0, rtx->awk->mmgr);
len1 = len0;
#else
mbs = qse_wcsntombsdupwithcmgr(str0, len0, &len1, rtx->awk->mmgr, rtx->awk->cmgr);
mbs = qse_wcsntombsdupwithcmgr(str0, len0, &len1, rtx->awk->mmgr, cmgr);
#endif
qse_awk_rtx_freevalstr (rtx, v, str0);
if (!mbs)
@ -1544,7 +1533,7 @@ qse_mchar_t* qse_awk_rtx_valtombsdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v
return mbs;
}
qse_wchar_t* qse_awk_rtx_valtowcsdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
qse_wchar_t* qse_awk_rtx_valtowcsdupwithcmgr (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len, qse_cmgr_t* cmgr)
{
qse_wchar_t* wcs;
qse_awk_val_type_t vtype;
@ -1557,7 +1546,7 @@ qse_wchar_t* qse_awk_rtx_valtowcsdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v
{
qse_size_t mbslen, wcslen;
mbslen = ((qse_awk_val_mbs_t*)v)->val.len;
wcs = qse_mbsntowcsalldupwithcmgr(((qse_awk_val_mbs_t*)v)->val.ptr, &mbslen, &wcslen, rtx->awk->mmgr, rtx->awk->cmgr);
wcs = qse_mbsntowcsalldupwithcmgr(((qse_awk_val_mbs_t*)v)->val.ptr, &mbslen, &wcslen, rtx->awk->mmgr, cmgr);
if (!wcs)
{
qse_awk_rtx_seterror (rtx, QSE_AWK_ENOMEM, QSE_NULL, QSE_NULL);
@ -1573,7 +1562,7 @@ qse_wchar_t* qse_awk_rtx_valtowcsdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v
#if defined(QSE_CHAR_IS_MCHAR)
qse_size_t wcslen, mbslen;
mbslen = ((qse_awk_val_str_t*)v)->val.len;
wcs = qse_mbsntowcsalldupwithcmgr(((qse_awk_val_str_t*)v)->val.ptr, &mbslen, &wcslen, rtx->awk->mmgr, rtx->awk->cmgr);
wcs = qse_mbsntowcsalldupwithcmgr(((qse_awk_val_str_t*)v)->val.ptr, &mbslen, &wcslen, rtx->awk->mmgr, cmgr);
#else
wcs = qse_strxdup(((qse_awk_val_str_t*)v)->val.ptr, ((qse_awk_val_str_t*)v)->val.len, rtx->awk->mmgr);
#endif
@ -1600,7 +1589,7 @@ qse_wchar_t* qse_awk_rtx_valtowcsdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v
dup = qse_awk_rtx_valtostrdup(rtx, v, &duplen);
if (!dup) return QSE_NULL;
wcs = qse_mbsntowcsalldupwithcmgr(dup, &duplen, &wcslen, rtx->awk->mmgr, rtx->awk->cmgr);
wcs = qse_mbsntowcsalldupwithcmgr(dup, &duplen, &wcslen, rtx->awk->mmgr, cmgr);
qse_awk_rtx_freemem (rtx, dup);
if (!wcs)
{
@ -1621,7 +1610,7 @@ qse_wchar_t* qse_awk_rtx_valtowcsdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v
return wcs;
}
qse_char_t* qse_awk_rtx_getvalstr (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
qse_char_t* qse_awk_rtx_getvalstrwithcmgr (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len, qse_cmgr_t* cmgr)
{
if (QSE_AWK_RTX_GETVALTYPE(rtx, v) == QSE_AWK_VAL_STR)
{
@ -1630,7 +1619,7 @@ qse_char_t* qse_awk_rtx_getvalstr (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, q
}
else
{
return qse_awk_rtx_valtostrdup(rtx, v, len);
return qse_awk_rtx_valtostrdupwithcmgr(rtx, v, len, cmgr);
}
}
@ -1643,22 +1632,6 @@ void qse_awk_rtx_freevalstr (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_cha
}
}
#if 0
qse_mchar_t* qse_awk_rtx_getvalmbs (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
{
if (QSE_AWK_RTX_GETVALTYPE(rtx, v) == QSE_AWK_VAL_MBS)
{
if (len) *len = ((qse_awk_val_mbs_t*)v)->val.len;
return ((qse_awk_val_mbs_t*)v)->val.ptr;
}
else
{
return qse_awk_rtx_valtombsdup(rtx, v, len);
}
}
#endif
qse_mchar_t* qse_awk_rtx_getvalmbswithcmgr (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len, qse_cmgr_t* cmgr)
{
if (QSE_AWK_RTX_GETVALTYPE(rtx, v) == QSE_AWK_VAL_MBS)
@ -1668,7 +1641,7 @@ qse_mchar_t* qse_awk_rtx_getvalmbswithcmgr (qse_awk_rtx_t* rtx, const qse_awk_va
}
else
{
return qse_awk_rtx_valtombsdup(rtx, v, len); /* TODO: */
return qse_awk_rtx_valtombsdupwithcmgr(rtx, v, len, cmgr);
}
}