parsecfg - a library for parsing a configuration file

Yuuki NINOMIYA <gm@debian.or.jp>

$Date: 2001/02/22 12:28:50 $


This document is the user's manual for parsecfg library. It doesn't seem to be a good document, so you should also read source codes of a sample program and the library. :-p

1. Introduction

2. Usage

3. License

4. Tested Operating Systems

5. Limitations

6. Known Bugs

7. Future Plans

8. FAQ

9. Request to Users

10. Author

11. The Latest Release

12. The Latest Source Code

13. Related Links

14. Acknowledgement

15. History


1. Introduction

1.1 Overview

Parsecfg is a library for parsing a configuration file. Values are stored in dynamically-allocated memory.

1.2 Features

1.3 Requirements

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.


2. Usage

2.1 Installation

Just include parsecfg.c and parsecfg.h as a part of your program.

2.2 Preparation

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'.

CFG_BOOL

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.

CFG_INT

Store as int.

CFG_UINT

Store as unsigned int.

CFG_LONG

Store as long.

CFG_ULONG

Store as unsigned long.

CFG_FLOAT

Store as float.

CFG_DOUBLE

Store as double.

CFG_STRING

Store as string.

CFG_STRING_LIST

Store as linked list for strings.

CFG_END

The last element in the array.

See the sample program (sample.c) for more details. Type `make' to compile the sample program.

2.3 Function References

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.

2.4 Format of Configuration File

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.


3. License

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.


4. Tested Operating Systems


5. Limitations

None.


6. Known Bugs


7. Future Plans

None.


8. FAQ

None.


9. Request to Users

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.


10. Author

Yuuki NINOMIYA <gm@debian.or.jp>
http://www.enjoy.ne.jp/~gm/index.html


11. The Latest Release

The latest official release of parsecfg is available from:
http://www.enjoy.ne.jp/~gm/program/parsecfg/index.html


12. The Latest Source Code

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.


13. Related Links

A Configuration File Parser Using C++ and STL


14. Acknowledgement

14.1 Borrowed Code / Library

14.2 Contributions of Code or Ducument

Kolb Norbert <nkolb@htl.de>

And. Andruikhanov <andy@euinf.dp.ua>

Andreas Schuldei <schuldei@andrive.de>

Pascal Lengard <pascal.lengard@wanadoo.fr>

Guillaume Hajduch <Guillaume.Hajduch@enst-bretagne.fr>

Richard Rowell <rrowell@shreve.net>

14.3 Translations

Krzysztof Krzyz.aniak <eloy@transilvania.eu.org>

Guillaume Hajduch <guillaume.hajduch@free.fr>

14.4 Other

Thanks to many people who send me comments, bug reports and suggestions.

And thank you for using parsecfg!


15. History

February 22, 2001 Ver 3.6.6

February 15, 2001 Ver 3.6.5

January 16, 2001 Ver 3.6.4

December 19, 2000 Ver 3.6.3

December 18, 2000 Ver 3.6.2

December 15, 2000 Ver 3.6.1

December 06, 2000 Ver 3.6.0

November 23, 2000 Ver 3.5.0

October 07, 2000 Ver 3.4.0

July 20, 2000 Ver 3.3.0

February 24, 2000 Ver 3.2.1

February 13, 2000 Ver 3.2.0

December 24, 1999 Ver 3.1.2

December 23, 1999 Ver 3.1.1

December 22, 1999 Ver 3.1.0

November 30, 1999 Ver 3.0.4a

November 07, 1999 Ver 3.0.4

October 19, 1999 Ver 3.0.3

October 17, 1999 Ver 3.0.2

October 11, 1999 Ver 3.0.1

October 10, 1999 Ver 3.0.0

September 27, 1999 Ver 2.0.3

August 12, 1999 Ver 2.0.2a

August 08, 1999 Ver 2.0.2

July 01, 1999 Ver 2.0.1b

June 29, 1999 Ver 2.0.1a

June 09, 1999 Ver 2.0.1

June 02, 1999 Ver 2.0.0

April 03, 1999 Ver 1.0.1

April 02, 1999 Ver 1.0.0


sgml21html conversion date: Thu Feb 22 21:29:05 JST 2001