added qse_xli_setpairwithstr() and qse_xli_deletepair()
This commit is contained in:
parent
b4a08c4fee
commit
dc43385559
@ -63,6 +63,7 @@ static int g_io_flags = 0;
|
||||
static qse_char_t* g_input_file = QSE_NULL;
|
||||
static qse_char_t* g_output_file = QSE_NULL;
|
||||
static qse_char_t* g_lookup_key = QSE_NULL;
|
||||
static qse_char_t* g_value = QSE_NULL;
|
||||
static qse_ulong_t g_memlimit = 0;
|
||||
static int g_trait = 0;
|
||||
|
||||
@ -148,7 +149,7 @@ static void print_usage (qse_sio_t* out, int argc, qse_char_t* argv[])
|
||||
{
|
||||
const qse_char_t* b = qse_basename (argv[0]);
|
||||
|
||||
qse_fprintf (out, QSE_T("USAGE: %s [options] -i input-file [key]\n"), b);
|
||||
qse_fprintf (out, QSE_T("USAGE: %s [options] -i input-file [key [value]]\n"), b);
|
||||
|
||||
qse_fprintf (out, QSE_T("options as follows:\n"));
|
||||
qse_fprintf (out, QSE_T(" -h/--help show this message\n"));
|
||||
@ -341,13 +342,20 @@ static int handle_args (int argc, qse_char_t* argv[])
|
||||
goto oops;
|
||||
}
|
||||
|
||||
if (opt.ind < argc) g_lookup_key = argv[opt.ind++];
|
||||
if (opt.ind < argc)
|
||||
{
|
||||
g_lookup_key = argv[opt.ind++];
|
||||
if (opt.ind < argc)
|
||||
{
|
||||
g_value = argv[opt.ind++];
|
||||
|
||||
if (opt.ind < argc)
|
||||
{
|
||||
print_usage (QSE_STDERR, argc, argv);
|
||||
goto oops;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
||||
@ -461,6 +469,7 @@ static int xli_main (int argc, qse_char_t* argv[])
|
||||
goto oops;
|
||||
}
|
||||
|
||||
#if 0
|
||||
{
|
||||
static const qse_cstr_t strs[] =
|
||||
{
|
||||
@ -477,8 +486,25 @@ static int xli_main (int argc, qse_char_t* argv[])
|
||||
);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (g_lookup_key)
|
||||
{
|
||||
if (g_value)
|
||||
{
|
||||
qse_cstr_t v;
|
||||
|
||||
v.ptr = g_value;
|
||||
v.len = qse_strlen(g_value);
|
||||
if (qse_xli_setpairwithstr (xli, QSE_NULL, g_lookup_key, &v, QSE_NULL) == QSE_NULL)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR,
|
||||
QSE_T("ERROR: cannot set a string pair - %s \n"),
|
||||
qse_xli_geterrmsg(xli)
|
||||
);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qse_xli_pair_t* pair;
|
||||
qse_size_t count;
|
||||
@ -509,16 +535,13 @@ static int xli_main (int argc, qse_char_t* argv[])
|
||||
}
|
||||
else
|
||||
{
|
||||
qse_printf (QSE_T("#LIST\n"));
|
||||
out.type = QSE_XLI_IOSTD_FILE;
|
||||
out.u.file.path = QSE_T("-");
|
||||
out.u.file.cmgr = g_outfile_cmgr;
|
||||
qse_xli_writestd (xli, pair->val, &out);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
if (g_value)
|
||||
{
|
||||
TODO: ... set value...
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
@ -526,7 +549,7 @@ static int xli_main (int argc, qse_char_t* argv[])
|
||||
out.u.file.path = g_output_file? g_output_file: QSE_T("-");
|
||||
out.u.file.cmgr = g_outfile_cmgr;
|
||||
|
||||
ret = (g_io_flags & IO_FLAG_INI_OUTPUT)? qse_xli_writeinistd(xli, &out): qse_xli_writestd(xli, &out);
|
||||
ret = (g_io_flags & IO_FLAG_INI_OUTPUT)? qse_xli_writeinistd(xli, QSE_NULL, &out): qse_xli_writestd(xli, QSE_NULL, &out);
|
||||
|
||||
oops:
|
||||
if (xli) qse_xli_close (xli);
|
||||
|
@ -72,7 +72,7 @@
|
||||
# undef QSE_HAVE_INLINE_NEVER
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32) || defined(__WATCOMC__)
|
||||
#if defined(_WIN32) || (defined(__WATCOMC__) && !defined(__WINDOWS_386__))
|
||||
# define QSE_IMPORT __declspec(dllimport)
|
||||
# define QSE_EXPORT __declspec(dllexport)
|
||||
# define QSE_PRIVATE
|
||||
|
@ -136,11 +136,13 @@ QSE_EXPORT int qse_xli_readinistd (
|
||||
|
||||
QSE_EXPORT int qse_xli_writestd (
|
||||
qse_xli_t* xli,
|
||||
qse_xli_list_t* root_list,
|
||||
qse_xli_iostd_t* out
|
||||
);
|
||||
|
||||
QSE_EXPORT int qse_xli_writeinistd (
|
||||
qse_xli_t* xli,
|
||||
qse_xli_list_t* root_list,
|
||||
qse_xli_iostd_t* out
|
||||
);
|
||||
|
||||
|
@ -587,6 +587,11 @@ QSE_EXPORT qse_xli_pair_t* qse_xli_insertpair (
|
||||
qse_xli_val_t* val
|
||||
);
|
||||
|
||||
QSE_EXPORT void qse_xli_deletepair (
|
||||
qse_xli_t* xli,
|
||||
qse_xli_pair_t* pair
|
||||
);
|
||||
|
||||
/**
|
||||
* The qse_xli_insertpairwithemptylist() function inserts a new pair
|
||||
* with an empty list as a value. You should call this function for adding
|
||||
@ -655,6 +660,14 @@ QSE_EXPORT qse_xli_pair_t* qse_xli_findpair (
|
||||
const qse_char_t* fqpn
|
||||
);
|
||||
|
||||
QSE_EXPORT qse_xli_pair_t* qse_xli_setpairwithstr (
|
||||
qse_xli_t* xli,
|
||||
const qse_xli_list_t* list,
|
||||
const qse_char_t* fqpn,
|
||||
const qse_cstr_t* value,
|
||||
const qse_char_t* strtag
|
||||
);
|
||||
|
||||
QSE_EXPORT qse_size_t qse_xli_countpairs (
|
||||
qse_xli_t* xli,
|
||||
const qse_xli_list_t* list,
|
||||
@ -736,11 +749,13 @@ QSE_EXPORT int qse_xli_readini (
|
||||
|
||||
QSE_EXPORT int qse_xli_write (
|
||||
qse_xli_t* xli,
|
||||
qse_xli_list_t* root_list,
|
||||
qse_xli_io_impl_t io
|
||||
);
|
||||
|
||||
QSE_EXPORT int qse_xli_writeini (
|
||||
qse_xli_t* xli,
|
||||
qse_xli_list_t* root_list,
|
||||
qse_xli_io_impl_t io
|
||||
);
|
||||
|
||||
|
@ -28,20 +28,25 @@
|
||||
#include <qse/cmn/chr.h>
|
||||
|
||||
/*
|
||||
* It reads key-value pairs under sections as shown below:
|
||||
*
|
||||
* [SECTION1]
|
||||
* key1 = value1
|
||||
* key2 = value2
|
||||
* [SECTION2]
|
||||
* key1 = value1
|
||||
* --------------------------------
|
||||
*
|
||||
* The above can get translated to the native XLI format shown below:
|
||||
*
|
||||
* SECTION1 {
|
||||
* key1 = value1;
|
||||
* key2 = value2;
|
||||
* key1 = "value1";
|
||||
* key2 = "value2";
|
||||
* }
|
||||
* SECTION2 {
|
||||
* key1 = value1;
|
||||
* key1 = "value1";
|
||||
* }
|
||||
*
|
||||
* The ini-format reader doesn't support file inclusion via @include.
|
||||
*/
|
||||
|
||||
enum
|
||||
|
@ -633,7 +633,7 @@ int qse_xli_readinistd (qse_xli_t* xli, qse_xli_iostd_t* in)
|
||||
}
|
||||
|
||||
|
||||
int qse_xli_writestd (qse_xli_t* xli, qse_xli_iostd_t* out)
|
||||
int qse_xli_writestd (qse_xli_t* xli, qse_xli_list_t* root_list, qse_xli_iostd_t* out)
|
||||
{
|
||||
int n;
|
||||
xtn_t* xtn = (xtn_t*) QSE_XTN (xli);
|
||||
@ -648,7 +648,7 @@ int qse_xli_writestd (qse_xli_t* xli, qse_xli_iostd_t* out)
|
||||
}
|
||||
|
||||
xtn->s.out.x = out;
|
||||
n = qse_xli_write (xli, sf_out);
|
||||
n = qse_xli_write (xli, root_list, sf_out);
|
||||
|
||||
if (out->type == QSE_XLI_IOSTD_STR)
|
||||
{
|
||||
@ -663,7 +663,7 @@ int qse_xli_writestd (qse_xli_t* xli, qse_xli_iostd_t* out)
|
||||
return n;
|
||||
}
|
||||
|
||||
int qse_xli_writeinistd (qse_xli_t* xli, qse_xli_iostd_t* out)
|
||||
int qse_xli_writeinistd (qse_xli_t* xli, qse_xli_list_t* root_list, qse_xli_iostd_t* out)
|
||||
{
|
||||
int n;
|
||||
xtn_t* xtn = (xtn_t*) QSE_XTN (xli);
|
||||
@ -678,7 +678,7 @@ int qse_xli_writeinistd (qse_xli_t* xli, qse_xli_iostd_t* out)
|
||||
}
|
||||
|
||||
xtn->s.out.x = out;
|
||||
n = qse_xli_writeini (xli, sf_out);
|
||||
n = qse_xli_writeini (xli, root_list, sf_out);
|
||||
|
||||
if (out->type == QSE_XLI_IOSTD_STR)
|
||||
{
|
||||
|
@ -148,7 +148,7 @@ static int write_list (qse_xli_t* xli, qse_xli_list_t* list, int depth)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int qse_xli_writeini (qse_xli_t* xli, qse_xli_io_impl_t io)
|
||||
int qse_xli_writeini (qse_xli_t* xli, qse_xli_list_t* root_list, qse_xli_io_impl_t io)
|
||||
{
|
||||
int n;
|
||||
|
||||
@ -169,7 +169,7 @@ int qse_xli_writeini (qse_xli_t* xli, qse_xli_io_impl_t io)
|
||||
if (qse_xli_openwstream (xli, QSE_NULL, 0) <= -1) return -1;
|
||||
|
||||
/* begin writing the root list */
|
||||
n = write_list (xli, &xli->root->list, 0);
|
||||
n = write_list (xli, (root_list? root_list: &xli->root->list), 0);
|
||||
|
||||
/* close all open streams. there should be only the
|
||||
* top-level stream here if there occurred no errors */
|
||||
|
@ -317,7 +317,7 @@ void qse_xli_clearwionames (qse_xli_t* xli)
|
||||
}
|
||||
}
|
||||
|
||||
int qse_xli_write (qse_xli_t* xli, qse_xli_io_impl_t io)
|
||||
int qse_xli_write (qse_xli_t* xli, qse_xli_list_t* root_list, qse_xli_io_impl_t io)
|
||||
{
|
||||
int n;
|
||||
|
||||
@ -338,7 +338,7 @@ int qse_xli_write (qse_xli_t* xli, qse_xli_io_impl_t io)
|
||||
if (qse_xli_openwstream (xli, QSE_NULL, 0) <= -1) return -1;
|
||||
|
||||
/* begin writing the root list */
|
||||
n = write_list (xli, &xli->root->list, 0);
|
||||
n = write_list (xli, (root_list? root_list: &xli->root->list), 0);
|
||||
|
||||
/* close all open streams. there should be only the
|
||||
* top-level stream here if there occurred no errors */
|
||||
|
@ -308,6 +308,37 @@ qse_xli_pair_t* qse_xli_insertpair (
|
||||
return insert_pair (xli, parent, peer, &k, ap, tp, value);
|
||||
}
|
||||
|
||||
void qse_xli_deletepair (qse_xli_t* xli, qse_xli_pair_t* pair)
|
||||
{
|
||||
qse_xli_list_t* list;
|
||||
|
||||
list = pair->super;
|
||||
|
||||
if (pair->prev)
|
||||
{
|
||||
pair->prev->next = pair->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
QSE_ASSERT (list->head == pair);
|
||||
list->head = pair->next;
|
||||
}
|
||||
|
||||
if (pair->next)
|
||||
{
|
||||
pair->next->prev = pair->prev;
|
||||
}
|
||||
else
|
||||
{
|
||||
QSE_ASSERT (list->tail == pair);
|
||||
list->tail = pair->prev;
|
||||
}
|
||||
|
||||
free_val (xli->root, pair->val);
|
||||
QSE_MMGR_FREE (xli->mmgr, pair);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------ */
|
||||
qse_xli_pair_t* qse_xli_insertpairwithemptylist (
|
||||
qse_xli_t* xli, qse_xli_list_t* parent, qse_xli_atom_t* peer,
|
||||
const qse_char_t* key, const qse_char_t* alias, const qse_char_t* keytag)
|
||||
@ -788,6 +819,56 @@ noent:
|
||||
return QSE_NULL;
|
||||
}
|
||||
|
||||
qse_xli_pair_t* qse_xli_setpairwithstr (qse_xli_t* xli, const qse_xli_list_t* list, const qse_char_t* fqpn, const qse_cstr_t* value, const qse_char_t* strtag)
|
||||
{
|
||||
qse_xli_pair_t* pair, * xpair;
|
||||
|
||||
pair = qse_xli_findpair (xli, list, fqpn);
|
||||
if (pair)
|
||||
{
|
||||
if (xli->opt.trait & QSE_XLI_VALIDATE)
|
||||
{
|
||||
qse_rbt_pair_t* scm_pair;
|
||||
const qse_xli_scm_t* scm;
|
||||
|
||||
scm_pair = qse_rbt_search (xli->schema, fqpn, qse_strlen(fqpn));
|
||||
if (!scm_pair)
|
||||
{
|
||||
qse_cstr_t key;
|
||||
|
||||
key.ptr = (qse_char_t*)fqpn;
|
||||
key.len = qse_strlen(fqpn);
|
||||
|
||||
qse_xli_seterror (xli, QSE_XLI_EUDKEY, &key, QSE_NULL);
|
||||
return QSE_NULL;
|
||||
}
|
||||
|
||||
scm = (qse_xli_scm_t*)QSE_RBT_VPTR(scm_pair);
|
||||
|
||||
if (scm && !(scm->flags & QSE_XLI_SCM_VALSTR))
|
||||
{
|
||||
/* check the value type */
|
||||
qse_cstr_t key;
|
||||
|
||||
key.ptr = (qse_char_t*)fqpn;
|
||||
key.len = qse_strlen(fqpn);
|
||||
|
||||
qse_xli_seterror (xli, QSE_XLI_EILVAL, (const qse_cstr_t*)&key, QSE_NULL);
|
||||
return QSE_NULL;
|
||||
}
|
||||
}
|
||||
|
||||
xpair = qse_xli_insertpairwithstr (xli, pair->super, (qse_xli_atom_t*)pair, pair->key, pair->alias, pair->tag, value, strtag);
|
||||
if (xpair) qse_xli_deletepair (xli, pair);
|
||||
return xpair;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* TODO: insert a new pair */
|
||||
return QSE_NULL;
|
||||
}
|
||||
}
|
||||
|
||||
qse_size_t qse_xli_countpairs (qse_xli_t* xli, const qse_xli_list_t* list, const qse_char_t* fqpn)
|
||||
{
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user