-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadSettings.c
362 lines (311 loc) · 8.8 KB
/
loadSettings.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
alphaCertified
Jonathan Hauenstein & Frank Sottile
May 7, 2010
Copyright 2010
loadSettings.c: Load the configurations from the settings file
*/
#include "alphaCertified.h"
void load_default_settings(configurations *S)
/***************************************************************\
* USAGE: load default settings into S *
\***************************************************************/
{
S->arithmeticType = 0;
S->startingPrecision = 96;
S->algorithm = 2;
S->refineDigits = 0;
S->numRandomSystems = 2;
S->randomDigits = 10;
S->randomSeed = (unsigned int) time(NULL);
S->newtonOnly = 0;
S->newtonIts = 2;
S->realityCheck = 1;
S->realityTest = 0;
return;
}
void move_to_eol(FILE *IN)
/***************************************************************\
* USAGE: Move to EOL (or EOF) *
\***************************************************************/
{
char ch;
do
{
ch = fgetc(IN);
} while (ch != '\n' && ch != EOF);
return;
}
void update_settings_item(configurations *S, char *str, FILE *IN)
/***************************************************************\
* USAGE: determine which item str describes and update it *
\***************************************************************/
{
int tempInt;
if (!strcmp(str, "ALGORITHM:"))
{ // read in the algorithm number
fscanf(IN, "%d", &tempInt);
// error checking
if (0 <= tempInt && tempInt <= 2)
{ // update algorithm
S->algorithm = tempInt;
}
else
{ // print error message
printf("NOTE: 'ALGORITHM' must be between 0 and 2. The value %d has been ignored.\n", tempInt);
}
// read in the rest of the line
move_to_eol(IN);
}
else if (!strcmp(str, "ARITHMETICTYPE:"))
{ // read in the arithmetic type
fscanf(IN, "%d", &tempInt);
// error checking
if (0 <= tempInt && tempInt <= 1)
{ // update arithmetic type
S->arithmeticType = tempInt;
}
else
{ // print error message
printf("NOTE: 'ARITHMETICTYPE' must be either 0 or 1. The value %d has been ignored.\n", tempInt);
}
// read in the rest of the line
move_to_eol(IN);
}
else if (!strcmp(str, "PRECISION:"))
{ // read in the precision
fscanf(IN, "%d", &tempInt);
// error checking
if (tempInt >= 64)
{ // update precision
S->startingPrecision = tempInt;
}
else
{ // print error message
printf("NOTE: 'PRECISION' must be >= 64. The value %d has been ignored.\n", tempInt);
}
// read in the rest of the line
move_to_eol(IN);
}
else if (!strcmp(str, "REFINEDIGITS:"))
{ // read in the number of digits
fscanf(IN, "%d", &tempInt);
// error checking
if (tempInt >= 0)
{ // update digits
S->refineDigits = tempInt;
}
else
{ // print error message
printf("NOTE: 'REFINEDIGITS' must be a nonnegative integer. The value %d has been ignored.\n", tempInt);
}
// read in the rest of the line
move_to_eol(IN);
}
else if (!strcmp(str, "NUMRANDOMSYSTEMS:"))
{ // read in the number of random systems
fscanf(IN, "%d", &tempInt);
// error checking
if (tempInt >= 2)
{ // update number of random systems
S->numRandomSystems = tempInt;
}
else
{ // print error message
printf("NOTE: 'NUMRANDOMSYSTEMS' must be >= 2. The value %d has been ignored.\n", tempInt);
}
// read in the rest of the line
move_to_eol(IN);
}
else if (!strcmp(str, "RANDOMDIGITS:"))
{ // read in the number of digits for random systems
fscanf(IN, "%d", &tempInt);
// error checking
if (tempInt > 0)
{ // update number of digits
S->randomDigits = tempInt;
}
else
{ // print error message
printf("NOTE: 'RANDOMDIGITS' must be positive. The value %d has been ignored.\n", tempInt);
}
// read in the rest of the line
move_to_eol(IN);
}
else if (!strcmp(str, "RANDOMSEED:"))
{ // read in the random seed
unsigned int randSeed = 0;
tempInt = fscanf(IN, "%u", &randSeed);
// error checking
if (tempInt > 0)
{ // update random seed
S->randomSeed = randSeed;
}
else
{ // print error message
printf("NOTE: 'RANDOMSEED' was not read in properly.\n");
}
// read in the rest of the line
move_to_eol(IN);
}
else if (!strcmp(str, "NEWTONONLY:"))
{ // read in if performing only Newton iterations
fscanf(IN, "%d", &tempInt);
// error checking
if (0 <= tempInt && tempInt <= 1)
{ // update newton only
S->newtonOnly = tempInt;
}
else
{ // print error message
printf("NOTE: 'NEWTONONLY' must be either 0 or 1. The value %d has been ignored.\n", tempInt);
}
// read in the rest of the line
move_to_eol(IN);
}
else if (!strcmp(str, "NUMITERATIONS:"))
{ // read in number of newton iterations
fscanf(IN, "%d", &tempInt);
// error checking
if (tempInt > 0)
{ // update newtonIts
S->newtonIts = tempInt;
}
else
{ // print error message
printf("NOTE: 'NUMITERATIONS' must be a positive integer. The value %d has been ignored.\n", tempInt);
}
// read in the rest of the line
move_to_eol(IN);
}
else if (!strcmp(str, "REALITYCHECK:"))
{ // read in the reality check configuration
fscanf(IN, "%d", &tempInt);
// error checking
if (tempInt == -1 || tempInt == 0 || tempInt == 1)
{ // update realityCheck
S->realityCheck = tempInt;
}
else
{ // print error message
printf("NOTE: 'REALITYCHECK' must be either -1, 0, or 1. The value %d has been ignored.\n", tempInt);
}
// read in the rest of the line
move_to_eol(IN);
}
else if (!strcmp(str, "REALITYTEST:"))
{ // read in the reality test configuration
fscanf(IN, "%d", &tempInt);
// error checking
if (tempInt == 0 || tempInt == 1)
{ // update realityTest
S->realityTest = tempInt;
}
else
{ // print error message
printf("NOTE: 'REALITYTEST' must be either 0 or 1. The value %d has been ignored.\n", tempInt);
}
// read in the rest of the line
move_to_eol(IN);
}
else if (strcmp(str, "\n") && strcmp(str, "\r\n")) // ignore blank lines
{ // display error message
printf("NOTE: The following line has been ignored: \"%s", str);
char ch;
do
{ // read in next ch
ch = fgetc(IN);
if (ch != '\r' && ch != '\n' && ch != EOF)
{ // print ch
printf("%c", ch);
}
else
{ // move to eol (if not there) and break
if (ch == '\r')
move_to_eol(IN);
break;
}
}
while (1);
printf("\"\n");
}
return;
}
int read_settings_item(configurations *S, FILE *IN)
/***************************************************************\
* USAGE: read a settings item from IN - return 0 if EOF *
\***************************************************************/
{
int strSize = 0;
char ch, *str = NULL;
// read in the next item
ch = fgetc(IN);
if (ch != EOF)
{ // reallocate str
strSize = 1;
str = errRealloc(str, strSize * sizeof(char));
str[0] = ch;
if (ch == ':' || ch == '\n')
{ // null terminate str
str = errRealloc(str, (strSize + 1) * sizeof(char));
str[strSize] = '\0';
strSize++;
}
else
{ // add on to str
while (1)
{ // read in next ch
ch = fgetc(IN);
if (ch != EOF)
{ // add to str
str = errRealloc(str, (strSize + 1) * sizeof(char));
str[strSize] = ch;
strSize++;
}
// see if to break
if (ch == ':' || ch == '\n' || ch == EOF)
{ // null terminate str
str = errRealloc(str, (strSize + 1) * sizeof(char));
str[strSize] = '\0';
strSize++;
// exit loop
break;
}
}
}
// determine if this item is valid and update S
update_settings_item(S, str, IN);
}
return (ch != EOF);
}
void load_settings_file(configurations *S, FILE *IN)
/***************************************************************\
* USAGE: load S from IN *
\***************************************************************/
{
// load default settings
load_default_settings(S);
// loop until complete
while (read_settings_item(S,IN));
return;
}
void load_settings(configurations *S, char *fileName)
/***************************************************************\
* USAGE: load S from fileName, if exists *
\***************************************************************/
{
FILE *IN = fopen(fileName, "r");
// see if file exists
if (IN == NULL)
{ // load default settings
load_default_settings(S);
}
else
{ // load settings from IN
load_settings_file(S, IN);
// close file
fclose(IN);
}
printf("\n");
return;
}