]>
jfr.im git - irc/quakenet/newserv.git/blob - core/config.c
4 * Facilities for handling the config file
10 #include "../lib/sstring.h"
11 #include "../lib/array.h"
32 Error("config",ERR_INFO
,"Rehashing config file.");
43 sects
=(configsection
*)(sections
.content
);
44 for (i
=0;i
<sections
.cursi
;i
++) {
45 items
=(configitem
*)(sects
[i
].items
.content
);
46 for (j
=0;j
<sects
[i
].items
.cursi
;j
++) {
47 freesstring(items
[j
].key
);
48 values
=(sstring
**)(items
[j
].values
.content
);
49 for (k
=0;k
<items
[j
].values
.cursi
;k
++) {
50 freesstring(values
[k
]);
52 array_free(&(items
[j
].values
));
54 array_free(&(sects
[i
].items
));
55 freesstring(sects
[i
].modulename
);
57 array_free(§ions
);
60 void initconfig(char *filename
) {
62 configsection
*sects
=NULL
;
63 configitem
*items
=NULL
;
64 sstring
**values
=NULL
;
77 array_init((§ions
),sizeof(configsection
));
79 if ((fp
=fopen(filename
,"r"))==NULL
) {
80 Error("core",ERR_FATAL
,"Couldn't load config file.");
87 /* Check we got something */
91 /* Allow some comment chars */
92 if (buf
[0]=='#' || buf
[0]==';' || (buf
[0]=='/' && buf
[1]=='/'))
95 /* Blow away the line ending */
97 if (*cp
=='\n' || *cp
=='\r') {
102 /* Check it's long enough */
107 /* New section (possibly) -- hunt for the ']' */
108 for (cp
=&(buf
[2]);*cp
;cp
++) {
110 si
=array_getfreeslot(§ions
);
111 sects
=(configsection
*)(sections
.content
);
112 array_init(&(sects
[si
].items
),sizeof(configitem
));
113 array_setlim1(&(sects
[si
].items
),10);
115 sects
[si
].modulename
=getsstring(&(buf
[1]),255);
120 /* Ignore if we're not in a valid section */
124 for (cp
=buf
;*cp
;cp
++) {
128 for (ii
=0;ii
<sects
[si
].items
.cursi
;ii
++) {
129 if (!strcmp(items
[ii
].key
->content
,buf
)) {
130 /* Another value for an existing key */
131 j
=array_getfreeslot(&(items
[ii
].values
));
132 values
=(sstring
**)(items
[ii
].values
.content
);
133 values
[j
]=getsstring(cp
+1,512);
140 ii
=array_getfreeslot(&(sects
[si
].items
));
141 items
=(configitem
*)(sects
[si
].items
.content
);
142 items
[ii
].key
=getsstring(buf
,512);
143 array_init(&(items
[ii
].values
),sizeof(sstring
*));
144 array_setlim1(&(items
[ii
].values
),5);
145 j
=array_getfreeslot(&(items
[ii
].values
));
146 values
=(sstring
**)(items
[ii
].values
.content
);
147 values
[j
]=getsstring(cp
+1,512); /* looks nasty but is OK, this char is '='
148 * and we know 'buf' is null-terminated */
161 configsection
*sects
;
165 printf("Dumping complete configuration database.\n");
166 printf("Total sections: %d\n",sections
.cursi
);
168 sects
=(configsection
*)(sections
.content
);
169 for (i
=0;i
<sections
.cursi
;i
++) {
170 printf ("\nSection %02d: [%s] has %d items\n",i
,sects
[i
].modulename
->content
,sects
[i
].items
.cursi
);
171 items
=(configitem
*)(sects
[i
].items
.content
);
172 for(j
=0;j
<sects
[i
].items
.cursi
;j
++) {
173 printf(" Item %02d: [%s] has %d values\n",j
,items
[j
].key
->content
,items
[j
].values
.cursi
);
174 values
=(sstring
**)(items
[j
].values
.content
);
175 for (k
=0;k
<items
[j
].values
.cursi
;k
++) {
176 printf(" Value %2d: [%s]\n",k
,values
[k
]->content
);
181 printf("\n\nEnd of configuration database.\n");
185 * Two routes for extacting config info:
187 * - getconfigitem() is for keys which can only meaningfully have one value.
188 * It returns the last value for that key (so the config file has "last
189 * value overrides" semantics.
190 * - getconfigitems() is for keys which can have multiple values, it returns
191 * a pointer to the array of values.
194 array
*getconfigitems(char *module, char *key
) {
196 configsection
*sects
;
199 sects
=(configsection
*)(sections
.content
);
200 for (i
=0;i
<sections
.cursi
;i
++) {
201 if (!strcmp(module,sects
[i
].modulename
->content
)) {
202 /* Found the module */
203 items
=(configitem
*)(sects
[i
].items
.content
);
204 for (j
=0;j
<sects
[i
].items
.cursi
;j
++) {
205 if (!strcmp(key
,items
[j
].key
->content
)) {
206 return (&items
[j
].values
);
215 sstring
*getconfigitem(char *module, char *key
) {
219 if ((a
=getconfigitems(module,key
))==NULL
) {
223 values
=(sstring
**)(a
->content
);
224 return values
[(a
->cursi
-1)];
227 sstring
*getcopyconfigitem(char *module, char *key
, char *defaultvalue
, int len
) {
230 ss
=getconfigitem(module,key
);
232 return getsstring(ss
->content
,len
);
234 return getsstring(defaultvalue
,len
);