added qse_freexliroot() and qse_xli_yeildroot().

changed qse_xli_getpairxtn() to qse_getxlipairxtn()
This commit is contained in:
hyung-hwan 2013-09-16 14:32:50 +00:00
parent c1c1f203ee
commit 3d6f9af82b
6 changed files with 96 additions and 40 deletions

View File

@ -123,7 +123,7 @@ static void print_usage (QSE_FILE* out, int argc, qse_char_t* argv[])
{ {
const qse_char_t* b = qse_basename (argv[0]); const qse_char_t* b = qse_basename (argv[0]);
qse_fprintf (out, QSE_T("USAGE: %s [options] -f input-file [key]\n"), b); qse_fprintf (out, QSE_T("USAGE: %s [options] -i input-file [key]\n"), b);
qse_fprintf (out, QSE_T("options as follows:\n")); qse_fprintf (out, QSE_T("options as follows:\n"));
qse_fprintf (out, QSE_T(" -h/--help show this message\n")); qse_fprintf (out, QSE_T(" -h/--help show this message\n"));
@ -565,6 +565,7 @@ oops:
qse_fprintf (QSE_STDERR, QSE_T("-------------------------------------------------------\n")); qse_fprintf (QSE_STDERR, QSE_T("-------------------------------------------------------\n"));
} }
#endif #endif
return ret; return ret;
} }

View File

