File: ini_file.c

package info (click to toggle)
xblast-tnt 2.10.4-4
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 4,336 kB
  • ctags: 6,123
  • sloc: ansic: 54,099; sh: 4,014; makefile: 129; sed: 16
file content (1520 lines) | stat: -rw-r--r-- 33,531 bytes parent folder | download | duplicates (4)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
/*
 * file ini_file.h - parsing ini-file into a database
 *
 * $Id: ini_file.c,v 1.25 2006/04/11 16:44:00 fzago Exp $
 *
 * Program XBLAST
 * (C) by Oliver Vogel (e-mail: m.vogel@ndh.net)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2; or (at your option)
 * any later version
 *
 * This program is distributed in the hope that it will be entertaining,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILTY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "xblast.h"

/*
 * local types
 */
/* single entry */
struct _db_entry
{
	XBAtom atom;				/* atom for entry key */
	char *value;				/* string value of key */
	struct _db_entry *next;		/* next entry for this section */
};

/* one complete section */
struct _db_section
{
	XBAtom atom;				/* name atom of section -> [...] line in file */
	XBBool changed;				/* flag for unsaved changes in section */
	DBEntry *entry;				/* list of entries */
	struct _db_section *next;	/* next section in this database */
};

/* one database */
struct _db_root
{
	DBType type;				/* in which list is database stored -> dirname */
	XBAtom atom;				/* name atom for database -> filename */
	XBBool changed;				/* flag for changes */
	DBSection *section;			/* pointer to sections */
	DBRoot *next;				/* next database of this type */
};

/*
 * local variables
 */

/* database lists */
static DBRoot *db_list[NUM_DT] = {
	NULL, NULL, NULL, NULL,
};

/* paths for database file */
static const char *db_path[NUM_DT] = {
	"config",
	GAME_DATADIR "/level",
	"demo",
	"central",
};

/* file extensions */
static const char *db_ext[NUM_DT] = {
	"cfg",
	"xal",
	"dem",
	"dat",
};

/*******************
 * local functions *
 *******************/

/* free memory of defined structures */

/*
 * free memory of an entry
 */
static void
FreeEntry (DBEntry * entry)
{
	assert (entry != NULL);
	assert (entry->value != NULL);
	free (entry->value);
	free (entry);
}								/* FreeEntry */

/*
 * free memory of a section and all its entries
 */
static void
FreeSection (DBSection * section)
{
	DBEntry *entry;
	DBEntry *nextEntry;

	assert (NULL != section);
	/* clear all entries */
	for (entry = section->entry; entry != NULL; entry = nextEntry) {
		nextEntry = entry->next;
		FreeEntry (entry);
	}
	free (section);
}								/* FreeSection */

/*
 * free memory of a given database
 */
static void
FreeRoot (DBRoot * db)
{
	DBSection *section;
	DBSection *nextSection;

	assert (db != NULL);
	/* clear all sections */
	for (section = db->section; section != NULL; section = nextSection) {
		nextSection = section->next;
		FreeSection (section);
	}
	free (db);
}								/* DB_Delete */

/* parse input lines */

/*
 * parse section name from given line
 */
static const char *
ParseSection (const char *line)
{
	const char *left;
	char *dst;
	static char result[256];

	/* check left side */
	for (left = line; *left != 0 && isspace ((unsigned char)*left); left++)
		continue;
	if (*left != '[') {
		return NULL;
	}
	left++;
	dst = result;
	while (*left != ']') {
		if ('\0' == *left) {
			/* premature end of line */
			return NULL;
		}
		*dst = *left;
		dst++;
		left++;
	}
	*dst = '\0';
	return result;
}								/* ParseSection */

/*
 * parse key part from a given line
 */
static const char *
ParseEntryName (const char *line)
{
	const char *src;
	char *dst;
	static char result[256];

	src = line;
	dst = result;
	while (*src != '=') {
		if ('\0' == *src) {
			/* premature end of line */
			return NULL;
		}
		*dst = *src;
		dst++;
		src++;
	}
	*dst = '\0';
	return result;
}								/* ParseEntryName */

/*
 * return pointer to a named entry in given section
 */
static const DBEntry *
GetEntry (const DBSection * section, XBAtom atom)
{
	const DBEntry *ptr;
	assert (section != NULL);
	/* find entry */
	for (ptr = section->entry; ptr != NULL; ptr = ptr->next) {
		if (ptr->atom == atom) {
			return ptr;
		}
	}
	return NULL;
}								/* GetEntry */

/*
 * return if character is eol
 * External level files could also come from windows environment,
 * where '\r' can be considered an EOL char for ini-file purposes.
 */
static int
XB_iniEOL (char c)
{
	switch (c) {
	case '\n':					/* FALLTHRU */
	case '\r':
		return 1;
	default:
		return 0;
	}
}								/* XB_iniEOL */

/*
 * parse the entry value part of a line
 */
