-
Notifications
You must be signed in to change notification settings - Fork 0
/
ini_file.cpp
executable file
·286 lines (254 loc) · 12.3 KB
/
ini_file.cpp
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
/* this is a generic ini file reader written by Jonathan Howland of the Woods Hole Oceanographic Institution
It is a modification of the ini file reader initially written for the rov software development
program conducted jointly with Louis Whitcomb of the Johns Hopkins University. The major change is the
"c++-ification" of the code
*/
#include <stdio.h>
#include <string.h>
#include "compilation.h"
#include "ini_file.h"
IniFile::IniFile()
{
fd = NULL;
fileName = NULL;
}
void IniFile::close_ini()
{
fclose(fd);
}
void IniFile::setFD(FILE *inFD)
{
fclose(fd);
fd = inFD;
}
FILE *IniFile::getFD()
{
return fd;
}
char *IniFile::getFilename()
{
return fileName;
}
int IniFile::open_ini(char *file_name)
{
fd = fopen(file_name,"r");
if(fd){
fileName = strdup(file_name);
return GOOD_INI_FILE_READ;
}
else{
return BAD_INI_FILE_READ;
}
}
int IniFile::read_int(const char *section, const char *attribute, int default_int)
{
int file_position, string_position, attribute_length, count, integer_value;
char my_line[MAX_CHARACTER_COUNT], *ch;
char identifier[MAX_CHARACTER_COUNT];
char section_label[MAX_CHARACTER_COUNT];
// form the section variable
if(!fd){
return default_int;
}
string_position = sprintf (section_label, "[%s]", section);
attribute_length = strlen (attribute);
// go to the beginning of the file
file_position = fseek (fd, 0, SEEK_SET);
if (file_position == -1){
return default_int;
}
else{
while (!feof (fd)){
ch = fgets (&(my_line[0]), MAX_CHARACTER_COUNT - 1, fd);
if (ch){
count = sscanf (my_line, "%s", identifier);
if ((count == 1) && (!_stricmp (section_label, identifier))){ // found the section, now read within it
while (!feof (fd)){
ch = fgets (&(my_line[0]), MAX_CHARACTER_COUNT - 1, fd);
if (my_line[0] == '['){ // this is a new section
return default_int;
}
if (!_strnicmp (&(my_line[0]), attribute, attribute_length)){ // the first part is right
if (my_line[attribute_length] == '='){ // the = is in the right place
count = sscanf (&(my_line[attribute_length + 1]), "%d", &integer_value);
if (count == 1){
return integer_value;
}
else{
return default_int;
}
} // no equal sign, keep reading
} // attribute doesn't match, keep reading
} // end while not feof looking within section
} // not the section identifier, keep reading
} // end of got my_line
else{
return default_int;
}
} // end while not feof looking for section
} // end of valid file pointer, must have not found what we were looking for
return default_int;
}
int IniFile::read_HexInt(const char *section, const char *attribute, int default_int)
{
int file_position, string_position, attribute_length, count, integer_value;
char my_line[MAX_CHARACTER_COUNT], *ch;
char identifier[MAX_CHARACTER_COUNT];
char section_label[MAX_CHARACTER_COUNT];
// form the section variable
if(!fd){
return default_int;
}
string_position = sprintf (section_label, "[%s]", section);
attribute_length = strlen (attribute);
// go to the beginning of the file
file_position = fseek (fd, 0, SEEK_SET);
if (file_position == -1){
return default_int;
}
else{
while (!feof (fd)){
ch = fgets (&(my_line[0]), MAX_CHARACTER_COUNT - 1, fd);
if (ch){
count = sscanf (my_line, "%s", identifier);
if ((count == 1) && (!_stricmp (section_label, identifier))){ // found the section, now read within it
while (!feof (fd)){
ch = fgets (&(my_line[0]), MAX_CHARACTER_COUNT - 1, fd);
if (my_line[0] == '['){ // this is a new section
return default_int;
}
if (!_strnicmp (&(my_line[0]), attribute, attribute_length)){ // the first part is right
if (my_line[attribute_length] == '='){ // the = is in the right place
count = sscanf (&(my_line[attribute_length + 1]), "%x", &integer_value);
if (count == 1){
return integer_value;
}
else{
return default_int;
}
} // no equal sign, keep reading
} // attribute doesn't match, keep reading
} // end while not feof looking within section
} // not the section identifier, keep reading
} // end of got my_line
else{
return default_int;
}
} // end while not feof looking for section
} // end of valid file pointer, must have not found what we were looking for
return default_int;
}
double IniFile::read_double (const char *section, const char *attribute, double default_double)
{
int file_position, string_position, attribute_length, count;
double double_value;
char my_line[MAX_CHARACTER_COUNT], *ch;
char identifier[MAX_CHARACTER_COUNT];
char section_label[MAX_CHARACTER_COUNT];
// form the section variable
if(!fd){
return default_double;
}
string_position = sprintf (section_label, "[%s]", section);
attribute_length = strlen (attribute);
// go to the beginning of the file
file_position = fseek (fd, 0, SEEK_SET);
if (file_position == -1){
return default_double;
}
else{
while (!feof (fd)){
ch = fgets (&(my_line[0]), MAX_CHARACTER_COUNT - 1, fd);
if (ch){
count = sscanf (my_line, "%s", identifier);
if ((count == 1) && (!_stricmp (section_label, identifier))){ // found the section, now read within it
while (!feof (fd)){
ch = fgets (&(my_line[0]), MAX_CHARACTER_COUNT - 1, fd);
if (my_line[0] == '['){ // this is a new section
return default_double;
}
if (!_strnicmp (&(my_line[0]), attribute, attribute_length)){ // the first part is right
if (my_line[attribute_length] == '='){ // the = is in the right place
count = sscanf (&(my_line[attribute_length + 1]), "%lf", &double_value);
if (count == 1){
return double_value;
}
else{
return default_double;
}
} // no equal sign, keep reading
} // attribute doesn't match, keep reading
} // end while not feof looking within section
} // not the section identifier, keep reading
} // end of got my_line
else{
return default_double;
}
} // end while not feof looking for section
} // end of valid file pointer, must have not found what we were looking for
return default_double;
}
char *IniFile::read_string (const char *section, const char *attribute, const char *default_string)
{
int file_position, string_position, attribute_length, count, j, my_char_position;
// change this to put in a strdup
char my_line[MAX_CHARACTER_COUNT], *ch;
char identifier[MAX_CHARACTER_COUNT];
char section_label[MAX_CHARACTER_COUNT];
char new_string[MAX_CHARACTER_COUNT];
// form the section variable
if(!fd){
return strdup (default_string);;
}
string_position = sprintf (section_label, "[%s]", section);
attribute_length = strlen (attribute);
// go to the beginning of the file
file_position = fseek (fd, 0, SEEK_SET);
if (file_position == -1){
return strdup (default_string);
}
else{
while (!feof (fd)){
ch = fgets (&(my_line[0]), MAX_CHARACTER_COUNT - 1, fd);
if (ch){
count = sscanf (my_line, "%s", identifier);
if ((count == 1) && (!_stricmp (section_label, identifier))){ // found the section, now read within it
while (!feof (fd)){
ch = fgets (&(my_line[0]), MAX_CHARACTER_COUNT - 1, fd);
if (my_line[0] == '['){ // this is a new section
return strdup (default_string);
}
if (!_strnicmp (&(my_line[0]), attribute, attribute_length)){ // the first part is right
if (my_line[attribute_length] == '='){ // the = is in the right place
// check for quotation marks
if (my_line[attribute_length + 1] == '"'){
j = 0;
my_char_position = attribute_length + 2;
while ((my_line[my_char_position] != '"') && (my_char_position < MAX_CHARACTER_COUNT)){
new_string[j] = my_line[my_char_position++];
j++;
}
count = 1;
new_string[j] = '\0';
}
else{
count = sscanf (&(my_line[attribute_length + 1]), "%s", new_string);
}
if ((count == 1) && (strlen (new_string) < MAX_CHARACTER_COUNT)){
return strdup (new_string);
}
else{
return strdup (default_string);
}
} // no equal sign, keep reading
} // attribute doesn't match, keep reading
} // end while not feof looking within section
} // not the section identifier, keep reading
} // end of got my_line
else{
return strdup (default_string);
}
} // end while not feof looking for section
} // end of valid file pointer, must have not found what we were looking for
return strdup (default_string);
}