@ -501,11 +501,6 @@ QSE_EXPORT void qse_xli_freemem (
void* ptr void* ptr
); );
QSE_EXPORT void* qse_xli_getpairxtn (
qse_xli_t* xli,
qse_xli_pair_t* pair
);
QSE_EXPORT qse_xli_pair_t* qse_xli_insertpair ( QSE_EXPORT qse_xli_pair_t* qse_xli_insertpair (
qse_xli_t* xli, qse_xli_t* xli,
qse_xli_list_t* list, qse_xli_list_t* list,
@ -606,6 +601,9 @@ QSE_EXPORT void qse_xli_clearroot (
qse_xli_t* xli qse_xli_t* xli
); );
QSE_EXPORT qse_xli_list_t* qse_xli_yieldroot (
qse_xli_t* xli
);
QSE_EXPORT void qse_xli_clear ( QSE_EXPORT void qse_xli_clear (
qse_xli_t* xli qse_xli_t* xli
@ -639,6 +637,18 @@ QSE_EXPORT int qse_xli_write (
qse_xli_io_impl_t io qse_xli_io_impl_t io
); );
QSE_EXPORT void* qse_getxlipairxtn (
qse_xli_pair_t* pair
);
/**
* The qse_freexliroot() function frees the root list acquired with qse_xli_yeildroot().
*/
QSE_EXPORT void qse_freexliroot (
qse_xli_list_t* root
);
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif

View File

@ -830,7 +830,7 @@ static int read_pair (qse_xli_t* xli)
xli->tok_status &= ~TOK_STATUS_ENABLE_NSTR; xli->tok_status &= ~TOK_STATUS_ENABLE_NSTR;
/* no value has been specified for the pair */ /* no value has been specified for the pair */
pair = qse_xli_insertpair (xli, parlist, QSE_NULL, key.ptr, name, (qse_xli_val_t*)&xli->xnil); pair = qse_xli_insertpair (xli, parlist, QSE_NULL, key.ptr, name, (qse_xli_val_t*)&xli->root->xnil);
if (pair == QSE_NULL) goto oops; if (pair == QSE_NULL) goto oops;
/* skip the semicolon */ /* skip the semicolon */
@ -937,7 +937,7 @@ static int read_root_list (qse_xli_t* xli)
{ {
qse_xli_list_link_t* link; qse_xli_list_link_t* link;
link = make_list_link (xli, &xli->root); link = make_list_link (xli, &xli->root->list);
if (link == QSE_NULL) return -1; if (link == QSE_NULL) return -1;
if (get_char (xli) <= -1 || get_token (xli) <= -1 || __read_list (xli) <= -1) if (get_char (xli) <= -1 || get_token (xli) <= -1 || __read_list (xli) <= -1)

View File

@ -317,7 +317,7 @@ int qse_xli_write (qse_xli_t* xli, qse_xli_io_impl_t io)
if (open_new_stream (xli, QSE_NULL, 0) <= -1) return -1; if (open_new_stream (xli, QSE_NULL, 0) <= -1) return -1;
/* begin writing the root list */ /* begin writing the root list */
n = write_list (xli, &xli->root, 0); n = write_list (xli, &xli->root->list, 0);
/* close all open streams. there should be only the /* close all open streams. there should be only the
* top-level stream here if there occurred no errors */ * top-level stream here if there occurred no errors */

View File

@ -21,9 +21,9 @@
#include "xli.h" #include "xli.h"
#include <qse/cmn/chr.h> #include <qse/cmn/chr.h>
static void free_val (qse_xli_t* xli, qse_xli_val_t* val); static void free_val (qse_xli_root_list_t* xli, qse_xli_val_t* val);
static void free_list (qse_xli_t* xli, qse_xli_list_t* list); static void free_list (qse_xli_root_list_t* xli, qse_xli_list_t* list);
static void free_atom (qse_xli_t* xli, qse_xli_atom_t* atom); static void free_atom (qse_xli_root_list_t* xli, qse_xli_atom_t* atom);
qse_xli_t* qse_xli_open (qse_mmgr_t* mmgr, qse_size_t xtnsize) qse_xli_t* qse_xli_open (qse_mmgr_t* mmgr, qse_size_t xtnsize)
{ {
@ -70,13 +70,18 @@ int qse_xli_init (qse_xli_t* xli, qse_mmgr_t* mmgr)
if (xli->schema == QSE_NULL) goto oops; if (xli->schema == QSE_NULL) goto oops;
qse_rbt_setstyle (xli->schema, qse_getrbtstyle(QSE_RBT_STYLE_INLINE_COPIERS)); qse_rbt_setstyle (xli->schema, qse_getrbtstyle(QSE_RBT_STYLE_INLINE_COPIERS));
xli->root.type = QSE_XLI_LIST; xli->root = QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(*xli->root));
xli->xnil.type = QSE_XLI_NIL; if (xli->root == QSE_NULL) goto oops;
QSE_MEMSET (xli->root, 0, QSE_SIZEOF(*xli->root));
xli->root->list.type = QSE_XLI_LIST;
xli->root->xnil.type = QSE_XLI_NIL;
xli->root->mmgr = mmgr;
return 0; return 0;
oops: oops:
qse_xli_seterrnum (xli, QSE_XLI_ENOMEM, QSE_NULL); qse_xli_seterrnum (xli, QSE_XLI_ENOMEM, QSE_NULL);
if (xli->root) QSE_MMGR_FREE (mmgr, xli->root);
if (xli->schema) qse_rbt_close (xli->schema); if (xli->schema) qse_rbt_close (xli->schema);
if (xli->tok.name) qse_str_close (xli->tok.name); if (xli->tok.name) qse_str_close (xli->tok.name);
if (xli->dotted_curkey) qse_str_close (xli->dotted_curkey); if (xli->dotted_curkey) qse_str_close (xli->dotted_curkey);
@ -86,6 +91,7 @@ oops:
void qse_xli_fini (qse_xli_t* xli) void qse_xli_fini (qse_xli_t* xli)
{ {
qse_xli_clear (xli); qse_xli_clear (xli);
QSE_MMGR_FREE (xli->mmgr, xli->root);
qse_rbt_close (xli->schema); qse_rbt_close (xli->schema);
qse_str_close (xli->tok.name); qse_str_close (xli->tok.name);
@ -182,7 +188,7 @@ static void insert_atom (
qse_xli_t* xli, qse_xli_list_t* parent, qse_xli_t* xli, qse_xli_list_t* parent,
qse_xli_atom_t* peer, qse_xli_atom_t* atom) qse_xli_atom_t* peer, qse_xli_atom_t* atom)
{ {
if (parent == QSE_NULL) parent = &xli->root; if (parent == QSE_NULL) parent = &xli->root->list;
if (peer == QSE_NULL) if (peer == QSE_NULL)
{ {
@ -244,10 +250,6 @@ static qse_xli_pair_t* insert_pair (
return pair; return pair;
} }
void* qse_xli_getpairxtn (qse_xli_t* xli, qse_xli_pair_t* pair)
{
return (void*)(pair + 1);
}
qse_xli_pair_t* qse_xli_insertpair ( qse_xli_pair_t* qse_xli_insertpair (
qse_xli_t* xli, qse_xli_list_t* parent, qse_xli_atom_t* peer, qse_xli_t* xli, qse_xli_list_t* parent, qse_xli_atom_t* peer,
@ -334,7 +336,7 @@ qse_xli_pair_t* qse_xli_insertpairwithstrs (
str = qse_xli_addsegtostr (xli, str, &value[i]); str = qse_xli_addsegtostr (xli, str, &value[i]);
if (str == QSE_NULL) if (str == QSE_NULL)
{ {
free_atom (xli, (qse_xli_atom_t*)tmp); free_atom (xli->root, (qse_xli_atom_t*)tmp);
return QSE_NULL; return QSE_NULL;
} }
} }
@ -400,12 +402,12 @@ qse_xli_eof_t* qse_xli_inserteof (
/* ------------------------------------------------------ */ /* ------------------------------------------------------ */
static void free_val (qse_xli_t* xli, qse_xli_val_t* val) static void free_val (qse_xli_root_list_t* root, qse_xli_val_t* val)
{ {
if ((qse_xli_nil_t*)val != &xli->xnil) if ((qse_xli_nil_t*)val != &root->xnil)
{ {
if (val->type == QSE_XLI_LIST) if (val->type == QSE_XLI_LIST)
free_list (xli, (qse_xli_list_t*)val); free_list (root, (qse_xli_list_t*)val);
else if (val->type == QSE_XLI_STR) else if (val->type == QSE_XLI_STR)
{ {
qse_xli_str_t* cur, * next; qse_xli_str_t* cur, * next;
@ -414,22 +416,22 @@ static void free_val (qse_xli_t* xli, qse_xli_val_t* val)
while (cur) while (cur)
{ {
next = cur->next; next = cur->next;
QSE_MMGR_FREE (xli->mmgr, cur); QSE_MMGR_FREE (root->mmgr, cur);
cur = next; cur = next;
} }
} }
QSE_MMGR_FREE (xli->mmgr, val); QSE_MMGR_FREE (root->mmgr, val);
} }
} }
static void free_atom (qse_xli_t* xli, qse_xli_atom_t* atom) static void free_atom (qse_xli_root_list_t* root, qse_xli_atom_t* atom)
{ {
if (atom->type == QSE_XLI_PAIR) free_val (xli, ((qse_xli_pair_t*)atom)->val); if (atom->type == QSE_XLI_PAIR) free_val (root, ((qse_xli_pair_t*)atom)->val);
QSE_MMGR_FREE (xli->mmgr, atom); QSE_MMGR_FREE (root->mmgr, atom);
} }
static void free_list (qse_xli_t* xli, qse_xli_list_t* list) static void free_list (qse_xli_root_list_t* root, qse_xli_list_t* list)
{ {
qse_xli_atom_t* p, * n; qse_xli_atom_t* p, * n;
@ -437,17 +439,20 @@ static void free_list (qse_xli_t* xli, qse_xli_list_t* list)
while (p) while (p)
{ {
n = p->next; n = p->next;
free_atom (xli, p); free_atom (root, p);
p = n; p = n;
} }
list->head = QSE_NULL; list->head = QSE_NULL;
list->tail = QSE_NULL; list->tail = QSE_NULL;
/* this doesn't destroy the list itself.
* the caller must destory the list if necessary. */
} }
void qse_xli_clear (qse_xli_t* xli) void qse_xli_clear (qse_xli_t* xli)
{ {
free_list (xli, &xli->root); free_list (xli->root, &xli->root->list);
qse_rbt_clear (xli->schema); qse_rbt_clear (xli->schema);
qse_xli_seterrnum (xli, QSE_XLI_ENOERR, QSE_NULL); qse_xli_seterrnum (xli, QSE_XLI_ENOERR, QSE_NULL);
@ -457,14 +462,35 @@ void qse_xli_clear (qse_xli_t* xli)
qse_xli_list_t* qse_xli_getroot (qse_xli_t* xli) qse_xli_list_t* qse_xli_getroot (qse_xli_t* xli)
{ {
return &xli->root; /*return &xli->root.list;*/
return (qse_xli_list_t*)xli->root;
} }
void qse_xli_clearroot (qse_xli_t* xli) void qse_xli_clearroot (qse_xli_t* xli)
{ {
free_list (xli, &xli->root); free_list (xli->root, &xli->root->list);
} }
qse_xli_list_t* qse_xli_yieldroot (qse_xli_t* xli)
{
qse_xli_root_list_t* tmp, * tmp2;
tmp = QSE_MMGR_ALLOC (xli->mmgr, QSE_SIZEOF(*tmp));
if (tmp == QSE_NULL)
{
qse_xli_seterrnum (xli, QSE_XLI_ENOMEM, QSE_NULL);
return QSE_NULL;
}
QSE_MEMSET (tmp, 0, QSE_SIZEOF(*tmp));
tmp->list.type = QSE_XLI_LIST;
tmp->xnil.type = QSE_XLI_NIL;
tmp->mmgr = xli->mmgr;
tmp2 = xli->root;
xli->root = tmp;
return (qse_xli_list_t*)tmp2;
}
/* ------------------------------------------------------ */ /* ------------------------------------------------------ */
@ -642,7 +668,7 @@ qse_xli_pair_t* qse_xli_findpair (qse_xli_t* xli, const qse_xli_list_t* list, co
const qse_xli_list_t* curlist; const qse_xli_list_t* curlist;
fqpn_seg_t seg; fqpn_seg_t seg;
curlist = list? list: &xli->root; curlist = list? list: &xli->root->list;
ptr = fqpn; ptr = fqpn;
while (1) while (1)
@ -705,7 +731,7 @@ qse_size_t qse_xli_countpairs (qse_xli_t* xli, const qse_xli_list_t* list, const
const qse_xli_list_t* curlist; const qse_xli_list_t* curlist;
fqpn_seg_t seg; fqpn_seg_t seg;
curlist = list? list: &xli->root; curlist = list? list: &xli->root->list;
ptr = fqpn; ptr = fqpn;
while (1) while (1)
@ -862,7 +888,7 @@ void qse_xli_undefinepairs (qse_xli_t* xli)
#if 0 #if 0
qse_xli_pair_t* qse_xli_getpair (qse_xli_t* xli, const qse_char_t* fqpn) qse_xli_pair_t* qse_xli_getpair (qse_xli_t* xli, const qse_char_t* fqpn)
{ {
return qse_xli_findpair (xli, &xli->root, fqpn); return qse_xli_findpair (xli, &xli->root->list, fqpn);
} }
qse_xli_pair_t* qse_xli_setpair (qse_xli_t* xli, const qse_char_t* fqpn, const qse_xli_val_t* val) -> str, val, nil qse_xli_pair_t* qse_xli_setpair (qse_xli_t* xli, const qse_char_t* fqpn, const qse_xli_val_t* val) -> str, val, nil
@ -871,7 +897,7 @@ qse_xli_pair_t* qse_xli_setpair (qse_xli_t* xli, const qse_char_t* fqpn, const q
const qse_xli_list_t* curlist; const qse_xli_list_t* curlist;
fqpn_seg_t seg; fqpn_seg_t seg;
curlist = list? list: &xli->root; curlist = list? list: &xli->root->list;
ptr = fqpn; ptr = fqpn;
while (1) while (1)
@ -947,3 +973,15 @@ noent:
return QSE_NULL; return QSE_NULL;
} }
#endif #endif
void* qse_getxlipairxtn (qse_xli_pair_t* pair)
{
return (void*)(pair + 1);
}
void qse_freexliroot (qse_xli_list_t* root)
{
qse_xli_root_list_t* real_root = (qse_xli_root_list_t*)root;
free_list (real_root, &real_root->list);
QSE_MMGR_FREE (real_root->mmgr, root);
}

View File

@ -43,6 +43,15 @@ struct qse_xli_list_link_t
qse_xli_list_t* list; qse_xli_list_t* list;
}; };
typedef struct qse_xli_root_list_t qse_xli_root_list_t;
struct qse_xli_root_list_t
{
qse_xli_list_t list;
qse_xli_nil_t xnil;
qse_mmgr_t* mmgr;
};
struct qse_xli_t struct qse_xli_t
{ {
qse_mmgr_t* mmgr; qse_mmgr_t* mmgr;
@ -60,9 +69,7 @@ struct qse_xli_t
qse_xli_ecb_t* ecb; qse_xli_ecb_t* ecb;
qse_xli_nil_t xnil; qse_xli_root_list_t* root;
qse_xli_list_t root;
qse_xli_list_link_t* parlink; /* link that points to the list being read currently */ qse_xli_list_link_t* parlink; /* link that points to the list being read currently */
qse_str_t* dotted_curkey; qse_str_t* dotted_curkey;