Confloader: a simple configuration file loader and parser.
In some languages (Perl, etc), it is very easy to parse a configuration file. C is not one of them. Even if anyone can do a configuration file loader for his application, it remains boring. Worse, there is nothing new in doing such a thing as a lot of pieces of software uses a similar system...
The goal of confloader is to provide such a parser which can be adapted with as few efforts as possible, so developers of software applications can concentrate on tasks which interest them.
In the source file in which you need to load the configuration file, add the confloader.h header and create a global "conf" array according to the options you want to allow in a configuration file, and ending with a {NULL}, for example:
#include "confloader.h" option conf[] = {{"Name", STRINGOPT, 1, {0}}, {"Port", INTOPT, 1, {0}}, {"UID", INTOPT, 1, {0}}, {"Hostlist", STRINGOPT, 1, {0}}, {"Root", YESNO, 1, {0}}, {"NBHostPerRequest", INTOPT, 1, {0}}, {"LocalClients" , YESNO, 1, {0}}, {"RemotePort", STRINGOPT, 1, {0}}, {"captain", FLOATOPT, 1, {0}}, {"LocalPort", STRINGOPT, 1, {0}}, {"pidfile", FILEOPT, 1, {0}}, {"log", LOGOPT, 1, {0}}, {NULL}};
As you can see, this is an array of "option". An option is a structure.
log = syslog log = file "/var/log/test" log = none log = stdout log = stderr
You can use a log function based on the following framework:
void logger(option log, char* logline) { static FILE* logfiledesc = NULL; if (log.type != LOGOPT) exit(1); switch(log.value.log.logtype) { case LOG__NOTDEF: /* use stderr if not log facility defined */ case LOG__STDERR: fprintf(stderr, logline); break; case LOG__STDOUT: fprintf(stdout, logline); break; case LOG__FILE: if (logfiledesc == NULL) logfiledesc = fopen(log.value.log.filename, "a"); fprintf(logfiledesc, logline); break; case LOG__SYSLOG: syslog(priority, logline); break; case LOG__NONE: break; } }
Another reason is that confloader's API is not freezed yet.
Copyright (c) 2004 Nicolas Bernard <http://www.lafraze/net/nbernard/>
Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.