enhanced qse_awk_rtx_valtostr()

This commit is contained in:
hyung-hwan 2011-05-25 09:14:58 +00:00
parent 8497ddae25
commit 26427dd4c3
13 changed files with 246 additions and 161 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: Awk.hpp 477 2011-05-24 04:22:40Z hyunghwan.chung $
* $Id: Awk.hpp 479 2011-05-24 15:14:58Z hyunghwan.chung $
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
@ -454,6 +454,16 @@ public:
this->set (x);
return *this;
}
const char_t* pointer () const
{
return this->ptr;
}
size_t length () const
{
return this->len;
}
};
///

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.h 477 2011-05-24 04:22:40Z hyunghwan.chung $
* $Id: awk.h 479 2011-05-24 15:14:58Z hyunghwan.chung $
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
@ -1059,6 +1059,7 @@ enum qse_awk_val_type_t
* following values:
*
* - #QSE_AWK_RTX_VALTOSTR_CPL
* - #QSE_AWK_RTX_VALTOSTR_CPLCP
* - #QSE_AWK_RTX_VALTOSTR_CPLDUP
* - #QSE_AWK_RTX_VALTOSTR_STRP
* - #QSE_AWK_RTX_VALTOSTR_STRPCAT
@ -1069,12 +1070,14 @@ enum qse_awk_rtx_valtostr_type_t
{
/** use u.cpl of #qse_awk_rtx_valtostr_out_t */
QSE_AWK_RTX_VALTOSTR_CPL = 0x00,
/** use u.cplcp of #qse_awk_rtx_valtostr_out_t */
QSE_AWK_RTX_VALTOSTR_CPLCP = 0x01,
/** use u.cpldup of #qse_awk_rtx_valtostr_out_t */
QSE_AWK_RTX_VALTOSTR_CPLDUP = 0x01,
QSE_AWK_RTX_VALTOSTR_CPLDUP = 0x02,
/** use u.strp of #qse_awk_rtx_valtostr_out_t */
QSE_AWK_RTX_VALTOSTR_STRP = 0x02,
QSE_AWK_RTX_VALTOSTR_STRP = 0x03,
/** use u.strpcat of #qse_awk_rtx_valtostr_out_t */
QSE_AWK_RTX_VALTOSTR_STRPCAT = 0x03,
QSE_AWK_RTX_VALTOSTR_STRPCAT = 0x04,
/** convert for print */
QSE_AWK_RTX_VALTOSTR_PRINT = 0x10
};
@ -1090,6 +1093,7 @@ struct qse_awk_rtx_valtostr_out_t
union
{
qse_xstr_t cpl;
qse_xstr_t cplcp;
qse_xstr_t cpldup; /* need to free cpldup.ptr */
qse_str_t* strp;
qse_str_t* strpcat;
@ -2063,8 +2067,8 @@ void qse_awk_rtx_refdownval_nofree (
* value.
*/
qse_bool_t qse_awk_rtx_valtobool (
qse_awk_rtx_t* rtx, /**< runtime context */
qse_awk_val_t* val /**< value pointer */
qse_awk_rtx_t* rtx, /**< runtime context */
const qse_awk_val_t* val /**< value pointer */
);
/**
@ -2075,6 +2079,7 @@ qse_bool_t qse_awk_rtx_valtobool (
* The type field is one of the following qse_awk_rtx_valtostr_type_t values:
*
* - #QSE_AWK_RTX_VALTOSTR_CPL
* - #QSE_AWK_RTX_VALTOSTR_CPLCP
* - #QSE_AWK_RTX_VALTOSTR_CPLDUP
* - #QSE_AWK_RTX_VALTOSTR_STRP
* - #QSE_AWK_RTX_VALTOSTR_STRPCAT
@ -2086,7 +2091,22 @@ qse_bool_t qse_awk_rtx_valtobool (
* You should initialize or free other fields before and after the call
* depending on the type field as shown below:
*
* If you have a static buffer, use #QSE_AWK_RTX_VALTOSTR_CPL.
* If you have a static buffer, use #QSE_AWK_RTX_VALTOSTR_CPLCP.
* the resulting string is copied to the buffer.
* @code
* qse_awk_rtx_valtostr_out_t out;
* qse_char_t buf[100];
* out.type = QSE_AWK_RTX_VALTOSTR_CPLCP;
* out.u.cplcp.ptr = buf;
* out.u.cplcp.len = QSE_COUNTOF(buf);
* if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops;
* qse_printf (QSE_T("%.*s\n"), (int)out.u.cplcp.len, out.u.cplcp.ptr);
* @endcode
*
* #QSE_AWK_RTX_VALTOSTR_CPL is different from #QSE_AWK_RTX_VALTOSTR_CPLCP
* in that it doesn't copy the string to the buffer if the type of the value
* is #QSE_AWK_VAL_STR. It copies the resulting string to the buffer if
* the value type is not #QSE_AWK_VAL_STR.
* @code
* qse_awk_rtx_valtostr_out_t out;
* qse_char_t buf[100];
@ -2136,7 +2156,7 @@ qse_bool_t qse_awk_rtx_valtobool (
*/
qse_char_t* qse_awk_rtx_valtostr (
qse_awk_rtx_t* rtx, /**< runtime context */
qse_awk_val_t* val, /**< value to convert */
const qse_awk_val_t* val, /**< value to convert */
qse_awk_rtx_valtostr_out_t* out /**< output buffer */
);
@ -2158,9 +2178,9 @@ qse_char_t* qse_awk_rtx_valtostr (
* #QSE_NULL on failure
*/
qse_char_t* qse_awk_rtx_valtocpldup (
qse_awk_rtx_t* rtx, /**< runtime context */
qse_awk_val_t* val, /**< value to convert */
qse_size_t* len /**< result length */
qse_awk_rtx_t* rtx, /**< runtime context */
const qse_awk_val_t* val, /**< value to convert */
qse_size_t* len /**< result length */
);
/**
@ -2187,10 +2207,10 @@ qse_char_t* qse_awk_rtx_valtocpldup (
* a floating-point number.
*/
int qse_awk_rtx_valtonum (
qse_awk_rtx_t* rtx,
qse_awk_val_t* val,
qse_long_t* l,
qse_real_t* r
qse_awk_rtx_t* rtx,
const qse_awk_val_t* val,
qse_long_t* l,
qse_real_t* r
);
/**

View File

@ -1,5 +1,5 @@
/*
* $Id: lda.h 474 2011-05-23 16:52:37Z hyunghwan.chung $
* $Id: lda.h 479 2011-05-24 15:14:58Z hyunghwan.chung $
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
@ -48,17 +48,18 @@ typedef enum qse_lda_walk_t qse_lda_walk_t;
#define QSE_LDA_SIZE(lda) (*(const qse_size_t*)&(lda)->size)
#define QSE_LDA_CAPA(lda) (*(const qse_size_t*)&(lda)->capa)
#define QSE_LDA_DATA(lda,index) ((const qse_xptl_t*)&(lda)->node[index]->val)
#define QSE_LDA_DPTR(lda,index) ((lda)->node[index]->val.ptr)
#define QSE_LDA_DLEN(lda,index) ((lda)->node[index]->val.len)
#define QSE_LDA_SLOT(lda,index) ((lda)->slot[index])
#define QSE_LDA_DPTL(lda,index) ((const qse_xptl_t*)&(lda)->slot[index]->val)
#define QSE_LDA_DPTR(lda,index) ((lda)->slot[index]->val.ptr)
#define QSE_LDA_DLEN(lda,index) ((lda)->slot[index]->val.len)
/**
* The qse_lda_copier_t type defines a callback function for node construction.
* A node is contructed when a user adds data to a list. The user can
* The qse_lda_copier_t type defines a callback function for slot construction.
* A slot is contructed when a user adds data to a list. The user can
* define how the data to add can be maintained in the list. A singly
* linked list not specified with any copiers stores the data pointer and
* the data length into a node. A special copier QSE_LDA_COPIER_INLINE copies
* the contents of the data a user provided into the node. You can use the
* the data length into a slot. A special copier QSE_LDA_COPIER_INLINE copies
* the contents of the data a user provided into the slot. You can use the
* qse_lda_setcopier() function to change the copier.
*
* A copier should return the pointer to the copied data. If it fails to copy
@ -72,7 +73,7 @@ typedef void* (*qse_lda_copier_t) (
);
/**
* The qse_lda_freeer_t type defines a node destruction callback.
* The qse_lda_freeer_t type defines a slot destruction callback.
*/
typedef void (*qse_lda_freeer_t) (
qse_lda_t* lda /**< array */,
@ -120,7 +121,7 @@ typedef qse_size_t (*qse_lda_sizer_t) (
typedef qse_lda_walk_t (*qse_lda_walker_t) (
qse_lda_t* lda /* array */,
qse_size_t index /* index to the visited node */,
qse_size_t index /* index to the visited slot */,
void* ctx /* user-defined context */
);
@ -139,11 +140,11 @@ struct qse_lda_t
qse_byte_t scale; /* scale factor */
qse_size_t size; /* number of items */
qse_size_t capa; /* capacity */
qse_lda_slot_t** node;
qse_lda_slot_t** slot;
};
/**
* The qse_lda_slot_t type defines a linear dynamic array node
* The qse_lda_slot_t type defines a linear dynamic array slot
*/
struct qse_lda_slot_t
{
@ -213,8 +214,8 @@ qse_lda_copier_t qse_lda_getcopier (
/**
* The qse_lda_setcopier() specifies how to clone an element. The special
* copier #QSE_LDA_COPIER_INLINE copies the data inline to the internal node.
* No freeer is invoked when the node is freeed. You may set the copier to
* copier #QSE_LDA_COPIER_INLINE copies the data inline to the internal slot.
* No freeer is invoked when the slot is freeed. You may set the copier to
* #QSE_LDA_COPIER_SIMPLE to perform no special operation when the data
* pointer is stored.
*/
@ -232,7 +233,7 @@ qse_lda_freeer_t qse_lda_getfreeer (
/**
* The qse_lda_setfreeer() function specifies how to destroy an element.
* The @a freeer is called when a node containing the element is destroyed.
* The @a freeer is called when a slot containing the element is destroyed.
*/
void qse_lda_setfreeer (
qse_lda_t* lda /**< lda */,
@ -330,7 +331,7 @@ qse_size_t qse_lda_delete (
);
/**
* The qse_lda_uplete() function deletes data node without compaction.
* The qse_lda_uplete() function deletes data slot without compaction.
* It returns the number of data affected.
*/
qse_size_t qse_lda_uplete (

View File

@ -1,5 +1,5 @@
/*
* $Id: run.c 477 2011-05-24 04:22:40Z hyunghwan.chung $
* $Id: run.c 479 2011-05-24 15:14:58Z hyunghwan.chung $
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
@ -2536,9 +2536,9 @@ static int delete_indexed (
qse_awk_rtx_valtostr_out_t out;
/* try with a fixed-size buffer */
out.type = QSE_AWK_RTX_VALTOSTR_CPL;
out.u.cpl.ptr = buf;
out.u.cpl.len = QSE_COUNTOF(buf);
out.type = QSE_AWK_RTX_VALTOSTR_CPLCP;
out.u.cplcp.ptr = buf;
out.u.cplcp.len = QSE_COUNTOF(buf);
key = qse_awk_rtx_valtostr (rtx, idx, &out);
if (key == QSE_NULL)
{
@ -2556,8 +2556,8 @@ static int delete_indexed (
return -1;
}
keylen = (out.type == QSE_AWK_RTX_VALTOSTR_CPL)?
out.u.cpl.len: out.u.cpldup.len;
keylen = (out.type == QSE_AWK_RTX_VALTOSTR_CPLCP)?
out.u.cplcp.len: out.u.cpldup.len;
qse_htb_delete (map, key, keylen);
@ -6601,14 +6601,14 @@ static qse_char_t* idxnde_to_str (
if (buf != QSE_NULL)
{
/* try with a fixed-size buffer if given */
out.type = QSE_AWK_RTX_VALTOSTR_CPL;
out.u.cpl.ptr = buf;
out.u.cpl.len = *len;
out.type = QSE_AWK_RTX_VALTOSTR_CPLCP;
out.u.cplcp.ptr = buf;
out.u.cplcp.len = *len;
if (qse_awk_rtx_valtostr (run, idx, &out) != QSE_NULL)
{
str = out.u.cpl.ptr;
*len = out.u.cpl.len;
str = out.u.cplcp.ptr;
*len = out.u.cplcp.len;
QSE_ASSERT (str == buf);
}
}

View File

@ -1,5 +1,5 @@
/*
* $Id: val.c 462 2011-05-18 14:36:40Z hyunghwan.chung $
* $Id: val.c 479 2011-05-24 15:14:58Z hyunghwan.chung $
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
@ -725,7 +725,7 @@ void qse_awk_rtx_freevalchunk (qse_awk_rtx_t* rtx, qse_awk_val_chunk_t* chunk)
}
}
qse_bool_t qse_awk_rtx_valtobool (qse_awk_rtx_t* run, qse_awk_val_t* val)
qse_bool_t qse_awk_rtx_valtobool (qse_awk_rtx_t* run, const qse_awk_val_t* val)
{
if (val == QSE_NULL) return QSE_FALSE;
@ -763,16 +763,23 @@ static qse_char_t* str_to_str (
{
case QSE_AWK_RTX_VALTOSTR_CPL:
{
if (str_len >= out->u.cpl.len)
out->u.cpl.len = str_len;
out->u.cpl.ptr = str;
return out->u.cpl.ptr;
}
case QSE_AWK_RTX_VALTOSTR_CPLCP:
{
if (str_len >= out->u.cplcp.len)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
out->u.cpl.len = str_len + 1;
out->u.cplcp.len = str_len + 1;
return QSE_NULL;
}
out->u.cpl.len =
qse_strncpy (out->u.cpl.ptr, str, str_len);
return out->u.cpl.ptr;
out->u.cplcp.len =
qse_strncpy (out->u.cplcp.ptr, str, str_len);
return out->u.cplcp.ptr;
}
case QSE_AWK_RTX_VALTOSTR_CPLDUP:
@ -826,7 +833,7 @@ static qse_char_t* str_to_str (
}
static qse_char_t* val_int_to_str (
qse_awk_rtx_t* rtx, qse_awk_val_int_t* v,
qse_awk_rtx_t* rtx, const qse_awk_val_int_t* v,
qse_awk_rtx_valtostr_out_t* out)
{
qse_char_t* tmp;
@ -841,18 +848,25 @@ static qse_char_t* val_int_to_str (
switch (type)
{
case QSE_AWK_RTX_VALTOSTR_CPL:
if (out->u.cpl.len <= 1)
/* CPL and CPLCP behave the same for int_t.
* i just fall through assuming that cplcp
* and cpl are the same type. the following
* assertion at least ensure that they have
* the same size. */
QSE_ASSERT (QSE_SIZEOF(out->u.cpl) == QSE_SIZEOF(out->u.cplcp));
case QSE_AWK_RTX_VALTOSTR_CPLCP:
if (out->u.cplcp.len <= 1)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
/* store the buffer size needed */
out->u.cpl.len = 2;
out->u.cplcp.len = 2;
return QSE_NULL;
}
out->u.cpl.len = 1; /* actual length */
out->u.cpl.ptr[0] = QSE_T('0');
out->u.cpl.ptr[1] = QSE_T('\0');
return out->u.cpl.ptr;
out->u.cplcp.len = 1; /* actual length */
out->u.cplcp.ptr[0] = QSE_T('0');
out->u.cplcp.ptr[1] = QSE_T('\0');
return out->u.cplcp.ptr;
case QSE_AWK_RTX_VALTOSTR_CPLDUP:
tmp = QSE_AWK_ALLOC (
@ -899,17 +913,25 @@ static qse_char_t* val_int_to_str (
switch (type)
{
case QSE_AWK_RTX_VALTOSTR_CPL:
if (rlen >= out->u.cpl.len)
/* CPL and CPLCP behave the same for int_t.
* i just fall through assuming that cplcp
* and cpl are the same type. the following
* assertion at least ensure that they have
* the same size. */
QSE_ASSERT (QSE_SIZEOF(out->u.cpl) == QSE_SIZEOF(out->u.cplcp));
case QSE_AWK_RTX_VALTOSTR_CPLCP:
if (rlen >= out->u.cplcp.len)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
/* store the buffer size needed */
out->u.cpl.len = rlen + 1;
out->u.cplcp.len = rlen + 1;
return QSE_NULL;
}
tmp = out->u.cpl.ptr;
tmp = out->u.cplcp.ptr;
tmp[rlen] = QSE_T('\0');
out->u.cpl.len = rlen;
out->u.cplcp.len = rlen;
break;
case QSE_AWK_RTX_VALTOSTR_CPLDUP:
@ -988,7 +1010,7 @@ static qse_char_t* val_int_to_str (
}
static qse_char_t* val_real_to_str (
qse_awk_rtx_t* rtx, qse_awk_val_real_t* v,
qse_awk_rtx_t* rtx, const qse_awk_val_real_t* v,
qse_awk_rtx_valtostr_out_t* out)
{
qse_char_t* tmp;
@ -1032,20 +1054,27 @@ static qse_char_t* val_real_to_str (
switch (type)
{
case QSE_AWK_RTX_VALTOSTR_CPL:
/* CPL and CPLCP behave the same for real_t.
* i just fall through assuming that cplcp
* and cpl are the same type. the following
* assertion at least ensure that they have
* the same size. */
QSE_ASSERT (QSE_SIZEOF(out->u.cpl) == QSE_SIZEOF(out->u.cplcp));
if (out->u.cpl.len <= tmp_len)
case QSE_AWK_RTX_VALTOSTR_CPLCP:
if (out->u.cplcp.len <= tmp_len)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
/* store the buffer size required */
out->u.cpl.len = tmp_len + 1;
out->u.cplcp.len = tmp_len + 1;
qse_str_close (&fbu);
qse_str_close (&buf);
return QSE_NULL;
}
qse_strncpy (out->u.cpl.ptr, tmp, tmp_len);
out->u.cpl.len = tmp_len;
tmp = out->u.cpl.ptr;
qse_strncpy (out->u.cplcp.ptr, tmp, tmp_len);
out->u.cplcp.len = tmp_len;
tmp = out->u.cplcp.ptr;
qse_str_fini (&fbu);
qse_str_fini (&buf);
@ -1111,20 +1140,27 @@ static qse_char_t* val_real_to_str (
}
qse_char_t* qse_awk_rtx_valtostr (
qse_awk_rtx_t* rtx, qse_awk_val_t* v, qse_awk_rtx_valtostr_out_t* out)
qse_awk_rtx_t* rtx, const qse_awk_val_t* v,
qse_awk_rtx_valtostr_out_t* out)
{
switch (v->type)
{
case QSE_AWK_VAL_NIL:
{
return str_to_str (rtx, QSE_T(""), 0, out);
}
case QSE_AWK_VAL_INT:
{
return val_int_to_str (
rtx, (qse_awk_val_int_t*)v, out);
}
case QSE_AWK_VAL_REAL:
{
return val_real_to_str (
rtx, (qse_awk_val_real_t*)v, out);
}
case QSE_AWK_VAL_STR:
{
@ -1144,7 +1180,7 @@ qse_char_t* qse_awk_rtx_valtostr (
}
qse_char_t* qse_awk_rtx_valtocpldup (
qse_awk_rtx_t* rtx, qse_awk_val_t* v, qse_size_t* len)
qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
{
qse_awk_rtx_valtostr_out_t out;
@ -1156,7 +1192,7 @@ qse_char_t* qse_awk_rtx_valtocpldup (
}
int qse_awk_rtx_valtonum (
qse_awk_rtx_t* rtx, qse_awk_val_t* v, qse_long_t* l, qse_real_t* r)
qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_long_t* l, qse_real_t* r)
{
if (v->type == QSE_AWK_VAL_NIL)
{

View File

@ -1,5 +1,5 @@
/*
* $Id: lda.c 474 2011-05-23 16:52:37Z hyunghwan.chung $
* $Id: lda.c 479 2011-05-24 15:14:58Z hyunghwan.chung $
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
@ -36,8 +36,8 @@ QSE_IMPLEMENT_COMMON_FUNCTIONS (lda)
#define size_t qse_size_t
#define TOB(lda,len) ((len)*(lda)->scale)
#define DPTR(node) ((node)->val.ptr)
#define DLEN(node) ((node)->val.len)
#define DPTR(slot) ((slot)->val.ptr)
#define DLEN(slot) ((slot)->val.len)
static int default_comparator (lda_t* lda,
const void* dptr1, size_t dlen1,
@ -58,7 +58,7 @@ static int default_comparator (lda_t* lda,
return n;
}
static QSE_INLINE slot_t* alloc_node (lda_t* lda, void* dptr, size_t dlen)
static QSE_INLINE slot_t* alloc_slot (lda_t* lda, void* dptr, size_t dlen)
{
slot_t* n;
@ -135,7 +135,7 @@ lda_t* qse_lda_init (lda_t* lda, mmgr_t* mmgr, size_t capa)
lda->mmgr = mmgr;
lda->size = 0;
lda->capa = 0;
lda->node = QSE_NULL;
lda->slot = QSE_NULL;
lda->copier = QSE_LDA_COPIER_SIMPLE;
lda->comper = default_comparator;
@ -148,10 +148,10 @@ void qse_lda_fini (lda_t* lda)
{
qse_lda_clear (lda);
if (lda->node != QSE_NULL)
if (lda->slot != QSE_NULL)
{
QSE_MMGR_FREE (lda->mmgr, lda->node);
lda->node = QSE_NULL;
QSE_MMGR_FREE (lda->mmgr, lda->slot);
lda->slot = QSE_NULL;
lda->capa = 0;
}
}
@ -250,41 +250,41 @@ lda_t* qse_lda_setcapa (lda_t* lda, size_t capa)
if (capa > 0)
{
if (lda->mmgr->realloc != QSE_NULL && lda->node != QSE_NULL)
if (lda->mmgr->realloc != QSE_NULL && lda->slot != QSE_NULL)
{
tmp = (slot_t**) QSE_MMGR_REALLOC (
lda->mmgr, lda->node,
QSE_SIZEOF(*lda->node)*capa);
lda->mmgr, lda->slot,
QSE_SIZEOF(*lda->slot)*capa);
if (tmp == QSE_NULL) return QSE_NULL;
}
else
{
tmp = (slot_t**) QSE_MMGR_ALLOC (
lda->mmgr, QSE_SIZEOF(*lda->node)*capa);
lda->mmgr, QSE_SIZEOF(*lda->slot)*capa);
if (tmp == QSE_NULL) return QSE_NULL;
if (lda->node != QSE_NULL)
if (lda->slot != QSE_NULL)
{
size_t x;
x = (capa > lda->capa)? lda->capa: capa;
QSE_MEMCPY (tmp, lda->node,
QSE_SIZEOF(*lda->node)*x);
QSE_MMGR_FREE (lda->mmgr, lda->node);
QSE_MEMCPY (tmp, lda->slot,
QSE_SIZEOF(*lda->slot)*x);
QSE_MMGR_FREE (lda->mmgr, lda->slot);
}
}
}
else
{
if (lda->node != QSE_NULL)
if (lda->slot != QSE_NULL)
{
qse_lda_clear (lda);
QSE_MMGR_FREE (lda->mmgr, lda->node);
QSE_MMGR_FREE (lda->mmgr, lda->slot);
}
tmp = QSE_NULL;
}
lda->node = tmp;
lda->slot = tmp;
lda->capa = capa;
return lda;
@ -296,10 +296,10 @@ size_t qse_lda_search (lda_t* lda, size_t pos, const void* dptr, size_t dlen)
for (i = pos; i < lda->size; i++)
{
if (lda->node[i] == QSE_NULL) continue;
if (lda->slot[i] == QSE_NULL) continue;
if (lda->comper (lda,
DPTR(lda->node[i]), DLEN(lda->node[i]),
DPTR(lda->slot[i]), DLEN(lda->slot[i]),
dptr, dlen) == 0) return i;
}
@ -316,10 +316,10 @@ size_t qse_lda_rsearch (lda_t* lda, size_t pos, const void* dptr, size_t dlen)
for (i = pos + 1; i-- > 0; )
{
if (lda->node[i] == QSE_NULL) continue;
if (lda->slot[i] == QSE_NULL) continue;
if (lda->comper (lda,
DPTR(lda->node[i]), DLEN(lda->node[i]),
DPTR(lda->slot[i]), DLEN(lda->slot[i]),
dptr, dlen) == 0) return i;
}
}
@ -336,14 +336,14 @@ size_t qse_lda_upsert (lda_t* lda, size_t pos, void* dptr, size_t dlen)
size_t qse_lda_insert (lda_t* lda, size_t pos, void* dptr, size_t dlen)
{
size_t i;
slot_t* node;
slot_t* slot;
/* allocate the node first */
node = alloc_node (lda, dptr, dlen);
if (node == QSE_NULL) return QSE_LDA_NIL;
/* allocate the slot first */
slot = alloc_slot (lda, dptr, dlen);
if (slot == QSE_NULL) return QSE_LDA_NIL;
/* do resizeing if necessary.
* resizing is performed after node allocation because that way, it
* resizing is performed after slot allocation because that way, it
* doesn't modify lda on any errors */
if (pos >= lda->capa || lda->size >= lda->capa)
{
@ -377,8 +377,8 @@ size_t qse_lda_insert (lda_t* lda, size_t pos, void* dptr, size_t dlen)
if (capa <= mincapa)
{
if (lda->freeer)
lda->freeer (lda, DPTR(node), DLEN(node));
QSE_MMGR_FREE (lda->mmgr, node);
lda->freeer (lda, DPTR(slot), DLEN(slot));
QSE_MMGR_FREE (lda->mmgr, slot);
return QSE_LDA_NIL;
}
@ -391,19 +391,19 @@ size_t qse_lda_insert (lda_t* lda, size_t pos, void* dptr, size_t dlen)
{
/* the buffer is not still enough after resizing */
if (lda->freeer)
lda->freeer (lda, DPTR(node), DLEN(node));
QSE_MMGR_FREE (lda->mmgr, node);
lda->freeer (lda, DPTR(slot), DLEN(slot));
QSE_MMGR_FREE (lda->mmgr, slot);
return QSE_LDA_NIL;
}
/* fill in the gap with QSE_NULL */
for (i = lda->size; i < pos; i++) lda->node[i] = QSE_NULL;
for (i = lda->size; i < pos; i++) lda->slot[i] = QSE_NULL;
/* shift values to the next cell */
for (i = lda->size; i > pos; i--) lda->node[i] = lda->node[i-1];
for (i = lda->size; i > pos; i--) lda->slot[i] = lda->slot[i-1];
/* set the value */
lda->node[pos] = node;
lda->slot[pos] = slot;
if (pos > lda->size) lda->size = pos + 1;
else lda->size++;
@ -417,12 +417,12 @@ size_t qse_lda_update (lda_t* lda, size_t pos, void* dptr, size_t dlen)
if (pos >= lda->size) return QSE_LDA_NIL;
c = lda->node[pos];
c = lda->slot[pos];
if (c == QSE_NULL)
{
/* no previous data */
lda->node[pos] = alloc_node (lda, dptr, dlen);
if (lda->node[pos] == QSE_NULL) return QSE_LDA_NIL;
lda->slot[pos] = alloc_slot (lda, dptr, dlen);
if (lda->slot[pos] == QSE_NULL) return QSE_LDA_NIL;
}
else
{
@ -434,14 +434,14 @@ size_t qse_lda_update (lda_t* lda, size_t pos, void* dptr, size_t dlen)
else
{
/* updated to different data */
slot_t* node = alloc_node (lda, dptr, dlen);
if (node == QSE_NULL) return QSE_LDA_NIL;
slot_t* slot = alloc_slot (lda, dptr, dlen);
if (slot == QSE_NULL) return QSE_LDA_NIL;
if (lda->freeer != QSE_NULL)
lda->freeer (lda, DPTR(c), DLEN(c));
QSE_MMGR_FREE (lda->mmgr, c);
lda->node[pos] = node;
lda->slot[pos] = slot;
}
}
@ -459,7 +459,7 @@ size_t qse_lda_delete (lda_t* lda, size_t index, size_t count)
for (i = index; i < index + count; i++)
{
slot_t* c = lda->node[i];
slot_t* c = lda->slot[i];
if (c != QSE_NULL)
{
@ -467,15 +467,15 @@ size_t qse_lda_delete (lda_t* lda, size_t index, size_t count)
lda->freeer (lda, DPTR(c), DLEN(c));
QSE_MMGR_FREE (lda->mmgr, c);
lda->node[i] = QSE_NULL;
lda->slot[i] = QSE_NULL;
}
}
for (i = index + count; i < lda->size; i++)
{
lda->node[i-count] = lda->node[i];
lda->slot[i-count] = lda->slot[i];
}
lda->node[lda->size-1] = QSE_NULL;
lda->slot[lda->size-1] = QSE_NULL;
lda->size -= count;
return count;
@ -492,7 +492,7 @@ size_t qse_lda_uplete (lda_t* lda, size_t index, size_t count)
for (i = index; i < index + count; i++)
{
slot_t* c = lda->node[i];
slot_t* c = lda->slot[i];
if (c != QSE_NULL)
{
@ -500,7 +500,7 @@ size_t qse_lda_uplete (lda_t* lda, size_t index, size_t count)
lda->freeer (lda, DPTR(c), DLEN(c));
QSE_MMGR_FREE (lda->mmgr, c);
lda->node[i] = QSE_NULL;
lda->slot[i] = QSE_NULL;
}
}
@ -513,13 +513,13 @@ void qse_lda_clear (lda_t* lda)
for (i = 0; i < lda->size; i++)
{
slot_t* c = lda->node[i];
slot_t* c = lda->slot[i];
if (c != QSE_NULL)
{
if (lda->freeer)
lda->freeer (lda, DPTR(c), DLEN(c));
QSE_MMGR_FREE (lda->mmgr, c);
lda->node[i] = QSE_NULL;
lda->slot[i] = QSE_NULL;
}
}
@ -535,7 +535,7 @@ size_t qse_lda_walk (lda_t* lda, walker_t walker, void* ctx)
while (1)
{
if (lda->node[i] != QSE_NULL)
if (lda->slot[i] != QSE_NULL)
{
w = walker (lda, i, ctx);
nwalks++;
@ -568,7 +568,7 @@ size_t qse_lda_rwalk (lda_t* lda, walker_t walker, void* ctx)
while (1)
{
if (lda->node[i] != QSE_NULL)
if (lda->slot[i] != QSE_NULL)
{
w = walker (lda, i, ctx);
nwalks++;
@ -623,14 +623,14 @@ size_t qse_lda_pushheap (lda_t* lda, void* dptr, size_t dlen)
/* compare with the parent */
par = HEAP_PARENT(cur);
n = lda->comper (lda,
DPTR(lda->node[cur]), DLEN(lda->node[cur]),
DPTR(lda->node[par]), DLEN(lda->node[par]));
DPTR(lda->slot[cur]), DLEN(lda->slot[cur]),
DPTR(lda->slot[par]), DLEN(lda->slot[par]));
if (n <= 0) break; /* ok */
/* swap the current with the parent */
tmp = lda->node[cur];
lda->node[cur] = lda->node[par];
lda->node[par] = tmp;
tmp = lda->slot[cur];
lda->slot[cur] = lda->slot[par];
lda->slot[par] = tmp;
cur = par;
}
@ -646,12 +646,12 @@ void qse_lda_popheap (lda_t* lda)
QSE_ASSERT (lda->size > 0);
/* destroy the top */
tmp = lda->node[0];
tmp = lda->slot[0];
if (lda->freeer) lda->freeer (lda, DPTR(tmp), DLEN(tmp));
QSE_MMGR_FREE (lda->mmgr, tmp);
/* move the last item to the top position also shrink the size */
lda->node[0] = lda->node[--lda->size];
lda->slot[0] = lda->slot[--lda->size];
if (lda->size <= 1) return; /* only 1 element. nothing further to do */
@ -679,20 +679,20 @@ void qse_lda_popheap (lda_t* lda)
{
/* get the larger child of the two */
n = lda->comper (lda,
DPTR(lda->node[left]), DLEN(lda->node[left]),
DPTR(lda->node[right]), DLEN(lda->node[right]));
DPTR(lda->slot[left]), DLEN(lda->slot[left]),
DPTR(lda->slot[right]), DLEN(lda->slot[right]));
child = (n > 0)? left: right;
}
/* compare the current one with the child */
n = lda->comper (lda,
DPTR(lda->node[cur]), DLEN(lda->node[cur]),
DPTR(lda->node[child]), DLEN(lda->node[child]));
DPTR(lda->slot[cur]), DLEN(lda->slot[cur]),
DPTR(lda->slot[child]), DLEN(lda->slot[child]));
if (n > 0) break; /* current one is larger. stop exchange */
/* swap the current with the child */
tmp = lda->node[cur];
lda->node[cur] = lda->node[child];
lda->node[child] = tmp;
tmp = lda->slot[cur];
lda->slot[cur] = lda->slot[child];
lda->slot[child] = tmp;
}
}

View File

@ -1,5 +1,5 @@
/*
* $Id: awk04.c 469 2011-05-21 16:16:18Z hyunghwan.chung $
* $Id: awk04.c 479 2011-05-24 15:14:58Z hyunghwan.chung $
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
@ -38,6 +38,9 @@ int main ()
qse_awk_fun_t* fun;
int ret, i, opt;
qse_awk_rtx_valtostr_out_t out;
qse_char_t numbuf[128];
/* create a main processor */
awk = qse_awk_openstd (0);
if (awk == QSE_NULL)
@ -119,7 +122,12 @@ int main ()
qse_printf (QSE_T("[%.*s]\n"), (int)len, str);
qse_awk_rtx_free (rtx, str);
if (rtv)
{
qse_awk_rtx_refdownval (rtx, rtv);
rtv = QSE_NULL;
}
/* call the function again using different API functions */
fun = qse_awk_rtx_findfun (rtx, QSE_T("pow"));
if (fun == QSE_NULL)
@ -137,22 +145,26 @@ int main ()
ret = -1; goto oops;
}
str = qse_awk_rtx_valtocpldup (rtx, rtv, &len);
if (str == QSE_NULL)
/* Convert a value to a string in a different way */
out.type = QSE_AWK_RTX_VALTOSTR_CPL;
out.u.cpl.ptr = numbuf; /* used if the value is not a string */
out.u.cpl.len = QSE_COUNTOF(numbuf);
if (qse_awk_rtx_valtostr (rtx, rtv, &out) == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
qse_awk_rtx_geterrmsg(rtx));
ret = -1; goto oops;
}
qse_printf (QSE_T("[%.*s]\n"), (int)len, str);
qse_awk_rtx_free (rtx, str);
qse_printf (QSE_T("[%.*s]\n"), (int)out.u.cpl.len, out.u.cpl.ptr);
oops:
/* clear the return value */
if (rtv) qse_awk_rtx_refdownval (rtx, rtv);
if (rtv)
{
qse_awk_rtx_refdownval (rtx, rtv);
rtv = QSE_NULL;
}
/* dereference all arguments */
for (i = 0; i < QSE_COUNTOF(arg); i++)

View File

@ -112,7 +112,9 @@ static int run_awk (QSE::StdAwk& awk)
if (r.getIndexed (idx, &v) <= -1) return -1;
qse_printf (QSE_T("\t[%.*s]=>[%lld]\n"),
(int)idx.len, idx.ptr, (long long)v.toInt());
(int)idx.length(), idx.pointer(),
(long long)v.toInt()
);
iter = r.getNextIndex (&idx, iter);
}

View File

@ -155,7 +155,7 @@ public:
{
Value::IntIndex iidx (i);
if (ret.setIndexedStr (
iidx, idx.ptr, idx.len) <= -1) return -1;
iidx, idx.pointer(), idx.length()) <= -1) return -1;
ii = args[0].getNextIndex (&idx, ii);
}

View File

@ -154,8 +154,8 @@ int main ()
}
qse_printf (QSE_T("ret [%.*s]=[%.*s]\n"),
(int)QSE_AWK_VAL_MAP_ITR_KEY_LEN(iptr),
QSE_AWK_VAL_MAP_ITR_KEY_PTR(iptr),
(int)QSE_AWK_VAL_MAP_ITR_KEY(iptr)->len,
QSE_AWK_VAL_MAP_ITR_KEY(iptr)->ptr,
(int)str.len, str.ptr
);
qse_awk_rtx_free (rtx, str.ptr);

View File

@ -342,13 +342,15 @@ static qse_htb_pair_t* test5_cbserter (
/* allocate a new pair, but without filling the actual value.
* note vptr is given QSE_NULL for that purpose */
new_pair = qse_htb_allocpair (
htb, kptr, klen, QSE_NULL, pair->vlen + 1 + v->len);
htb, kptr, klen, QSE_NULL,
QSE_HTB_VLEN(pair) + 1 + v->len);
if (new_pair == QSE_NULL) return QSE_NULL;
/* fill in the value space */
vptr = new_pair->vptr;
qse_memcpy (vptr, pair->vptr, pair->vlen*QSE_SIZEOF(qse_char_t));
vptr += pair->vlen*QSE_SIZEOF(qse_char_t);
vptr = QSE_HTB_VPTR(new_pair);
qse_memcpy (vptr, QSE_HTB_VPTR(pair),
QSE_HTB_VLEN(pair)*QSE_SIZEOF(qse_char_t));
vptr += QSE_HTB_VLEN(pair) * QSE_SIZEOF(qse_char_t);
qse_memcpy (vptr, &comma, QSE_SIZEOF(qse_char_t));
vptr += QSE_SIZEOF(qse_char_t);
qse_memcpy (vptr, v->ptr, v->len*QSE_SIZEOF(qse_char_t));

View File

@ -119,7 +119,7 @@ qse_lda_setcapa (s1, 3);
static int test2 ()
{
qse_lda_t* s1;
qse_lda_node_t* p;
qse_lda_slot_t* p;
const qse_char_t* x[] =
{
QSE_T("this is so good"),
@ -168,7 +168,7 @@ static int test2 ()
for (i = 0; i < QSE_LDA_SIZE(s1); i++)
{
if (QSE_LDA_NODE(s1,i))
if (QSE_LDA_SLOT(s1,i))
{
qse_printf (QSE_T("[%d] %d => [%.*s]\n"),
j, i, (int)QSE_LDA_DLEN(s1,i), QSE_LDA_DPTR(s1,i));
@ -258,7 +258,7 @@ static int test3 ()
{
if (i < QSE_LDA_SIZE(s1))
{
if (QSE_LDA_NODE(s1,i))
if (QSE_LDA_SLOT(s1,i))
{
qse_printf (QSE_T("deleted at %d => [%.*s]\n"),
i, (int)QSE_LDA_DLEN(s1,i), QSE_LDA_DPTR(s1,i));
@ -287,7 +287,7 @@ static int test3 ()
for (i = 0; i < QSE_LDA_SIZE(s1); i++)
{
if (QSE_LDA_NODE(s1,i))
if (QSE_LDA_SLOT(s1,i))
{
qse_printf (QSE_T("[%d] %d => [%.*s]\n"),
j, i, (int)QSE_LDA_DLEN(s1,i), QSE_LDA_DPTR(s1,i));
@ -303,7 +303,7 @@ static int test3 ()
for (i = 0; i < QSE_LDA_SIZE(s1); i++)
{
if (QSE_LDA_NODE(s1,i))
if (QSE_LDA_SLOT(s1,i))
{
qse_printf (QSE_T("[%d] %d => [%.*s]\n"),
j, i, (int)QSE_LDA_DLEN(s1,i), QSE_LDA_DPTR(s1,i));

View File

@ -150,13 +150,15 @@ static qse_rbt_pair_t* test5_cbserter (
/* allocate a new pair, but without filling the actual value.
* note vptr is given QSE_NULL for that purpose */
new_pair = qse_rbt_allocpair (
rbt, kptr, klen, QSE_NULL, pair->vlen + 1 + v->len);
rbt, kptr, klen, QSE_NULL,
QSE_RBT_VLEN(pair) + 1 + v->len);
if (new_pair == QSE_NULL) return QSE_NULL;
/* fill in the value space */
vptr = new_pair->vptr;
qse_memcpy (vptr, pair->vptr, pair->vlen*QSE_SIZEOF(qse_char_t));
vptr += pair->vlen*QSE_SIZEOF(qse_char_t);
vptr = QSE_RBT_VPTR(new_pair);
qse_memcpy (vptr, QSE_RBT_VPTR(pair),
QSE_RBT_VLEN(pair) * QSE_SIZEOF(qse_char_t));
vptr += QSE_RBT_VLEN(pair) * QSE_SIZEOF(qse_char_t);
qse_memcpy (vptr, &comma, QSE_SIZEOF(qse_char_t));
vptr += QSE_SIZEOF(qse_char_t);
qse_memcpy (vptr, v->ptr, v->len*QSE_SIZEOF(qse_char_t));