1 | /*********************************************** |
---|
2 | ** |
---|
3 | ** cfg.h v0.01 |
---|
4 | ** |
---|
5 | ** Generic config file handler |
---|
6 | ** (c) Gareth Watts 1998 |
---|
7 | */ |
---|
8 | |
---|
9 | |
---|
10 | #define CFG_READONLY 1 |
---|
11 | #define CFG_READWRITE 2 |
---|
12 | |
---|
13 | |
---|
14 | struct cfg_node { |
---|
15 | char *optname; |
---|
16 | char *optvalue; |
---|
17 | int dirty; |
---|
18 | struct cfg_node *next; |
---|
19 | }; |
---|
20 | |
---|
21 | |
---|
22 | struct cfg_queuenode { |
---|
23 | struct cfg_node *ref; |
---|
24 | struct cfg_queuenode *next; |
---|
25 | }; |
---|
26 | |
---|
27 | |
---|
28 | #define CFG_HASHSIZE 1297 |
---|
29 | typedef struct cfg { |
---|
30 | |
---|
31 | // PUBLIC METHODS |
---|
32 | void (*free)(struct cfg *self); |
---|
33 | int (*openfile)(struct cfg *self,char *fn,int mode); |
---|
34 | int (*autoflush)(struct cfg *self,int flushmode); |
---|
35 | char *(*getstring)(struct cfg *self,char *key); |
---|
36 | int (*getint)(struct cfg *self,char *key); |
---|
37 | float (*getfloat)(struct cfg *self,char *key); |
---|
38 | int (*setstring)(struct cfg *self,char *key, char *value); |
---|
39 | int (*setint)(struct cfg *self,char *key, int value); |
---|
40 | int (*setfloat)(struct cfg *self,char *key, float value); |
---|
41 | int (*flush)(struct cfg *self); |
---|
42 | |
---|
43 | // PUBLIC DATA |
---|
44 | char *lasterror; |
---|
45 | |
---|
46 | |
---|
47 | // PRIVATE METHODS |
---|
48 | int (*_init)(struct cfg *self); |
---|
49 | int (*_getkey)(struct cfg *self,char *str); |
---|
50 | int (*_insert)(struct cfg *self,char *keyname,char *keyval,int dirty); |
---|
51 | struct cfg_node *(*_getnode)(struct cfg *self,char *keyname); |
---|
52 | char *(*_lookup)(struct cfg *self,char *keyname); |
---|
53 | int (*_cleardirty)(struct cfg *self,struct cfg_node *ptr); |
---|
54 | int (*_splitline)(struct cfg *self,char *src, char **f, char **s); |
---|
55 | int (*_set)(struct cfg *self, char *keyname, char *keyvalue); |
---|
56 | int (*_queuepush)(struct cfg *self, struct cfg_node *node); |
---|
57 | struct cfg_queuenode *(*_queuenext)(struct cfg *self, struct cfg_queuenode *last); |
---|
58 | int (*_queuefree)(struct cfg *self); |
---|
59 | int (*_flush)(struct cfg *self); |
---|
60 | |
---|
61 | // PRIVATE DATA |
---|
62 | struct cfg_node *cfg_hash[CFG_HASHSIZE]; |
---|
63 | struct cfg_queuenode *cfg_newsthead; |
---|
64 | struct cfg_queuenode *cfg_newsttail; |
---|
65 | char *filename; |
---|
66 | int filemode; |
---|
67 | int aflush; |
---|
68 | } cfg; |
---|
69 | |
---|
70 | cfg *cfg_new(); |
---|