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

Can parse basic file, store results to struct as strings and retrieve...

Can parse basic file, store results to struct as strings and retrieve parameters by parsing strings held with struct.
parent 17e95c91
No related branches found
No related tags found
1 merge request!129Parameter file
...@@ -22,9 +22,10 @@ ...@@ -22,9 +22,10 @@
/* Needs to be included so that strtok returns char * instead of a int *. */ /* Needs to be included so that strtok returns char * instead of a int *. */
#include <string.h> #include <string.h>
#include <stdlib.h>
#define MAX_LINE_SIZE 128 #define MAX_LINE_SIZE 128
#define MAX_NO_OF_PARAMS 4 #define MAX_NO_OF_PARAMS 512
struct parameter { struct parameter {
char name [MAX_LINE_SIZE]; char name [MAX_LINE_SIZE];
...@@ -33,17 +34,24 @@ struct parameter { ...@@ -33,17 +34,24 @@ struct parameter {
struct swift_params { struct swift_params {
struct parameter data [MAX_NO_OF_PARAMS]; struct parameter data [MAX_NO_OF_PARAMS];
int count;
}; };
/* Public API. */
void parseFile(const char *file_name, struct swift_params *params);
void printParameters(struct swift_params *params); void printParameters(struct swift_params *params);
void getParamInt(struct swift_params *params, char *name, int *retParam);
void getParamFloat(struct swift_params *params, char *name, float *retParam);
void getParamString(struct swift_params *params, char *name, char *retParam);
void getParam(struct swift_params *params, char * name, int * retParam); /* Private functions. */
static void readParameter(FILE *fp, struct swift_params *params);
void parseFile(struct swift_params *params, const char *file_name) { void parseFile(const char *file_name, struct swift_params *params) {
FILE *fp; FILE *fp;
char line[MAX_LINE_SIZE];
int param_count = 0; params->count = 0;
/* Open file for reading */ /* Open file for reading */
fp = fopen(file_name, "r"); fp = fopen(file_name, "r");
...@@ -54,44 +62,54 @@ void parseFile(struct swift_params *params, const char *file_name) { ...@@ -54,44 +62,54 @@ void parseFile(struct swift_params *params, const char *file_name) {
/* Read until the end of the file is reached.*/ /* Read until the end of the file is reached.*/
while(!feof(fp)) { while(!feof(fp)) {
readParameter(fp,params);
/* Read a line of the file */
if(fgets(line,MAX_LINE_SIZE,fp)!=NULL) {
/* Check if the line contains a value */
if(strchr(line,':')) {
char * token;
token = strtok(&(line[0]),":");
//while (token != NULL) {
strcpy(params->data[param_count].name,token);
token = strtok (NULL, ":");
strcpy(params->data[param_count++].value,token);
//}
}
}
} }
fclose(fp); fclose(fp);
} }
void getParam(struct swift_params *params, char * name, int * retParam) { void getParamInt(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<MAX_NO_OF_PARAMS; i++) {
if(strcmp(name,params->data[i].name)) {
/*strcmp returns 0 if both strings are the same.*/
if(!strcmp(name,params->data[i].name)) {
*retParam = atoi(params->data[i].value); *retParam = atoi(params->data[i].value);
return; return;
} }
} }
} }
void getParamFloat(struct swift_params *params, char * name, float * retParam) {
int i;
for(i=0; i<MAX_NO_OF_PARAMS; i++) {
/*strcmp returns 0 if both strings are the same.*/
if(!strcmp(name,params->data[i].name)) {
*retParam = atof(params->data[i].value);
return;
}
}
}
void getParamString(struct swift_params *params, char * name, char * retParam) {
int i;
for(i=0; i<MAX_NO_OF_PARAMS; i++) {
/*strcmp returns 0 if both strings are the same.*/
if(!strcmp(name,params->data[i].name)) {
strcpy(retParam, params->data[i].value);
return;
}
}
}
void printParameters(struct swift_params *params) { void printParameters(struct swift_params *params) {
int i; int i;
...@@ -100,11 +118,34 @@ void printParameters(struct swift_params *params) { ...@@ -100,11 +118,34 @@ void printParameters(struct swift_params *params) {
printf("SWIFT Parameter File\n"); printf("SWIFT Parameter File\n");
printf("--------------------\n"); printf("--------------------\n");
for(i=0; i<MAX_NO_OF_PARAMS; i++) { for(i=0; i<params->count; i++) {
printf("Name: %s\n",params->data[i].name); printf("Name: %s\n",params->data[i].name);
printf("Value: %s\n",params->data[i].value); printf("Value: %s\n",params->data[i].value);
} }
} }
static void readParameter(FILE *fp, struct swift_params *params) {
char line [MAX_LINE_SIZE];
/* Read a line of the file */
if(fgets(line,MAX_LINE_SIZE,fp) != "...") {
/* Check if the line contains a value */
if(strchr(line,':')) {
char * token;
/*Take first token as the parameter name. */
token = strtok(line,":");
strcpy(params->data[params->count].name,token);
/*Take second token as the parameter value. */
token = strtok (NULL, ":");
strcpy(params->data[params->count++].value,token);
}
}
}
#endif /* SWIFT_PARSER_H */ #endif /* SWIFT_PARSER_H */
...@@ -6,8 +6,20 @@ int main(int argc, char *argv[]) { ...@@ -6,8 +6,20 @@ int main(int argc, char *argv[]) {
const char * input_file = argv[1]; const char * input_file = argv[1];
struct swift_params param_file; struct swift_params param_file;
int no_of_threads = 0;
parseFile(&param_file,input_file); int no_of_time_steps = 0;
float max_h = 0.0f;
char ic_file [MAX_LINE_SIZE];
parseFile(input_file,&param_file);
printParameters(&param_file); printParameters(&param_file);
getParamInt(&param_file,"no_of_threads",&no_of_threads);
getParamInt(&param_file,"no_of_time_steps",&no_of_time_steps);
getParamFloat(&param_file,"max_h",&max_h);
getParamString(&param_file,"ic_file",ic_file);
printf("no_of_threads: %d, no_of_time_steps: %d, max_h: %f, ic_file: %s\n",no_of_threads, no_of_time_steps, max_h, ic_file);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment