diff options
| -rw-r--r-- | any_ini.h | 116 | ||||
| -rw-r--r-- | test/ini.c | 32 |
2 files changed, 74 insertions, 74 deletions
@@ -2,26 +2,36 @@ #define ANY_INI_INCLUDE #ifndef ANY_INI_MALLOC -#define ANY_INI_MALLOC malloc #include <stdlib.h> +#define ANY_INI_MALLOC malloc #endif #include <stddef.h> +#include <stdbool.h> typedef struct { const char *source; size_t length; size_t cursor; + size_t line; } any_ini_t; void any_ini_init(any_ini_t *ini, const char *source, size_t length); +bool any_ini_eof(any_ini_t *ini); + char *any_ini_next_section(any_ini_t *ini); char *any_ini_next_key(any_ini_t *ini); char *any_ini_next_value(any_ini_t *ini); +//typedef struct { +// FILE *stream; +//} any_ini_stream_t; +// +//void any_ini_stream_init(any_ini_stream_t *ini, const char *source, size_t length); + #endif #ifdef ANY_INI_IMPLEMENT @@ -34,10 +44,6 @@ char *any_ini_next_value(any_ini_t *ini); #define ANY_INI_DELIM_COMMENT ';' #endif -#ifndef ANY_INI_DELIM_COMMENT2 -#define ANY_INI_DELIM_COMMENT2 '#' -#endif - #ifndef ANY_INI_DELIM_PAIR #define ANY_INI_DELIM_PAIR '=' #endif @@ -50,18 +56,6 @@ char *any_ini_next_value(any_ini_t *ini); #define ANY_INI_SECTION_END ']' #endif -void any_ini_init(any_ini_t *ini, const char *source, size_t length) -{ - ini->source = source; - ini->length = length; - ini->cursor = 0; -} - -static inline bool any_ini_more(any_ini_t *ini) -{ - return ini->cursor < ini->length; -} - static inline bool any_ini_special(char c) { return c == ANY_INI_DELIM_PAIR @@ -77,24 +71,37 @@ static size_t any_ini_trim(any_ini_t *ini, size_t start, size_t end) return end - start; } -static void any_ini_skip_comment(any_ini_t *ini) +static char *any_ini_copy(const char *start, size_t length) +{ + char *string = ANY_INI_MALLOC(length + 1); + if (string) { + memcpy(string, start, length); + string[length] = '\0'; + } + return string; +} + +static void any_ini_skip(any_ini_t *ini) { - while (ini->cursor < ini->length) { + while (!any_ini_eof(ini)) { switch (ini->source[ini->cursor]) { case ' ': case '\t': case '\v': case '\r': + break; + case '\n': + ini->line++; break; case ANY_INI_DELIM_COMMENT: #ifdef ANY_INI_DELIM_COMMENT2 case ANY_INI_DELIM_COMMENT2: #endif - while (any_ini_more(ini) && ini->source[ini->cursor] != '\n') + while (!any_ini_eof(ini) && ini->source[ini->cursor] != '\n') ini->cursor++; - break; + continue; default: return; @@ -103,43 +110,49 @@ static void any_ini_skip_comment(any_ini_t *ini) } } + +void any_ini_init(any_ini_t *ini, const char *source, size_t length) +{ + ini->source = source; + ini->length = length; + ini->cursor = 0; + ini->line = 1; +} + +bool any_ini_eof(any_ini_t *ini) +{ + return ini->cursor >= ini->length; +} + char *any_ini_next_section(any_ini_t *ini) { - any_ini_skip_comment(ini); + any_ini_skip(ini); - if (!any_ini_more(ini) || ini->source[ini->cursor] != ANY_INI_SECTION_START) + if (any_ini_eof(ini) || ini->source[ini->cursor] != ANY_INI_SECTION_START) return NULL; size_t start = ++ini->cursor; - - while (any_ini_more(ini) && ini->source[ini->cursor] != '\n') + while (!any_ini_eof(ini) && ini->source[ini->cursor] != '\n') ini->cursor++; - size_t end = ini->cursor; - // NOTE: Does not handle the case where ANY_INI_SECTION_END is not found + size_t end = ini->cursor; while (end > start && ini->source[end] != ANY_INI_SECTION_END) end--; - size_t length = end - start; - - char *section = ANY_INI_MALLOC(length + 1); - memcpy(section, &ini->source[start], length); - section[length] = '\0'; - - return section; + size_t length = any_ini_trim(ini, start, end); + return any_ini_copy(ini->source + start, length); } char *any_ini_next_key(any_ini_t *ini) { - any_ini_skip_comment(ini); + any_ini_skip(ini); - if (!any_ini_more(ini) || any_ini_special(ini->source[ini->cursor])) + if (any_ini_eof(ini) || any_ini_special(ini->source[ini->cursor])) return NULL; size_t start = ini->cursor; - - while (any_ini_more(ini)) { + while (!any_ini_eof(ini)) { switch (ini->source[ini->cursor]) { case '\n': case ANY_INI_DELIM_COMMENT: @@ -156,27 +169,20 @@ char *any_ini_next_key(any_ini_t *ini) break; } - size_t end = ini->cursor; - size_t length = any_ini_trim(ini, start, end); - - char *key = ANY_INI_MALLOC(length + 1); - memcpy(key, &ini->source[start], length); - key[length] = '\0'; - - return key; + size_t length = any_ini_trim(ini, start, ini->cursor); + return any_ini_copy(ini->source + start, length); } char *any_ini_next_value(any_ini_t *ini) { - if (!any_ini_more(ini) || ini->source[ini->cursor] != ANY_INI_DELIM_PAIR) + if (any_ini_eof(ini) || ini->source[ini->cursor] != ANY_INI_DELIM_PAIR) return NULL; ++ini->cursor; - any_ini_skip_comment(ini); + any_ini_skip(ini); size_t start = ini->cursor; - - while (any_ini_more(ini)) { + while (!any_ini_eof(ini)) { switch (ini->source[ini->cursor]) { case '\n': case ANY_INI_DELIM_COMMENT: @@ -192,14 +198,8 @@ char *any_ini_next_value(any_ini_t *ini) break; } - size_t end = ini->cursor; - size_t length = any_ini_trim(ini, start, end); - - char *value = ANY_INI_MALLOC(length + 1); - memcpy(value, &ini->source[start], length); - value[length] = '\0'; - - return value; + size_t length = any_ini_trim(ini, start, ini->cursor); + return any_ini_copy(ini->source + start, length); } #endif @@ -2,36 +2,36 @@ #include <stdio.h> #define ANY_INI_IMPLEMENT +#define ANY_INI_DELIM_COMMENT2 '#' #include "any_ini.h" int main() { const char *src = - "ciao = 10\n" - "global = yes\n" - " complex name with space = value with space \n" - "\n[sus]\n" - "nice = 1\n" - ";comment\n" - "another=10;x\n" - "true=1 ;xx\n" - " # comment 2 ;\n" - "\ntry = catch 123 bool\n" - " k e y = value pair! ; comment\n"; + /* 1*/ "ciao = 10\n" + /* 2*/ "global = yes\n" + /* 3*/ " complex name with space = value with space \n\n" + /* 5*/ "[sus]\n" + /* 6*/ "nice = 1\n" + /* 7*/ ";comment\n\n" + /* 9*/ "another=10;x\n" + /*10*/ "true=1 ;xx\n" + /*11*/ " # comment 2 ;\n\n" + /*13*/ "try = catch 123 bool\n" + /*14*/ " k e y = value pair! ; comment\n"; any_ini_t ini; any_ini_init(&ini, src, strlen(src)); - char *section = "", *key, *value; - + char *section = ""; do { - printf("SECTION: %s\n", section); + printf("%ld: SECTION \"%s\"\n", ini.line, section); + char *key, *value; while ((key = any_ini_next_key(&ini)) != NULL) { value = any_ini_next_value(&ini); - printf("PAIR: `%s` = `%s`\n", key, value); + printf("%ld: \"%s\" = \"%s\"\n", ini.line, key, value); } - } while ((section = any_ini_next_section(&ini)) != NULL); return 0; |
