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

Added get_double_parameter and added constants for special characters.

parent e4c497b7
No related branches found
No related tags found
1 merge request!129Parameter file
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "error.h" #include "error.h"
/* Reads an input file and stores each parameter in a structure.*/
void parser_read_file(const char *file_name, struct swift_params *params) { void parser_read_file(const char *file_name, struct swift_params *params) {
FILE *fp; FILE *fp;
...@@ -39,12 +40,13 @@ void parser_read_file(const char *file_name, struct swift_params *params) { ...@@ -39,12 +40,13 @@ void parser_read_file(const char *file_name, struct swift_params *params) {
/* Read until the end of the file is reached.*/ /* Read until the end of the file is reached.*/
while(!feof(fp)) { while(!feof(fp)) {
read_param(fp,params); parse_line(fp,params);
} }
fclose(fp); fclose(fp);
} }
/* Counts the number of characters in a string.*/
static int count_char(char *str, char val) { static int count_char(char *str, char val) {
int i, count = 0; int i, count = 0;
...@@ -59,7 +61,8 @@ static int count_char(char *str, char val) { ...@@ -59,7 +61,8 @@ static int count_char(char *str, char val) {
return count; return count;
} }
static void read_param(FILE *fp, struct swift_params *params) { /* Parses a line from a file and stores any parameters in a structure. */
static void parse_line(FILE *fp, struct swift_params *params) {
char line [MAX_LINE_SIZE]; char line [MAX_LINE_SIZE];
char trim_line [MAX_LINE_SIZE]; char trim_line [MAX_LINE_SIZE];
...@@ -69,21 +72,21 @@ static void read_param(FILE *fp, struct swift_params *params) { ...@@ -69,21 +72,21 @@ static void read_param(FILE *fp, struct swift_params *params) {
char *token; char *token;
/* Remove comments */ /* Remove comments */
token = strtok(line,COMMENT); token = strtok(line,COMMENT_CHAR);
strcpy(trim_line,token); strcpy(trim_line,token);
/* Check if the line contains a value */ /* Check if the line contains a value */
if(strchr(trim_line,':')) { if(strchr(trim_line,VALUE_CHAR)) {
/* Check for more than one parameter on the same line. */ /* Check for more than one parameter on the same line. */
if(count_char(trim_line,':') > 1) { if(count_char(trim_line,VALUE_CHAR) > 1) {
error("Found more than one parameter in '%s', only one allowed.",line); error("Found more than one parameter in '%s', only one allowed.",line);
} }
else { else {
/*Take first token as the parameter name. */ /* Take first token as the parameter name. */
token = strtok(trim_line,":"); token = strtok(trim_line,VALUE_STRING);
strcpy(params->data[params->count].name,token); strcpy(params->data[params->count].name,token);
/*Take second token as the parameter value. */ /* Take second token as the parameter value. */
token = strtok (NULL, " #\n"); token = strtok (NULL, " #\n");
strcpy(params->data[params->count++].value,token); strcpy(params->data[params->count++].value,token);
} }
...@@ -91,27 +94,28 @@ static void read_param(FILE *fp, struct swift_params *params) { ...@@ -91,27 +94,28 @@ static void read_param(FILE *fp, struct swift_params *params) {
} }
} }
/* Retrieve integer parameter from structure. */
void parser_get_param_int(struct swift_params *params, char * name, int * retParam) { void parser_get_param_int(struct swift_params *params, char * name, int * retParam) {
int i; int i;
for(i=0; i<MAX_NO_OF_PARAMS; i++) { for(i=0; i<params->count; i++) {
/*strcmp returns 0 if both strings are the same.*/ /*strcmp returns 0 if both strings are the same.*/
if(!strcmp(name,params->data[i].name)) { if(!strcmp(name,params->data[i].name)) {
/* Check if value is 0, to avoid integers of zero causing an error. */ /* Check if value is 0, to avoid integers of 0 causing an error in the following checks. */
if(!strcmp("0",params->data[i].value)) { if(!strcmp("0",params->data[i].value)) {
*retParam = 0; *retParam = 0;
} }
/* Check if the value is a string. */ /* Check if the value is a string. */
else if(!atoi(params->data[i].value)) { else if(!atoi(params->data[i].value)) {
error("Tried to parse '%s', when expecting an integer.",params->data[i].value); error("Tried to parse '%s' but found '%s', when expecting an integer.",params->data[i].name,params->data[i].value);
} }
/* Check if the value is a float.*/ /* Check if the value is a float.*/
else if( strchr(params->data[i].value,'.') || strchr(params->data[i].value,'e')|| else if( strchr(params->data[i].value,'.') || strchr(params->data[i].value,'e') ||
strchr(params->data[i].value,'E') ) { strchr(params->data[i].value,'E') ) {
error("Tried to parse '%s', when expecting an integer.",params->data[i].value); error("Tried to parse '%s' but found '%s', when expecting an integer.",params->data[i].name,params->data[i].value);
} }
else { else {
*retParam = atoi(params->data[i].value); *retParam = atoi(params->data[i].value);
...@@ -122,11 +126,12 @@ void parser_get_param_int(struct swift_params *params, char * name, int * retPar ...@@ -122,11 +126,12 @@ void parser_get_param_int(struct swift_params *params, char * name, int * retPar
} }
} }
/* Retrieve float parameter from structure. */
void parser_get_param_float(struct swift_params *params, char * name, float * retParam) { void parser_get_param_float(struct swift_params *params, char * name, float * retParam) {
int i; int i;
for(i=0; i<MAX_NO_OF_PARAMS; i++) { for(i=0; i<params->count; i++) {
/*strcmp returns 0 if both strings are the same.*/ /*strcmp returns 0 if both strings are the same.*/
if(!strcmp(name,params->data[i].name)) { if(!strcmp(name,params->data[i].name)) {
...@@ -136,11 +141,27 @@ void parser_get_param_float(struct swift_params *params, char * name, float * re ...@@ -136,11 +141,27 @@ void parser_get_param_float(struct swift_params *params, char * name, float * re
} }
} }
/* Retrieve double parameter from structure. */
void parser_get_param_double(struct swift_params *params, char * name, double * retParam) {
int i;
for(i=0; i<params->count; i++) {
/*strcmp returns 0 if both strings are the same.*/
if(!strcmp(name,params->data[i].name)) {
*retParam = atof(params->data[i].value);
return;
}
}
}
/* Retrieve string parameter from structure. */
void parser_get_param_string(struct swift_params *params, char * name, char * retParam) { void parser_get_param_string(struct swift_params *params, char * name, char * retParam) {
int i; int i;
for(i=0; i<MAX_NO_OF_PARAMS; i++) { for(i=0; i<params->count; i++) {
/*strcmp returns 0 if both strings are the same.*/ /*strcmp returns 0 if both strings are the same.*/
if(!strcmp(name,params->data[i].name)) { if(!strcmp(name,params->data[i].name)) {
...@@ -150,17 +171,18 @@ void parser_get_param_string(struct swift_params *params, char * name, char * re ...@@ -150,17 +171,18 @@ void parser_get_param_string(struct swift_params *params, char * name, char * re
} }
} }
/* Prints the contents of the parameter structure. */
void parser_print_params(struct swift_params *params) { void parser_print_params(struct swift_params *params) {
int i; int i;
printf("\n--------------------\n"); printf("\n--------------------------\n");
printf("SWIFT Parameter File\n"); printf("| SWIFT Parameter File |\n");
printf("--------------------\n"); printf("--------------------------\n");
for(i=0; i<params->count; i++) { for(i=0; i<params->count; i++) {
printf("Name: %s\n",params->data[i].name); printf("Parameter name: %s\n",params->data[i].name);
printf("Value: %s\n",params->data[i].value); printf("Parameter value: %s\n",params->data[i].value);
} }
} }
...@@ -25,8 +25,9 @@ ...@@ -25,8 +25,9 @@
#define MAX_LINE_SIZE 128 #define MAX_LINE_SIZE 128
#define MAX_NO_OF_PARAMS 512 #define MAX_NO_OF_PARAMS 512
#define COMMENT "#" #define COMMENT_CHAR "#"
#define VALUE ':' #define VALUE_CHAR ':'
#define VALUE_STRING ":"
#define END_OF_FILE "..." #define END_OF_FILE "..."
struct parameter { struct parameter {
...@@ -44,10 +45,11 @@ void parser_read_file(const char *file_name, struct swift_params *params); ...@@ -44,10 +45,11 @@ void parser_read_file(const char *file_name, struct swift_params *params);
void parser_print_params(struct swift_params *params); void parser_print_params(struct swift_params *params);
void parser_get_param_int(struct swift_params *params, char *name, int *retParam); void parser_get_param_int(struct swift_params *params, char *name, int *retParam);
void parser_get_param_float(struct swift_params *params, char *name, float *retParam); void parser_get_param_float(struct swift_params *params, char *name, float *retParam);
void parser_get_param_double(struct swift_params *params, char * name, double * retParam);
void parser_get_param_string(struct swift_params *params, char *name, char *retParam); void parser_get_param_string(struct swift_params *params, char *name, char *retParam);
/* Private functions. */ /* Private functions. */
static void read_param(FILE *fp, struct swift_params *params); static void parse_line(FILE *fp, struct swift_params *params);
static int count_char(char *str, char val); static int count_char(char *str, char val);
#endif /* SWIFT_PARSER_H */ #endif /* SWIFT_PARSER_H */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment