config-parser
a simple ini parser written in C
 
Loading...
Searching...
No Matches
config.h File Reference

Go to the source code of this file.

Classes

struct  configEntry
 

Macros

#define NO_SECTION   0
 
#define FOUND_SECTION   10
 
#define ERROR_STR   1001
 
#define ERROR_MAX_LEN   2
 
#define NO_ERROR   0
 
#define ERROR_DELIMITER_NOT_FOUND   40
 
#define ST_INIT   0
 
#define ST_FOUND_LEFT_DELIMITER   1
 
#define ST_FOUND_RIGHT_DELIMITER   2
 
#define ST_ERROR_NOT_FOUND_RIGHT_DELIMITER   3
 
#define ST_ERROR_NOT_FOUND_LEFT_DELIMITER   4
 
#define ST_FOUND_SECTION   5
 
#define ST_SKIP_READ   6
 
#define ST_FINISH   20
 
#define ST_ERROR_GETSTRATPOS   30
 
#define MAX_LEN_SECTIONNAME   128
 
#define ERR_PARSECONFIG_UNKNOWN   201
 

Functions

int loadConfig (char *file, char **str_entry, char **host, int *intervall, int size)
 
int checkSection (char *str, char delimiterLeft, char delimiterRight, char **sectionName)
 Here we check if the given string contains a section for example [SECTIONName] here delimiterLeft is [ and delimiterRight is ] if a section is found, the section name will be written to sectionName.
 
int getStrAtPos (char *str, int fromPos, int toPos, char **name, int sizeName)
 Here we get / cut from fromPos to toPos and write it to an address.
 
int getNameValuePair (char *str, char leftDelimiterPos, char rightDelimiterPos, char **name, char **value, int sizeName, int sizeValue)
 Input:
 
int parseConfig (char *buffer, struct configEntry **entry, int configSize, int *returnedCount)
 

Macro Definition Documentation

◆ ERR_PARSECONFIG_UNKNOWN

#define ERR_PARSECONFIG_UNKNOWN   201

Definition at line 28 of file config.h.

◆ ERROR_DELIMITER_NOT_FOUND

#define ERROR_DELIMITER_NOT_FOUND   40

Definition at line 7 of file config.h.

◆ ERROR_MAX_LEN

#define ERROR_MAX_LEN   2

Definition at line 4 of file config.h.

◆ ERROR_STR

#define ERROR_STR   1001

Definition at line 3 of file config.h.

◆ FOUND_SECTION

#define FOUND_SECTION   10

Definition at line 2 of file config.h.

◆ MAX_LEN_SECTIONNAME

#define MAX_LEN_SECTIONNAME   128

Definition at line 25 of file config.h.

◆ NO_ERROR

#define NO_ERROR   0

Definition at line 5 of file config.h.

◆ NO_SECTION

#define NO_SECTION   0

Definition at line 1 of file config.h.

◆ ST_ERROR_GETSTRATPOS

#define ST_ERROR_GETSTRATPOS   30

Definition at line 22 of file config.h.

◆ ST_ERROR_NOT_FOUND_LEFT_DELIMITER

#define ST_ERROR_NOT_FOUND_LEFT_DELIMITER   4

Definition at line 15 of file config.h.

◆ ST_ERROR_NOT_FOUND_RIGHT_DELIMITER

#define ST_ERROR_NOT_FOUND_RIGHT_DELIMITER   3

Definition at line 14 of file config.h.

◆ ST_FINISH

#define ST_FINISH   20

Definition at line 19 of file config.h.

◆ ST_FOUND_LEFT_DELIMITER

#define ST_FOUND_LEFT_DELIMITER   1

Definition at line 12 of file config.h.

◆ ST_FOUND_RIGHT_DELIMITER

#define ST_FOUND_RIGHT_DELIMITER   2

Definition at line 13 of file config.h.

◆ ST_FOUND_SECTION

#define ST_FOUND_SECTION   5

Definition at line 16 of file config.h.

◆ ST_INIT

#define ST_INIT   0

Definition at line 11 of file config.h.

◆ ST_SKIP_READ

#define ST_SKIP_READ   6

Definition at line 17 of file config.h.

Function Documentation

◆ checkSection()

int checkSection ( char * str,
char delimiterLeft,
char delimiterRight,
char ** sectionName )

Here we check if the given string contains a section for example [SECTIONName] here delimiterLeft is [ and delimiterRight is ] if a section is found, the section name will be written to sectionName.

Input:

Parameters
char**str: the string to check
chardelimiterLeft: the left delimiter to check for
chardelimiterRight: the right delimiter to check for

Output:

Parameters
char**sectionName: if found the section Name of the section

Return:

Parameters
intstatus_code: one of the following values:

Definition at line 24 of file config.c.

◆ getNameValuePair()

int getNameValuePair ( char * str,
char leftDelimiterPos,
char rightDelimiterPos,
char ** name,
char ** value,
int sizeName,
int sizeValue )

Input:

Here we get a "pair" of data, parsed from *str, witch consists of a keyname and a keyvalue. These are written to the pointers at char **name and char **value. These pair can be in the form of:
ex1:
NAME=VALUE
____^_____^
____|_____Here we have no delimiter this means rightDelimiterPos must be NULL
____This is the left delimiter
ex2:
name(value)
____^_____^
____|_____This is the rightDelimiterPos and must be ')'
____This is leftDelimiterPos which must be '('

Parameters
char*str: the line in the form of a string where a key value pair is stored
charleftDelimiterPos: the left delimiter for example '='
charrightDelimiterPos: the right delimiter can be NULL in most cases, if NULL we assume that there is only one delimiter, which is in this case the leftDelimiterPos

Output:

Parameters
char**name: The address where we store the name of the Key
char**value: The address where we store the key value
intsizeName: for size checking against memory allocated at **name
intsizeValue: for size checking against memory allocated at **value

Return:

Returns
will return NO_ERROR (0) if successfull. If not it will return the error of the subroutine

Definition at line 151 of file config.c.

◆ getStrAtPos()

int getStrAtPos ( char * str,
int fromPos,
int toPos,
char ** name,
int sizeName )

Here we get / cut from fromPos to toPos and write it to an address.

Input:

Parameters
char*str: the string to cut
intfromPos: from which position we want to copy, the pos is included and zero indexed
inttoPos: to which position we want to copy, the pos is included and zero indexed

Output:

Parameters
char**name: here we will write the section name to

Input:

Parameters
intsizeName: the size of the user allocated buffer at **name

Definition at line 98 of file config.c.

◆ loadConfig()

int loadConfig ( char * file,
char ** str_entry,
char ** host,
int * intervall,
int size )

◆ parseConfig()

int parseConfig ( char * originalBuffer,
struct configEntry ** entry,
int configSizeCount,
int * returnedCount )

Given a null terminated buffer of content(ex. from a file) an parse it with a default syntax of: Sections -> [SECTIONNAME] name / value -> name=value

Input: char *originalBuffer: the buffer which holds the string for parsing. struct configEntry **entry: a user allocated buffer to which we write the parsed config data. int configSizeCount: the user allocated size of **entry. Output: int *returnedCount: to this variable the function writes the count of struct, which will be required to store all alavaible data.

Note: The funtion returns the maximum allowed data structures, determined by configSizeCount. All data which exceeds will be dropped. To get the whole data we run the funtion a second time with the realloced configSizeCount in the size of [returnedCount] structures.

Definition at line 215 of file config.c.