writing mbs_to_str() in awk/val.c
This commit is contained in:
parent
711320d847
commit
f16437d7c0
@ -123,12 +123,10 @@ protected:
|
||||
int __build_environ (Run* run, void* envptr);
|
||||
|
||||
// intrinsic functions
|
||||
qse_cmgr_t* getcmgr (const char_t* ioname);
|
||||
qse_cmgr_t* getiocmgr (const char_t* ioname);
|
||||
|
||||
int setioattr (Run& run, Value& ret, Value* args, size_t nargs,
|
||||
const char_t* name, size_t len);
|
||||
int getioattr (Run& run, Value& ret, Value* args, size_t nargs,
|
||||
const char_t* name, size_t len);
|
||||
int setioattr (Run& run, Value& ret, Value* args, size_t nargs, const char_t* name, size_t len);
|
||||
int getioattr (Run& run, Value& ret, Value* args, size_t nargs, const char_t* name, size_t len);
|
||||
|
||||
// pipe io handlers
|
||||
int openPipe (Pipe& io);
|
||||
|
@ -179,12 +179,12 @@ QSE_EXPORT void* qse_awk_rtx_getxtnstd (
|
||||
|
||||
|
||||
/**
|
||||
* The qse_awk_rtx_getcmgrstd() function gets the current character
|
||||
* The qse_awk_rtx_getiocmgrstd() function gets the current character
|
||||
* manager associated with a particular I/O target indicated by the name
|
||||
* \a ioname if #QSE_CHAR_IS_WCHAR is defined. It always returns #QSE_NULL
|
||||
* if #QSE_CHAR_IS_MCHAR is defined.
|
||||
*/
|
||||
QSE_EXPORT qse_cmgr_t* qse_awk_rtx_getcmgrstd (
|
||||
QSE_EXPORT qse_cmgr_t* qse_awk_rtx_getiocmgrstd (
|
||||
qse_awk_rtx_t* rtx,
|
||||
const qse_char_t* ioname
|
||||
);
|
||||
|
@ -329,7 +329,7 @@ int StdAwk::make_additional_globals (Run* run)
|
||||
return 0;
|
||||
}
|
||||
|
||||
qse_cmgr_t* StdAwk::getcmgr (const char_t* ioname)
|
||||
qse_cmgr_t* StdAwk::getiocmgr (const char_t* ioname)
|
||||
{
|
||||
QSE_ASSERT (this->cmgrtab_inited == true);
|
||||
|
||||
@ -521,7 +521,7 @@ int StdAwk::open_nwio (Pipe& io, int flags, void* nwad)
|
||||
if (handle == QSE_NULL) return -1;
|
||||
|
||||
#if defined(QSE_CHAR_IS_WCHAR)
|
||||
qse_cmgr_t* cmgr = this->getcmgr (io.getName());
|
||||
qse_cmgr_t* cmgr = this->getiocmgr(io.getName());
|
||||
if (cmgr) qse_nwio_setcmgr (handle, cmgr);
|
||||
#endif
|
||||
|
||||
@ -566,7 +566,7 @@ int StdAwk::open_pio (Pipe& io)
|
||||
if (pio == QSE_NULL) return -1;
|
||||
|
||||
#if defined(QSE_CHAR_IS_WCHAR)
|
||||
qse_cmgr_t* cmgr = this->getcmgr (io.getName());
|
||||
qse_cmgr_t* cmgr = this->getiocmgr(io.getName());
|
||||
if (cmgr)
|
||||
{
|
||||
qse_pio_setcmgr (pio, QSE_PIO_IN, cmgr);
|
||||
@ -698,9 +698,9 @@ int StdAwk::openFile (File& io)
|
||||
}
|
||||
|
||||
sio = qse_sio_open(this->getMmgr(), 0, io.getName(), flags);
|
||||
if (sio == NULL) return -1;
|
||||
if (!sio) return -1;
|
||||
#if defined(QSE_CHAR_IS_WCHAR)
|
||||
qse_cmgr_t* cmgr = this->getcmgr (io.getName());
|
||||
qse_cmgr_t* cmgr = this->getiocmgr(io.getName());
|
||||
if (cmgr) qse_sio_setcmgr (sio, cmgr);
|
||||
#endif
|
||||
|
||||
|
@ -385,6 +385,7 @@ struct qse_awk_rtx_t
|
||||
qse_awk_errinf_t errinf;
|
||||
|
||||
qse_awk_t* awk;
|
||||
qse_cmgr_t* cmgr; /* internal default cmgr */
|
||||
qse_awk_rtx_ecb_t* ecb;
|
||||
};
|
||||
|
||||
|
@ -825,8 +825,7 @@ static qse_rbt_walk_t fini_module (qse_rbt_t* rbt, qse_rbt_pair_t* pair, void* c
|
||||
return QSE_RBT_WALK_FORWARD;
|
||||
}
|
||||
|
||||
qse_awk_rtx_t* qse_awk_rtx_open (
|
||||
qse_awk_t* awk, qse_size_t xtnsize, qse_awk_rio_t* rio)
|
||||
qse_awk_rtx_t* qse_awk_rtx_open (qse_awk_t* awk, qse_size_t xtnsize, qse_awk_rio_t* rio)
|
||||
{
|
||||
qse_awk_rtx_t* rtx;
|
||||
struct module_init_ctx_t mic;
|
||||
@ -846,9 +845,8 @@ qse_awk_rtx_t* qse_awk_rtx_open (
|
||||
}
|
||||
|
||||
/* allocate the storage for the rtx object */
|
||||
rtx = (qse_awk_rtx_t*) QSE_AWK_ALLOC (
|
||||
awk, QSE_SIZEOF(qse_awk_rtx_t) + xtnsize);
|
||||
if (rtx == QSE_NULL)
|
||||
rtx = (qse_awk_rtx_t*)QSE_AWK_ALLOC(awk, QSE_SIZEOF(qse_awk_rtx_t) + xtnsize);
|
||||
if (!rtx)
|
||||
{
|
||||
/* if it fails, the failure is reported thru
|
||||
* the awk object */
|
||||
@ -987,6 +985,7 @@ static int init_rtx (qse_awk_rtx_t* rtx, qse_awk_t* awk, qse_awk_rio_t* rio)
|
||||
};
|
||||
|
||||
rtx->awk = awk;
|
||||
rtx->cmgr = qse_getdflcmgr();
|
||||
|
||||
CLRERR (rtx);
|
||||
|
||||
@ -1016,25 +1015,21 @@ static int init_rtx (qse_awk_rtx_t* rtx, qse_awk_t* awk, qse_awk_rio_t* rio)
|
||||
if (qse_str_init(&rtx->format.fmt, MMGR(rtx), 256) <= -1) goto oops_4;
|
||||
|
||||
rtx->named = qse_htb_open(MMGR(rtx), QSE_SIZEOF(rtx), 1024, 70, QSE_SIZEOF(qse_char_t), 1);
|
||||
if (rtx->named == QSE_NULL) goto oops_5;
|
||||
if (!rtx->named) goto oops_5;
|
||||
*(qse_awk_rtx_t**)QSE_XTN(rtx->named) = rtx;
|
||||
qse_htb_setstyle (rtx->named, &style_for_named);
|
||||
|
||||
rtx->format.tmp.ptr = (qse_char_t*)
|
||||
QSE_AWK_ALLOC (rtx->awk, 4096*QSE_SIZEOF(qse_char_t*));
|
||||
if (rtx->format.tmp.ptr == QSE_NULL) goto oops_6;
|
||||
rtx->format.tmp.ptr = (qse_char_t*)QSE_AWK_ALLOC(rtx->awk, 4096*QSE_SIZEOF(qse_char_t*));
|
||||
if (!rtx->format.tmp.ptr) goto oops_6;
|
||||
rtx->format.tmp.len = 4096;
|
||||
rtx->format.tmp.inc = 4096*2;
|
||||
|
||||
if (rtx->awk->tree.chain_size > 0)
|
||||
{
|
||||
rtx->pattern_range_state = (qse_byte_t*) QSE_AWK_ALLOC (
|
||||
rtx->awk, rtx->awk->tree.chain_size*QSE_SIZEOF(qse_byte_t));
|
||||
if (rtx->pattern_range_state == QSE_NULL) goto oops_7;
|
||||
rtx->pattern_range_state = (qse_byte_t*)QSE_AWK_ALLOC(rtx->awk, rtx->awk->tree.chain_size * QSE_SIZEOF(qse_byte_t));
|
||||
if (!rtx->pattern_range_state) goto oops_7;
|
||||
|
||||
QSE_MEMSET (
|
||||
rtx->pattern_range_state, 0,
|
||||
rtx->awk->tree.chain_size * QSE_SIZEOF(qse_byte_t));
|
||||
QSE_MEMSET (rtx->pattern_range_state, 0, rtx->awk->tree.chain_size * QSE_SIZEOF(qse_byte_t));
|
||||
}
|
||||
else rtx->pattern_range_state = QSE_NULL;
|
||||
|
||||
|
@ -1080,7 +1080,7 @@ static qse_ssize_t nwio_handler_open (
|
||||
|
||||
#if defined(QSE_CHAR_IS_WCHAR)
|
||||
{
|
||||
qse_cmgr_t* cmgr = qse_awk_rtx_getcmgrstd (rtx, riod->name);
|
||||
qse_cmgr_t* cmgr = qse_awk_rtx_getiocmgrstd(rtx, riod->name);
|
||||
if (cmgr) qse_nwio_setcmgr (handle, cmgr);
|
||||
}
|
||||
#endif
|
||||
@ -1202,7 +1202,7 @@ static qse_ssize_t pio_handler_open (
|
||||
|
||||
#if defined(QSE_CHAR_IS_WCHAR)
|
||||
{
|
||||
qse_cmgr_t* cmgr = qse_awk_rtx_getcmgrstd (rtx, riod->name);
|
||||
qse_cmgr_t* cmgr = qse_awk_rtx_getiocmgrstd(rtx, riod->name);
|
||||
if (cmgr)
|
||||
{
|
||||
qse_pio_setcmgr (handle, QSE_PIO_IN, cmgr);
|
||||
@ -1306,8 +1306,7 @@ static qse_ssize_t awk_rio_pipe (
|
||||
|
||||
rxtn = (rxtn_t*)QSE_XTN(rtx);
|
||||
|
||||
ioattr = get_ioattr (
|
||||
&rxtn->cmgrtab, riod->name, qse_strlen(riod->name));
|
||||
ioattr = get_ioattr(&rxtn->cmgrtab, riod->name, qse_strlen(riod->name));
|
||||
if (ioattr)
|
||||
{
|
||||
tmout = &tmout_buf;
|
||||
@ -1369,7 +1368,7 @@ static qse_ssize_t awk_rio_file (
|
||||
|
||||
#if defined(QSE_CHAR_IS_WCHAR)
|
||||
{
|
||||
qse_cmgr_t* cmgr = qse_awk_rtx_getcmgrstd (rtx, riod->name);
|
||||
qse_cmgr_t* cmgr = qse_awk_rtx_getiocmgrstd(rtx, riod->name);
|
||||
if (cmgr) qse_sio_setcmgr (handle, cmgr);
|
||||
}
|
||||
#endif
|
||||
@ -2021,27 +2020,20 @@ qse_awk_rtx_t* qse_awk_rtx_openstd (
|
||||
rio.file = awk_rio_file;
|
||||
rio.console = awk_rio_console;
|
||||
|
||||
rtx = qse_awk_rtx_open (
|
||||
awk,
|
||||
QSE_SIZEOF(rxtn_t) + xtnsize,
|
||||
&rio
|
||||
);
|
||||
if (rtx == QSE_NULL) return QSE_NULL;
|
||||
rtx = qse_awk_rtx_open(awk, QSE_SIZEOF(rxtn_t) + xtnsize, &rio);
|
||||
if (!rtx) return QSE_NULL;
|
||||
|
||||
rxtn = (rxtn_t*) QSE_XTN (rtx);
|
||||
|
||||
if (rtx->awk->opt.trait & QSE_AWK_RIO)
|
||||
{
|
||||
if (qse_htb_init (
|
||||
&rxtn->cmgrtab, awk->mmgr, 256, 70,
|
||||
QSE_SIZEOF(qse_char_t), 1) <= -1)
|
||||
if (qse_htb_init(&rxtn->cmgrtab, awk->mmgr, 256, 70, QSE_SIZEOF(qse_char_t), 1) <= -1)
|
||||
{
|
||||
qse_awk_rtx_close (rtx);
|
||||
qse_awk_seterrnum (awk, QSE_AWK_ENOMEM, QSE_NULL);
|
||||
return QSE_NULL;
|
||||
}
|
||||
qse_htb_setstyle (&rxtn->cmgrtab,
|
||||
qse_gethtbstyle(QSE_HTB_STYLE_INLINE_COPIERS));
|
||||
qse_htb_setstyle (&rxtn->cmgrtab, qse_gethtbstyle(QSE_HTB_STYLE_INLINE_COPIERS));
|
||||
rxtn->cmgrtab_inited = 1;
|
||||
}
|
||||
|
||||
@ -2358,7 +2350,7 @@ done:
|
||||
return ret;
|
||||
}
|
||||
|
||||
qse_cmgr_t* qse_awk_rtx_getcmgrstd (qse_awk_rtx_t* rtx, const qse_char_t* ioname)
|
||||
qse_cmgr_t* qse_awk_rtx_getiocmgrstd (qse_awk_rtx_t* rtx, const qse_char_t* ioname)
|
||||
{
|
||||
#if defined(QSE_CHAR_IS_WCHAR)
|
||||
rxtn_t* rxtn;
|
||||
|
@ -812,6 +812,10 @@ void qse_awk_rtx_freeval (qse_awk_rtx_t* rtx, qse_awk_val_t* val, int cache)
|
||||
break;
|
||||
}
|
||||
|
||||
case QSE_AWK_VAL_BYTEARR:
|
||||
QSE_AWK_FREE (rtx->awk, val);
|
||||
break;
|
||||
|
||||
case QSE_AWK_VAL_REX:
|
||||
{
|
||||
/* don't free ptr as it is inlined to val
|
||||
@ -1077,22 +1081,31 @@ static int mbs_to_str (qse_awk_rtx_t* rtx, const qse_mchar_t* str, qse_size_t st
|
||||
* behave like CPLCPY. fall thru */
|
||||
case QSE_AWK_RTX_VALTOSTR_CPLCPY:
|
||||
{
|
||||
if (str_len >= out->u.cplcpy.len)
|
||||
qse_size_t mbslen, wcslen;
|
||||
|
||||
mbslen = str_len;
|
||||
wcslen = out->u.cplcpy.len;
|
||||
if (qse_mbsntowcsnwithcmgr(str, &mbslen, out->u.cplcpy.ptr, &wcslen, rtx->cmgr) <= -1)
|
||||
{
|
||||
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
|
||||
out->u.cplcpy.len = str_len + 1; /* set the required length */
|
||||
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); /* TODO: change error code */
|
||||
return -1;
|
||||
}
|
||||
|
||||
out->u.cplcpy.len = qse_strncpy(out->u.cplcpy.ptr, str, str_len);
|
||||
if (mbslen < str_len)
|
||||
{
|
||||
/* TODO: ... */
|
||||
}
|
||||
out->u.cplcpy.len = wcslen;
|
||||
return 0;
|
||||
}
|
||||
|
||||
case QSE_AWK_RTX_VALTOSTR_CPLDUP:
|
||||
{
|
||||
qse_char_t* tmp;
|
||||
qse_size_t mbslen, wcslen;
|
||||
|
||||
tmp = QSE_AWK_STRXDUP(rtx->awk, str, str_len);
|
||||
mbslen = str_len;
|
||||
tmp = qse_mbsntowcsdupwithcmgr(str, &mbslen, &wcslen, rtx->awk->mmgr, rtx->cmgr);
|
||||
if (!tmp)
|
||||
{
|
||||
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
||||
@ -1100,7 +1113,7 @@ static int mbs_to_str (qse_awk_rtx_t* rtx, const qse_mchar_t* str, qse_size_t st
|
||||
}
|
||||
|
||||
out->u.cpldup.ptr = tmp;
|
||||
out->u.cpldup.len = str_len;
|
||||
out->u.cpldup.len = wcslen;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1206,7 +1219,7 @@ static int val_int_to_str (qse_awk_rtx_t* rtx, const qse_awk_val_int_t* v, qse_a
|
||||
tmp = QSE_STR_PTR(out->u.strp);
|
||||
|
||||
/* extend the buffer */
|
||||
n = qse_str_nccats(out->u.strp, QSE_T(' '), rlen);
|
||||
n = qse_str_nccat(out->u.strp, QSE_T(' '), rlen);
|
||||
if (n == (qse_size_t)-1)
|
||||
{
|
||||
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
||||
@ -1886,7 +1899,7 @@ int qse_awk_rtx_setrefval (qse_awk_rtx_t* rtx, qse_awk_val_ref_t* ref, qse_awk_v
|
||||
return x;
|
||||
}
|
||||
#endif
|
||||
/* fall thru */
|
||||
/* fall thru otherwise */
|
||||
|
||||
default:
|
||||
{
|
||||
|
@ -272,20 +272,17 @@ static qse_wchar_t* mbsn_to_wcs_dup_with_cmgr (
|
||||
return wcs;
|
||||
}
|
||||
|
||||
qse_wchar_t* qse_mbsntowcsdupwithcmgr (
|
||||
const qse_mchar_t* mbs, qse_size_t* mbslen, qse_size_t* wcslen, qse_mmgr_t* mmgr, qse_cmgr_t* cmgr)
|
||||
qse_wchar_t* qse_mbsntowcsdupwithcmgr (const qse_mchar_t* mbs, qse_size_t* mbslen, qse_size_t* wcslen, qse_mmgr_t* mmgr, qse_cmgr_t* cmgr)
|
||||
{
|
||||
return mbsn_to_wcs_dup_with_cmgr (mbs, mbslen, wcslen, mmgr, cmgr, 0);
|
||||
}
|
||||
|
||||
qse_wchar_t* qse_mbsntowcsalldupwithcmgr (
|
||||
const qse_mchar_t* mbs, qse_size_t* mbslen, qse_size_t* wcslen, qse_mmgr_t* mmgr, qse_cmgr_t* cmgr)
|
||||
qse_wchar_t* qse_mbsntowcsalldupwithcmgr (const qse_mchar_t* mbs, qse_size_t* mbslen, qse_size_t* wcslen, qse_mmgr_t* mmgr, qse_cmgr_t* cmgr)
|
||||
{
|
||||
return mbsn_to_wcs_dup_with_cmgr (mbs, mbslen, wcslen, mmgr, cmgr, 1);
|
||||
}
|
||||
|
||||
static qse_wchar_t* mbs_to_wcs_dup_with_cmgr (
|
||||
const qse_mchar_t* mbs, qse_size_t* wcslen, qse_mmgr_t* mmgr, qse_cmgr_t* cmgr, int all)
|
||||
static qse_wchar_t* mbs_to_wcs_dup_with_cmgr (const qse_mchar_t* mbs, qse_size_t* wcslen, qse_mmgr_t* mmgr, qse_cmgr_t* cmgr, int all)
|
||||
{
|
||||
qse_size_t ml, wl;
|
||||
qse_wchar_t* wcs;
|
||||
|
Loading…
Reference in New Issue
Block a user