diff --git a/include/iini.h b/include/iini.h index 6d3328d..1c52d09 100644 --- a/include/iini.h +++ b/include/iini.h @@ -51,50 +51,33 @@ static INICHAR* _ini_str_skip_trailing(INICHAR* str) return str; } -//static TCHAR* cleanstring(TCHAR* string, enum quote_option* quotes) -//{ -// int isstring; -// TCHAR* ep; -// -// assert(string != NULL); -// assert(quotes != NULL); -// -// /* Remove a trailing comment */ -// isstring = 0; -// for (ep = string; *ep != '\0' && ((*ep != ';' && *ep != '#') || isstring); ep++) { -// if (*ep == '"') { -// if (*(ep + 1) == '"') -// ep++; /* skip "" (both quotes) */ -// else -// isstring = !isstring; /* single quote, toggle isstring */ -// } -// else if (*ep == '\\' && *(ep + 1) == '"') { -// ep++; /* skip \" (both quotes */ -// } -// } -// assert(ep != NULL && (*ep == '\0' || *ep == ';' || *ep == '#')); -// *ep = '\0'; /* terminate at a comment */ -// striptrailing(string); -// /* Remove double quotes surrounding a value */ -// *quotes = QUOTE_NONE; -// if (*string == '"' && (ep = _tcschr(string, '\0')) != NULL && *(ep - 1) == '"') { -// string++; -// *--ep = '\0'; -// *quotes = QUOTE_DEQUOTE; /* this is a string, so remove escaped characters */ -// } -// return string; -//} - static void _ini_clean_string(INICHAR** start_pptr, INICHAR** end_pptr) { /* Remove a trailing comment */ int is_string = 0; *start_pptr = _ini_skip_leading(*end_pptr + 1); for (*end_pptr = *start_pptr; **end_pptr != '#' && **end_pptr != ';' && **end_pptr != '\0'; *end_pptr += 1) { - //if(end) + if (**end_pptr == '"') { + if (*(*end_pptr + 1) == '"') { + *end_pptr += 1; /* skip "" (both quotes) */ + } + else { + is_string = !is_string;/* single quote, toggle isstring */ + } + } + else if (*(*end_pptr) == '\\' && *(*end_pptr + 1) == '"') { + *end_pptr += 1; /* skip \" (both quotes */ + } } **end_pptr = ' '; *end_pptr = _ini_skip_trailing(*end_pptr, *start_pptr); **end_pptr = '\0'; + + /* Remove double quotes surrounding a value */ + if (**start_pptr == '"' && *(*end_pptr - 1) == '"') { + *end_pptr -= 1; + *start_pptr += 1; + **end_pptr = '\0'; + } } static inline int _ini_str_get_section(INICHAR **start_pptr, INICHAR **end_pptr, @@ -125,7 +108,7 @@ static inline int _ini_str_get_key(INICHAR** start_pptr, INICHAR** end_pptr, return 0; } -static int _get_key_string(FILE* fp, const INICHAR* section, const INICHAR* key, +static int _int_get_all_string(FILE* fp, const INICHAR* section, const INICHAR* key, int idx_section, int idx_key, INICHAR* buf, size_t szbuf) { int len = 0, idx = 0; INICHAR* start_ptr, * end_ptr; @@ -168,7 +151,7 @@ static int ini_get_str(const INICHAR* filename, const INICHAR* section, const IN int res = 0; if (fp = fopen(filename, "rb")) { - res = _get_key_string(fp, section, key, -1, -1, buf, szbuf); + res = _int_get_all_string(fp, section, key, -1, -1, buf, szbuf); fclose(fp); } diff --git a/test/ini/ini.c b/test/ini/ini.c index 2486917..76be7dc 100644 --- a/test/ini/ini.c +++ b/test/ini/ini.c @@ -1,26 +1,43 @@ #include -//[First] -//String = noot # trailing commment -//Val = 1 -// -//[Second] -//Val = 2 -//#comment = 3 -//String = mies - #define FILE_PATH "../test.ini" +#define BUFF_MAX 128 + +void test01(char* buf) { + printf("[First]\n"); + ini_get_str(FILE_PATH, "First", "String", "nan", buf, BUFF_MAX); + printf("%s\n", buf); + ini_get_str(FILE_PATH, "First", "Val", "nan", buf, BUFF_MAX); + printf("%s\n", buf); + ini_get_str(FILE_PATH, "First", "Other", "nan", buf, BUFF_MAX); + printf("%s\n", buf); +} + +void test02(char* buf) { + printf("[Second]\n"); + ini_get_str(FILE_PATH, "Second", "String", "nan", buf, BUFF_MAX); + printf("%s\n", buf); + ini_get_str(FILE_PATH, "Second", "Val", "nan", buf, BUFF_MAX); + printf("%s\n", buf); + ini_get_str(FILE_PATH, "Second", "Other", "nan", buf, BUFF_MAX); + printf("%s\n", buf); +} + +void test03(char* buf) { + printf("[Third]\n"); + ini_get_str(FILE_PATH, "Third", "String", "nan", buf, BUFF_MAX); + printf("%s\n", buf); + ini_get_str(FILE_PATH, "Third", "Val", "nan", buf, BUFF_MAX); + printf("%s\n", buf); + ini_get_str(FILE_PATH, "Third", "Other", "nan", buf, BUFF_MAX); + printf("%s\n", buf); +} int main() { - char buf[128] = { 0 }; - printf("First\n"); - ini_get_str(FILE_PATH, "First", "String", "nan", buf, 128); - printf("%s\n", buf); - ini_get_str(FILE_PATH, "First", "Val", "nan", buf, 128); - printf("%s\n", buf); - ini_get_str(FILE_PATH, "First", "Other", "nan", buf, 128); - printf("%s\n", buf); - + char buf[BUFF_MAX] = { 0 }; + test01(buf); + test02(buf); + test03(buf); if (getchar()); return 0; } \ No newline at end of file diff --git a/test/ini/test.ini b/test/ini/test.ini index 565aef7..66e8f16 100644 --- a/test/ini/test.ini +++ b/test/ini/test.ini @@ -6,3 +6,8 @@ Val=1 Val = 2 #comment=3 String = mies + +[Third] +Val = 333#haha +# Other = 3 +String = "mies []:=)"; this \ No newline at end of file