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
d324392c
Commit
d324392c
authored
9 years ago
by
James Willis
Browse files
Options
Downloads
Patches
Plain Diff
Removed atof and atoi calls with more robust calls to sscanf.
parent
f87f7791
No related branches found
No related tags found
1 merge request
!129
Parameter file
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/parser.c
+31
-21
31 additions, 21 deletions
src/parser.c
with
31 additions
and
21 deletions
src/parser.c
+
31
−
21
View file @
d324392c
...
...
@@ -133,31 +133,23 @@ static void parse_line(FILE *fp, struct swift_params *params) {
void
parser_get_param_int
(
struct
swift_params
*
params
,
char
*
name
,
int
*
retParam
)
{
char
str
[
128
];
for
(
int
i
=
0
;
i
<
params
->
count
;
i
++
)
{
/*strcmp returns 0 if both strings are the same.*/
if
(
!
strcmp
(
name
,
params
->
data
[
i
].
name
))
{
/* Check if value is 0, to avoid integers of 0 causing an error in the following checks. */
if
(
!
strcmp
(
"0"
,
params
->
data
[
i
].
value
))
{
*
retParam
=
0
;
}
/* Check if the value is a string. */
else
if
(
!
atoi
(
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.*/
else
if
(
strchr
(
params
->
data
[
i
].
value
,
'.'
)
||
strchr
(
params
->
data
[
i
].
value
,
'e'
)
||
strchr
(
params
->
data
[
i
].
value
,
'E'
)
)
{
error
(
"Tried to parse '%s' but found '%s', when expecting an integer."
,
params
->
data
[
i
].
name
,
params
->
data
[
i
].
value
);
}
else
{
*
retParam
=
atoi
(
params
->
data
[
i
].
value
);
}
return
;
/* Check that exactly one number is parsed. */
if
(
sscanf
(
params
->
data
[
i
].
value
,
"%d%s"
,
retParam
,
str
)
!=
1
)
{
error
(
"Tried parsing int '%s' but found '%s' with illegal integer characters '%s'."
,
params
->
data
[
i
].
name
,
params
->
data
[
i
].
value
,
str
);
}
return
;
}
}
message
(
"Cannot find '%s' in the structure."
,
name
);
}
/**
...
...
@@ -171,14 +163,23 @@ void parser_get_param_int(struct swift_params *params, char * name, int * retPar
void
parser_get_param_float
(
struct
swift_params
*
params
,
char
*
name
,
float
*
retParam
)
{
char
str
[
128
];
for
(
int
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
);
/* Check that exactly one number is parsed. */
if
(
sscanf
(
params
->
data
[
i
].
value
,
"%f%s"
,
retParam
,
str
)
!=
1
)
{
error
(
"Tried parsing float '%s' but found '%s' with illegal float characters '%s'."
,
params
->
data
[
i
].
name
,
params
->
data
[
i
].
value
,
str
);
}
return
;
}
}
message
(
"Cannot find '%s' in the structure."
,
name
);
}
...
...
@@ -193,14 +194,23 @@ void parser_get_param_float(struct swift_params *params, char * name, float * re
void
parser_get_param_double
(
struct
swift_params
*
params
,
char
*
name
,
double
*
retParam
)
{
char
str
[
128
];
for
(
int
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
);
/* Check that exactly one number is parsed. */
if
(
sscanf
(
params
->
data
[
i
].
value
,
"%lf"
,
retParam
)
!=
1
)
{
error
(
"Tried parsing double '%s' but found '%s' with illegal double characters '%s'."
,
params
->
data
[
i
].
name
,
params
->
data
[
i
].
value
,
str
);
}
return
;
}
}
message
(
"Cannot find '%s' in the structure."
,
name
);
}
/**
...
...
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