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
357989a2
Commit
357989a2
authored
9 years ago
by
Matthieu Schaller
Browse files
Options
Downloads
Patches
Plain Diff
Change in the parser's public interface to simplify calls.
parent
4be8ec86
No related branches found
No related tags found
1 merge request
!140
First version of main() using a parameter file to get constants.
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/parser.c
+24
-25
24 additions, 25 deletions
src/parser.c
src/parser.h
+7
-8
7 additions, 8 deletions
src/parser.h
src/units.c
+10
-11
10 additions, 11 deletions
src/units.c
with
41 additions
and
44 deletions
src/parser.c
+
24
−
25
View file @
357989a2
...
@@ -289,30 +289,30 @@ static void parse_section_param(char *line, int *isFirstParam,
...
@@ -289,30 +289,30 @@ static void parse_section_param(char *line, int *isFirstParam,
*
*
* @param params Structure that holds the parameters
* @param params Structure that holds the parameters
* @param name Name of the parameter to be found
* @param name Name of the parameter to be found
* @param retParam Value of the parameter found
* @return Value of the parameter found
*
*/
*/
int
parser_get_param_int
(
const
struct
swift_params
*
params
,
const
char
*
name
)
{
void
parser_get_param_int
(
const
struct
swift_params
*
params
,
char
*
name
,
int
*
retParam
)
{
char
str
[
PARSER_MAX_LINE_SIZE
];
char
str
[
PARSER_MAX_LINE_SIZE
];
int
retParam
=
0
;
for
(
int
i
=
0
;
i
<
params
->
count
;
i
++
)
{
for
(
int
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 that exactly one number is parsed. */
/* Check that exactly one number is parsed. */
if
(
sscanf
(
params
->
data
[
i
].
value
,
"%d%s"
,
retParam
,
str
)
!=
1
)
{
if
(
sscanf
(
params
->
data
[
i
].
value
,
"%d%s"
,
&
retParam
,
str
)
!=
1
)
{
error
(
error
(
"Tried parsing int '%s' but found '%s' with illegal integer "
"Tried parsing int '%s' but found '%s' with illegal integer "
"characters '%s'."
,
"characters '%s'."
,
params
->
data
[
i
].
name
,
params
->
data
[
i
].
value
,
str
);
params
->
data
[
i
].
name
,
params
->
data
[
i
].
value
,
str
);
}
}
return
;
return
retParam
;
}
}
}
}
error
(
"Cannot find '%s' in the structure."
,
name
);
error
(
"Cannot find '%s' in the structure."
,
name
);
return
0
;
}
}
/**
/**
...
@@ -320,30 +320,31 @@ void parser_get_param_int(const struct swift_params *params, char *name,
...
@@ -320,30 +320,31 @@ void parser_get_param_int(const struct swift_params *params, char *name,
*
*
* @param params Structure that holds the parameters
* @param params Structure that holds the parameters
* @param name Name of the parameter to be found
* @param name Name of the parameter to be found
* @param retParam Value of the parameter found
* @return Value of the parameter found
*
*/
*/
float
parser_get_param_float
(
const
struct
swift_params
*
params
,
const
char
*
name
)
{
void
parser_get_param_float
(
const
struct
swift_params
*
params
,
char
*
name
,
float
*
retParam
)
{
char
str
[
PARSER_MAX_LINE_SIZE
];
char
str
[
PARSER_MAX_LINE_SIZE
];
float
retParam
=
0
.
f
;
for
(
int
i
=
0
;
i
<
params
->
count
;
i
++
)
{
for
(
int
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 that exactly one number is parsed. */
/* Check that exactly one number is parsed. */
if
(
sscanf
(
params
->
data
[
i
].
value
,
"%f%s"
,
retParam
,
str
)
!=
1
)
{
if
(
sscanf
(
params
->
data
[
i
].
value
,
"%f%s"
,
&
retParam
,
str
)
!=
1
)
{
error
(
error
(
"Tried parsing float '%s' but found '%s' with illegal float "
"Tried parsing float '%s' but found '%s' with illegal float "
"characters '%s'."
,
"characters '%s'."
,
params
->
data
[
i
].
name
,
params
->
data
[
i
].
value
,
str
);
params
->
data
[
i
].
name
,
params
->
data
[
i
].
value
,
str
);
}
}
return
;
return
retParam
;
}
}
}
}
error
(
"Cannot find '%s' in the structure."
,
name
);
error
(
"Cannot find '%s' in the structure."
,
name
);
return
0
.
f
;
}
}
/**
/**
...
@@ -351,30 +352,30 @@ void parser_get_param_float(const struct swift_params *params, char *name,
...
@@ -351,30 +352,30 @@ void parser_get_param_float(const struct swift_params *params, char *name,
*
*
* @param params Structure that holds the parameters
* @param params Structure that holds the parameters
* @param name Name of the parameter to be found
* @param name Name of the parameter to be found
* @param retParam Value of the parameter found
* @return Value of the parameter found
*
*/
*/
double
parser_get_param_double
(
const
struct
swift_params
*
params
,
const
char
*
name
)
{
void
parser_get_param_double
(
const
struct
swift_params
*
params
,
char
*
name
,
double
*
retParam
)
{
char
str
[
PARSER_MAX_LINE_SIZE
];
char
str
[
PARSER_MAX_LINE_SIZE
];
double
retParam
=
0
.;
for
(
int
i
=
0
;
i
<
params
->
count
;
i
++
)
{
for
(
int
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 that exactly one number is parsed. */
/* Check that exactly one number is parsed. */
if
(
sscanf
(
params
->
data
[
i
].
value
,
"%lf"
,
retParam
)
!=
1
)
{
if
(
sscanf
(
params
->
data
[
i
].
value
,
"%lf"
,
&
retParam
)
!=
1
)
{
error
(
error
(
"Tried parsing double '%s' but found '%s' with illegal double "
"Tried parsing double '%s' but found '%s' with illegal double "
"characters '%s'."
,
"characters '%s'."
,
params
->
data
[
i
].
name
,
params
->
data
[
i
].
value
,
str
);
params
->
data
[
i
].
name
,
params
->
data
[
i
].
value
,
str
);
}
}
return
retParam
;
return
;
}
}
}
}
error
(
"Cannot find '%s' in the structure."
,
name
);
error
(
"Cannot find '%s' in the structure."
,
name
);
return
0
.;
}
}
/**
/**
...
@@ -382,12 +383,10 @@ void parser_get_param_double(const struct swift_params *params, char *name,
...
@@ -382,12 +383,10 @@ void parser_get_param_double(const struct swift_params *params, char *name,
*
*
* @param params Structure that holds the parameters
* @param params Structure that holds the parameters
* @param name Name of the parameter to be found
* @param name Name of the parameter to be found
* @param retParam Value of the parameter found
* @param retParam of the parameter found
*
*/
*/
void
parser_get_param_string
(
const
struct
swift_params
*
params
,
void
parser_get_param_string
(
const
struct
swift_params
*
params
,
char
*
name
,
const
char
*
name
,
char
*
retParam
)
{
char
*
retParam
)
{
for
(
int
i
=
0
;
i
<
params
->
count
;
i
++
)
{
for
(
int
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
))
{
...
@@ -425,7 +424,7 @@ void parser_print_params(const struct swift_params *params) {
...
@@ -425,7 +424,7 @@ void parser_print_params(const struct swift_params *params) {
*
*
*/
*/
void
parser_write_params_to_file
(
const
struct
swift_params
*
params
,
void
parser_write_params_to_file
(
const
struct
swift_params
*
params
,
const
char
*
file_name
)
{
const
char
*
file_name
)
{
FILE
*
file
=
fopen
(
file_name
,
"w"
);
FILE
*
file
=
fopen
(
file_name
,
"w"
);
char
section
[
PARSER_MAX_LINE_SIZE
];
char
section
[
PARSER_MAX_LINE_SIZE
];
...
...
This diff is collapsed.
Click to expand it.
src/parser.h
+
7
−
8
View file @
357989a2
...
@@ -43,13 +43,12 @@ void parser_read_file(const char *file_name, struct swift_params *params);
...
@@ -43,13 +43,12 @@ void parser_read_file(const char *file_name, struct swift_params *params);
void
parser_print_params
(
const
struct
swift_params
*
params
);
void
parser_print_params
(
const
struct
swift_params
*
params
);
void
parser_write_params_to_file
(
const
struct
swift_params
*
params
,
void
parser_write_params_to_file
(
const
struct
swift_params
*
params
,
const
char
*
file_name
);
const
char
*
file_name
);
void
parser_get_param_int
(
const
struct
swift_params
*
params
,
char
*
name
,
int
parser_get_param_int
(
const
struct
swift_params
*
params
,
const
char
*
name
);
int
*
retParam
);
float
parser_get_param_float
(
const
struct
swift_params
*
params
,
void
parser_get_param_float
(
const
struct
swift_params
*
params
,
char
*
name
,
const
char
*
name
);
float
*
retParam
);
double
parser_get_param_double
(
const
struct
swift_params
*
params
,
void
parser_get_param_double
(
const
struct
swift_params
*
params
,
char
*
name
,
const
char
*
name
);
double
*
retParam
);
void
parser_get_param_string
(
const
struct
swift_params
*
params
,
void
parser_get_param_string
(
const
struct
swift_params
*
params
,
char
*
name
,
const
char
*
name
,
char
*
retParam
);
char
*
retParam
);
#endif
/* SWIFT_PARSER_H */
#endif
/* SWIFT_PARSER_H */
This diff is collapsed.
Click to expand it.
src/units.c
+
10
−
11
View file @
357989a2
...
@@ -49,18 +49,17 @@
...
@@ -49,18 +49,17 @@
void
initUnitSystem
(
struct
UnitSystem
*
us
,
const
struct
swift_params
*
params
)
{
void
initUnitSystem
(
struct
UnitSystem
*
us
,
const
struct
swift_params
*
params
)
{
parser_get_param_double
(
params
,
"UnitSystem:UnitMass_in_cgs"
,
us
->
UnitMass_in_cgs
=
&
us
->
UnitMass_in_cgs
);
parser_get_param_double
(
params
,
"UnitSystem:UnitMass_in_cgs"
);
parser_get_param_double
(
params
,
"UnitSystem:UnitLength_in_cgs"
,
us
->
UnitLength_in_cgs
=
&
us
->
UnitLength_in_cgs
);
parser_get_param_double
(
params
,
"UnitSystem:UnitLength_in_cgs"
);
double
unitVelocity
;
const
double
unitVelocity
=
parser_get_param_double
(
params
,
"UnitSystem:UnitVelocity_in_cgs"
,
parser_get_param_double
(
params
,
"UnitSystem:UnitVelocity_in_cgs"
);
&
unitVelocity
);
us
->
UnitTime_in_cgs
=
us
->
UnitLength_in_cgs
/
unitVelocity
;
us
->
UnitTime_in_cgs
=
us
->
UnitLength_in_cgs
/
unitVelocity
;
parser_get_param_double
(
params
,
"UnitSystem:
UnitCurrent_in_cgs
"
,
us
->
UnitCurrent_in_cgs
=
&
us
->
UnitCurrent_in_cgs
);
parser_get_param_double
(
params
,
"UnitSystem:
UnitCurrent_in_cgs
"
);
parser_get_param_double
(
params
,
"UnitSystem:UnitTemp
_in_cgs
"
,
us
->
UnitTemperature
_in_cgs
=
&
us
->
UnitTemp
erature
_in_cgs
);
parser_get_param_double
(
params
,
"UnitSystem:
UnitTemp_in_cgs
"
);
}
}
/**
/**
...
...
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