Skip to content
Snippets Groups Projects
Commit faa38a1d authored by James Willis's avatar James Willis
Browse files

Added duplicate name checking for standalone parameters and section names.

parent 9169e2c9
No related branches found
No related tags found
1 merge request!161Fix parameter file parsing for duplicate names
...@@ -49,7 +49,8 @@ static void parse_section_param(char *line, int *isFirstParam, ...@@ -49,7 +49,8 @@ static void parse_section_param(char *line, int *isFirstParam,
char *sectionName, struct swift_params *params); char *sectionName, struct swift_params *params);
static void find_duplicate_params(const struct swift_params *params, static void find_duplicate_params(const struct swift_params *params,
const char *param_name); const char *param_name);
static void find_duplicate_section(const struct swift_params *params,
const char *section_name);
static int lineNumber = 0; static int lineNumber = 0;
/** /**
...@@ -163,6 +164,23 @@ static void find_duplicate_params(const struct swift_params *params, ...@@ -163,6 +164,23 @@ static void find_duplicate_params(const struct swift_params *params,
} }
} }
/**
* @brief Look for duplicate sections.
*
* @param params Structure that holds the parameters
* @param param_name Name of section to be searched for
*/
static void find_duplicate_section(const struct swift_params *params,
const char *section_name) {
for (int i = 0; i < params->sectionCount; i++) {
/*strcmp returns 0 if both strings are the same.*/
if (!strcmp(section_name, params->section[i].name)) {
error("Invalid line:%d '%s', section is a duplicate.",
lineNumber, section_name);
}
}
}
/** /**
* @brief Parses a line from a file and stores any parameters in a structure. * @brief Parses a line from a file and stores any parameters in a structure.
* *
...@@ -215,6 +233,7 @@ static void parse_value(char *line, struct swift_params *params) { ...@@ -215,6 +233,7 @@ static void parse_value(char *line, struct swift_params *params) {
name. */ name. */
static int isFirstParam = 1; static int isFirstParam = 1;
char tmpStr[PARSER_MAX_LINE_SIZE]; char tmpStr[PARSER_MAX_LINE_SIZE];
char tmpSectionName[PARSER_MAX_LINE_SIZE];
char *token; char *token;
...@@ -245,25 +264,31 @@ static void parse_value(char *line, struct swift_params *params) { ...@@ -245,25 +264,31 @@ static void parse_value(char *line, struct swift_params *params) {
/* If second token is NULL then the line must be a section heading. */ /* If second token is NULL then the line must be a section heading. */
if (token == NULL) { if (token == NULL) {
strcat(tmpStr, PARSER_VALUE_STRING); strcpy(tmpSectionName, tmpStr);
strcat(tmpSectionName, PARSER_VALUE_STRING);
/* Check for duplicate section name. */ /* Check for duplicate section name. */
for (int i = 0; i < params->sectionCount; i++) { find_duplicate_section(params,tmpSectionName);
/*strcmp returns 0 if both strings are the same.*/
if (!strcmp(tmpStr, params->section[i].name)) { /* Check for duplicate standalone parameter name used as a section name. */
error("Invalid line:%d '%s', section is a duplicate.", find_duplicate_params(params,tmpStr);
lineNumber, line);
} strcpy(section, tmpSectionName);
} strcpy(params->section[params->sectionCount++].name, tmpSectionName);
strcpy(section, tmpStr);
strcpy(params->section[params->sectionCount++].name, tmpStr);
inSection = 1; inSection = 1;
isFirstParam = 1; isFirstParam = 1;
} else { } else {
/* Create string with standalone parameter name appended with ":" to aid
* duplicate search as section names are stored with ":" at the end.*/
strcpy(tmpSectionName, tmpStr);
strcat(tmpSectionName, PARSER_VALUE_STRING);
/* Check for duplicate parameter name. */ /* Check for duplicate parameter name. */
find_duplicate_params(params,tmpStr); find_duplicate_params(params,tmpStr);
/* Check for duplicate section name used as standalone parameter name. */
find_duplicate_section(params,tmpSectionName);
/* Must be a standalone parameter so no need to prefix name with a /* Must be a standalone parameter so no need to prefix name with a
* section. */ * section. */
strcpy(params->data[params->paramCount].name, tmpStr); strcpy(params->data[params->paramCount].name, tmpStr);
...@@ -531,5 +556,3 @@ void parser_write_params_to_file(const struct swift_params *params, ...@@ -531,5 +556,3 @@ void parser_write_params_to_file(const struct swift_params *params,
fclose(file); fclose(file);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment