-
Notifications
You must be signed in to change notification settings - Fork 23
/
GetInit.c
executable file
·484 lines (419 loc) · 12 KB
/
GetInit.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
* SUMMARY: GetInit.c - Get initialization data from file
* USAGE: Not a stand-alone program
*
* AUTHOR: Bart Nijssen
* ORG: University of Washington, Department of Civil Engineering
* E-MAIL: [email protected]
* ORIG-DATE: 6-May-97 at 09:10:10
* DESCRIPTION: Functions are designed to behave more or less the same as the
* GetPrivateProfileString and GetPrivateProfileInt functions
* that are part of MFC (the functions here are a little less
* general). The functions search in a specified file for the
* entry that is associated with a certain key.
* The file is assumed to be organized in the same way as
* windows .ini files, i.e. the file consists of sections and
* key-value pairs. In addition there can be comments. Thus
* input files have the format:
*
* # comment
* [section]
* key=value # comment
* .
* .
* .
*
* DESCRIP-END.
* FUNCTIONS: GetInitString()
* GetInitLong()
* GetInitDouble()
* LocateKey()
* LocateSection()
* Strip()
* CopyDouble()
* CopyFloat()
* CopyInt()
* CopyLong()
* CopyShort()
* CopyUChar()
* IsEmptyStr()
* ReadInitFile()
* CreateNode()
* DeleteList()
* CountLines()
* Modification:
* $Id: GetInit.c, v 4.0 2012/10/31 Ning Exp $
* Comments:
*
*/
#define _CRT_SECURE_NO_DEPRECATE
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "DHSVMerror.h"
#include "fileio.h"
#include "getinit.h"
unsigned long GetInitString(const char *Section, const char *Key,
const char *Default, char *ReturnBuffer,
unsigned long BufferSize, LISTPTR Input)
{
LISTPTR SectionHead = NULL;
if ((SectionHead = LocateSection(Section, Input)) == NULL) {
strncpy(ReturnBuffer, Default, BufferSize);
return (unsigned long) strlen(ReturnBuffer);
}
if (!LocateKey(Key, ReturnBuffer, SectionHead)) {
strncpy(ReturnBuffer, Default, BufferSize);
return (unsigned long) strlen(ReturnBuffer);
}
return (unsigned long) strlen(ReturnBuffer);
}
/*#####################################################################################*/
long GetInitLong(const char *Section, const char *Key, long Default,
LISTPTR Input)
{
LISTPTR SectionHead = NULL;
char Buffer[BUFSIZE + 1];
char *EndPtr = NULL;
long Entry;
if ((SectionHead = LocateSection(Section, Input)) == NULL) {
return Default;
}
if (!LocateKey(Key, Buffer, SectionHead)) {
return Default;
}
Entry = strtol(Buffer, &EndPtr, 0);
if (EndPtr == Buffer) {
return Default;
}
return Entry;
}
/*#####################################################################################*/
double GetInitDouble(const char *Section, const char *Key, double Default,
LISTPTR Input)
{
LISTPTR SectionHead = NULL;
char Buffer[BUFSIZE + 1];
char *EndPtr = NULL;
double Entry;
if ((SectionHead = LocateSection(Section, Input)) == NULL) {
return Default;
}
if (!LocateKey(Key, Buffer, SectionHead)) {
return Default;
}
Entry = strtod(Buffer, &EndPtr);
if (EndPtr == Buffer) {
return Default;
}
return (Entry);
}
/*#####################################################################################
This function is used to find the matching key word in the input file for the "key"
specified in the fucntion: InitVegTable( )
#####################################################################################*/
unsigned char LocateKey(const char *Key, char *Entry, LISTPTR Input)
{
unsigned char Found = FALSE;
char Buffer[BUFSIZE + 1];
char KeyBuffer[BUFSIZE + 1];
char EntryBuffer[BUFSIZE + 1];
char *StrPtr = NULL;
/* Find a key in the current section */
if (Input) {
strncpy(Buffer, Input->Str, BUFSIZE);
while (!IsSection(Buffer)) {
/* Check whether the current line contains a key-entry pair */
if (IsKeyEntryPair(Buffer)) {
StrPtr = strchr(Buffer, SEPARATOR);
*StrPtr = '\0';
/* strcpy(KeyBuffer, Buffer); */
memmove(KeyBuffer, Buffer, strlen(Buffer)+1);
++StrPtr;
/* strcpy(EntryBuffer, StrPtr); */
memmove(EntryBuffer, StrPtr, strlen(StrPtr)+1);
Strip(KeyBuffer);
MakeKeyString(KeyBuffer);
if (strcmp(Key, KeyBuffer) == 0) {
Found = TRUE;
Strip(EntryBuffer);
/* strcpy(Entry, EntryBuffer); */
memmove(Entry, EntryBuffer, strlen(EntryBuffer)+1);
break;
}
}
/* Get the next line */
Input = Input->Next;
if (Input)
strncpy(Buffer, Input->Str, BUFSIZE);
else
break;
}
}
return Found;
}
/*#####################################################################################*/
LISTPTR LocateSection(const char *Section, LISTPTR Input)
{
char Buffer[BUFSIZE + 1];
char *StartPtr = NULL;
char *EndPtr = NULL;
while (Input) {
strncpy(Buffer, Input->Str, BUFSIZE);
if (IsSection(Buffer)) {
if (Buffer[0] == OPENSECTION) {
StartPtr = &Buffer[1];
EndPtr = strchr(Buffer, CLOSESECTION);
*EndPtr = '\0';
/* strcpy(Buffer, StartPtr);*/
memmove(Buffer, StartPtr, strlen(StartPtr)+1);
Strip(Buffer);
MakeKeyString(Buffer);
if (strcmp(Section, Buffer) == 0) {
Input = Input->Next;
break;
}
}
}
Input = Input->Next;
}
return Input;
}
/*#####################################################################################*/
unsigned char IsKeyEntryPair(char *Buffer)
{
char *StrSeparator = NULL;
StrSeparator = strchr(Buffer, SEPARATOR);
if (StrSeparator == NULL)
return FALSE;
return TRUE;
}
/*#####################################################################################*/
unsigned char IsSection(char *Buffer)
{
char *StrEndSection = NULL;
char *StrStartComment = NULL;
if (Buffer[0] != OPENSECTION)
return FALSE;
StrStartComment = strchr(Buffer, OPENCOMMENT);
StrEndSection = strchr(Buffer, CLOSESECTION);
if (StrEndSection == NULL)
return FALSE;
if (StrStartComment != NULL && StrStartComment < StrEndSection)
return FALSE;
return TRUE;
}
/*#####################################################################################*/
void Strip(char *Buffer)
{
char *StrEnd = NULL;
char *StrStart = Buffer;
/* remove leading whitespace */
while (*StrStart != '\0' && isspace((int) *StrStart))
++StrStart;
/* remove comment */
StrEnd = strchr(Buffer, OPENCOMMENT);
if (StrEnd != NULL)
*StrEnd = '\0';
/* remove trailing whitespace */
StrEnd = &Buffer[strlen(Buffer)];
--StrEnd;
while (StrEnd >= StrStart && isspace((int) *StrEnd)) {
*StrEnd = '\0';
--StrEnd;
}
/* strcpy(Buffer, StrStart); */
/* commented out by Ning b/c source and destination overlap */
memmove(Buffer, StrStart, strlen(StrStart)+1);
}
/*#####################################################################################*/
void MakeKeyString(char *Buffer)
{
char Str[BUFSIZE + 1];
char *PtrStr = Str;
char *PtrBuffer = Buffer;
/* toupper( ) converts the Buffer to uppercase and strip multiple spaces */
while (*PtrBuffer != '\0') {
if (*PtrBuffer == '\t') { //ignore the tab space
PtrBuffer++;
}
else {
*PtrStr = (char) toupper(*PtrBuffer);
if (isspace((int) *PtrBuffer)) {
while (isspace((int) *PtrBuffer))
PtrBuffer++;
}
else {
*PtrStr = (char) toupper(*PtrBuffer);
PtrBuffer++;}
PtrStr++;
}
}
*PtrStr = '\0';
strcpy(Buffer, Str);
}
/*#####################################################################################*/
int CopyDouble(double *Value, char *Str, const int NValues)
{
char *EndPtr = NULL;
int i;
for (i = 0; i < NValues; i++) {
Value[i] = strtod(Str, &EndPtr);
if (EndPtr == Str)
return FALSE;
Str = EndPtr;
}
if (EndPtr && *EndPtr != '\0')
return FALSE;
return TRUE;
}
/*#####################################################################################*/
int CopyFloat(float *Value, char *Str, const int NValues)
{
char *EndPtr = NULL;
int i;
for (i = 0; i < NValues; i++) {
Value[i] = (float) strtod(Str, &EndPtr);
if (EndPtr == Str)
return FALSE;
Str = EndPtr;
}
if (EndPtr && *EndPtr != '\0')
return FALSE;
return TRUE;
}
/*#####################################################################################*/
int CopyInt(int *Value, char *Str, const int NValues)
{
char *EndPtr = NULL;
int i;
for (i = 0; i < NValues; i++) {
Value[i] = (int) strtol(Str, &EndPtr, 0);
if (EndPtr == Str)
return FALSE;
Str = EndPtr;
}
if (EndPtr && *EndPtr != '\0')
return FALSE;
return TRUE;
}
/*#####################################################################################*/
int CopyLong(long *Value, char *Str, const int NValues)
{
char *EndPtr = NULL;
int i;
for (i = 0; i < NValues; i++) {
Value[i] = strtol(Str, &EndPtr, 0);
if (EndPtr == Str)
return FALSE;
Str = EndPtr;
}
if (EndPtr && *EndPtr != '\0')
return FALSE;
return TRUE;
}
/*#####################################################################################*/
int CopyShort(short *Value, char *Str, const int NValues)
{
char *EndPtr = NULL;
int i;
for (i = 0; i < NValues; i++) {
Value[i] = (short) strtol(Str, &EndPtr, 0);
if (EndPtr == Str)
return FALSE;
Str = EndPtr;
}
if (EndPtr && *EndPtr != '\0')
return FALSE;
return TRUE;
}
/*#####################################################################################*/
int CopyUChar(unsigned char *Value, char *Str, const int NValues)
{
char *EndPtr = NULL;
int i;
for (i = 0; i < NValues; i++) {
Value[i] = (unsigned char) strtol(Str, &EndPtr, 0);
if (EndPtr == Str)
return FALSE;
Str = EndPtr;
}
if (EndPtr && *EndPtr != '\0')
return FALSE;
return TRUE;
}
/*#####################################################################################*/
int IsEmptyStr(char *Str)
{
if (Str == NULL)
return TRUE;
if (Str[0] == '\0')
return TRUE;
return FALSE;
}
/*#####################################################################################*/
void ReadInitFile(char *TemplateFileName, LISTPTR * Input)
{
FILE *InFile = NULL; /* File with input information */
char Buffer[BUFSIZE + 1]; /* Tempora */
int i; /* counter */
int NLines; /* Number of lines in the input file */
LISTPTR Current = NULL; /* pointer to current node in list */
LISTPTR Head = NULL; /* pointer to the start of the list */
OpenFile(&InFile, (char *) TemplateFileName, "r", FALSE);
NLines = CountLines(InFile);
rewind(InFile);
for (i = 0; i < NLines; i++) {
fgets(Buffer, BUFSIZE, InFile);
Strip(Buffer);
if (IsSection(Buffer) || IsKeyEntryPair(Buffer)) {
if (Head == NULL) {
Head = CreateNode();
Current = Head;
*Input = Head;
}
else {
Current->Next = CreateNode();
Current = Current->Next;
}
strncpy(Current->Str, Buffer, BUFSIZE);
}
}
fclose(InFile);
return;
}
/*#####################################################################################*/
LISTPTR CreateNode(void)
{
LISTPTR NewNode = NULL;
NewNode = calloc(1, sizeof(INPUTSTRUCT));
if (NewNode == NULL)
ReportError("CreateNode", 1);
NewNode->Next = NULL;
return NewNode;
}
/*#####################################################################################*/
void DeleteList(LISTPTR Head)
{
LISTPTR Current = NULL;
Current = Head;
while (Current != NULL) {
Head = Head->Next;
free(Current);
Current = Head;
}
return;
}
/*#####################################################################################*/
int CountLines(FILE * InFile)
{
int NLines = 0;
char Buffer;
while ((Buffer = fgetc(InFile)) != EOF) {
if (Buffer == '\n')
NLines++;
}
return NLines;
}
/*#####################################################################################*/