Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
sf_cfg(3)		    Library Functions Manual		     sf_cfg(3)

NAME
       cfgread,	 cfgget,  cfgget2 -- functions to read and parse configuration
       files

SYNOPSIS
       #include	<strfunc.h>

       int
       cfgread(char *filename);

       char *
       cfgget(char *key);

       svect *
       cfgget2(char *key);

DESCRIPTION
       These routines give the user an easy way	of creating nice configuration
       files.  The basic idea of configuration files  is  the  attribute=value
       scheme.	 Attribute  may	 be  writtten  as the ordinary english literal
       word, or	placed into the	quotes,	like a value. Value may	also be	quoted
       or be an	ordinary english word or combination of	 alphanumeric  charac-
       ters.  Attributes  and  values may be quoted using the double or	single
       quotes. When quotes are used, escaping can be  made  much  like	as  in
       Bourne shell or in the C	code. Once read, file is closed, but attribute
       and values stored internally and	may be used multiple times.

       There  are  two general forms of	defining an attribute=value pairs. Two
       forms are defined using the following BNF:

	       <simple_word>   :=      1*<A-Z0-9>

	       <quoted_string> :=      <QUOTE> *<any character except 0> <QUOTE>

	       <attr>	       := <simple_word>	| <quoted_string>

	       <value>	       := <quoted_string> | <simple_word>

	       <generic_form>  := <attribute> =	<value>	[ ; ]

	       <multiple>      := <attribute> {	<value>	*<[ , <value> ]> }  [ ;	]

       cfgread() function used to read and parse the configuration file.

       cfgget()	returns	an appropriate value for the specified key, or NULL if
       key is not found.

       cfgget2() may be	required if multiple values are	expected. It allocates
       the svect * structure that must be freed	by caller  with	 sfree().   It
       will return NULL	if svect allocation failed. If no key values found, it
       will return valid, but empty string vector.

EXAMPLE
       void cfgparse() {
	       char *value1;
	       svect *values2;
	       svect *values3;
	       int r;

	       if((r=cfgread("/path/to/config.file")) != 0) {
		       if(r > 0) {
			       printf("Wrong file format.0);
		       } else {
			       printf("File access failed.0);
		       };
		       return;
	       };

	       value1  = cfgget("key1");
	       values2 = cfgget2("key2");
	       values3 = cfgget2("key3");

	       /* Free allocated structures */
	       sfree(values2);
	       sfree(values3);

       };

CONFIGURATION FILE EXAMPLE
       key1 = "value";

       key2 = "this multiline
		       value will be
		       \"passed\" to values2.";

       key3 = {
	       "value1",
	       "value2",
	       "value3"
       };

       key2 = "Hello again!";

SEE ALSO
       strfunc(3), sf_svect(3).

AUTHORS
       Lev Walkin <vlm@lionet.info>

FreeBSD	Ports 14.quarterly	October	1, 2000			     sf_cfg(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=cfgget2&sektion=3&manpath=FreeBSD+Ports+14.3.quarterly>

home | help