diff options
-rw-r--r-- | common/file.c | 123 |
1 files changed, 48 insertions, 75 deletions
diff --git a/common/file.c b/common/file.c index 9fd14c29..09a8d57f 100644 --- a/common/file.c +++ b/common/file.c @@ -26,6 +26,36 @@ #define FILE_MAX_LINE_BYTES 2048 +static int APP_CC +file_read_ini_line(struct stream *s, char *text, int text_bytes); + +/* look up for a section name within str (i.e. pattern [section_name]) + * if a section name is found, this function return 1 and copy the section + * inplace of str. */ +static int APP_CC +line_lookup_for_section_name(char * str) { + int name_index_start; + int index; + char c; + name_index_start = -1; + index = 0; + while((c = str[index]) != 0) { + if (c == '[') + { + name_index_start = index+1; + } + else if (c == ']' && name_index_start > 0) + { + g_memcpy(str, &str[name_index_start], index - name_index_start); + str[index - name_index_start] = 0; + return 1; + } + ++index; + } + return 0; +} + + /*****************************************************************************/ /* returns error returns 0 if everything is ok @@ -36,16 +66,13 @@ l_file_read_sections(int fd, int max_file_size, struct list *names) struct stream *s; char text[FILE_MAX_LINE_BYTES]; char c; - int in_it; - int in_it_index; int len; - int index; int rv; + int line_length; + char * section_name; rv = 0; g_file_seek(fd, 0); - in_it_index = 0; - in_it = 0; g_memset(text, 0, FILE_MAX_LINE_BYTES); list_clear(names); make_stream(s); @@ -55,26 +82,9 @@ l_file_read_sections(int fd, int max_file_size, struct list *names) if (len > 0) { s->end = s->p + len; - - for (index = 0; index < len; index++) - { - in_uint8(s, c); - - if (c == '[') - { - in_it = 1; - } - else if (c == ']') - { + while(file_read_ini_line(s, text, FILE_MAX_LINE_BYTES) == 0) { + if(line_lookup_for_section_name(text) != 0) { list_add_item(names, (tbus)g_strdup(text)); - in_it = 0; - in_it_index = 0; - g_memset(text, 0, FILE_MAX_LINE_BYTES); - } - else if (in_it) - { - text[in_it_index] = c; - in_it_index++; } } } @@ -88,15 +98,17 @@ l_file_read_sections(int fd, int max_file_size, struct list *names) } /*****************************************************************************/ -/* returns error */ +/* Read a line in the stream 's', removing comments. + * returns error + * returns 0 if everything is ok + * returns 1 if problem reading file */ static int APP_CC -file_read_line(struct stream *s, char *text, int text_bytes) +file_read_ini_line(struct stream *s, char *text, int text_bytes) { int i; int skip_to_end; int at_end; char c; - char *hold; skip_to_end = 0; @@ -105,7 +117,6 @@ file_read_line(struct stream *s, char *text, int text_bytes) return 1; } - hold = s->p; i = 0; in_uint8(s, c); @@ -163,15 +174,10 @@ file_read_line(struct stream *s, char *text, int text_bytes) text[i] = 0; - if (text[0] == '[') - { - s->p = hold; - return 1; - } - return 0; } + /*****************************************************************************/ /* returns error */ static int APP_CC @@ -253,36 +259,16 @@ l_file_read_section(int fd, int max_file_size, const char *section, if (len > 0) { s->end = s->p + len; - - for (index = 0; index < len; index++) - { - if (!s_check_rem(s, 1)) - { - break; - } - in_uint8(s, c); - if ((c == '#') || (c == ';')) - { - if (file_read_line(s, text, FILE_MAX_LINE_BYTES) != 0) - { - break; - } - in_it = 0; - in_it_index = 0; - g_memset(text, 0, FILE_MAX_LINE_BYTES); - continue; - } - if (c == '[') - { - in_it = 1; - } - else if (c == ']') - { + while(file_read_ini_line(s, text, FILE_MAX_LINE_BYTES) == 0) { + if(line_lookup_for_section_name(text) != 0) { if (g_strcasecmp(section, text) == 0) { - file_read_line(s, text, FILE_MAX_LINE_BYTES); - while (file_read_line(s, text, FILE_MAX_LINE_BYTES) == 0) + while (file_read_ini_line(s, text, FILE_MAX_LINE_BYTES) == 0) { + if(line_lookup_for_section_name(text) != 0) { + break; + } + if (g_strlen(text) > 0) { file_split_name_value(text, name, value); @@ -307,27 +293,14 @@ l_file_read_section(int fd, int max_file_size, const char *section, } } } - free_stream(s); g_free(data); return 0; } - - in_it = 0; - in_it_index = 0; - g_memset(text, 0, FILE_MAX_LINE_BYTES); - } - else if (in_it) - { - text[in_it_index] = c; - in_it_index++; - if (in_it_index >= FILE_MAX_LINE_BYTES) - { - break; - } } } } + free_stream(s); g_free(data); return 1; |