Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SWIFTsim
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SWIFT
SWIFTsim
Commits
41325a2c
Commit
41325a2c
authored
9 years ago
by
James Willis
Browse files
Options
Downloads
Patches
Plain Diff
Read simple file into struct of strings.
parent
202a4abe
No related branches found
No related tags found
1 merge request
!129
Parameter file
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/parser.h
+51
-69
51 additions, 69 deletions
src/parser.h
src/testParser.c
+4
-4
4 additions, 4 deletions
src/testParser.c
with
55 additions
and
73 deletions
src/parser.h
+
51
−
69
View file @
41325a2c
...
...
@@ -20,21 +20,30 @@
#ifndef SWIFT_PARSER_H
#define SWIFT_PARSER_H
/* Needs to be included so that strtok returns char * instead of a int *. */
#include
<string.h>
#define MAX_LINE_SIZE 128
#define MAX_NO_OF_PARAMS 4
struct
model_params
{
//char *names[] = {"no_of_threads", "no_of_time_steps", "max_h", "ic_file"};
struct
parameter
{
char
name
[
MAX_LINE_SIZE
];
char
value
[
MAX_LINE_SIZE
];
};
int
no_of_threads
;
int
no_of_time_steps
;
float
max_h
;
char
*
ic_file
;
struct
swift_params
{
struct
parameter
data
[
MAX_NO_OF_PARAMS
];
};
void
parseFile
(
struct
model_params
*
params
,
const
char
*
file_name
)
{
void
printParameters
(
struct
swift_params
*
params
);
void
getParam
(
struct
swift_params
*
params
,
char
*
name
,
int
*
retParam
);
void
parseFile
(
struct
swift_params
*
params
,
const
char
*
file_name
)
{
FILE
*
fp
;
char
line
[
MAX_LINE_SIZE
];
int
param_count
=
0
;
/* Open file for reading */
fp
=
fopen
(
file_name
,
"r"
);
...
...
@@ -47,82 +56,55 @@ void parseFile(struct model_params *params, const char * file_name) {
while
(
!
feof
(
fp
))
{
/* Read a line of the file */
if
(
fgets
(
line
,
MAX_LINE_SIZE
,
fp
)
!=
NULL
)
{
if
(
fgets
(
line
,
MAX_LINE_SIZE
,
fp
)
!=
NULL
)
{
/* Check if the line contains a value */
if
(
strchr
(
line
,
':'
))
{
//int size = atoi(line);
//int i;
//char *p;
//char *elements[size];
//for(i=0; i<8; i++) elements[i] = (char *)malloc(MAX_LINE_SIZE);
//
////for(i=0; i<size; i++) {
// fgets(line,MAX_LINE_SIZE,fp);
// strtok(line,":");
// strcpy(elements[0],line);
// p = (char *)strtok(NULL,":");
// strcpy(elements[1],p);
////}
int
i
;
char
*
token
;
char
*
elements
[
8
];
for
(
i
=
0
;
i
<
8
;
i
++
)
elements
[
i
]
=
(
char
*
)
malloc
(
MAX_LINE_SIZE
);
int
element_count
=
0
;
printf
(
"Splitting string: '%s' into tokens:
\n
"
,
line
);
token
=
(
char
*
)
strtok
(
line
,
":"
);
printf
(
"Token:%s
\n
"
,
token
);
token
=
strtok
(
&
(
line
[
0
]),
":"
);
while
(
token
!=
NULL
)
{
strcpy
(
elements
[
element_count
],
token
);
printf
(
"Element:%s
\n
"
,
elements
[
element_count
]);
token
=
(
char
*
)
strtok
(
NULL
,
":"
);
element_count
++
;
}
//if(strcmp("no_of_threads",elements[0])) {
// params->no_of_threads = atoi(elements[1]);
//}
//else if(strcmp("no_of_time_steps",elements[0])) {
// params->no_of_time_steps = atoi(elements[1]);
//}
//else if(strcmp("max_h",elements[0])) {
// params->max_h = atof(elements[1]);
//}
//else if(strcmp("ic_file",elements[0])) {
// params->ic_file = elements[1];
//}
//for(i=0; i<element_count; i++)
// printf("Element[%d]: %s\n",i,elements[i]);
//while (token != NULL) {
strcpy
(
params
->
data
[
param_count
].
name
,
token
);
token
=
strtok
(
NULL
,
":"
);
strcpy
(
params
->
data
[
param_count
++
].
value
,
token
);
//}
//float fp_number;
//char * str;
//fscanf(fp, "%f %s", &fp_number,str);
//
///* writing content to stdout */
//printf("%s",line);
////printf("Float: %f, comment: %s\n",fp_number,str);
}
}
}
}
printf
(
"no_of_threads:%d, no_of_time_steps:%d, max_h:%f, ic_file:%s
\n
"
,
params
->
no_of_threads
,
params
->
no_of_time_steps
,
params
->
max_h
,
params
->
ic_file
);
fclose
(
fp
);
}
void
storeParam
()
{
void
getParam
(
struct
swift_params
*
params
,
char
*
name
,
int
*
retParam
)
{
int
i
;
for
(
i
=
0
;
i
<
MAX_NO_OF_PARAMS
;
i
++
)
{
if
(
strcmp
(
name
,
params
->
data
[
i
].
name
))
{
*
retParam
=
atoi
(
params
->
data
[
i
].
value
);
return
;
}
}
}
void
printParameters
(
struct
swift_params
*
params
)
{
int
i
;
printf
(
"
\n
--------------------
\n
"
);
printf
(
"SWIFT Parameter File
\n
"
);
printf
(
"--------------------
\n
"
);
for
(
i
=
0
;
i
<
MAX_NO_OF_PARAMS
;
i
++
)
{
printf
(
"Name: %s
\n
"
,
params
->
data
[
i
].
name
);
printf
(
"Value: %s
\n
"
,
params
->
data
[
i
].
value
);
}
}
#endif
/* SWIFT_
VECTO
R_H */
#endif
/* SWIFT_
PARSE
R_H */
This diff is collapsed.
Click to expand it.
src/testParser.c
+
4
−
4
View file @
41325a2c
...
...
@@ -4,10 +4,10 @@
int
main
(
int
argc
,
char
*
argv
[])
{
struct
model_params
params
;
const
char
*
input_file
=
argv
[
1
];
struct
swift_params
param_file
;
parseFile
(
&
param_file
,
input_file
);
//parseFile(¶ms,"testInput.dat");
parseFile
(
&
params
,
input_file
);
printParameters
(
&
param_file
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment