-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathotherlib.c
More file actions
174 lines (154 loc) · 4.14 KB
/
otherlib.c
File metadata and controls
174 lines (154 loc) · 4.14 KB
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
/**
* @author Daniel Bubenicek (xbuben05)
* Projekt do predmetu IMS na FIT VUT v Brne (2019/2020)
* @date 5.12.2019
*/
#include <stdbool.h>
#include <errno.h>
#include "otherlib.h"
char *inputFlags[] = {"years:", "coal:", "solar:", "wind:", "hydro:", "nuclear:", "biomass:", "gas:", "other:", "verbose:", "frequency:"}; //names of input flags in config file
bool checkVars(long *vars);
void parseConfiguration(char *filename)
{
int c;
long values[NUMBER_OF_INPUT_FLAGS];
FILE *fd;
fd = fopen(filename, "r");
if(fd == NULL)
{
err = errFileOpen;
return; //nepovedlo se otevrit sooubor
}
for (int i = 0; i < NUMBER_OF_INPUT_FLAGS; i++)
{
for (int j = 0; j < FLAG_MAX_LEN; j++)
{
if((c = fgetc(fd)) == EOF)
{
fclose(fd);
err = errFileFormat;
return; //spatny format souboru
}
if(c != inputFlags[i][j])
{
fclose(fd);
err = errFileFormat;
return; //spatny format souboru
}
if(c == ':') //konec flagu
{
if((values[i] = parseValue(fd)) == -1)
{
fclose(fd);
err = errFileFormat;
return;
}
break;
}
}
}
fclose(fd);
if(!checkVars(values))
{
err = errSum100;
return;
}
setVarNamesFromConfFile(values);
}
bool checkVars(long *vars) {
//check if finalPercentageProduce give 100% in the sum
long sum = 100;
for(int i = 1; i < 9; i++)
sum -= vars[i];
if(sum)
return false;
return true;
}
void setVarNamesFromConfFile(const long *values) {
int i = 0;
MlimitYears = (int) values[i++];
MfinalPercentageProduceCoal = (float) values[i++];
MfinalPercentageProduceSolar = (float) values[i++];
MfinalPercentageProduceWind = (float) values[i++];
MfinalPercentageProduceHydro = (float) values[i++];
MfinalPercentageProduceNuclear = (float) values[i++];
MfinalPercentageProduceBiomass = (float) values[i++];
MfinalPercentageProduceGas = (float) values[i++];
MfinalPercentageProduceOther = (float) values[i++];
Mverbose = (int) values[i++];
MlogFrequency = (int) values[i++];
}
long parseValue(FILE *fd)
{
int c;
char value[VALUE_MAX_LEN];
long retValue;
char *endptr;
for (int i = 0; i < VALUE_MAX_LEN-1; i++)
{
if((c = fgetc(fd)) == EOF)
{
return -1; //spatny format souboru
}
if(c == '\r')
continue;
if(c == '\n' && i == 0)
{
return -1;
}
if(c == '\n') //the end of value
{
value[i] = '\0';
errno = 0;
retValue = strtol(value, &endptr, 10);
if(strcmp("", endptr) != 0)
{
return -1;
}
if(errno != 0)
return -1;
return retValue;
}
else
value[i] = (char) c;
}
return -1;
}
void messageAndExit() {
switch (err)
{
case errFileFormat:
fprintf(stderr, "Config file has wrong format!\n");
break;
case errFileOpen:
fprintf(stderr, "Config file cannot be opened!\n");
break;
case errSum100:
fprintf(stderr, "Sum of final ratio of sources is not 100 %%! Please check imsConf.txt.\n");
break;
case errArgs:
fprintf(stderr, "Wrong number of program arguments! There is just one argument - name of config file.\n");
break;
default:
fprintf(stderr, "Unspecified error!\n");
break;
}
exit(err);
}
long Mpow10(unsigned int power)
{
long base = 10;
long result = 1;
for (unsigned int i = 0; i < power; i++) {
result *= base;
}
return result;
}
void parseArg(int argc, char **argv, char *filename) {
if(argc != 2)
{
err = errArgs;
return;
}
strncpy(filename, argv[1], FILENAME_MAX_LEN-1);
}