Tou
Tou header library
Loading...
Searching...
No Matches
INI operations

Loading, saving and working with .ini files. More...

Topics

 Lower level functions
 
 

Functions

tou_llist_ttou_ini_parse_fp (FILE *fp)
 Parses given FILE* as .INI file and constructs structured data.
 
tou_llist_ttou_ini_parse_buffer (char *buf)
 Parses given buffer as .INI file and constructs structured data.
 
int tou_ini_parse_line (tou_llist_t **inicontents, char *line)
 Parses a single line as .INI section/property and adds it to structure.
 
void tou_ini_destroy (tou_llist_t *inicontents)
 Destroys contents of .INI structure including deallocation of inner structures; assumes head was passed.
 
void tou_ini_print (tou_llist_t *inicontents)
 Prints the contents of the parsed .INI structure to stdout in a structured graphical format.
 
void * tou_ini_get (tou_llist_t *inicontents, const char *section_name, const char *key)
 Returns the contents of 'key' under the given 'section_name'.
 
void * tou_ini_set (tou_llist_t **inicontents, const char *section_name, const char *key, char *new_value)
 Sets the contents of 'key' under the given 'section_name' to the specified value, reallocating memory if necessary.
 
tou_llist_ttou_ini_section (tou_llist_t *inicontents, const char *section_name)
 Return a pointer to the llist entry of the matching section, or NULL.
 
void * tou_ini_property (tou_llist_t *section, const char *property_name)
 Return the value of the matching property in the given section, or NULL.
 
int tou_ini_save_fp (tou_llist_t *inicontents, FILE *fp)
 Writes .INI structure data to file stream.
 
int tou_ini_save_fp_json (tou_llist_t *inicontents, FILE *fp)
 Exports .INI structure data as JSON to the file stream.
 
int tou_ini_save_fp_xml (tou_llist_t *inicontents, FILE *fp)
 Exports .INI structure data as XML to the file stream.
 

Detailed Description

Loading, saving and working with .ini files.

Loaded data is internally structured as a linked list (tou_llist_t) of INI sections with first parameter being the section name and second being a sub-linked-list containing its properties where first and second property represent the property's key and value respectively. You may get a relatively similar visual by calling tou_ini_print.

<SECTION> -(first)-> "section 1"
: |
: (second)
: |
: +-> <PROPERTY> -(first)-> "persons name"
: : |
: : (second)
: : |
: : +-> "John"
: :
: +-> <PROPERTY> -> "persons age"
: |
: +-> "654"
:
<SECTION> -> "section 2"
: |
: +-> <PROPERTY> -> "Text contents"
: : |
: . +-> "Lorem ipsum dolor sit amet, and even more text ... "
.

Function Documentation

◆ tou_ini_destroy()

void tou_ini_destroy ( tou_llist_t * inicontents)

Destroys contents of .INI structure including deallocation of inner structures; assumes head was passed.

Parameters
[in]inicontentsParsed INI structure

◆ tou_ini_get()

void * tou_ini_get ( tou_llist_t * inicontents,
const char * section_name,
const char * key )

Returns the contents of 'key' under the given 'section_name'.

Parameters
[in]inicontentsPointer to the parsed .INI structure
[in]section_name
[in]key
Returns
Pointer to the contents, or NULL

◆ tou_ini_parse_buffer()

tou_llist_t * tou_ini_parse_buffer ( char * buf)

Parses given buffer as .INI file and constructs structured data.

[!] Modifies buffer. Refer to tou_ini_parse_fp docs for more info about how data is parsed.

Parameters
[in]bufBuffer to read from
Returns
Newly allocated structured data

◆ tou_ini_parse_fp()

tou_llist_t * tou_ini_parse_fp ( FILE * fp)

Parses given FILE* as .INI file and constructs structured data.