static const char *
ParseEntryValue (const char *line)
{
	const char *src;
	char *dst;
	static char result[256];
	dst = result;
	src = strchr (line, '=');
	if (NULL == src) {
		return NULL;
	}
	src++;
	while (isspace ((unsigned char)*src)) {
		src++;
	}
	while ((!XB_iniEOL (*src)) && (*src != '\0')) {
		*dst = *src;
		dst++;
		src++;
	}

	while (isspace ((unsigned char)*(dst - 1))) {
		--dst;
	}
	*dst = 0;
	return result;
}								/* ParseEntryValue */

/* file operations */

/*
 * open file corresponding to given database
 */
static FILE *
OpenDBFile (const DBRoot * db, const char *mode)
{
	char db_name[320];
	assert (NULL != db);
	assert (NULL != mode);
	/* add index for demos */
	sprintf (db_name, "%s", GUI_AtomToString (db->atom));
	return FileOpen (db_path[db->type], db_name, db_ext[db->type], mode);
}								/* OpenDBFile */

/*
 * check if file directory database must be updated
 */
static XBBool
CheckInsertFile (const DBRoot * db, const char *name, XBAtom timeAtom, time_t mtime)
{
	const DBSection *section;
	time_t ltime;
	/* does file exist as section name in database */
	section = DB_GetSection (db, GUI_StringToAtom (name));
	if (NULL == section) {
		return XBFalse;
	}
	/* has the file been modified */
	if (!DB_GetEntryTime (section, timeAtom, &ltime) || ltime != mtime) {
		return XBFalse;
	}
	/* file entry exists and is up-to-date */
	return XBTrue;
}								/* CheckInsertFile */

/*
 * insert file into database
 */
static XBBool
InsertFile (DBRoot * db, DBType type, const char *name, XBAtom timeAtom, time_t mtime,
			XBAtom fromAtom, DBLoadDirFunc func)
{
	DBRoot *dbFile = NULL;
	DBSection *toSection = NULL;
	const DBSection *fromSection = NULL;
	const DBEntry *entry = NULL;
	XBAtom toAtom = GUI_StringToAtom (name);
	/* try to load file type/name.ext into a temp database */
	dbFile = DB_Create (type, toAtom);
	assert (dbFile != NULL);
	if (!DB_Load (dbFile)) {
		goto Error;
	}
	/* try to find given section */
	fromSection = DB_GetSection (dbFile, fromAtom);
	if (NULL == fromSection) {
		goto Error;
	}
	/* create new section for file data */
	toSection = DB_CreateSection (db, toAtom);
	assert (NULL != toSection);
	/* create entry for file modification time */
	DB_CreateEntryTime (toSection, timeAtom, mtime);
	/* now copy section */
	for (entry = fromSection->entry; NULL != entry; entry = entry->next) {
		(void)DB_CreateEntryString (toSection, entry->atom, entry->value);
	}
	/* call user function if any */
	if (NULL != func) {
		(*func) (toSection);
	}
	/* clean up */
	DB_Delete (dbFile);
	return XBTrue;

  Error:
	if (NULL != dbFile) {
		DB_Delete (dbFile);
	}
	if (NULL != toSection) {
		DB_DeleteSection (db, toAtom);
	}
	return XBFalse;
}								/* CheckInsertFile */

/*
 * create named section in given database and mark it changed
 */
static void
UpdateFile (DBRoot * db, const char *name)
{
	DBSection *section = DB_CreateSection (db, GUI_StringToAtom (name));
	assert (NULL != section);
	section->changed = XBTrue;
}								/* UpdateFile */

/*
 * delete any unchanged sections
 */
static XBBool
DeleteUnchangedSections (DBRoot * db)
{
	DBSection *ptr;
	DBSection *next;
	XBBool deleted = XBFalse;
	assert (NULL != db);
	/* delete first section, while unchanged */
	while (NULL != db->section && !db->section->changed) {
		next = db->section->next;
		FreeSection (db->section);
		db->section = next;
		deleted = XBTrue;
#ifdef DEBUG
		putchar ('-');
#endif
	}
	/* now proceed with the rest */
	if (NULL != db->section) {
		for (ptr = db->section; ptr != NULL && ptr->next != NULL; ptr = ptr->next) {
			while (NULL != ptr->next && !ptr->next->changed) {
				next = ptr->next->next;
				FreeSection (ptr->next);
				ptr->next = next;
				deleted = XBTrue;
#ifdef DEBUG
				putchar ('-');
#endif
			}
		}
	}
#ifdef DEBUG
	fflush (stdout);
#endif
	return deleted;
}								/* DeleteUnchangedSections */

/*************************
 * get/set database info *
 *************************/

/*
 * create a new database of given type and name
 */
DBRoot *
DB_Create (DBType type, XBAtom atom)
{
	DBRoot *db;
	assert (ATOM_INVALID != atom);
	/* create new database */
	db = calloc (1, sizeof (DBRoot));
	assert (NULL != db);
	db->atom = atom;
	db->type = type;
	db->changed = XBTrue;
	/* store in list */
	db->next = db_list[type];
	db_list[type] = db;
	/* thats all */
	return db;
}								/* DB_Create */

