-
Notifications
You must be signed in to change notification settings - Fork 0
/
gwypini.c
1070 lines (874 loc) · 29.3 KB
/
gwypini.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*----------------------------------------------------------------------
| gwini.c
|
| This file contains portable routines to read and write "INI" files. These
| routines came from prime95. Thus, there are a few idiosyncracies that may
| not be useful to a wide audience.
|
| NOTE: These routines only work if you open no more than 10 ini files. Also,
| you must not change the working directory at any time during program execution.
|
| Copyright 2016-2017 Mersenne Research, Inc. All rights reserved
+---------------------------------------------------------------------*/
#ifdef __cplusplus
extern "C" {
#endif
/* Include files */
#ifdef _WIN32
#include <io.h>
#include <malloc.h>
#else
#include <unistd.h>
#define _O_APPEND O_APPEND
#define _O_RDONLY O_RDONLY
#define _O_WRONLY O_WRONLY
#define _O_RDWR O_RDWR
#define _O_CREAT O_CREAT
#define _O_TRUNC O_TRUNC
#define _O_BINARY 0
#define _O_TEXT 0
#define _open open
#define _close close
#define _read read
#define _write write
#define _unlink unlink
#define _stricmp strcasecmp
#endif
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include "gwcommon.h"
#include "gwypini.h"
//#include "gwthread.h"
//#include "gwutil.h"
/* Define the world/group/owner read/write attributes for creating files */
/* I've always used 0666 in Unix (everyone gets R/W access), but MSVC 8 */
/* now refuses to work with that setting -- insisting on 0600 instead. */
#ifdef _WIN32
#define CREATE_FILE_ACCESS 0600
#else
#define CREATE_FILE_ACCESS 0666
#endif
#ifndef _O_CREAT
#define _O_APPEND O_APPEND
#define _O_RDONLY O_RDONLY
#define _O_WRONLY O_WRONLY
#define _O_RDWR O_RDWR
#define _O_CREAT O_CREAT
#define _O_TRUNC O_TRUNC
#define _O_BINARY O_BINARY
#define _O_TEXT O_TEXT
#endif
/* Structures used in managing INI files */
#define INI_LINE_NORMAL 0 /* A normal keyword=value INI line */
#define INI_LINE_COMMENT 2 /* A comment line */
#define INI_LINE_HEADER 3 /* A section header line */
struct IniLine {
char *keyword;
char *value;
int line_type;
};
struct IniCache {
char *filename;
int immediate_writes;
int dirty;
unsigned int num_lines;
unsigned int array_size;
struct IniLine **lines;
};
/* Global variables */
//gwmutex INI_MUTEX = NULL; /* Lock for accessing INI files */
//gwmutex INI_ADD_MUTEX = NULL; /* Lock for accessing INI add-in files */
void (*INI_ERROR_CALLBACK)(const char *, int, const char *); /* Callback routine when illegal line read from INI file. */
/* Arguments are file name, line number, text on the line */
/* Forward declarations of internal routines */
struct IniCache *openIniFile (const char *, int);
void growIniLineArray (struct IniCache *);
void parse_timed_ini_value (const char *, unsigned int *, unsigned int *, unsigned int *);
/****************************************************************************/
/* Routines to read and write INI files */
/****************************************************************************/
/*void IniFileReread ( /* Read or reread an INI file */
/* const char *filename)
{
struct IniCache *p;
/* Read the ini file while locking out other callers */
/* if (INI_MUTEX == NULL) gwmutex_init (&INI_MUTEX);
gwmutex_lock (&INI_MUTEX);
p = openIniFile (filename, 1);
gwmutex_unlock (&INI_MUTEX);
}
*/
void writeIniFile ( /* Write a changed INI file to disk */
struct IniCache *p)
{
int fd;
unsigned int j;
char buf[2000];
/* Delay writing the file unless this INI file is written */
/* to immediately */
if (!p->immediate_writes) {
p->dirty = 1;
return;
}
/* Create and write out the INI file */
fd = _open (p->filename, _O_CREAT | _O_TRUNC | _O_WRONLY | _O_TEXT, CREATE_FILE_ACCESS);
if (fd < 0) return;
for (j = 0; j < p->num_lines; j++) {
if (p->lines[j]->line_type == INI_LINE_COMMENT) {
strcpy (buf, p->lines[j]->value);
} else if (p->lines[j]->line_type == INI_LINE_HEADER) {
strcpy (buf, p->lines[j]->value);
} else {
strcpy (buf, p->lines[j]->keyword);
strcat (buf, "=");
strcat (buf, p->lines[j]->value);
}
strcat (buf, "\n");
(void) _write (fd, buf, (unsigned int) strlen (buf));
}
p->dirty = 0;
_close (fd);
}
/* Delay writing changes to the INI file */
void IniDelayWrites (
const char *filename)
{
struct IniCache *p;
p = openIniFile (filename, 0);
p->immediate_writes = FALSE;
}
/* Resume immediately writing changes to the INI file */
void IniResumeImmediateWrites (
const char *filename)
{
struct IniCache *p;
p = openIniFile (filename, 0);
p->immediate_writes = TRUE;
if (p->dirty) writeIniFile (p);
}
/* Merge one "add file" into an ini file. Assumes the ini file has been */
/* freshly re-read from disk. This can also is used to copy one ini file */
/* into a section of another ini file. */
/*void IniAddFileMerge (
const char *ini_filename,
const char *add_filename,
const char *section_to_copy_to)
{
struct IniCache *p, *q;
const char *section;
unsigned int j;
/* Obtain a lock so that only one thread adds to the INI file. We will release */
/* lock once we've deleted the add file. */
/* if (INI_ADD_MUTEX == NULL) gwmutex_init (&INI_ADD_MUTEX);
gwmutex_lock (&INI_ADD_MUTEX);
/* Open ini files */
/* p = openIniFile (ini_filename, 0);
q = openIniFile (add_filename, 1);
/* Save up all the writes */
/* p->immediate_writes = FALSE;
/* Loop through all the lines in the add file, adding them to the */
/* base ini file */
/* section = section_to_copy_to;
for (j = 0; j < q->num_lines; j++) {
if (q->lines[j]->line_type == INI_LINE_HEADER) {
if (section_to_copy_to == NULL)
section = q->lines[j]->keyword;
}
else if (q->lines[j]->line_type != INI_LINE_COMMENT)
IniSectionWriteString (ini_filename, section, q->lines[j]->keyword, q->lines[j]->value);
}
/* Output all the saved up writes */
/* p->immediate_writes = TRUE;
writeIniFile (p);
/* Delete the add file */
/* _unlink (add_filename);
/* Unlock and return */
/* gwmutex_unlock (&INI_ADD_MUTEX);
}
/****************************************************************************/
/* Routines to read and write string values */
/****************************************************************************/
void IniGetString ( /* Get a string value from the global section of the INI file */
const char *filename,
const char *keyword,
char *val,
unsigned int val_bufsize,
const char *default_val)
{
IniSectionGetString (filename, NULL, keyword, val, val_bufsize, default_val);
}
void IniSectionGetString ( /* Get a string value from the specified section of the INI file */
const char *filename,
const char *section,
const char *keyword,
char *val,
unsigned int val_bufsize,
const char *default_val)
{
IniSectionGetNthString (filename, section, keyword, 1, val, val_bufsize, default_val);
}
void IniGetNthString ( /* Get keyword's Nth string value from the specified section of the INI file */
const char *filename,
const char *keyword,
int nth,
char *val,
unsigned int val_bufsize,
const char *default_val)
{
IniSectionGetNthString (filename, NULL, keyword, nth, val, val_bufsize, default_val);
}
void IniSectionGetNthString ( /* Get keyword's Nth string value from the specified section of the INI file */
const char *filename,
const char *section,
const char *keyword,
int nth,
char *val,
unsigned int val_bufsize,
const char *default_val)
{
unsigned int seconds;
IniSectionGetNthTimedString (filename, section, keyword, nth, val, val_bufsize, default_val, &seconds);
}
void IniGetTimedString ( /* Get a time-sensitive string value from the global section of the INI file */
const char *filename,
const char *keyword,
char *val,
unsigned int val_bufsize,
const char *default_val,
unsigned int *seconds) /* Return length of time this timed INI setting is good for. */
{
IniSectionGetTimedString (filename, NULL, keyword, val, val_bufsize, default_val, seconds);
}
void IniSectionGetTimedString ( /* Get a time-sensitive string value from the specified section of the INI file */
const char *filename,
const char *section,
const char *keyword,
char *val,
unsigned int val_bufsize,
const char *default_val,
unsigned int *seconds) /* Return length of time this timed INI setting is good for. */
{
IniSectionGetNthTimedString (filename, section, keyword, 1, val, val_bufsize, default_val, seconds);
}
/*void IniSectionGetNthTimedString ( /* Get keyword's Nth time-sensitive string value from the specified section of the INI file */
/* const char *filename,
const char *section,
const char *keyword,
int nth,
char *val,
unsigned int val_bufsize,
const char *default_val,
unsigned int *seconds) /* Return length of time this timed INI setting is good for. */
/*{
const char *p;
unsigned int start, len;
/* Lookup the keyword */
/* p = IniSectionGetNthStringRaw (filename, section, keyword, nth);
/* If we found the keyword in the INI file, then */
/* support different return values based on the time of day. */
/* if (p != NULL) {
parse_timed_ini_value (p, &start, &len, seconds);
if (len) {
truncated_strcpy_with_len (val, val_bufsize, p+start, len);
return;
}
} else {
*seconds = 0;
}
/* Copy the default value to the caller's buffer */
/* if (default_val)
truncated_strcpy (val, val_bufsize, default_val);
else
val[0] = 0;
}*/
const char *IniSectionGetStringRaw ( /* Return keyword's raw string value from a specific section of the INI file. */
const char *filename,
const char *section,
const char *keyword)
{
return (IniSectionGetNthStringRaw (filename, section, keyword, 1));
}
const char *IniSectionGetNthStringRaw ( /* Return keyword's Nth raw string value from global section of the INI file. */
const char *filename,
const char *section,
const char *keyword,
int nth) /* Nth occurrence of the keyword (nth starts at 1) */
{
struct IniCache *p;
unsigned int i;
const char *retval;
Open ini file */
if (INI_MUTEX == NULL) gwmutex_init (&INI_MUTEX);
gwmutex_lock (&INI_MUTEX);
p = openIniFile (filename, 0);
/* Skip to the correct section */
i = 0;
if (section != NULL) {
for ( ; i < p->num_lines; i++) {
if (p->lines[i]->line_type == INI_LINE_HEADER &&
_stricmp (section, p->lines[i]->keyword) == 0) {
i++;
break;
}
}
}
/* Look for the keyword within this section */
for ( ; ; i++) {
if (i == p->num_lines ||
p->lines[i]->line_type == INI_LINE_HEADER) {
retval = NULL;
break;
}
if (p->lines[i]->line_type == INI_LINE_NORMAL &&
_stricmp (keyword, p->lines[i]->keyword) == 0 &&
--nth == 0) {
retval = p->lines[i]->value;
break;
}
}
/* Unlock and return */
gwmutex_unlock (&INI_MUTEX);
return (retval);
}
void IniWriteString ( /* Write a string value to the global section of the INI file. */
const char *filename,
const char *keyword,
const char *val)
{
IniSectionWriteString (filename, NULL, keyword, val);
}
void IniSectionWriteString ( /* Write a string value to a specified section of the INI file. */
const char *filename,
const char *section,
const char *keyword,
const char *val)
{
IniSectionWriteNthString (filename, section, keyword, 1, val);
}
void IniWriteNthString ( /* Write keyword's Nth string value to a specified section of the INI file. */
const char *filename,
const char *keyword,
int nth,
const char *val) /* New value. If NULL, delete all occurrences of keyword entry */
{
IniSectionWriteNthString (filename, NULL, keyword, nth, val);
}
/*void IniSectionWriteNthString ( /* Write keyword's Nth string value to a specified section of the INI file. */
/* const char *filename,
const char *section,
const char *keyword,
int nth,
const char *val) /* New value. If NULL, delete all occurrences of keyword entry */
/*{
struct IniCache *p;
unsigned int i, j, insertion_point;
int lines_were_deleted;
/* Open ini file */
/* if (INI_MUTEX == NULL) gwmutex_init (&INI_MUTEX);
gwmutex_lock (&INI_MUTEX);
p = openIniFile (filename, 0);
/* Skip to the correct section. If the section does not exist, create it */
/* i = 0;
if (section != NULL) {
for ( ; ; i++) {
if (i == p->num_lines) {
if (val == NULL) goto nowrite_done;
growIniLineArray (p);
p->lines[i] = (struct IniLine *) malloc (sizeof (struct IniLine));
p->lines[i]->line_type = INI_LINE_COMMENT;
p->lines[i]->keyword = NULL;
p->lines[i]->value = (char *) malloc (1);
p->lines[i]->value[0] = 0;
p->num_lines++;
i++;
growIniLineArray (p);
p->lines[i] = (struct IniLine *) malloc (sizeof (struct IniLine));
p->lines[i]->line_type = INI_LINE_HEADER;
p->lines[i]->keyword = (char *) malloc (strlen (section) + 1);
strcpy (p->lines[i]->keyword, section);
p->lines[i]->value = (char *) malloc (strlen (section) + 3);
sprintf (p->lines[i]->value, "[%s]", section);
p->num_lines++;
i++;
break;
}
if (p->lines[i]->line_type == INI_LINE_HEADER &&
_stricmp (section, p->lines[i]->keyword) == 0) {
i++;
break;
}
}
}
/* Look for the keyword within this section */
/* insertion_point = i;
lines_were_deleted = FALSE;
for ( ; ; i++) {
if (i == p->num_lines ||
p->lines[i]->line_type == INI_LINE_HEADER ||
(p->lines[i]->line_type != INI_LINE_COMMENT &&
_stricmp (p->lines[i]->keyword, "Time") == 0)) {
/* Ignore request if we are deleting line */
/* if (val == NULL) {
if (lines_were_deleted) goto write_done;
goto nowrite_done;
}
/* Make sure the line array has room for the new line */
/* growIniLineArray (p);
/* Shuffle entries down to make room for this entry */
/* i = insertion_point;
for (j = p->num_lines; j > i; j--)
p->lines[j] = p->lines[j-1];
/* Allocate and fill in a new line structure */
/* p->lines[i] = (struct IniLine *) malloc (sizeof (struct IniLine));
p->lines[i]->line_type = INI_LINE_NORMAL;
p->lines[i]->keyword = (char *) malloc (strlen (keyword) + 1);
strcpy (p->lines[i]->keyword, keyword);
p->lines[i]->value = NULL;
p->num_lines++;
break;
}
/* If this is not a blank line, then if we need to insert a new line, */
/* insert it after this line. In other words, insert new entries before */
/* any blank lines at the end of a section */
/* if (p->lines[i]->line_type != INI_LINE_COMMENT ||
p->lines[i]->value[0]) {
insertion_point = i + 1;
}
/* If this is the keyword we are looking for, then we will replace the */
/* value if it has changed. */
/* if (p->lines[i]->line_type == INI_LINE_NORMAL &&
_stricmp (keyword, p->lines[i]->keyword) == 0) {
/* Delete the entry (all occurrences) if there is no new value */
/* if (val == NULL) {
/* Free the data associated with the given line */
/* free (p->lines[i]->keyword);
free (p->lines[i]->value);
free (p->lines[i]);
/* Delete the line from the lines array */
/* for (j = i + 1; j < p->num_lines; j++) p->lines[j-1] = p->lines[j];
p->num_lines--;
i--;
lines_were_deleted = TRUE;
}
/* Replace the entry if this is the nth occurrence */
/* else if (--nth == 0) {
if (strcmp (val, p->lines[i]->value) == 0) goto nowrite_done;
break;
}
}
}
/* Replace the value associated with the keyword */
/* if (val != NULL) {
free (p->lines[i]->value);
p->lines[i]->value = (char *) malloc (strlen (val) + 1);
strcpy (p->lines[i]->value, val);
}
/* Write the INI file back to disk */
/*write_done:
writeIniFile (p);
/* Unlock and return */
/*nowrite_done:
gwmutex_unlock (&INI_MUTEX);
}
/****************************************************************************/
/* Routines to read and write integer values */
/****************************************************************************/
long IniGetInt ( /* Get an integer value from the global section of the INI file */
const char *filename,
const char *keyword,
long default_val)
{
return (IniSectionGetInt (filename, NULL, keyword, default_val));
}
long IniSectionGetInt ( /* Get an integer value from specified section of the INI file */
const char *filename,
const char *section,
const char *keyword,
long default_val)
{
unsigned int seconds;
return (IniSectionGetTimedInt (filename, section, keyword, default_val, &seconds));
}
long IniGetTimedInt ( /* Get a time-sensitive integer value from the global section of the INI file */
const char *filename,
const char *keyword,
long default_val,
unsigned int *seconds) /* Return length of time this timed INI setting is good for. */
{
return (IniSectionGetTimedInt (filename, NULL, keyword, default_val, seconds));
}
long IniSectionGetTimedInt ( /* Get a time-sensitive integer value from specified section of the INI file */
const char *filename,
const char *section,
const char *keyword,
long default_val,
unsigned int *seconds) /* Return length of time this timed INI setting is good for. */
{
char buf[20], defval[20];
sprintf (defval, "%ld", default_val);
IniSectionGetTimedString (filename, section, keyword, buf, 20, defval, seconds);
return (atol (buf));
}
void IniWriteInt ( /* Write an integer value to the global section of the INI file */
const char *filename,
const char *keyword,
long val)
{
IniSectionWriteInt (filename, NULL, keyword, val);
}
void IniSectionWriteInt ( /* Write an integer value to specified section of the INI file */
const char *filename,
const char *section,
const char *keyword,
long val)
{
char buf[20];
sprintf (buf, "%ld", val);
IniSectionWriteString (filename, section, keyword, buf);
}
/****************************************************************************/
/* Routines to read and write float values */
/****************************************************************************/
float IniGetFloat ( /* Get a floating point value from the global section of the INI file */
const char *filename,
const char *keyword,
float default_val)
{
return (IniSectionGetFloat (filename, NULL, keyword, default_val));
}
float IniSectionGetFloat ( /* Get a floating point value from the specified section of the INI file */
const char *filename,
const char *section,
const char *keyword,
float default_val)
{
unsigned int seconds;
return (IniSectionGetTimedFloat (filename, section, keyword, default_val, &seconds));
}
float IniGetTimedFloat ( /* Get a time-sensitive floating point value from the global section of the INI file */
const char *filename,
const char *keyword,
float default_val,
unsigned int *seconds) /* Return length of time this timed INI setting is good for. */
{
return (IniSectionGetTimedFloat (filename, NULL, keyword, default_val, seconds));
}
float IniSectionGetTimedFloat ( /* Get a time-sensitive floating point value from the specified section of the INI file */
const char *filename,
const char *section,
const char *keyword,
float default_val,
unsigned int *seconds) /* Return length of time this timed INI setting is good for. */
{
char buf[20], defval[20];
sprintf (defval, "%f", default_val);
IniSectionGetTimedString (filename, section, keyword, buf, 20, defval, seconds);
return ((float) atof (buf));
}
void IniWriteFloat ( /* Write a floating point value to the global section of the INI file */
const char *filename,
const char *keyword,
float val)
{
IniSectionWriteFloat (filename, NULL, keyword, val);
}
void IniSectionWriteFloat ( /* Write a floating point value to the specified section of the INI file */
const char *filename,
const char *section,
const char *keyword,
float val)
{
/* Assume FLT_MAX is 3.40282e+038, the maximum significant digits that */
/* can be stored in this buf is 12. ((sizeof(buf))-sizeof("-.E+038")) */
char buf[20];
sprintf (buf, "%11g", val);
IniSectionWriteString (filename, section, keyword, buf);
}
/****************************************************************************/
/* Internal routines */
/****************************************************************************/
/* Open and read an INI file */
struct IniCache *openIniFile (
const char *filename,
int forced_read)
{
static struct IniCache *cache[10] = {0};
struct IniCache *p;
FILE *fd;
unsigned int i;
char line[1024];
char *val;
/* See if file is cached */
for (i = 0; i < 10; i++) {
p = cache[i];
if (p == NULL) {
p = (struct IniCache *) malloc (sizeof (struct IniCache));
p->filename = (char *) malloc (strlen (filename) + 1);
strcpy (p->filename, filename);
p->immediate_writes = 1;
p->dirty = 0;
p->num_lines = 0;
p->array_size = 0;
p->lines = NULL;
forced_read = 1;
cache[i] = p;
break;
}
if (strcmp (filename, p->filename) == 0)
break;
}
/* Skip reading the ini file if appropriate */
if (!forced_read) return (p);
if (p->dirty) return (p);
/* Free the data if we've already read some in */
for (i = 0; i < p->num_lines; i++) {
free (p->lines[i]->keyword);
free (p->lines[i]->value);
free (p->lines[i]);
}
p->num_lines = 0;
/* Read the IniFile */
fd = fopen (filename, "r");
if (fd == NULL) return (p);
while (fgets (line, sizeof (line), fd)) {
if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
if (line[0] && line[strlen(line)-1] == '\r') line[strlen(line)-1] = 0;
/* Allocate and fill in a new line structure */
growIniLineArray (p);
i = p->num_lines++;
p->lines[i] = (struct IniLine *) malloc (sizeof (struct IniLine));
/* Flag section headers */
if (line[0] == '[') {
char *q;
p->lines[i]->keyword = (char *) malloc (strlen (line) + 1);
p->lines[i]->value = (char *) malloc (strlen (line) + 1);
p->lines[i]->line_type = INI_LINE_HEADER;
strcpy (p->lines[i]->value, line);
strcpy (p->lines[i]->keyword, line+1);
for (q = p->lines[i]->keyword; *q; q++)
if (*q == ']') {
*q = 0;
break;
}
}
/* Save comment lines - any line that doesn't begin with a letter */
else if ((line[0] < 'A' || line[0] > 'Z') &&
(line[0] < 'a' || line[0] > 'z')) {
p->lines[i]->keyword = NULL;
p->lines[i]->value = (char *) malloc (strlen (line) + 1);
p->lines[i]->line_type = INI_LINE_COMMENT;
strcpy (p->lines[i]->value, line);
}
/* Otherwise, parse keyword=value lines */
else {
val = strchr (line, '=');
if (val == NULL) {
/* Unparseable line, call user error handling routine */
if (INI_ERROR_CALLBACK != NULL) (*INI_ERROR_CALLBACK)(filename, p->num_lines, line);
/* Save unparseable line as a comment */
p->lines[i]->keyword = NULL;
p->lines[i]->value = (char *) malloc (strlen (line) + 1);
p->lines[i]->line_type = INI_LINE_COMMENT;
strcpy (p->lines[i]->value, line);
} else {
*val++ = 0;
p->lines[i]->keyword = (char *) malloc (strlen (line) + 1);
p->lines[i]->value = (char *) malloc (strlen (val) + 1);
p->lines[i]->line_type = INI_LINE_NORMAL;
strcpy (p->lines[i]->keyword, line);
strcpy (p->lines[i]->value, val);
}
}
}
fclose (fd);
return (p);
}
void growIniLineArray (
struct IniCache *p)
{
struct IniLine **newlines;
if (p->num_lines != p->array_size) return;
newlines = (struct IniLine **) malloc ((p->num_lines + 100) * sizeof (struct IniLine *));
if (p->num_lines) {
memcpy (newlines, p->lines, p->num_lines * sizeof (struct IniLine *));
free (p->lines);
}
p->lines = newlines;
p->array_size = p->num_lines + 100;
}
/* Routines to help analyze a timed line in an INI file */
void parseTimeLine (
const char **line,
int *start_day,
int *end_day,
int *start_time,
int *end_time)
{
const char *p;
/* Get the days of the week, e.g. 1-5 */
p = *line;
*start_day = atoi (p); while (isdigit (*p)) p++;
if (*p == '-') {
p++;
*end_day = atoi (p); while (isdigit (*p)) p++;
} else
*end_day = *start_day;
/* Now do time portion. If none present, then assume the numbers we */
/* parsed above were times, not days of the week. */
if (*p == '/')
p++;
else {
p = *line;
*start_day = 1;
*end_day = 7;
}
*start_time = atoi (p) * 60; while (isdigit (*p)) p++;
if (*p == ':') {
p++;
*start_time += atoi (p); while (isdigit (*p)) p++;
}
if (*p == '-') p++; /* Skip '-' */
*end_time = atoi (p) * 60; while (isdigit (*p)) p++;
if (*p == ':') {
p++;
*end_time += atoi (p); while (isdigit (*p)) p++;
}
/* Return ptr to next time interval on the line */
if (*p++ == ',') *line = p;
else *line = NULL;
}
int analyzeTimeLine (
const char *line,
time_t current_t,
unsigned int *wakeup_time)
{
struct tm *x;
int current_time;
const char *p;
int day, start_day, end_day, start_time, end_time;
int full_start_time, full_end_time;
int wakeup_t, min_wakeup_t;
/* Break current time into a more easily maniupulated form */
x = localtime (¤t_t);
current_time = (x->tm_wday ? x->tm_wday : 7) * 24 * 60;
current_time += x->tm_hour * 60 + x->tm_min;
/* Process each interval on the line */
p = line;
min_wakeup_t = 0;
while (p != NULL) {
parseTimeLine (&p, &start_day, &end_day, &start_time, &end_time);
/* Treat each day in the range as a separate time interval to process */
for (day = start_day; day <= end_day; day++) {
/* We allow end_time to be less than start_time. We treat this as */
/* the next day. Thus 15:00-01:00 means 3PM to 1AM the next day. */
full_start_time = day * 24 * 60 + start_time;
full_end_time = day * 24 * 60 + end_time;
if (end_time < start_time) full_end_time += 24 * 60;
/* Is the current time in this interval? */
if (current_time >= full_start_time &&
current_time < full_end_time)
goto winner;
/* Now check for the really sick case, where end_time was less than */
/* start_time and we've wrapped from day 7 back to day 1 */
if (end_time < start_time && day == 7 &&
current_time < full_end_time - 7 * 24 * 60)
goto winner;
/* No, see if this start time should be our new wakeup time. */
if (full_start_time >= current_time)
wakeup_t = (full_start_time - current_time) * 60;
else
wakeup_t = (full_start_time + 7 * 24 * 60 - current_time) * 60;
if (min_wakeup_t == 0 || min_wakeup_t > wakeup_t)
min_wakeup_t = wakeup_t;
}
}
/* Current time was not in any of the intervals */
*wakeup_time = min_wakeup_t;
return (FALSE);
/* Current time is in this interval, compute the wakeup time */
winner: wakeup_t = (full_end_time - current_time) * 60;
/* Also, look for a start time that matches the end time and replace */
/* the end time. For example, if current time is 18:00 and the */
/* Time= entry is 0:00-8:00,17:00-24:00, then the */
/* end time of 24:00 should be replaced with 8:00 of the next day. */
/* Be sure not to infinite loop in this time entry: 0:00-8:00,8:00-24:00 */
p = line;
while (p != NULL && wakeup_t < 10 * 24 * 60) {
parseTimeLine (&p, &start_day, &end_day, &start_time, &end_time);
/* Treat each day in the range as a separate time interval to process */
for (day = start_day; day <= end_day; day++) {
int this_full_start_time, this_full_end_time;
/* If this start time is the same as the winning end time, then set the new */
/* wakeup time to be the end of this interval. Be sure to handle the tricky */
/* wrap around that occurs when end_time < start_time. */
this_full_start_time = day * 24 * 60 + start_time;
if (this_full_start_time != full_end_time &&
this_full_start_time != full_end_time - 7 * 24 * 60) continue;
this_full_end_time = day * 24 * 60 + end_time;
if (end_time < start_time) this_full_end_time += 24 * 60;
wakeup_t += (this_full_end_time - this_full_start_time) * 60;
full_end_time = this_full_end_time;
p = line;
break;
}
}
/* Return indicator that current time was covered by one of the intervals */
*wakeup_time = wakeup_t + 1;
return (TRUE);