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
ed970c2f
Commit
ed970c2f
authored
9 years ago
by
James Willis
Browse files
Options
Downloads
Patches
Plain Diff
Added separate private function for parsing parameters inside of sections.
parent
c35bd7c0
No related branches found
No related tags found
1 merge request
!140
First version of main() using a parameter file to get constants.
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/parser.c
+65
-45
65 additions, 45 deletions
src/parser.c
with
65 additions
and
45 deletions
src/parser.c
+
65
−
45
View file @
ed970c2f
...
...
@@ -37,7 +37,8 @@ static int count_char(const char *str, char val);
static
int
is_empty
(
const
char
*
str
);
static
int
count_indentation
(
const
char
*
str
);
static
void
parse_line
(
char
*
line
,
struct
swift_params
*
params
);
static
void
parse_param
(
char
*
line
,
struct
swift_params
*
params
);
static
void
parse_value
(
char
*
line
,
struct
swift_params
*
params
);
static
void
parse_section_param
(
char
*
line
,
int
*
isFirstParam
,
char
*
sectionName
,
struct
swift_params
*
params
);
/**
* @brief Reads an input file and stores each parameter in a structure.
...
...
@@ -162,7 +163,7 @@ static void parse_line(char *line, struct swift_params *params) {
/* Check if the line contains a value and parse it. */
if
(
strchr
(
trim_line
,
PARSER_VALUE_CHAR
))
{
parse_
param
(
trim_line
,
params
);
parse_
value
(
trim_line
,
params
);
}
/* Check for invalid lines,not including the start and end of file. */
/* Note: strcmp returns 0 if both strings are the same.*/
...
...
@@ -182,77 +183,96 @@ static void parse_line(char *line, struct swift_params *params) {
*
*/
static
void
parse_
param
(
char
*
line
,
struct
swift_params
*
params
)
{
static
int
in
_s
ection
=
0
;
static
void
parse_
value
(
char
*
line
,
struct
swift_params
*
params
)
{
static
int
in
S
ection
=
0
;
static
char
section
[
PARSER_MAX_LINE_SIZE
];
/* Keeps track of current section name. */
static
int
is_first_param
=
1
;
static
int
section_indent
=
0
;
char
tmp_str
[
PARSER_MAX_LINE_SIZE
];
char
param_name
[
PARSER_MAX_LINE_SIZE
];
static
int
isFirstParam
=
1
;
char
tmpStr
[
PARSER_MAX_LINE_SIZE
];
char
*
token
;
/* Check for more than one
parameter
on the same line. */
/* Check for more than one
value
on the same line. */
if
(
count_char
(
line
,
PARSER_VALUE_CHAR
)
>
1
)
{
error
(
"Found more than one
parameter
in '%s', only one allowed."
,
line
);
error
(
"Found more than one
value
in '%s', only one allowed."
,
line
);
}
if
(
!
in_section
&&
*
line
==
' '
)
{
/* Check that standalone parameters have correct indentation. */
if
(
!
inSection
&&
*
line
==
' '
)
{
error
(
"Invalid line: '%s', standalone parameter defined with incorrect indentation."
,
line
);
}
/* Check that it is a parameter inside a section.*/
if
(
*
line
==
' '
||
*
line
==
'\t'
)
{
/* Count indentation of each parameter and check that it
* is consistent with the first parameter in the section. */
if
(
is_first_param
)
{
section_indent
=
count_indentation
(
line
);
is_first_param
=
0
;
}
else
if
(
count_indentation
(
line
)
!=
section_indent
)
{
error
(
"Invalid line: '%s', parameter has incorrect indentation."
,
line
);
}
/* Take first token as the parameter name and trim leading white space. */
token
=
strtok
(
line
,
" :
\t
"
);
strcpy
(
tmp_str
,
token
);
/* Take second token as the parameter value. */
token
=
strtok
(
NULL
,
" #
\n
"
);
/* Prefix the parameter name with its section name and
* copy it into the parameter structure. */
strcpy
(
param_name
,
section
);
strcat
(
param_name
,
tmp_str
);
strcpy
(
params
->
data
[
params
->
count
].
name
,
param_name
);
strcpy
(
params
->
data
[
params
->
count
++
].
value
,
token
);
}
else
{
/*Else it is the start of a new section or standalone parameter. */
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
(
tmp
_s
tr
,
token
);
strcpy
(
tmp
S
tr
,
token
);
/* Take second token as the parameter value. */
token
=
strtok
(
NULL
,
" #
\n
"
);
/* If second token is NULL then the line must be a section heading. */
if
(
token
==
NULL
)
{
strcat
(
tmp
_s
tr
,
PARSER_VALUE_STRING
);
strcpy
(
section
,
tmp
_s
tr
);
in
_s
ection
=
1
;
is
_f
irst
_p
aram
=
1
;
strcat
(
tmp
S
tr
,
PARSER_VALUE_STRING
);
strcpy
(
section
,
tmp
S
tr
);
in
S
ection
=
1
;
is
F
irst
P
aram
=
1
;
}
else
{
/* Must be a standalone parameter so no need to prefix name with a
* section. */
strcpy
(
params
->
data
[
params
->
count
].
name
,
tmp
_s
tr
);
strcpy
(
params
->
data
[
params
->
count
].
name
,
tmp
S
tr
);
strcpy
(
params
->
data
[
params
->
count
++
].
value
,
token
);
in
_s
ection
=
0
;
is
_f
irst
_p
aram
=
1
;
in
S
ection
=
0
;
is
F
irst
P
aram
=
1
;
}
}
}
/**
* @brief Parses a parameter that appears in a section and stores it in a structure.
*
* @param line Line containing the parameter
* @param isFirstParam Shows if the first parameter of a section has been found
* @param sectionName String containing the current section name
* @param params Structure to be written to
*
*/
static
void
parse_section_param
(
char
*
line
,
int
*
isFirstParam
,
char
*
sectionName
,
struct
swift_params
*
params
)
{
static
int
sectionIndent
=
0
;
char
tmpStr
[
PARSER_MAX_LINE_SIZE
];
char
paramName
[
PARSER_MAX_LINE_SIZE
];
char
*
token
;
/* Count indentation of each parameter and check that it
* is consistent with the first parameter in the section. */
if
(
*
isFirstParam
)
{
sectionIndent
=
count_indentation
(
line
);
*
isFirstParam
=
0
;
}
else
if
(
count_indentation
(
line
)
!=
sectionIndent
)
{
error
(
"Invalid line: '%s', parameter has incorrect indentation."
,
line
);
}
/* Take first token as the parameter name and trim leading white space. */
token
=
strtok
(
line
,
" :
\t
"
);
strcpy
(
tmpStr
,
token
);
/* Take second token as the parameter value. */
token
=
strtok
(
NULL
,
" #
\n
"
);
/* Prefix the parameter name with its section name and
* copy it into the parameter structure. */
strcpy
(
paramName
,
sectionName
);
strcat
(
paramName
,
tmpStr
);
strcpy
(
params
->
data
[
params
->
count
].
name
,
paramName
);
strcpy
(
params
->
data
[
params
->
count
++
].
value
,
token
);
}
/**
* @brief Retrieve integer parameter from structure.
*
...
...
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