/*
 * return pointer of an existing database
 */
const DBRoot *
DB_Get (DBType type, XBAtom atom)
{
	const DBRoot *db;
	assert (ATOM_INVALID != atom);
	for (db = db_list[type]; db != NULL; db = db->next) {
		if (db->atom == atom) {
			return db;
		}
	}
	return NULL;
}								/* DB_Get */

/*
 * get name atom of database (filename)
 */
XBAtom
DB_Atom (const DBRoot * db)
{
	assert (NULL != db);
	return db->atom;
}								/* DB_Changed */

/*
 * check if database has changed
 */
XBBool
DB_Changed (const DBRoot * db)
{
	const DBSection *section;
	if (db->changed) {
		return XBTrue;
	}
	for (section = db->section; section != NULL; section = section->next) {
		if (section->changed) {
			return XBTrue;
		}
	}
	return XBFalse;
}								/* DB_Changed */

/*
 * set name atom of given database (filename)
 */
void
DB_SetAtom (DBRoot * db, XBAtom atom)
{
	db->atom = atom;
	db->changed = XBTrue;
}								/* DB_SetAtom */

/*
 * mark given database as unchanged
 */
static void
MarkUnchanged (DBRoot * db)
{
	DBSection *section;
	/* mark all as unchanged */
	db->changed = XBFalse;
	for (section = db->section; section != NULL; section = section->next) {
		section->changed = XBFalse;
	}
}								/* MarkUnchanged */

/*
 * delete a given database
 */
void
DB_Delete (DBRoot * db)
{
	DBRoot *ptr;
	assert (db_list[db->type] != NULL);
	/* check if first element in list */
	if (db_list[db->type] == db) {
		db_list[db->type] = db_list[db->type]->next;
	}
	else {
		for (ptr = db_list[db->type]; ptr->next != NULL; ptr = ptr->next) {
			if (db == ptr->next) {
				ptr->next = db->next;
				break;
			}
		}
	}
	FreeRoot (db);
}								/* DB_Delete */

/************************
 * get/set section info *
 ************************/

/*
 * create a new section in a given database
 */
DBSection *
DB_CreateSection (DBRoot * db, XBAtom atom)
{
	DBSection *section;

	/* create new data structure */
	assert (NULL != db);
	assert (ATOM_INVALID != atom);
	/* try to find section first */
	for (section = db->section; section != NULL; section = section->next) {
		if (section->atom == atom) {
			return section;
		}
	}
	/* create new section */
	db->changed = XBTrue;
	section = calloc (1, sizeof (DBSection));
	assert (NULL != section);
	section->atom = atom;
	section->changed = XBTrue;
	/* store in database */
	section->next = db->section;
	db->section = section;
	/* thats all */
	return section;
}								/* DB_CreateSection */

/*
 * return pointer to a named section in given database
 */
DBSection *
DB_GetSection (const DBRoot * db, XBAtom atom)
{
	DBSection *ptr;

	assert (NULL != db);
	assert (ATOM_INVALID != atom);
	/* find section */
	for (ptr = db->section; ptr != NULL; ptr = ptr->next) {
		if (ptr->atom == atom) {
			return ptr;
		}
	}
	return NULL;
}								/* DB_GetSection */

/*
 * get section atom
 */
XBAtom
DB_SectionAtom (const DBSection * section)
{
	assert (section != NULL);
	return section->atom;
}								/* DB_SectionAtom */

/*
 * get the name of the nth section in given database
 */
XBAtom
DB_IndexSection (const DBRoot * db, int index)
{
	const DBSection *ptr;
	int num = 0;
	assert (NULL != db);
	/* find section */
	for (ptr = db->section; ptr != NULL; ptr = ptr->next) {
		if (num == index) {
			return ptr->atom;
		}
		num++;
	}
	return ATOM_INVALID;
}								/* DB_IndexSection */

/*
 * number of sections in a given database
 */
int
DB_NumSections (const DBRoot * db)
{
	const DBSection *ptr;
	int num = 0;
	assert (NULL != db);
	/* find section */
	for (ptr = db->section; ptr != NULL; ptr = ptr->next) {
		num++;
	}
	return num;
}								/* DB_NumSections */

/*
 * delete a section in a given database
 */
void
DB_DeleteSection (DBRoot * db, XBAtom atom)
{
	DBSection *save, *ptr;
	assert (db != NULL);
	db->changed = XBTrue;
	/* do we have any sections ? */
	if (NULL == db->section) {
		return;
	}
	assert (ATOM_INVALID != atom);
	/* it is the first section ? */
	if (db->section->atom == atom) {
		save = db->section;
		db->section = db->section->next;
		FreeSection (save);
	}
	else {
		for (ptr = db->section; ptr->next != NULL; ptr = ptr->next) {
			if (ptr->next->atom == atom) {
				save = ptr->next;
				ptr->next = ptr->next->next;
				FreeSection (save);
				break;
			}
		}
	}
}								/* DB_DeleteSection */

/*
 * delete all sections in a given database
 */
void
DB_DeleteAll (DBRoot * db)
{
	DBSection *save;
	assert (db != NULL);
	db->changed = XBTrue;
	/* do we have any sections ? */
	if (NULL == db->section) {
		return;
	}
	while (db->section != NULL) {
		save = db->section;
		db->section = db->section->next;
		FreeSection (save);
	}
}								/* DB_DeleteAll */

/*************************
 * get entry information *
 *************************/

/*
 * create a string entry in a given section
 */
XBBool
DB_CreateEntryString (DBSection * section, XBAtom atom, const char *value)
{
	DBEntry *entry;

	assert (ATOM_INVALID != atom);
	assert (NULL != section);
	/* mark as changed */
	section->changed = XBTrue;
	/* try to find entry first */
	for (entry = section->entry; entry != NULL; entry = entry->next) {
		if (entry->atom == atom) {
			if (NULL != entry->value) {
				free (entry->value);
			}
			entry->value = DupString (value);
			assert (NULL != entry->value);
			return XBTrue;
		}
	}
	/* create new data strutcure */
	entry = calloc (1, sizeof (DBEntry));
	assert (NULL != entry);
	entry->atom = atom;
	entry->value = DupString (value);
	assert (NULL != entry->value);
	/* store in database */
	entry->next = section->entry;
	section->entry = entry;
	/* that's all */
	return XBTrue;
}								/* DB_CreateEntryString */

/*
 * create an atom entry in a given section
 */
XBBool
DB_CreateEntryAtom (DBSection * section, XBAtom atom, XBAtom value)
{
	return DB_CreateEntryString (section, atom, GUI_AtomToString (value));
}								/* DB_CreateEntryAtom */

/*
 * create an integer entry in a given section
 */
XBBool
DB_CreateEntryInt (DBSection * section, XBAtom atom, int value)
{
	char tmp[32];
	sprintf (tmp, "%d", value);
	return DB_CreateEntryString (section, atom, tmp);
}								/* DB_CreateEntryInt */

/*
 * create a game result entry in a given section
 */
XBBool
DB_CreateEntryGameResult (DBSection * section, XBAtom atom, int score)
{
	char tmp[32];
	sprintf (tmp, "%d", score);
	return DB_CreateEntryString (section, atom, tmp);
}								/* DB_CreateEntry */

/*
 * create a player rating entry in a given section
 */
XBBool
DB_CreateEntryPlayerRating (DBSection * section, XBAtom atom, int PID, float rating)
{
	char tmp[32];
	sprintf (tmp, "%d %f", PID, rating);
	return DB_CreateEntryString (section, atom, tmp);
}								/* DB_CreateEntryPlayerRating */

/*
 * create a bool entry in given section
 */
XBBool
DB_CreateEntryBool (DBSection * section, XBAtom atom, XBBool value)
{
	return DB_CreateEntryString (section, atom, value ? "true" : "false");
}								/* DB_CreateEntryBool */

/*
 * create a color entry in given section
 */
XBBool
DB_CreateEntryColor (DBSection * section, XBAtom atom, XBColor color)
{
	char tmp[32];
	sprintf (tmp, "#%02x%02x%02x", GET_RED (color) << 3, GET_GREEN (color) << 3,
			 GET_BLUE (color) << 3);
	return DB_CreateEntryString (section, atom, tmp);
}								/* DB_CreateEntryColor */

/*
 * create a time entry in given section
 */
XBBool
DB_CreateEntryTime (DBSection * section, XBAtom atom, time_t value)
{
	char tmp[64];
	size_t len;
	len = sprintf (tmp, "%lu ; %s", value, ctime (&value));
	/* cut trailing newline */
	tmp[len - 1] = 0;
	return DB_CreateEntryString (section, atom, tmp);
}								/* DB_CreateEntryTime */

/*
 * create a float entry in given section
 */
XBBool
DB_CreateEntryFloat (DBSection * section, XBAtom atom, double value)
{
	char tmp[32];
	sprintf (tmp, "%f", value);
	return DB_CreateEntryString (section, atom, tmp);
}								/* DB_CreateEntryFloat */

/*
 * create a position entry in given section
 */
XBBool
DB_CreateEntryPos (DBSection * section, XBAtom atom, BMPosition * pValue)
{
	char tmp[64];
	sprintf (tmp, "%d %d", pValue->x, pValue->y);
	return DB_CreateEntryString (section, atom, tmp);
}								/* DB_CreateEntryPos */

/*
 * create entry in given section, defined by given key=value line
 */
XBBool
DB_ParseEntry (DBSection * section, const char *line)
{
	const char *entryName;
	assert (NULL != section);
	assert (NULL != line);
	/* parse entry */
	if (NULL == (entryName = ParseEntryName (line))) {
		return XBFalse;
	}
	return DB_CreateEntryString (section, GUI_StringToAtom (entryName), ParseEntryValue (line));
}								/* DB_ParseEntry */

/*
 * copy name section/entry from given section to given database
 */
XBBool
DB_CopyEntry (DBRoot * db, const DBSection * section, const XBAtom atom)
{
	DBSection *sec;
	const DBEntry *ptr;
	assert (NULL != db);
	assert (NULL != section);
	assert (NULL != db);
	/* try to create section in database */
	sec = DB_CreateSection (db, section->atom);
	if (NULL == sec) {
		return XBFalse;
	}
	/* try to get entry */
	ptr = GetEntry (section, atom);
	if (NULL == ptr) {
		return XBFalse;
	}
	/* now duplicate entry */
	return DB_CreateEntryString (sec, atom, ptr->value);
}								/* DB_CopyEntry */

/*
 * get the name of the nth entry in given section
 */
XBAtom
DB_IndexEntry (const DBSection * section, int index)
{
	const DBEntry *ptr;
	int num = 0;
	assert (NULL != section);
	/* find section */
	for (ptr = section->entry; ptr != NULL; ptr = ptr->next) {
		if (num == index) {
			return ptr->atom;
		}
		num++;
	}
	return ATOM_INVALID;
}								/* DB_IndexSection */

/*
 * number of entries in given database
 */
int
DB_NumAllEntries (const DBRoot * db)
{
	const DBSection *sec;
	int num = 0;
	assert (NULL != db);
	for (sec = db->section; sec != NULL; sec = sec->next) {
		num += DB_NumEntries (sec);
	}
	return num;
}								/* DB_NumAllEntries */

/*
 * number of entries in given section
 */
int
DB_NumEntries (const DBSection * section)
{
	const DBEntry *ptr;
	int num = 0;
	assert (NULL != section);
	/* find section */
	for (ptr = section->entry; ptr != NULL; ptr = ptr->next) {
		num++;
	}
	return num;
}								/* DB_NumEntries */

/*
 * get string value for named entry in given section
 */
XBBool
DB_GetEntryString (const DBSection * section, XBAtom atom, const char **pValue)
{
	const DBEntry *entry;
	assert (pValue != NULL);
	entry = GetEntry (section, atom);
	if (NULL == entry) {
		return XBFalse;
	}
	assert (entry->value != NULL);
	*pValue = entry->value;
	return XBTrue;
}								/* DB_GetEntryString */

/*
 * get atom value for named entry in given section
 */
XBBool
DB_GetEntryAtom (const DBSection * section, XBAtom atom, XBAtom * pValue)
{
	const DBEntry *entry;
	assert (pValue != NULL);
	entry = GetEntry (section, atom);
	if (NULL == entry) {
		return XBFalse;
	}
	assert (NULL != entry->value);
	*pValue = GUI_StringToAtom (entry->value);
	return (ATOM_INVALID != *pValue);
}								/* DB_GetEntryAtom */

/*
 * get int value for named entry in given section
 */
XBBool
DB_GetEntryInt (const DBSection * section, XBAtom atom, int *pValue)
{
	const DBEntry *entry;
	assert (pValue != NULL);
	entry = GetEntry (section, atom);
	if (NULL == entry) {
		return XBFalse;
	}
	assert (NULL != entry->value);
	return (1 == sscanf (entry->value, "%d", pValue)) ? XBTrue : XBFalse;
}								/* DB_GetEntryInt */

/*
 * get bool value for named entry in given section
 */
XBBool
DB_GetEntryBool (const DBSection * section, XBAtom atom, XBBool * pValue)
{
	const DBEntry *entry;
	assert (pValue != NULL);
	entry = GetEntry (section, atom);
	if (NULL == entry) {
		return XBFalse;
	}
	assert (NULL != entry->value);
	if (0 == strcmp (entry->value, "true")) {
		*pValue = XBTrue;
	}
	else if (0 == strcmp (entry->value, "false")) {
		*pValue = XBFalse;
	}
	else {
		return XBFalse;
	}
	return XBTrue;
}								/* DB_GetEntryBool */

/*
 * get color value for named entry in given section
 */
XBBool
DB_GetEntryColor (const DBSection * section, XBAtom atom, XBColor * pValue)
{
	const DBEntry *entry;
	unsigned red, green, blue;
	char hash;
	assert (pValue != NULL);
	entry = GetEntry (section, atom);
	if (NULL == entry) {
		return XBFalse;
	}
	assert (NULL != entry->value);
	if (4 != sscanf (entry->value, "%c%2X%2X%2X", &hash, &red, &green, &blue)) {
		return XBFalse;
	}
	if (hash != '#') {
		return XBFalse;
	}
	*pValue = SET_COLOR (red >> 3, green >> 3, blue >> 3);
	return XBTrue;
}								/* DB_GetEntryColor */

/*
 * get time value for named entry in given sectiong
 */