Parser supports using semicolon (;) and hashtag (#) for comments, however they must be on a separate line by themselves. Specifying the same section name more than once is not supported (that may change in the future where the definitions would merge and "flatten" into one combined section).

Whitespaces may be added between sections, properties and section name brackets for better readability. When specifying properties both keys and values may have some whitespaces in them. However, all whitespaces at the beginning and the end of the key will be removed, as well as all whitespaces at the end of the value. If the value starts with a whitespace, only that single whitespace will be removed. Keep in mind that keys having whitespaces inside of them will be sensitive to them when reading the data.

Structured data is constructed as two layers of llists where the primary list that gets returned from function represents sections in .INI file where the section names and properties are stored in .dat1 and .dat2 fields respectively. Properties in .dat2 are stored as sub-llist where keys and values are allocated and are stored as strings in .dat1 and .dat2 fields respectively.

Parameters
[in]fpFile pointer to where to read the .INI data from
Returns
Newly allocated structured data

◆ tou_ini_parse_line()

int tou_ini_parse_line ( tou_llist_t ** inicontents,
char * line )

Parses a single line as .INI section/property and adds it to structure.

[!] Modifies data. Refer to tou_ini_parse_fp docs for more info about how data is parsed.

Parameters
[in,out]inicontentsStructure to which append
[in]lineBuffer containing line to be parsed
Returns
Continue parsing (TOU_CONTINUE) or parsing error encountered (TOU_BREAK)

◆ tou_ini_print()

void tou_ini_print ( tou_llist_t * inicontents)

Prints the contents of the parsed .INI structure to stdout in a structured graphical format.

Parameters
[in]inicontentsParsed INI structure

◆ tou_ini_property()

void * tou_ini_property ( tou_llist_t * section,
const char * property_name )

Return the value of the matching property in the given section, or NULL.

Parameters
[in]sectionPointer to the parsed .INI structure
[in]property_name
Returns
Value of the property

◆ tou_ini_save_fp()

int tou_ini_save_fp ( tou_llist_t * inicontents,
FILE * fp )

Writes .INI structure data to file stream.

Return error codes: 0 = OK, no error, -1 = NULL received in params, -2 = Cannot write to stream, -3 = Invalid section name encountered in struct, -4 = Invalid property data encountered in struct

Parameters
[in]inicontentsPointer to the starting element
[in]fpFile* stream to write to
Returns
Zero if successful, error code if failed

◆ tou_ini_save_fp_json()

int tou_ini_save_fp_json ( tou_llist_t * inicontents,
FILE * fp )

Exports .INI structure data as JSON to the file stream.

Return error codes:

  • 0 = OK, no error,
  • -1 = NULL received in params,
  • -2 = Cannot write to stream,
  • -3 = Invalid section name encountered in struct,
  • -4 = Invalid property data encountered in struct
Parameters
[in]inicontentsPointer to the starting element
[in]fpFile* stream to write to
Returns
Zero if successful, error code if failed

◆ tou_ini_save_fp_xml()

int tou_ini_save_fp_xml ( tou_llist_t * inicontents,
FILE * fp )

Exports .INI structure data as XML to the file stream.

Return error codes:

  • 0 = OK, no error,
  • -1 = NULL received in params,
  • -2 = Cannot write to stream,
  • -3 = Invalid section name encountered in struct,
  • -4 = Invalid property data encountered in struct
Parameters
[in]inicontentsPointer to the starting element
[in]fpFile* stream to write to
Returns
Zero if successful, error code if failed

◆ tou_ini_section()

tou_llist_t * tou_ini_section ( tou_llist_t * inicontents,
const char * section_name )

Return a pointer to the llist entry of the matching section, or NULL.

Parameters
[in]inicontentsPointer to the parsed .INI structure
[in]section_name
Returns
Pointer to the section

◆ tou_ini_set()

void * tou_ini_set ( tou_llist_t ** inicontents,
const char * section_name,
const char * key,
char * new_value )

Sets the contents of 'key' under the given 'section_name' to the specified value, reallocating memory if necessary.

Parameters
[in,out]inicontentsPointer to the parsed .INI structure
[in]section_name
[in]key
[in]new_value
Returns
Pointer to the new value that was stored in struct, or NULL