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
9dd8f360
Commit
9dd8f360
authored
6 years ago
by
Peter W. Draper
Browse files
Options
Downloads
Patches
Plain Diff
Working towards basic array support in our pseudo YAML files
parent
17eb2b16
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!564
Yaml arrays
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
examples/main.c
+1
-1
1 addition, 1 deletion
examples/main.c
src/parser.c
+84
-7
84 additions, 7 deletions
src/parser.c
src/parser.h
+5
-0
5 additions, 0 deletions
src/parser.h
tests/testParserInput.yaml
+3
-0
3 additions, 0 deletions
tests/testParserInput.yaml
with
93 additions
and
8 deletions
examples/main.c
+
1
−
1
View file @
9dd8f360
...
...
@@ -470,7 +470,7 @@ int main(int argc, char *argv[]) {
}
/* And dump the parameters as used. */
// parser_print_params(
&
params);
// parser_print_params(params);
parser_write_params_to_file
(
params
,
"used_parameters.yml"
);
}
#ifdef WITH_MPI
...
...
This diff is collapsed.
Click to expand it.
src/parser.c
+
84
−
7
View file @
9dd8f360
...
...
@@ -56,6 +56,26 @@ static void find_duplicate_section(const struct swift_params *params,
const
char
*
section_name
);
static
int
lineNumber
=
0
;
// Functions to trim strings of spaces.
static
char
*
trim_leading
(
char
*
s
)
{
if
(
s
==
NULL
||
strlen
(
s
)
<
2
)
return
s
;
while
(
isspace
(
*
s
))
s
++
;
return
s
;
}
static
char
*
trim_trailing
(
char
*
s
)
{
if
(
s
==
NULL
||
strlen
(
s
)
<
2
)
return
s
;
char
*
end
=
s
+
strlen
(
s
)
-
1
;
while
(
isspace
(
*
end
))
end
--
;
*
(
end
+
1
)
=
'\0'
;
return
s
;
}
char
*
trim_both
(
char
*
s
)
{
if
(
s
==
NULL
||
strlen
(
s
)
<
2
)
return
s
;
return
trim_trailing
(
trim_leading
(
s
));
}
/**
* @brief Initialize the parser structure.
*
...
...
@@ -324,11 +344,11 @@ static void parse_value(char *line, struct swift_params *params) {
parse_section_param
(
line
,
&
isFirstParam
,
section
,
params
);
}
else
{
/*Else it is the start of a new section or standalone parameter. */
/* Take first token as the parameter name. */
token
=
strtok
(
line
,
"
:
\t
"
);
strcpy
(
tmpStr
,
token
);
token
=
strtok
(
line
,
":
\t
"
);
strcpy
(
tmpStr
,
trim_trailing
(
token
)
)
;
/* Take second token as the parameter value. */
token
=
strtok
(
NULL
,
"
#
\n
"
);
token
=
trim_both
(
strtok
(
NULL
,
"#
\n
"
)
)
;
/* If second token is NULL then the line must be a section heading. */
if
(
token
==
NULL
)
{
...
...
@@ -411,11 +431,11 @@ static void parse_section_param(char *line, int *isFirstParam,
}
/* Take first token as the parameter name and trim leading white space. */
token
=
strtok
(
line
,
"
:
\t
"
);
token
=
trim_both
(
strtok
(
line
,
":
\t
"
)
)
;
strcpy
(
tmpStr
,
token
);
/* Take second token as the parameter value. */
token
=
strtok
(
NULL
,
"
#
\n
"
);
token
=
trim_both
(
strtok
(
NULL
,
"#
\n
"
)
)
;
/* Prefix the parameter name with its section name and
* copy it into the parameter structure. */
...
...
@@ -529,6 +549,63 @@ float parser_get_param_float(const struct swift_params *params,
return
0
.
f
;
}
/**
* @brief Retrieve float array parameter from structure.
*
* @param params Structure that holds the parameters
* @param name Name of the parameter to be found
* @param required whether it is an error if the parameter has not been set
* @param nval number of values expected.
* @param values Values of the parameter found, of size at least nvals.
* @return whether the parameter has been found.
*/
int
parser_get_param_float_array
(
const
struct
swift_params
*
params
,
const
char
*
name
,
int
required
,
int
nval
,
float
*
values
)
{
char
str
[
PARSER_MAX_LINE_SIZE
];
char
cpy
[
PARSER_MAX_LINE_SIZE
];
char
*
cp
=
cpy
;
for
(
int
i
=
0
;
i
<
params
->
paramCount
;
i
++
)
{
if
(
!
strcmp
(
name
,
params
->
data
[
i
].
name
))
{
strcpy
(
cp
,
params
->
data
[
i
].
value
);
cp
=
trim_both
(
cp
);
/* Strip off []. */
if
(
cp
[
0
]
!=
'['
)
error
(
"Array '%s' does not start with '['"
,
name
);
cp
++
;
int
l
=
strlen
(
cp
);
if
(
cp
[
l
-
1
]
!=
']'
)
error
(
"Array '%s' does not end with ']'"
,
name
);
cp
[
l
-
1
]
=
'\0'
;
cp
=
trim_both
(
cp
);
/* Parse out values which should now be "v, v, v" with internal
* whitespace variations. */
char
*
p
=
strtok
(
cp
,
","
);
for
(
int
k
=
0
;
k
<
nval
;
k
++
)
{
if
(
p
!=
NULL
)
{
if
(
sscanf
(
p
,
" %f%s "
,
&
values
[
k
],
str
)
!=
1
)
{
error
(
"Tried parsing float '%s' but found '%s' with "
"illegal float characters '%s'."
,
name
,
p
,
str
);
}
}
else
{
error
(
"Array '%s' with value '%s' has too few values, expected %d"
,
name
,
params
->
data
[
i
].
value
,
nval
);
}
if
(
k
<
nval
-
1
)
p
=
strtok
(
NULL
,
","
);
}
return
1
;
}
}
if
(
required
)
error
(
"Cannot find '%s' in the structure, in file '%s'."
,
name
,
params
->
fileName
);
return
0
;
}
/**
* @brief Retrieve double parameter from structure.
*
...
...
@@ -771,7 +848,7 @@ void parser_write_params_to_file(const struct swift_params *params,
}
/* Remove white space from parameter name and write it to the file. */
token
=
strtok
(
NULL
,
"
#
\n
"
);
token
=
trim_both
(
strtok
(
NULL
,
"#
\n
"
)
)
;
fprintf
(
file
,
" %s%c %s
\n
"
,
token
,
PARSER_VALUE_CHAR
,
params
->
data
[
i
].
value
);
...
...
@@ -782,7 +859,7 @@ void parser_write_params_to_file(const struct swift_params *params,
}
/* End of file identifier in YAML. */
fprintf
(
file
,
PARSER_END_OF_FILE
);
fprintf
(
file
,
"%s
\n
"
,
PARSER_END_OF_FILE
);
fclose
(
file
);
}
...
...
This diff is collapsed.
Click to expand it.
src/parser.h
+
5
−
0
View file @
9dd8f360
...
...
@@ -66,6 +66,11 @@ char parser_get_param_char(const struct swift_params *params, const char *name);
int
parser_get_param_int
(
const
struct
swift_params
*
params
,
const
char
*
name
);
float
parser_get_param_float
(
const
struct
swift_params
*
params
,
const
char
*
name
);
int
parser_get_param_float_array
(
const
struct
swift_params
*
params
,
const
char
*
name
,
int
required
,
int
nval
,
float
*
values
);
double
parser_get_param_double
(
const
struct
swift_params
*
params
,
const
char
*
name
);
void
parser_get_param_string
(
const
struct
swift_params
*
params
,
...
...
This diff is collapsed.
Click to expand it.
tests/testParserInput.yaml
+
3
−
0
View file @
9dd8f360
...
...
@@ -16,4 +16,7 @@ Simulation:
IO
:
#Input file
ic_file
:
ic_file.ini
#Box:
# sides: [2, 3, 4]
...
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