-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.c
114 lines (100 loc) · 2.48 KB
/
parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "parser.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
long
to_long (const char* s)
{
long val;
errno = 0;
val = strtol (s, NULL, 10);
if (errno != 0)
{
fprintf (stderr, "*** Can\'t read string \'%s\' as a long; errno = %d ***\n", s, errno);
errno = 0;
exit (EXIT_FAILURE);
}
return val;
}
double
to_double (const char* s)
{
double val;
errno = 0;
val = strtod (s, NULL);
if (errno != 0)
{
fprintf (stderr, "*** Can\'t read string \'%s\' as a double; errno = %d ***\n", s, errno);
errno = 0;
exit (EXIT_FAILURE);
}
return val;
}
int
to_int (const char* s)
{
int val;
errno = 0;
val = (int) strtol (s, NULL, 10);
if (errno != 0)
{
fprintf (stderr, "*** Can\'t read string \'%s\' as an int; errno = %d ***\n", s, errno);
errno = 0;
exit (EXIT_FAILURE);
}
return val;
}
void
parse_parameters (double* S,
double* E,
double* r,
double* sigma,
double* T,
long* M,
const char* filename)
{
char line[400];
FILE* stream = NULL;
stream = fopen (filename, "r");
if (stream == NULL)
{
fprintf (stderr, "*** Failed to open parameters file \'%s\' ***\n", filename);
exit (EXIT_FAILURE);
}
if (NULL == fgets (line, 400, stream))
{
fprintf (stderr, "*** Failed to read parameter S from parameters file \'%s\' ***\n", filename);
exit (EXIT_FAILURE);
}
*S = to_double (line);
if (NULL == fgets (line, 400, stream))
{
fprintf (stderr, "*** Failed to read parameter E from parameters file \'%s\' ***\n", filename);
exit (EXIT_FAILURE);
}
*E = to_double (line);
if (NULL == fgets (line, 400, stream))
{
fprintf (stderr, "*** Failed to read parameter r from parameters file \'%s\' ***\n", filename);
exit (EXIT_FAILURE);
}
*r = to_double (line);
if (NULL == fgets (line, 400, stream))
{
fprintf (stderr, "*** Failed to read parameter sigma from parameters file \'%s\' ***\n", filename);
exit (EXIT_FAILURE);
}
*sigma = to_double (line);
if (NULL == fgets (line, 400, stream))
{
fprintf (stderr, "*** Failed to read parameter T from parameters file \'%s\' ***\n", filename);
exit (EXIT_FAILURE);
}
*T = to_double (line);
if (NULL == fgets (line, 400, stream))
{
fprintf (stderr, "*** Failed to read parameter M from parameters file \'%s\' ***\n", filename);
exit (EXIT_FAILURE);
}
*M = to_double (line);
}