XBBool
DB_GetEntryTime (const DBSection * section, XBAtom atom, time_t * pValue)
{
	const DBEntry *entry;
	assert (pValue != NULL);
	entry = GetEntry (section, atom);
	if (NULL == entry) {
		return XBFalse;
	}
	assert (NULL != entry->value);
	return (1 == sscanf (entry->value, "%lu", pValue)) ? XBTrue : XBFalse;
}								/* DB_GetEntryTime */

/*
 * get float value for named entry in given section
 */
XBBool
DB_GetEntryFloat (const DBSection * section, XBAtom atom, double *pValue)
{
	const DBEntry *entry;
	assert (pValue != NULL);
	entry = GetEntry (section, atom);
	if (NULL == entry) {
		return XBFalse;
	}
	assert (NULL != entry->value);
	return (1 == sscanf (entry->value, "%lf", pValue)) ? XBTrue : XBFalse;
}								/* DB_GetEntryFloat */

/*
 * get position value for named entry in given section
 */
XBBool
DB_GetEntryPos (const DBSection * section, XBAtom atom, BMPosition * pValue)
{
	const DBEntry *entry;
	assert (pValue != NULL);
	entry = GetEntry (section, atom);
	if (NULL == entry) {
		return XBFalse;
	}
	assert (NULL != entry->value);
	return (2 == sscanf (entry->value, "%hd%hd", &pValue->x, &pValue->y)) ? XBTrue : XBFalse;
}								/* DB_GetEntryPos */

/*
 * get string value for named entry in given section and convert to integer
 */
DBConversionResult
DB_ConvertEntryInt (const DBSection * section, XBAtom atom, int *pValue, const DBToInt * convTable)
{
	const DBEntry *entry;
	const DBToInt *ptr;
	int cmp;
	if (NULL == (entry = GetEntry (section, atom))) {
		return DCR_NoSuchEntry;
	}
	assert (convTable != NULL);
	for (ptr = convTable; ptr->key != NULL; ptr++) {
		cmp = strcmp (ptr->key, entry->value);
		if (0 == cmp) {
			/* match */
			assert (pValue != NULL);
			*pValue = ptr->value;
			return DCR_Okay;
		}
		else if (cmp > 0) {
			return DCR_Failure;
		}
	}
	return DCR_Failure;
}								/* DB_ConvertEntryInt */

/*
 * get string value for named entry in given section and convert to a generic pointer
 */
DBConversionResult
DB_ConvertEntryData (const DBSection * section, XBAtom atom, void **pValue,
					 const DBToData * convTable)
{
	const DBEntry *entry;
	const DBToData *ptr;
	int cmp;
	if (NULL == (entry = GetEntry (section, atom))) {
		return DCR_NoSuchEntry;
	}
	assert (convTable != NULL);
	for (ptr = convTable; ptr->key != NULL; ptr++) {
		cmp = strcmp (ptr->key, entry->value);
		if (0 == cmp) {
			/* match */
			assert (pValue != NULL);
			*pValue = ptr->value;
			return DCR_Okay;
		}
		else if (cmp > 0) {
			return DCR_Failure;
		}
	}
	return DCR_Failure;
}								/* DB_ConvertEntryData */

/*
 * get string value for named entry in given section and convert to an entry flag number
 */
DBConversionResult
DB_ConvertEntryFlags (const DBSection * section, XBAtom atom, unsigned *pValue,
					  const DBToInt * convTable)
{
	const DBEntry *entry;
	const DBToInt *ptr;
	int cmp;
	int i, argc;
	char **argv;
	if (NULL == (entry = GetEntry (section, atom))) {
		return DCR_NoSuchEntry;
	}
	assert (convTable != NULL);
	assert (pValue != NULL);
	*pValue = 0;
	/* split value string */
	argv = SplitString (entry->value, &argc);
	assert (NULL != argv);
	/* parse list */
	for (i = 0; i < argc; i++) {
		for (ptr = convTable; ptr->key != NULL; ptr++) {
			cmp = strcmp (ptr->key, argv[i]);
			if (0 == cmp) {
				/* match */
				*pValue |= ptr->value;
				break;
			}
			else if (cmp > 0) {
				free ((char *)argv);
				return DCR_Failure;
			}
		}
	}
	free ((char *)argv);
	return DCR_Okay;
}								/* DB_ConvertEntryFlag */

/*
 * create a string line for n-th entry in given section, return length
 */
size_t
DB_PrintEntry (char *buffer, const DBSection * section, int index)
{
	const DBEntry *ptr;
	int num = 0;
	assert (NULL != buffer);
	assert (NULL != section);
	/* find entry */
	for (ptr = section->entry; ptr != NULL; ptr = ptr->next) {
		if (num == index) {
			return sprintf (buffer, "%s=%s", GUI_AtomToString (ptr->atom), ptr->value);
		}
		num++;
	}
	/* entry not found */
	buffer[0] = 0;
	return 0;
}								/* DB_PrintEntry */

/*
 * add string to sized buffer, fill with x if added string does not fit
 */
