Parsecfg is a library for parsing a configuration file. Values are stored in dynamically-allocated memory.
Parsecfg requires an ANSI C compiler. I tested this program with gcc version 2.92.2.
I strongly recommend that your program uses GNU gettext though it is non-essential for parsecfg. It is very helpful for non-native English speakers to internationalize messages by gettext. See http://www.gnu.org/manual/gettext/html_node/gettext_toc.html for details of gettext.
Just include parsecfg.c and parsecfg.h as a part of your program.
Create and initialize an array of cfgStruct to parse the configuration file. Set parameter name, type and address of the variable in each element in the array. (If type of the configuration file is INI, specify address of pointer to variable instead of address of the variable).
cfgStruct is declared as the following in parsecfg.h.
typedef struct{ char *parameterName; cfgValueType type; void *value; } cfgStruct;
`parameterName' is the name of parameter (keyword), `type' is the type of parameter, `value` is the address of variable. (or the address of pointer)
There is the following types as `type'.
Store 1 if the value is `True' or `Yes' or `T' or `Y' `1'. Store 0 if the value is `False' or `No' or `F' or `N' `0'. They are not case sensitive.
Store as int.
Store as unsigned int.
Store as long.
Store as unsigned long.
Store as float.
Store as double.
Store as string.
Store as linked list for strings.
The last element in the array.
See the sample program (sample.c) for more details. Type `make' to compile the sample program.
Include parsecfg.h before using the following functions.
void cfgSetFatalFunc(void (*f)(int, const char *, int, const char *))
Set error handler by this function. Before calls this function, parsecfg uses default error handler written in parsecfg.c.
int cfgParse(const char *file, cfgStruct cfg[], cfgFileType type)
Call this function to parse the configuration file `file'.
`type' is type of the configuration file. If it is CFG_SIMPLE, the file is parsed as simple format (PARAMETER = VALUE). If it is CFG_INI, this function parses it as Windoze INI-like format (using sections).
Return value is the number of read sections (1,2,3,...). Return 0 usually if the type is CFG_SIMPLE. Return -1 on error.
int cfgDump(const char *file, cfgStruct cfg[], cfgFileType type, int max)
Call this function to output configurations to a file.
Configurations are written to `file'. `type' is type of the configuration file. `max' is the number of sections. If `type' is CFG_SIMPLE, you can specify any number. (Normally, you want to specify 0.)
Return 0 if successful, -1 on error.
int fetchVarFromCfgFile(const char *file, char *parameter_name, void *result_value, cfgValueType value_type, cfgFileType file_type, int section_num, const char *section_name)
Call this function to fetch value of `parameter_name' from `file'.
Result is stored in `result_value'. The type of this variable is specified by `value_type'.
`file_type' is type of the configuration file.
`section_num' is section number you want to fetch from. If it is less than 1, fetch from the section specified in `section_name' instead of `section_num'. If `type' is `CFG_SIMPLE', you can specify any value. (Normally, you want to specify 0 and NULL.)
Return 0 if successful, -1 on error.
int cfgSectionNameToNumber(const char *name)
Call this function to convert section name to section number.
Return value is section number (0,1,2,...). Section name is not case sensitive. Return -1 if name doesn't match.
char *cfgSectionNumberToName(int num)
Call this function to convert section number to section name.
Return NULL if there is no specified section.
int cfgAllocForNewSection(cfgStruct cfg[], const char *name)
Call this function to alloc memories for defining a new section. The new section is named string specified in `name'.
Return the maximum number of section if successful, -1 on error.
int cfgStoreValue(cfgStruct cfg[], const char *parameter, const char *value, cfgFileType type, int section)
Call this function to store `value' according to `cfg'. `type' is type of the configuration file. `section' is section number (0,1,2,...) that you want to store value in. If `type' is CFG_SIMPLE, you can specify any number. (Normally, you want to specify 0.)
Return 0 if successful, -1 on error.
White spaces and tabs are always skipped. If you want to use
such a special character, quote strings with "
or '
.
If you want to use "
, use '
.
If you want to use '
, use "
.
Lines that begin with the #
character are ignored as comment.
Parameter names are not case sensitive. Basic format is "PARAMETER = VALUE".
# PARAMETER = VALUE ParameterINT = 65535 ParameterSTRING = GNU/Linux QuotedString = 'I pronounce "Linux" as "Leenooks".' TRUEorFALSE = FaLsE
If there are same parameters and parameter type is CFG_STRING_LIST , all values are stored. If other type, the last one is stored.
# multiple parameters # CFG_INT ParameterINT = 255 # ignored ParameterINT = 65535 # stored # CFG_STRING_LIST ListString = "Debian GNU/Linux" # stored ListString = "FreeBSD" # stored
If you want to specify multiple values to linked list, the following format is also available.
# for linked list ListString = { Internet "Exploder" }
If the configuration file type is CFG_INI,
quote section name with [
and ]
.
# Windoze INI-like file [Linux] STRING = rule! VALUE = 99999999 [Windoze] STRING = suck! VALUE = -99999999
See sample.cfg and sample.ini for more details.
Copyright (C) 1999-2001 Yuuki NINOMIYA <gm@debian.or.jp>
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 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 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.
None.
None.
None.
I have an incomplete mastery of the English language, so please help correct the text of the documents and messages in this program. Comments, suggestions, bug reports and patches are always welcome. Please mail them to the following address.
Yuuki NINOMIYA <gm@debian.or.jp>
http://www.enjoy.ne.jp/~gm/index.html
The latest official release of parsecfg is available from:
http://www.enjoy.ne.jp/~gm/program/parsecfg/index.html
You can get the latest source code with Anonymous CVS. Specify `:pserver:anonymous@linuxlovers.yi.org:/var/cvs' as repository, `parsecfg' as project name, `anonymous' as password.
% cvs -d :pserver:anonymous@linuxlovers.yi.org:/var/cvs login (Logging in to anonymous@linuxlovers.yi.org) CVS password: anonymous % cvs -d :pserver:anonymous@linuxlovers.yi.org:/var/cvs checkout parsecfg
See http://www.cvshome.org/docs/manual/index.html for details about CVS.
A Configuration File Parser Using C++ and STL
Thanks to many people who send me comments, bug reports and suggestions.
And thank you for using parsecfg!