MGE System Utility Library - Full Internal Documentation v1.6.2
Library of system utility functions.
Loading...
Searching...
No Matches
configfile.c File Reference

Configuration file processing. More...

#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <libmgec/mge-errno.h>
#include <libmgesysutils/mge-configfile.h>
Include dependency graph for configfile.c:

Functions

static char * getparamline (char *, FILE *)
 
static int parseline (struct confsection *, int, char *)
 
static int parsesection (struct confsection *, int, char *)
 
static int validatesection (struct confsection *, int, char *)
 
static int parseparam (char *)
 
static int isolatekey (char *, char *, char *)
 
static int isolatevalue (char *, char *, char *)
 
static int validatekeyvalue (char *, char *)
 
static int chkmandatories (struct confsection *, int)
 
static int chkkeys (struct confsection *)
 
static int chkfileerr (FILE *)
 
int parsefile (struct confsection *params, int nparams, const char *filename)
 Parse a configuration file.
 

Variables

static char line [MAX_LINE_LENGTH]
 
static char currentsection [MAX_KEYVAL_LENGTH]
 
static struct confsectionpcursect
 

Detailed Description

Configuration file processing.

configfile library source to process configuration files consisting of sections and parameters of key / value pairs.

Whitespace lines and empty lines are ignored.

Lines with a '#' as the first non-whitespace character are ignored as comment lines.

The first parameter line must be a section header.

Section headers are enclosed in [], [ must be the first non-whitespace character on the line and anything after the first ] is ignored.

All other lines are assumed to be parameter lines and after any initial whitespace characters they must have a parameter name, optional whitespace, a mandatory '=', optional whitespace and finishing with an optional word of characters and / or numbers.
e.g.

‍# Section General
[General]
# pollint is the polling interval in seconds.
pollint=5 <== This is legal.
large pollint=5 <== This is legal.
pollint = <== This is legal.
pollint <== This is illegal.

If a parameter is repeated then the last occurrence prevails.

An array of struct confsection's is provided by the library user to specify valid parameters and whether they are mandatory. A mandatory Key / Value pair for an optional Section is only enforced if the Section does appear, (is defined), in the config file.

Author
Copyright (C) 2015-2019, 2021-2023 Mark Grant

Released under the GPLv3 only.
SPDX-License-Identifier: GPL-3.0-only

Version
v1.1.0 ==== 04/11/2023

Function Documentation

◆ chkfileerr()

static int chkfileerr ( FILE *  fp)
static

◆ chkkeys()

static int chkkeys ( struct confsection section)
static

◆ chkmandatories()

static int chkmandatories ( struct confsection params,
int  nparams 
)
static

◆ getparamline()

static char * getparamline ( char *  pline,
FILE *  stream 
)
static

◆ isolatekey()

static int isolatekey ( char *  pline,
char *  endkey,
char *  key 
)
static

◆ isolatevalue()

static int isolatevalue ( char *  startvalue,
char *  endvalue,
char *  value 
)
static

◆ parsefile()

int parsefile ( struct confsection params,
int  nparams,
const char *  filename 
)

Parse a configuration file.

On error mge_errno is set.

Parameters
paramsPointer to a struct confsection array of parameters.
nparamsThe number of parameters.
filenameThe config filename.
Returns
0 for success, -mge_errno on failure.

◆ parseline()

static int parseline ( struct confsection params,
int  nparams,
char *  pline 
)
static

◆ parseparam()

static int parseparam ( char *  pline)
static

◆ parsesection()

static int parsesection ( struct confsection params,
int  nparams,
char *  pline 
)
static

◆ validatekeyvalue()

static int validatekeyvalue ( char *  key,
char *  value 
)
static

◆ validatesection()

static int validatesection ( struct confsection params,
int  nparams,
char *  section 
)
static

Variable Documentation

◆ currentsection

char currentsection[MAX_KEYVAL_LENGTH]
static

◆ line

char line[MAX_LINE_LENGTH]
static

◆ pcursect

struct confsection* pcursect
static