fix wrong handle of commented section in configuration file.

- fix parssing of commented section like the line:
  #[section_name]
 or
  ![section_name]
- add few comments
- improve redability
This commit is contained in:
Benoit Gschwind 2015-08-19 23:02:50 +02:00
parent 4430119d40
commit c9762fa3a7

View File

@ -26,6 +26,36 @@
#define FILE_MAX_LINE_BYTES 2048 #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 error
returns 0 if everything is ok 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; struct stream *s;
char text[FILE_MAX_LINE_BYTES]; char text[FILE_MAX_LINE_BYTES];
char c; char c;
int in_it;
int in_it_index;
int len; int len;
int index;
int rv; int rv;
int line_length;
char * section_name;
rv = 0; rv = 0;
g_file_seek(fd, 0); g_file_seek(fd, 0);
in_it_index = 0;
in_it = 0;
g_memset(text, 0, FILE_MAX_LINE_BYTES); g_memset(text, 0, FILE_MAX_LINE_BYTES);
list_clear(names); list_clear(names);
make_stream(s); make_stream(s);
@ -55,26 +82,9 @@ l_file_read_sections(int fd, int max_file_size, struct list *names)
if (len > 0) if (len > 0)
{ {
s->end = s->p + len; s->end = s->p + len;
while(file_read_ini_line(s, text, FILE_MAX_LINE_BYTES) == 0) {
for (index = 0; index < len; index++) if(line_lookup_for_section_name(text) != 0) {
{
in_uint8(s, c);
if (c == '[')
{
in_it = 1;
}
else if (c == ']')
{
list_add_item(names, (tbus)g_strdup(text)); 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 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 i;
int skip_to_end; int skip_to_end;
int at_end; int at_end;
char c; char c;
char *hold;
skip_to_end = 0; skip_to_end = 0;
@ -105,7 +117,6 @@ file_read_line(struct stream *s, char *text, int text_bytes)
return 1; return 1;
} }
hold = s->p;
i = 0; i = 0;
in_uint8(s, c); in_uint8(s, c);
@ -163,15 +174,10 @@ file_read_line(struct stream *s, char *text, int text_bytes)
text[i] = 0; text[i] = 0;
if (text[0] == '[')
{
s->p = hold;
return 1;
}
return 0; return 0;
} }
/*****************************************************************************/ /*****************************************************************************/
/* returns error */ /* returns error */
static int APP_CC static int APP_CC
@ -253,36 +259,16 @@ l_file_read_section(int fd, int max_file_size, const char *section,
if (len > 0) if (len > 0)
{ {
s->end = s->p + len; s->end = s->p + len;
while(file_read_ini_line(s, text, FILE_MAX_LINE_BYTES) == 0) {
for (index = 0; index < len; index++) if(line_lookup_for_section_name(text) != 0) {
{
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 == ']')
{
if (g_strcasecmp(section, text) == 0) if (g_strcasecmp(section, text) == 0)
{ {
file_read_line(s, text, FILE_MAX_LINE_BYTES); while (file_read_ini_line(s, text, FILE_MAX_LINE_BYTES) == 0)
while (file_read_line(s, text, FILE_MAX_LINE_BYTES) == 0)
{ {
if(line_lookup_for_section_name(text) != 0) {
break;
}
if (g_strlen(text) > 0) if (g_strlen(text) > 0)
{ {
file_split_name_value(text, name, value); 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); free_stream(s);
g_free(data); g_free(data);
return 0; 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); free_stream(s);
g_free(data); g_free(data);
return 1; return 1;