static XBBool
AddStringToBuffer (char *buf, size_t len, const char *add)
{
	int i, nul, write;
	/* sanity checks */
	assert (NULL != buf);
	assert (NULL != add);
	/* find first null byte */
	nul = -1;
	for (i = 0; (i < len) && (nul < 0); i++) {
		if (0 == *(buf + i)) {
			nul = i;
		}
	}
	/* check if buffer is full */
	if (nul == len - 1) {
		return XBFalse;
	}
	/* check if there is a null byte */
	if (nul < 0) {
		Dbg_Out ("Buffer has no null byte!\n");
		memset (buf + len - 1, 0, 1);
		return XBFalse;
	}
	/* check if add string fits */
	write = strlen (add);
	if (nul + write < len) {
		memcpy (buf + nul, add, write + 1);
		return XBTrue;
	}
	/* no, does not fit */
	memset (buf + nul, (unsigned char)"x", len - nul - 1);
	memset (buf + len - 1, 0, 1);
	return XBFalse;
}								/* AddStringToBuffer */

/*
 * return a string for entry in given section
 */
char *
DB_SectionEntryString (const DBSection * section, XBAtom atom)
{
	static char buffer[100];
	const char *tmp;
	const DBEntry *ptr;
	/* init buffer */
	memset (buffer, 0, 1);
	/* get section name */
	AddStringToBuffer (buffer, sizeof (buffer), "[");
	assert (NULL != section);
	tmp = GUI_AtomToString (section->atom);
	if (NULL == tmp) {
		tmp = "???";
	}
	AddStringToBuffer (buffer, sizeof (buffer), tmp);
	AddStringToBuffer (buffer, sizeof (buffer), "] ");
	/* get entry name */
	tmp = GUI_AtomToString (atom);
	if (NULL == tmp) {
		tmp = "???";
	}
	AddStringToBuffer (buffer, sizeof (buffer), tmp);
	AddStringToBuffer (buffer, sizeof (buffer), "=");
	/* find entry value */
	ptr = GetEntry (section, atom);
	if (NULL == ptr) {
		tmp = "<none>";
	}
	else {
		tmp = ptr->value;
	}
	AddStringToBuffer (buffer, sizeof (buffer), tmp);
	return buffer;
}								/* DB_PrintEntry */

/*
 * delete an entry from section
 */
void
DB_DeleteEntry (DBSection * section, XBAtom atom)
{
	DBEntry *save, *ptr;
	assert (section != NULL);
	section->changed = XBTrue;
	/* do we have any entries ? */
	if (NULL == section->entry) {
		return;
	}
	assert (ATOM_INVALID != atom);
	/* it is the first entry ? */
	if (section->entry->atom == atom) {
		save = section->entry;
		section->entry = section->entry->next;
		FreeEntry (save);
	}
	else {
		for (ptr = section->entry; ptr->next != NULL; ptr = ptr->next) {
			if (ptr->next->atom == atom) {
				save = ptr->next;
				ptr->next = ptr->next->next;
				FreeEntry (save);
				break;
			}
		}
	}
}								/* DB_DeleteEntry */

/************************
 * conversion functions *
 ************************/

/*
 * translate an integer value into its conversion string
 */
const char *
DB_IntToString (const DBToInt * table, int value)
{
	const DBToInt *ptr;
	assert (NULL != table);
	for (ptr = table; ptr->key != NULL; ptr++) {
		if (ptr->value == value) {
			return ptr->key;
		}
	}
	return NULL;
}								/* DB_IntToString */

/*
 * translate a data pointer into its conversion string
 */
const char *
DB_DataToString (const DBToData * table, void *pValue)
{
	const DBToData *ptr;
	assert (NULL != table);
	for (ptr = table; ptr->key != NULL; ptr++) {
		if (ptr->value == pValue) {
			return ptr->key;
		}
	}
	return NULL;
}								/* DB_DataToString */

/*******************
 * file operations *
 *******************/

/*
 * load given database from file
 */
XBBool
DB_Load (DBRoot * db)
{
	FILE *fp;
	DBSection *section;
	char line[256];
	const char *sectionName;
	const char *entryName;
	assert (db != NULL);
	assert (ATOM_INVALID != db->atom);
	/* try to open file for reading */
	if (NULL == (fp = OpenDBFile (db, "r"))) {
		return XBFalse;
	}
	/* parse it */
	section = NULL;
	while (NULL != fgets (line, sizeof (line), fp)) {
		if (NULL != (sectionName = ParseSection (line))) {
			section = DB_CreateSection (db, GUI_StringToAtom (sectionName));
		}
		else if (NULL != section && NULL != (entryName = ParseEntryName (line))) {
			(void)DB_CreateEntryString (section, GUI_StringToAtom (entryName),
										ParseEntryValue (line));
		}
	}
	/* mark all as unchanged */
	MarkUnchanged (db);
	/* that's all */
	fclose (fp);
	return XBTrue;
}								/* DB_Load */

