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
ca470e48
Commit
ca470e48
authored
9 years ago
by
James Willis
Browse files
Options
Downloads
Patches
Plain Diff
Moved main body of functions to parser.c.
parent
a88726d2
No related branches found
No related tags found
1 merge request
!129
Parameter file
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/parser.c
+166
-0
166 additions, 0 deletions
src/parser.c
src/parser.h
+5
-104
5 additions, 104 deletions
src/parser.h
src/testParser.c
+0
-2
0 additions, 2 deletions
src/testParser.c
with
171 additions
and
106 deletions
src/parser.c
0 → 100644
+
166
−
0
View file @
ca470e48
/*******************************************************************************
* This file is part of SWIFT.
* Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include
"parser.h"
/* Needs to be included so that strtok returns char * instead of a int *. */
#include
<string.h>
#include
<stdlib.h>
#include
"error.h"
void
parseFile
(
const
char
*
file_name
,
struct
swift_params
*
params
)
{
FILE
*
fp
;
params
->
count
=
0
;
/* Open file for reading */
fp
=
fopen
(
file_name
,
"r"
);
if
(
fp
==
NULL
)
{
error
(
"Error opening parameter file: %s"
,
file_name
);
}
/* Read until the end of the file is reached.*/
while
(
!
feof
(
fp
))
{
readParameter
(
fp
,
params
);
}
fclose
(
fp
);
}
static
int
countChar
(
char
*
str
,
char
val
)
{
int
i
,
count
=
0
;
for
(
i
=
0
;
i
<
strlen
(
str
);
i
++
)
{
/* Check if the line contains the character */
if
(
str
[
i
]
==
val
)
count
++
;
}
return
count
;
}
static
void
readParameter
(
FILE
*
fp
,
struct
swift_params
*
params
)
{
char
line
[
MAX_LINE_SIZE
];
char
trim_line
[
MAX_LINE_SIZE
];
/* Read a line of the file */
if
(
fgets
(
line
,
MAX_LINE_SIZE
,
fp
)
!=
NULL
)
{
char
*
token
;
/* Remove comments */
token
=
strtok
(
line
,
COMMENT
);
strcpy
(
trim_line
,
token
);
/* Check if the line contains a value */
if
(
strchr
(
trim_line
,
VALUE
))
{
/* Check for more than one parameter on the same line. */
if
(
countChar
(
trim_line
,
VALUE
)
>
1
)
{
error
(
"Found more than one parameter in '%s', only one allowed."
,
line
);
}
else
{
/*Take first token as the parameter name. */
token
=
strtok
(
trim_line
,
VALUE
);
strcpy
(
params
->
data
[
params
->
count
].
name
,
token
);
/*Take second token as the parameter value. */
token
=
strtok
(
NULL
,
" #
\n
"
);
strcpy
(
params
->
data
[
params
->
count
++
].
value
,
token
);
}
}
}
}
void
getParamInt
(
struct
swift_params
*
params
,
char
*
name
,
int
*
retParam
)
{
int
i
;
for
(
i
=
0
;
i
<
MAX_NO_OF_PARAMS
;
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 zero causing an error. */
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', when expecting an integer."
,
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', when expecting an integer."
,
params
->
data
[
i
].
value
);
}
else
{
*
retParam
=
atoi
(
params
->
data
[
i
].
value
);
}
return
;
}
}
}
void
getParamFloat
(
struct
swift_params
*
params
,
char
*
name
,
float
*
retParam
)
{
int
i
;
for
(
i
=
0
;
i
<
MAX_NO_OF_PARAMS
;
i
++
)
{
/*strcmp returns 0 if both strings are the same.*/
if
(
!
strcmp
(
name
,
params
->
data
[
i
].
name
))
{
*
retParam
=
atof
(
params
->
data
[
i
].
value
);
return
;
}
}
}
void
getParamString
(
struct
swift_params
*
params
,
char
*
name
,
char
*
retParam
)
{
int
i
;
for
(
i
=
0
;
i
<
MAX_NO_OF_PARAMS
;
i
++
)
{
/*strcmp returns 0 if both strings are the same.*/
if
(
!
strcmp
(
name
,
params
->
data
[
i
].
name
))
{
strcpy
(
retParam
,
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
<
params
->
count
;
i
++
)
{
printf
(
"Name: %s
\n
"
,
params
->
data
[
i
].
name
);
printf
(
"Value: %s
\n
"
,
params
->
data
[
i
].
value
);
}
}
This diff is collapsed.
Click to expand it.
src/parser.h
+
5
−
104
View file @
ca470e48
...
@@ -20,13 +20,15 @@
...
@@ -20,13 +20,15 @@
#ifndef SWIFT_PARSER_H
#ifndef SWIFT_PARSER_H
#define SWIFT_PARSER_H
#define SWIFT_PARSER_H
/* Needs to be included so that strtok returns char * instead of a int *. */
#include
<stdio.h>
#include
<string.h>
#include
<stdlib.h>
#define MAX_LINE_SIZE 128
#define MAX_LINE_SIZE 128
#define MAX_NO_OF_PARAMS 512
#define MAX_NO_OF_PARAMS 512
#define COMMENT "#"
#define VALUE char ":"
#define END_OF_FILE "..."
struct
parameter
{
struct
parameter
{
char
name
[
MAX_LINE_SIZE
];
char
name
[
MAX_LINE_SIZE
];
char
value
[
MAX_LINE_SIZE
];
char
value
[
MAX_LINE_SIZE
];
...
@@ -47,105 +49,4 @@ void getParamString(struct swift_params *params, char *name, char *retParam);
...
@@ -47,105 +49,4 @@ void getParamString(struct swift_params *params, char *name, char *retParam);
/* Private functions. */
/* Private functions. */
static
void
readParameter
(
FILE
*
fp
,
struct
swift_params
*
params
);
static
void
readParameter
(
FILE
*
fp
,
struct
swift_params
*
params
);
void
parseFile
(
const
char
*
file_name
,
struct
swift_params
*
params
)
{
FILE
*
fp
;
params
->
count
=
0
;
/* Open file for reading */
fp
=
fopen
(
file_name
,
"r"
);
if
(
fp
==
NULL
)
{
error
(
"Error opening parameter file: %s"
,
file_name
);
}
/* Read until the end of the file is reached.*/
while
(
!
feof
(
fp
))
{
readParameter
(
fp
,
params
);
}
fclose
(
fp
);
}
void
getParamInt
(
struct
swift_params
*
params
,
char
*
name
,
int
*
retParam
)
{
int
i
;
for
(
i
=
0
;
i
<
MAX_NO_OF_PARAMS
;
i
++
)
{
/*strcmp returns 0 if both strings are the same.*/
if
(
!
strcmp
(
name
,
params
->
data
[
i
].
name
))
{
*
retParam
=
atoi
(
params
->
data
[
i
].
value
);
return
;
}
}
}
void
getParamFloat
(
struct
swift_params
*
params
,
char
*
name
,
float
*
retParam
)
{
int
i
;
for
(
i
=
0
;
i
<
MAX_NO_OF_PARAMS
;
i
++
)
{
/*strcmp returns 0 if both strings are the same.*/
if
(
!
strcmp
(
name
,
params
->
data
[
i
].
name
))
{
*
retParam
=
atof
(
params
->
data
[
i
].
value
);
return
;
}
}
}
void
getParamString
(
struct
swift_params
*
params
,
char
*
name
,
char
*
retParam
)
{
int
i
;
for
(
i
=
0
;
i
<
MAX_NO_OF_PARAMS
;
i
++
)
{
/*strcmp returns 0 if both strings are the same.*/
if
(
!
strcmp
(
name
,
params
->
data
[
i
].
name
))
{
strcpy
(
retParam
,
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
<
params
->
count
;
i
++
)
{
printf
(
"Name: %s
\n
"
,
params
->
data
[
i
].
name
);
printf
(
"Value: %s
\n
"
,
params
->
data
[
i
].
value
);
}
}
static
void
readParameter
(
FILE
*
fp
,
struct
swift_params
*
params
)
{
char
line
[
MAX_LINE_SIZE
];
/* Read a line of the file */
if
(
fgets
(
line
,
MAX_LINE_SIZE
,
fp
)
!=
"..."
)
{
/* Check if the line contains a value */
if
(
strchr
(
line
,
':'
))
{
char
*
token
;
/*Take first token as the parameter name. */
token
=
strtok
(
line
,
":"
);
strcpy
(
params
->
data
[
params
->
count
].
name
,
token
);
/*Take second token as the parameter value. */
token
=
strtok
(
NULL
,
":"
);
strcpy
(
params
->
data
[
params
->
count
++
].
value
,
token
);
}
}
}
#endif
/* SWIFT_PARSER_H */
#endif
/* SWIFT_PARSER_H */
This diff is collapsed.
Click to expand it.
src/testParser.c
+
0
−
2
View file @
ca470e48
#include
<stdio.h>
#include
"parser.h"
#include
"parser.h"
int
main
(
int
argc
,
char
*
argv
[])
{
int
main
(
int
argc
,
char
*
argv
[])
{
...
...
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