summaryrefslogtreecommitdiff
path: root/src/shared/dlt_config_file_parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/dlt_config_file_parser.c')
-rw-r--r--src/shared/dlt_config_file_parser.c170
1 files changed, 78 insertions, 92 deletions
diff --git a/src/shared/dlt_config_file_parser.c b/src/shared/dlt_config_file_parser.c
index 802d3ed..8c882e2 100644
--- a/src/shared/dlt_config_file_parser.c
+++ b/src/shared/dlt_config_file_parser.c
@@ -58,12 +58,13 @@ static void dlt_config_file_trim_line(char *line)
char *i = line;
char *j = line;
- while(*j != '\0')
- {
+ while (*j != '\0') {
*i = *j++;
- if(!isspace(*i))
+
+ if (!isspace(*i))
i++;
}
+
*i = '\0';
}
@@ -80,12 +81,12 @@ static int dlt_config_file_ignore_line(char *line)
int i = 0;
int len = strlen(line);
- for (i = 0; i < len; i++)
- {
- if (line[i] == '#' || line[i] == ';' || line[i] == '\n' || line[i] == '\0')
+ for (i = 0; i < len; i++) {
+ if ((line[i] == '#') || (line[i] == ';') || (line[i] == '\n') || (line[i] == '\0'))
return 0; /* ignore */
else
return -1; /* do not ignore */
+
}
return -1;
@@ -104,11 +105,10 @@ static int dlt_config_file_is_section_name(DltConfigFile *file, char *name)
{
int i = 0;
- if (file == NULL || name == NULL)
+ if ((file == NULL) || (name == NULL))
return -1;
- for (i = 0; i < file->num_sections; i++)
- {
+ for (i = 0; i < file->num_sections; i++) {
DltConfigFileSection *s = &file->sections[i];
if (strncmp(s->name, name, DLT_CONFIG_FILE_ENTRY_MAX_LEN) == 0)
@@ -130,16 +130,15 @@ static int dlt_config_file_is_section_name(DltConfigFile *file, char *name)
static int dlt_config_file_set_section(DltConfigFile *file, char *name)
{
int section = file->num_sections;
+
/* check if adding another section would exceed max number of sections */
- if (section+1 >= DLT_CONFIG_FILE_SECTIONS_MAX)
- {
+ if (section + 1 >= DLT_CONFIG_FILE_SECTIONS_MAX) {
dlt_log(LOG_WARNING, "Cannot store more sections\n");
return -1; /* reached max number of sections */
}
/* do not store section with same name again */
- if (dlt_config_file_is_section_name(file, name) != 0)
- {
+ if (dlt_config_file_is_section_name(file, name) != 0) {
dlt_log(LOG_WARNING, "Cannot store section name again\n");
return -1;
}
@@ -148,15 +147,15 @@ static int dlt_config_file_set_section(DltConfigFile *file, char *name)
/* alloc data for entries */
s->name = calloc(sizeof(char), DLT_CONFIG_FILE_ENTRY_MAX_LEN + 1);
- if (s->name == NULL)
- {
+
+ if (s->name == NULL) {
dlt_log(LOG_ERR, "Cannot allocate memory for internal data structure\n");
return -1;
}
s->keys = calloc(sizeof(char), DLT_CONFIG_FILE_ENTRY_MAX_LEN * DLT_CONFIG_FILE_KEYS_MAX + 1);
- if (s->keys == NULL)
- {
+
+ if (s->keys == NULL) {
free(s->name);
dlt_log(LOG_ERR, "Cannot allocate memory for internal data structure\n");
return -1;
@@ -181,14 +180,13 @@ static int dlt_config_file_set_section_data(DltConfigFile *file, char *str1, cha
{
DltConfigKeyData **tmp = NULL;
- if (file == NULL || str1 == NULL || str2 == NULL)
+ if ((file == NULL) || (str1 == NULL) || (str2 == NULL))
return -1;
- DltConfigFileSection *s = &file->sections[file->num_sections-1];
+ DltConfigFileSection *s = &file->sections[file->num_sections - 1];
int key_number = s->num_entries;
- if (key_number+1 >= DLT_CONFIG_FILE_KEYS_MAX)
- {
+ if (key_number + 1 >= DLT_CONFIG_FILE_KEYS_MAX) {
dlt_log(LOG_WARNING, "Cannot store more keys in section\n");
return -1; /* reached max number of keys per section */
}
@@ -196,33 +194,32 @@ static int dlt_config_file_set_section_data(DltConfigFile *file, char *str1, cha
/* copy data into structure */
strncpy(&s->keys[key_number * DLT_CONFIG_FILE_ENTRY_MAX_LEN], str1, DLT_CONFIG_FILE_ENTRY_MAX_LEN);
- if (s->list == NULL)
- {
+ if (s->list == NULL) {
/* creating a list if it doesnt exists */
s->list = malloc(sizeof(DltConfigKeyData));
- if (s->list == NULL)
- {
+
+ if (s->list == NULL) {
dlt_log(LOG_WARNING, "Could not allocate initial memory to list \n");
return -1;
}
+
tmp = &s->list;
}
- else
- {
+ else {
tmp = &s->list;
while (*(tmp) != NULL)
- {
tmp = &(*tmp)->next;
- }
+
/* Adding new entry to the list */
*tmp = malloc(sizeof(DltConfigKeyData));
- if (*tmp == NULL)
- {
+
+ if (*tmp == NULL) {
dlt_log(LOG_WARNING, "Could not allocate memory to list \n");
return -1;
}
}
+
(*tmp)->key = strdup(str1);
(*tmp)->data = strdup(str2);
(*tmp)->next = NULL;
@@ -243,7 +240,8 @@ static int dlt_config_file_set_section_data(DltConfigFile *file, char *str1, cha
static int dlt_config_file_line_has_section(char *line)
{
line = line; /* avoid compiler warnings */
- if(line[0] == '[') /* section found */
+
+ if (line[0] == '[') /* section found */
return 0;
else
return -1;
@@ -262,14 +260,13 @@ static int dlt_config_file_get_section_name_from_string(char *line, char *name)
int i = 0;
int j = 0;
- if (line == NULL || name == NULL)
+ if ((line == NULL) || (name == NULL))
return -1;
- for(i = 0; i < DLT_CONFIG_FILE_ENTRY_MAX_LEN; i++)
- {
- if(line[i] == '[' || isspace(line[i]))
+ for (i = 0; i < DLT_CONFIG_FILE_ENTRY_MAX_LEN; i++) {
+ if ((line[i] == '[') || isspace(line[i]))
continue;
- else if (line[i] == ']' || line[i] == '\n' || line[i] == '\0')
+ else if ((line[i] == ']') || (line[i] == '\n') || (line[i] == '\0'))
break;
else
name[j++] = line[i];
@@ -294,7 +291,7 @@ static int dlt_config_file_get_key_value(char *line, char *str1, char *str2)
char *ptr;
char *save_ptr;
- if (line == NULL || str1 == NULL || str2 == NULL)
+ if ((line == NULL) || (str1 == NULL) || (str2 == NULL))
return -1;
ptr = strtok_r(line, delimiter, &save_ptr);
@@ -326,7 +323,7 @@ static int dlt_config_file_get_key_value(char *line, char *str1, char *str2)
*/
static int dlt_config_file_read_line(char *line, char *str1, char *str2)
{
- if (line == NULL || str1 == NULL || str2 == NULL)
+ if ((line == NULL) || (str1 == NULL) || (str2 == NULL))
return -1;
/* reset values to zero */
@@ -334,8 +331,7 @@ static int dlt_config_file_read_line(char *line, char *str1, char *str2)
memset(str2, 0, DLT_CONFIG_FILE_ENTRY_MAX_LEN);
/* check if line contains a section */
- if ((dlt_config_file_line_has_section(line)) == 0)
- {
+ if ((dlt_config_file_line_has_section(line)) == 0) {
/* retrieve section name */
if (dlt_config_file_get_section_name_from_string(line, str1) != 0)
return -1;
@@ -361,16 +357,15 @@ static int dlt_config_file_read_line(char *line, char *str1, char *str2)
static void dlt_config_file_read_file(DltConfigFile *file, FILE *hdl)
{
int ret = 0;
- char error_str[DLT_DAEMON_TEXTBUFSIZE] = {'\0'};
- char line[DLT_CONFIG_FILE_LINE_MAX_LEN] = {'\0'};
- char str1[DLT_CONFIG_FILE_ENTRY_MAX_LEN] = {'\0'};
- char str2[DLT_CONFIG_FILE_ENTRY_MAX_LEN] = {'\0'};
+ char error_str[DLT_DAEMON_TEXTBUFSIZE] = { '\0' };
+ char line[DLT_CONFIG_FILE_LINE_MAX_LEN] = { '\0' };
+ char str1[DLT_CONFIG_FILE_ENTRY_MAX_LEN] = { '\0' };
+ char str2[DLT_CONFIG_FILE_ENTRY_MAX_LEN] = { '\0' };
int line_number = 0;
int is_section_valid = -1; /* to check if section name is given twice or invalid */
/* read configuration file line by line */
- while (fgets(line, DLT_CONFIG_FILE_LINE_MAX_LEN, hdl) != NULL)
- {
+ while (fgets(line, DLT_CONFIG_FILE_LINE_MAX_LEN, hdl) != NULL) {
line_number++;
/* ignore empty and comment lines */
@@ -383,24 +378,23 @@ static void dlt_config_file_read_file(DltConfigFile *file, FILE *hdl)
/* parse content of line */
ret = dlt_config_file_read_line(line, str1, str2);
- switch(ret)
- {
- case DLT_CONFIG_FILE_NEW_SECTION: /* store str1 as new section */
- is_section_valid = -1;
- if ((ret = dlt_config_file_set_section(file, str1)) == 0)
- {
- is_section_valid = 0;
- }
- break;
- case DLT_CONFIG_FILE_NEW_DATA: /* store str1 and str2 as new data for section */
- if (is_section_valid == 0)
- {
- ret = dlt_config_file_set_section_data(file, str1, str2);
- }
- break;
- default: /* something is wrong with the line */
- snprintf(error_str, DLT_DAEMON_TEXTBUFSIZE, "Line (%d) \"%s\" is invalid\n", line_number, line);
- dlt_log(LOG_WARNING, error_str);
+ switch (ret) {
+ case DLT_CONFIG_FILE_NEW_SECTION: /* store str1 as new section */
+ is_section_valid = -1;
+
+ if ((ret = dlt_config_file_set_section(file, str1)) == 0)
+ is_section_valid = 0;
+
+ break;
+ case DLT_CONFIG_FILE_NEW_DATA: /* store str1 and str2 as new data for section */
+
+ if (is_section_valid == 0)
+ ret = dlt_config_file_set_section_data(file, str1, str2);
+
+ break;
+ default: /* something is wrong with the line */
+ snprintf(error_str, DLT_DAEMON_TEXTBUFSIZE, "Line (%d) \"%s\" is invalid\n", line_number, line);
+ dlt_log(LOG_WARNING, error_str);
}
}
}
@@ -419,15 +413,14 @@ static int dlt_config_file_find_section(const DltConfigFile *file,
{
int i = 0;
- if (file == NULL || section == NULL || file->num_sections <= 0)
- {
+ if ((file == NULL) || (section == NULL) || (file->num_sections <= 0)) {
dlt_log(LOG_WARNING, "Section cannot be found due to invalid parameters\n");
return -1;
}
- for (i = 0; i < file->num_sections; i++)
- {
+ for (i = 0; i < file->num_sections; i++) {
DltConfigFileSection *s = &file->sections[i];
+
if (strncmp(s->name, section, DLT_CONFIG_FILE_ENTRY_MAX_LEN) == 0)
return i;
}
@@ -441,15 +434,14 @@ DltConfigFile *dlt_config_file_init(char *file_name)
DltConfigFile *file;
FILE *hdl = NULL;
- if (file_name == NULL || (strlen(file_name) >= DLT_CONFIG_FILE_PATH_MAX_LEN))
- {
+ if ((file_name == NULL) || (strlen(file_name) >= DLT_CONFIG_FILE_PATH_MAX_LEN)) {
dlt_log(LOG_ERR, "Given configuration file invalid\n");
return NULL;
}
file = calloc(sizeof(DltConfigFile), 1);
- if (file == NULL)
- {
+
+ if (file == NULL) {
dlt_log(LOG_ERR, "Setup internal data structure to parse config file failed\n");
return NULL;
}
@@ -457,8 +449,7 @@ DltConfigFile *dlt_config_file_init(char *file_name)
file->sections = calloc(sizeof(DltConfigFileSection), DLT_CONFIG_FILE_SECTIONS_MAX);
/* open file */
- if ((hdl = fopen(file_name, "r")) == NULL)
- {
+ if ((hdl = fopen(file_name, "r")) == NULL) {
dlt_log(LOG_ERR, "Cannot open configuration file\n");
free(file);
return NULL;
@@ -476,21 +467,18 @@ void dlt_config_file_release(DltConfigFile *file)
{
int i = 0;
- if(file != NULL)
- {
+ if (file != NULL) {
int max = file->num_sections;
- for (i = 0; i < max; i++)
- {
+
+ for (i = 0; i < max; i++) {
DltConfigFileSection *s = &file->sections[i];
DltConfigKeyData *node = file->sections[i].list;
free(s->name);
if (s->keys != NULL)
- {
free(s->keys);
- }
- while (node)
- {
+
+ while (node) {
DltConfigKeyData *tmp = node;
node = node->next;
free(tmp->key);
@@ -508,7 +496,7 @@ int dlt_config_file_get_section_name(const DltConfigFile *file,
int num,
char *name)
{
- if (file == NULL || name == NULL || num < 0 || num >= file->num_sections)
+ if ((file == NULL) || (name == NULL) || (num < 0) || (num >= file->num_sections))
return -1;
strncpy(name, (file->sections + num)->name, DLT_CONFIG_FILE_ENTRY_MAX_LEN);
@@ -518,7 +506,7 @@ int dlt_config_file_get_section_name(const DltConfigFile *file,
int dlt_config_file_get_num_sections(const DltConfigFile *file, int *num)
{
- if(file == NULL || file->num_sections < 0)
+ if ((file == NULL) || (file->num_sections < 0))
return -1;
*num = file->num_sections;
@@ -534,13 +522,14 @@ int dlt_config_file_get_value(const DltConfigFile *file,
DltConfigKeyData **tmp = NULL;
int num_section = 0;
- if (file == NULL || section == NULL || key == NULL || value == NULL)
+ if ((file == NULL) || (section == NULL) || (key == NULL) || (value == NULL))
return -1;
/* clean value */
memset(value, 0, DLT_CONFIG_FILE_ENTRY_MAX_LEN);
num_section = dlt_config_file_find_section(file, section);
+
if (num_section == -1)
return -1;
@@ -548,15 +537,12 @@ int dlt_config_file_get_value(const DltConfigFile *file,
tmp = &s->list;
- while (*(tmp) != NULL)
- {
- if (strncmp((*tmp)->key, key, DLT_CONFIG_FILE_ENTRY_MAX_LEN) == 0)
- {
+ while (*(tmp) != NULL) {
+ if (strncmp((*tmp)->key, key, DLT_CONFIG_FILE_ENTRY_MAX_LEN) == 0) {
strncpy(value, (*tmp)->data, DLT_CONFIG_FILE_ENTRY_MAX_LEN);
return 0;
}
- else /* not found yet see list for more */
- {
+ else { /* not found yet see list for more */
tmp = &(*tmp)->next;
}
}