/*
 * store database in file
 */
XBBool
DB_Store (DBRoot * db)
{
	FILE *fp;
	const DBSection *section;
	const DBEntry *entry;
	/* try to open file for writing */
	if (NULL == (fp = OpenDBFile (db, "w"))) {
		return XBFalse;
	}
	/* now write it */
	for (section = db->section; section != NULL; section = section->next) {
		fprintf (fp, "[%s]\n", GUI_AtomToString (section->atom));
		for (entry = section->entry; entry != NULL; entry = entry->next) {
			fprintf (fp, "%s=%s\n", GUI_AtomToString (entry->atom), entry->value);
		}
		fprintf (fp, "\n");
	}
	/* mark all as unchanged */
	MarkUnchanged (db);
	/* that's all */
	fclose (fp);
	return XBTrue;
}								/* DB_Store */

/*
 * append database to file
 */
XBBool
DB_Append (DBRoot * db)
{
	FILE *fp;
	const DBSection *section;
	const DBEntry *entry;
	/* try to open for appending */
	if (NULL == (fp = OpenDBFile (db, "a"))) {
		return XBFalse;
	}
	/* now write it */
	for (section = db->section; section != NULL; section = section->next) {
		fprintf (fp, "[%s]\n", GUI_AtomToString (section->atom));
		for (entry = section->entry; entry != NULL; entry = entry->next) {
			fprintf (fp, "%s=%s\n", GUI_AtomToString (entry->atom), entry->value);
		}
		fprintf (fp, "\n");
	}
	/* mark all as unchanged */
	MarkUnchanged (db);
	/* that's all */
	fclose (fp);
	return XBTrue;
}								/* DB_Append */

/*
 * dump database to stderr
 */
XBBool
DB_Dump (const DBRoot * db)
{
	const DBSection *section;
	const DBEntry *entry;
	/* now write it */
	fprintf (stderr, "---database dump----\n");
	for (section = db->section; section != NULL; section = section->next) {
		fprintf (stderr, "[%s]\n", GUI_AtomToString (section->atom));
		for (entry = section->entry; entry != NULL; entry = entry->next) {
			fprintf (stderr, "%s=%s\n", GUI_AtomToString (entry->atom), entry->value);
		}
		fprintf (stderr, "\n");
	}
	fprintf (stderr, "---end of database dump---\n");
	/* that's all */
	return XBTrue;
}								/* DB_Dump */

/*
 * load complete directory of files into database,
 */
XBBool
DB_LoadDir (DBRoot * db, const char *path, const char *ext, DBType type, XBAtom timeAtom,
			XBAtom section, DBLoadDirFunc func, XBBool rec)
{
	XBBool changed;
	XBDir *fileList;
	XBDir *ptr;
	/* sanity checks */
	assert (NULL != db);
	assert (NULL != path);
	assert (NULL != ext);
	/* load database from file */
	DB_Load (db);
	/* now compare with current directory contents */
	changed = XBFalse;
	fileList = CreateFileList (path, ext, rec);
	for (ptr = fileList; NULL != ptr; ptr = ptr->next) {
		if (!CheckInsertFile (db, ptr->name, timeAtom, ptr->mtime)) {
			InsertFile (db, type, ptr->name, timeAtom, ptr->mtime, section, func);
			changed = XBTrue;
#ifdef DEBUG
			putchar ('*');
#endif
		}
		else {
			UpdateFile (db, ptr->name);
#ifdef DEBUG
			putchar ('.');
#endif
		}
#ifdef DEBUG
		fflush (stdout);
#endif
	}
	/* now delete "unchanged section", because they do not have any related files */
	if (DeleteUnchangedSections (db)) {
		changed = XBTrue;
	}
	if (NULL != fileList) {
		DeleteFileList (fileList);
#ifdef DEBUG
		putchar ('\n');
#endif
	}
	return changed;
}								/* DB_LoadDir */

/*
 * read message of the day, return line count
 */
int
ReadMessageOfTheDay (int m, char *s, int *l)
{
	FILE *fp;
	int i;
	char *c, *d;
	i = 0;
	/* try to open the file */
	fp = FileOpen (db_path[3], "message", "txt", "r");
	if (NULL != fp) {
		c = s;
		i = 0;
		l[i] = 0;
		while (1) {
			/* get character */
			d = fgets (c, m - l[i], fp);
			if (d == NULL) {
				break;
			}
			i++;
			l[i] = strlen (s) - 1;
			c = s + strlen (s) - 1;
		}
		l[i + 1] = strlen (s);
	}
	return i;
}								/* ReadmessageOfTheDay */

/*
 * clean up databases
 */
void
DB_Finish (void)
{
	DBType t;
	for (t = DT_Config; t < NUM_DT; t++) {
		while (NULL != db_list[t]) {
			DB_Delete (db_list[t]);
		}
		db_list[t] = NULL;
	}
}								/* FinishControl */

/*
 * end of file ini_file.c
 */