diff --git a/qse/include/qse/xli/stdxli.h b/qse/include/qse/xli/stdxli.h index 33ddc231..aa387819 100644 --- a/qse/include/qse/xli/stdxli.h +++ b/qse/include/qse/xli/stdxli.h @@ -139,6 +139,11 @@ QSE_EXPORT int qse_xli_writestd ( qse_xli_iostd_t* out ); +QSE_EXPORT int qse_xli_writeinistd ( + qse_xli_t* xli, + qse_xli_iostd_t* out +); + #if defined(__cplusplus) } #endif diff --git a/qse/include/qse/xli/xli.h b/qse/include/qse/xli/xli.h index 81aea0c0..ebdddb73 100644 --- a/qse/include/qse/xli/xli.h +++ b/qse/include/qse/xli/xli.h @@ -739,6 +739,11 @@ QSE_EXPORT int qse_xli_write ( qse_xli_io_impl_t io ); +QSE_EXPORT int qse_xli_writeini ( + qse_xli_t* xli, + qse_xli_io_impl_t io +); + QSE_EXPORT void* qse_getxlipairxtn ( qse_xli_pair_t* pair ); diff --git a/qse/lib/xli/Makefile.am b/qse/lib/xli/Makefile.am index 8a121eaa..8cdca396 100644 --- a/qse/lib/xli/Makefile.am +++ b/qse/lib/xli/Makefile.am @@ -5,7 +5,7 @@ AM_CPPFLAGS = \ -I$(top_srcdir)/include lib_LTLIBRARIES = libqsexli.la -libqsexli_la_SOURCES = xli.h xli.c err.c read.c read-ini.c write.c std.c +libqsexli_la_SOURCES = xli.h xli.c err.c read.c read-ini.c write.c write-ini.c std.c libqsexli_la_LDFLAGS = -L../cmn -version-info 1:0:0 -no-undefined libqsexli_la_LIBADD = -lqsecmn diff --git a/qse/lib/xli/Makefile.in b/qse/lib/xli/Makefile.in index eae44e22..565ee4e6 100644 --- a/qse/lib/xli/Makefile.in +++ b/qse/lib/xli/Makefile.in @@ -126,7 +126,7 @@ am__uninstall_files_from_dir = { \ am__installdirs = "$(DESTDIR)$(libdir)" LTLIBRARIES = $(lib_LTLIBRARIES) libqsexli_la_DEPENDENCIES = -am_libqsexli_la_OBJECTS = xli.lo err.lo read.lo read-ini.lo write.lo \ +am_libqsexli_la_OBJECTS = xli.lo err.lo read.lo read-ini.lo write.lo write-ini.lo \ std.lo libqsexli_la_OBJECTS = $(am_libqsexli_la_OBJECTS) AM_V_lt = $(am__v_lt_@AM_V@) @@ -378,7 +378,7 @@ AM_CPPFLAGS = \ -I$(top_srcdir)/include lib_LTLIBRARIES = libqsexli.la -libqsexli_la_SOURCES = xli.h xli.c err.c read.c read-ini.c write.c std.c +libqsexli_la_SOURCES = xli.h xli.c err.c read.c read-ini.c write.c write-ini.c std.c libqsexli_la_LDFLAGS = -L../cmn -version-info 1:0:0 -no-undefined libqsexli_la_LIBADD = -lqsecmn all: all-am @@ -465,6 +465,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/read.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/std.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/write.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/write-ini.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xli.Plo@am__quote@ .c.o: diff --git a/qse/lib/xli/read-ini.c b/qse/lib/xli/read-ini.c index 1d55d55b..2d9f12ee 100644 --- a/qse/lib/xli/read-ini.c +++ b/qse/lib/xli/read-ini.c @@ -112,12 +112,16 @@ static int skip_comment (qse_xli_t* xli, qse_xli_tok_t* tok) GET_CHAR_TO (xli, c); if (c == QSE_T('\n') || c == QSE_CHAR_EOF) break; - if (xli->opt.trait & QSE_XLI_KEEPTEXT) ADD_TOKEN_CHAR (xli, tok, c); + /* i don't honor QSE_XLI_KEEPTEXT in the ini-format reader... + if (xli->opt.trait & QSE_XLI_KEEPTEXT) ADD_TOKEN_CHAR (xli, tok, c); + */ } while (1); + /* i don't honor QSE_XLI_KEEPTEXT in the ini-format reader... if ((xli->opt.trait & QSE_XLI_KEEPTEXT) && - qse_xli_inserttext (xli, xli->parlink->list, QSE_NULL, QSE_STR_PTR(tok->name)) == QSE_NULL) return -1; + qse_xli_inserttext (xli, &xli->root->list, QSE_NULL, QSE_STR_PTR(tok->name)) == QSE_NULL) return -1; + */ GET_CHAR (xli); /* eat the new line letter */ return 1; /* comment by ; */ @@ -135,7 +139,11 @@ static int get_token_into (qse_xli_t* xli, qse_xli_tok_t* tok) do { if (skip_spaces (xli) <= -1) return -1; - if ((n = skip_comment (xli, tok)) <= -1) return -1; + if (!(xli->tok_status & TOK_STATUS_UPTO_EOL)) + { + if ((n = skip_comment (xli, tok)) <= -1) return -1; + } + else n = 0; } while (n >= 1); @@ -297,6 +305,9 @@ static int read_list (qse_xli_t* xli) qse_cstr_t key; qse_xli_list_t* curlist; + key.ptr = QSE_NULL; + key.len = 0; + while (1) { if (MATCH(xli, QSE_XLI_TOK_EOF)) break; @@ -305,19 +316,19 @@ static int read_list (qse_xli_t* xli) { /* insert a pair with an empty list */ pair = qse_xli_insertpairwithemptylist (xli, &xli->root->list, QSE_NULL, QSE_STR_PTR(xli->tok.name), QSE_NULL, QSE_NULL); - if (pair == QSE_NULL) return -1; + if (pair == QSE_NULL) goto oops; curlist = (qse_xli_list_t*)pair->val; while (1) { - if (get_token(xli) <= -1) return -1; + if (get_token(xli) <= -1) goto oops; if (MATCH(xli, QSE_XLI_TOK_EOF)) break; if (MATCH(xli, QSE_XLI_TOK_TAG)) { /* switch to a new tag */ pair = qse_xli_insertpairwithemptylist (xli, &xli->root->list, QSE_NULL, QSE_STR_PTR(xli->tok.name), QSE_NULL, QSE_NULL); - if (pair == QSE_NULL) return -1; + if (pair == QSE_NULL) goto oops; curlist = (qse_xli_list_t*)pair->val; continue; } @@ -325,7 +336,7 @@ static int read_list (qse_xli_t* xli) if (!MATCH(xli, QSE_XLI_TOK_IDENT)) { qse_xli_seterror (xli, QSE_XLI_EKEY, QSE_STR_XSTR(xli->tok.name), &xli->tok.loc); - return -1; + goto oops; } if (xli->opt.trait & QSE_XLI_KEYNODUP) @@ -341,41 +352,35 @@ static int read_list (qse_xli_t* xli) qse_strcmp (((qse_xli_pair_t*)atom)->key, QSE_STR_PTR(xli->tok.name)) == 0) { qse_xli_seterror (xli, QSE_XLI_EEXIST, QSE_STR_XSTR(xli->tok.name), &xli->tok.loc); - return -1; + goto oops; } atom = atom->prev; } } + QSE_ASSERT (key.ptr == QSE_NULL); key.len = QSE_STR_LEN(xli->tok.name); key.ptr = qse_strdup (QSE_STR_PTR(xli->tok.name), xli->mmgr); if (key.ptr == QSE_NULL) { qse_xli_seterrnum (xli, QSE_XLI_ENOMEM, QSE_NULL); - return -1; + goto oops; } xli->tok_status |= TOK_STATUS_SAME_LINE; - if (get_token (xli) <= -1) - { - QSE_MMGR_FREE (xli->mmgr, key.ptr); - return -1; - } + if (get_token (xli) <= -1) goto oops; + if (!MATCH(xli, QSE_XLI_TOK_EQ)) { - QSE_MMGR_FREE (xli->mmgr, key.ptr); qse_xli_seterror (xli, QSE_XLI_EEQ, QSE_STR_XSTR(xli->tok.name), &xli->tok.loc); - return -1; + goto oops; } /* read the value */ xli->tok_status |= TOK_STATUS_UPTO_EOL; - if (get_token (xli) <= -1) - { - QSE_MMGR_FREE (xli->mmgr, key.ptr); - return -1; - } + if (get_token (xli) <= -1) goto oops; + xli->tok_status &= ~(TOK_STATUS_SAME_LINE | TOK_STATUS_UPTO_EOL); if (MATCH(xli, QSE_XLI_TOK_EOF)) @@ -387,8 +392,11 @@ static int read_list (qse_xli_t* xli) empty.len = 0; pair = qse_xli_insertpairwithstr (xli, curlist, QSE_NULL, key.ptr, QSE_NULL, QSE_NULL, &empty, QSE_NULL); + QSE_MMGR_FREE (xli->mmgr, key.ptr); - if (pair == QSE_NULL) return -1; + key.ptr = QSE_NULL; + + if (pair == QSE_NULL) goto oops; break; } @@ -396,24 +404,31 @@ static int read_list (qse_xli_t* xli) { /* add a new pair with the initial string segment */ pair = qse_xli_insertpairwithstr (xli, curlist, QSE_NULL, key.ptr, QSE_NULL, QSE_NULL, QSE_STR_XSTR(xli->tok.name), QSE_NULL); + QSE_MMGR_FREE (xli->mmgr, key.ptr); - if (pair == QSE_NULL) return -1; + key.ptr = QSE_NULL; + + if (pair == QSE_NULL) goto oops; } else { qse_xli_seterror (xli, QSE_XLI_EVAL, QSE_STR_XSTR(xli->tok.name), &xli->tok.loc); - return -1; + goto oops; } } } else { qse_xli_seterror (xli, QSE_XLI_ESECTAG, QSE_STR_XSTR(xli->tok.name), &xli->tok.loc); - return -1; + goto oops; } } return 0; + +oops: + if (key.ptr) QSE_MMGR_FREE (xli->mmgr, key.ptr); + return -1; } static int read_root_list (qse_xli_t* xli) @@ -461,8 +476,6 @@ int qse_xli_readini (qse_xli_t* xli, qse_xli_io_impl_t io) if (read_root_list (xli) <= -1) goto oops; - QSE_ASSERT (xli->parlink == QSE_NULL); - if (!MATCH (xli, QSE_XLI_TOK_EOF)) { qse_xli_seterror (xli, QSE_XLI_ESYNTAX, QSE_NULL, &xli->tok.loc); diff --git a/qse/lib/xli/std.c b/qse/lib/xli/std.c index d1991058..12c56efe 100644 --- a/qse/lib/xli/std.c +++ b/qse/lib/xli/std.c @@ -662,3 +662,33 @@ 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 n; + xtn_t* xtn = (xtn_t*) QSE_XTN (xli); + + if (out == QSE_NULL || (out->type != QSE_XLI_IOSTD_FILE && + out->type != QSE_XLI_IOSTD_STR)) + { + /* the input is a must. at least 1 file or 1 string + * must be specified */ + qse_xli_seterrnum (xli, QSE_XLI_EINVAL, QSE_NULL); + return -1; + } + + xtn->s.out.x = out; + n = qse_xli_writeini (xli, sf_out); + + if (out->type == QSE_XLI_IOSTD_STR) + { + if (n >= 0) + { + QSE_ASSERT (xtn->s.out.u.str.buf != QSE_NULL); + qse_str_yield (xtn->s.out.u.str.buf, &out->u.str, 0); + } + if (xtn->s.out.u.str.buf) qse_str_close (xtn->s.out.u.str.buf); + } + + return n; +} diff --git a/qse/lib/xli/write-ini.c b/qse/lib/xli/write-ini.c new file mode 100644 index 00000000..e9d163bb --- /dev/null +++ b/qse/lib/xli/write-ini.c @@ -0,0 +1,32 @@ +/* + * $Id$ + * + Copyright (c) 2006-2014 Chung, Hyung-Hwan. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "xli.h" + +int qse_xli_writeini (qse_xli_t* xli, qse_xli_io_impl_t io) +{ +return -1; +} diff --git a/qse/lib/xli/write.c b/qse/lib/xli/write.c index 7273ea22..6f9e8539 100644 --- a/qse/lib/xli/write.c +++ b/qse/lib/xli/write.c @@ -127,7 +127,7 @@ static int close_current_stream (qse_xli_t* xli, int* org_depth) xli->wio.inp = arg->prev; if (arg == &xli->wio.top) { - if (org_depth) *org_depth = 0; + if (org_depth) *org_depth = 0; } else { @@ -208,7 +208,7 @@ static int write_list (qse_xli_t* xli, qse_xli_list_t* list, int depth) write_to_current_stream (xli, pair->tag, qse_strlen(pair->tag), 0) <= -1 || write_to_current_stream (xli, QSE_T("]"), 1, 0) <= -1) return -1; } - + if (write_to_current_stream (xli, pair->key, qse_strlen(pair->key), 0) <= -1) return -1; if (pair->alias) @@ -247,7 +247,7 @@ static int write_list (qse_xli_t* xli, qse_xli_list_t* list, int depth) str = str->next; } if (write_to_current_stream (xli, QSE_T(";\n"), 2, 0) <= -1) return -1; - break; + break; } case QSE_XLI_LIST: