From 2bccba995cd72926aaaf6b8af8ea3cd62196f6b7 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 2 Jul 2013 13:10:11 +0000 Subject: [PATCH] fixed a xli reader bug of not placing the comment text inside a proper list --- qse/lib/xli/read.c | 7 ++++--- qse/lib/xli/write.c | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/qse/lib/xli/read.c b/qse/lib/xli/read.c index 266ab231..3842f3bb 100644 --- a/qse/lib/xli/read.c +++ b/qse/lib/xli/read.c @@ -721,8 +721,6 @@ static int read_pair (qse_xli_t* xli) { xli->tok_status &= ~TOK_STATUS_ENABLE_NSTR; - if (get_token (xli) <= -1) goto oops; - /* insert a pair with an empty list */ pair = qse_xli_insertpairwithemptylist (xli, parlist, QSE_NULL, key, name); if (pair == QSE_NULL) goto oops; @@ -832,7 +830,10 @@ static int read_list (qse_xli_t* xli, qse_xli_list_t* parlist) link = make_list_link (xli, parlist); if (link == QSE_NULL) return -1; - if (__read_list (xli) <= -1) + /* get_token() here is to read the token after the left brace. + * it must be called after the xli->parlink has been updated + * in case there are comments at the beginning of the list */ + if (get_token (xli) <= -1 || __read_list (xli) <= -1) { free_list_link (xli, link); return -1; diff --git a/qse/lib/xli/write.c b/qse/lib/xli/write.c index 436b874b..58117d38 100644 --- a/qse/lib/xli/write.c +++ b/qse/lib/xli/write.c @@ -156,6 +156,32 @@ static int write_to_current_stream (qse_xli_t* xli, const qse_char_t* ptr, qse_s return 0; } +static int write_indentation (qse_xli_t* xli, int depth) +{ + static const qse_char_t tabs[16] = + { + QSE_T('\t'), QSE_T('\t'), QSE_T('\t'), QSE_T('\t'), + QSE_T('\t'), QSE_T('\t'), QSE_T('\t'), QSE_T('\t'), + QSE_T('\t'), QSE_T('\t'), QSE_T('\t'), QSE_T('\t'), + QSE_T('\t'), QSE_T('\t'), QSE_T('\t'), QSE_T('\t') + }; + + if (depth <= QSE_COUNTOF(tabs)) + { + if (write_to_current_stream (xli, tabs, depth, 0) <= -1) return -1; + } + else + { + int i; + if (write_to_current_stream (xli, tabs, QSE_COUNTOF(tabs), 0) <= -1) return -1; + for (i = QSE_COUNTOF(tabs); i < depth; i++) + { + if (write_to_current_stream (xli, QSE_T("\t"), 1, 0) <= -1) return -1; + } + } + return 0; +} + static int write_list (qse_xli_t* xli, qse_xli_list_t* list, int depth) { qse_xli_atom_t* curatom; @@ -166,15 +192,10 @@ static int write_list (qse_xli_t* xli, qse_xli_list_t* list, int depth) { case QSE_XLI_PAIR: { - int i; qse_xli_pair_t* pair = (qse_xli_pair_t*)curatom; - for (i = 0; i < depth; i++) - { - if (write_to_current_stream (xli, QSE_T("\t"), 1, 0) <= -1) return -1; - } - - if (write_to_current_stream (xli, pair->key, qse_strlen(pair->key), 0) <= -1) return -1; + if (write_indentation (xli, depth) <= -1 || + write_to_current_stream (xli, pair->key, qse_strlen(pair->key), 0) <= -1) return -1; if (pair->alias) { @@ -211,7 +232,8 @@ static int write_list (qse_xli_t* xli, qse_xli_list_t* list, int depth) case QSE_XLI_LIST: { if (write_to_current_stream (xli, QSE_T(" {\n"), 3, 0) <= -1 || - write_list (xli, (qse_xli_list_t*)pair->val, ++depth) <= -1 || + write_list (xli, (qse_xli_list_t*)pair->val, depth + 1) <= -1 || + write_indentation (xli, depth) <= -1 || write_to_current_stream (xli, QSE_T("}\n"), 2, 0) <= -1) return -1; break; }