diff --git a/docs/changes.md b/docs/changes.md index f7aa78a86..c8d0b8419 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -2,6 +2,7 @@ 2021/02/20 * 完善编译脚本(感谢雨欣提供补丁) + * 修复ini的值为空的问题。 2021/02/19 * fscript支持return语句。 diff --git a/src/conf_io/conf_ini.c b/src/conf_io/conf_ini.c index 4db490a76..6fb7d82fa 100644 --- a/src/conf_io/conf_ini.c +++ b/src/conf_io/conf_ini.c @@ -126,9 +126,14 @@ conf_doc_t* conf_doc_load_ini(const char* data) { break; } case STATE_BEFORE_VALUE: { - if (!isspace(c)) { + if (c != ' ' && c != '\t') { state = STATE_VALUE; - str_set_with_len(s, &c, 1); + if (c == '\r' || c == '\n') { + str_set(s, ""); + p -= 1; + } else { + str_set_with_len(s, &c, 1); + } } break; diff --git a/tests/conf_ini_test.cc b/tests/conf_ini_test.cc index 411a364df..1ea7e47f0 100644 --- a/tests/conf_ini_test.cc +++ b/tests/conf_ini_test.cc @@ -302,6 +302,19 @@ TEST(Ini, last_first) { conf_doc_destroy(doc); } +TEST(Ini, empty) { + value_t v; + conf_doc_t* doc = conf_doc_load_ini("[hello]\na=\nb=2\nc=3\n"); + ASSERT_EQ(conf_doc_get(doc, "hello.a", &v), RET_OK); + ASSERT_STREQ(value_str(&v), ""); + ASSERT_EQ(conf_doc_get(doc, "hello.b", &v), RET_OK); + ASSERT_STREQ(value_str(&v), "2"); + ASSERT_EQ(conf_doc_get(doc, "hello.c", &v), RET_OK); + ASSERT_STREQ(value_str(&v), "3"); + + conf_doc_destroy(doc); +} + TEST(Ini, move_up) { value_t v; conf_doc_t* doc = conf_doc_load_ini("[hello]\n[ world ]\n[awtk]\nname=aaa\n");