CAPS Universe documentation  1.0.4
All you need to know to be successful
Data Structures | Macros | Functions | Variables
libcapsinif.c File Reference

Handle options read from INI style config files. More...

Data Structures

struct  data_set
 INI data set. More...
 
struct  caps_inif_cursor
 Cursor to manage databases in form of INI files. More...
 
struct  caps_inif_lock
 INI file lock management. More...
 

Macros

#define INCREMENT_STEP   10
 
#define INI_MAX_LINE   200
 
#define MAX_SECTION   50
 
#define MAX_NAME   50
 

Functions

void caps_libinif_init (void)
 
void caps_libinif_version_get (unsigned *major, unsigned *minor, unsigned *micro)
 
static void caps_inif_init_entry (struct data_set *ds)
 
static void caps_inif_cleanup_entry (struct data_set *ds)
 
void caps_inif_table_init (struct caps_inif_table *dst)
 
void caps_inif_table_cleanup (struct caps_inif_table *dst)
 
static struct data_setcaps_inif_create_new_data_set (struct caps_inif_table *dst)
 
static char * rstrip (char *s)
 
static char * lskip (const char *s)
 
static char * find_char_or_comment (const char *s, char c)
 
int caps_inif_option_add (struct caps_inif_table *dst, const char *level, const char *section, const char *key, const char *val)
 
static struct data_setinitf_entry_check (const struct caps_inif_table *dst, const char *level, const char *section, const char *key)
 
static int inif_entry_fold (struct caps_inif_table *dst, const char *level, const char *section, const char *key, const char *val)
 
static int parse_ini_file (FILE *file, struct caps_inif_table *dst, const char *level, int(*entry)(struct caps_inif_table *dst, const char *level, const char *section, const char *key, const char *val))
 
static bool is_ini_file (const char *file_name)
 
static void destroy_list_files_in_dir (struct dirent **e, size_t cnt)
 
static int ini_file_filter (const struct dirent *e)
 
static int list_ini_files_in_dir (const char *dir, struct dirent ***e, size_t *cnt)
 
static char * get_file_name (const char *file_name)
 
int caps_inif_file_read (struct caps_inif_table *dst, const char *level, const char *file_name)
 
int caps_inif_file_merge (struct caps_inif_table *dst, const char *level, const char *ini_file)
 
static int caps_inif_dir_process (struct caps_inif_table *dst, const char *level, const char *dir, int merge)
 
int caps_inif_dir_read (struct caps_inif_table *dst, const char *level, const char *dir)
 
int caps_inif_dir_merge (struct caps_inif_table *dst, const char *level, const char *dir)
 
const char * caps_inif_option_get (const struct caps_inif_table *dst, const char *level, const char *section, const char *key)
 
void caps_inif_option_list_print (const struct caps_inif_table *dst)
 
struct caps_inif_cursorcaps_inif_cursor_create (void)
 
void caps_inif_cursor_destroy (struct caps_inif_cursor *cursor)
 
const char * caps_inif_next_key_get (const struct caps_inif_table *dst, struct caps_inif_cursor *cursor)
 
const char * caps_inif_next_section_get (const struct caps_inif_table *dst, struct caps_inif_cursor *cursor)
 
const char * caps_inif_next_level_get (const struct caps_inif_table *dst, struct caps_inif_cursor *cursor)
 
static int dump_ini_table (FILE *file, const struct caps_inif_table *dst)
 
static void caps_inif_free_lock_info (struct caps_inif_lock *li)
 
struct caps_inif_lockcaps_inif_locked_file_read (struct caps_inif_table *dst, const char *level, const char *file_name)
 
int caps_inif_locked_file_write (const struct caps_inif_table *dst, struct caps_inif_lock *li)
 
void caps_inif_locked_file_close (struct caps_inif_lock *li)
 
size_t caps_inif_option_count_get (const struct caps_inif_table *dst)
 

Variables

static const struct caps_table_desc data_set_desc
 

Detailed Description

Author
Jürgen Borleis
Warning
Use as experimental

To avoid to re-invent the wheel I'm using an INI parser called "inih" from "http://code.google.com/p/inih/" with the following licence:

The "inih" library is distributed under the New BSD license:

  Copyright (c) 2009, Brush Technology
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
      * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
      * Neither the name of Brush Technology nor the names of its contributors
        may be used to endorse or promote products derived from this software
        without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY BRUSH TECHNOLOGY ''AS IS'' AND ANY
  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL BRUSH TECHNOLOGY BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Macro Definition Documentation

◆ INCREMENT_STEP

#define INCREMENT_STEP   10

◆ INI_MAX_LINE

#define INI_MAX_LINE   200

INI file constraints:

  • max 200 chars per line

◆ MAX_SECTION

#define MAX_SECTION   50

INI file constraints:

  • max 50 chars for a section name

◆ MAX_NAME

#define MAX_NAME   50

INI file constraints:

  • max 50 chars for a variable name

Function Documentation

◆ caps_inif_init_entry()

static void caps_inif_init_entry ( struct data_set ds)
static

Init an entry structure

Parameters
[in,out]dsThe data set structure to init

Init means to set all members (string pointer) to NULL

◆ caps_inif_cleanup_entry()

static void caps_inif_cleanup_entry ( struct data_set ds)
static

Free an entry structure

Parameters
[in,out]dsThe data set structure to free

Init means to free all strings (reserved via malloc())

◆ caps_inif_create_new_data_set()

static struct data_set * caps_inif_create_new_data_set ( struct caps_inif_table dst)
static

does not return in case of memory failure

◆ rstrip()

static char * rstrip ( char *  s)
static

Strip trailing whitespace, in place.

Parameters
[in,out]sstring which can contain trailing whitespaces
Returns
pointer to the stripped string
Note
From inih

◆ lskip()

static char * lskip ( const char *  s)
static

Skip leading whitespaces

Parameters
[in]sstring which may contains leading whitespaces
Returns
pointer to first non-whitespace char in given string
Note
From inih

◆ find_char_or_comment()

static char * find_char_or_comment ( const char *  s,
char  c 
)
static

Find character c or begin of a comment

Parameters
[in]sWhere to search in
[in]cThe character to search for
Returns
Pointer to the first occurrence of c and ';' or '\0' if neither was found.
Note
';' must be prefixed by a whitespace character to register as a comment.

◆ initf_entry_check()

static struct data_set * initf_entry_check ( const struct caps_inif_table dst,
const char *  level,
const char *  section,
const char *  key 
)
static

Search for an entry if it already exists

Parameters
[in]dstPointer to INI table
[in]levelThe level of the new/existing entry
[in]sectionThe section of the new/existing entry
[in]keyThe key of the new/existing entry
Return values
PointerPointer to the entry if found
NULLMarks a non existing entry

◆ inif_entry_fold()

static int inif_entry_fold ( struct caps_inif_table dst,
const char *  level,
const char *  section,
const char *  key,
const char *  val 
)
static

Replace the value of an existing entry in the table or add it to the table if it doesn't exist yet

Parameters
[in,out]dstPointer to INI table
[in]levelThe level of the new/existing entry
[in]sectionThe section of the new/existing entry
[in]keyThe key of the new/existing entry
[in]valThe new value of the entry (can be NULL)
Return values
0On success
Note
This function does not return on memory failure

◆ parse_ini_file()

static int parse_ini_file ( FILE *  file,
struct caps_inif_table dst,
const char *  level,
int(*)(struct caps_inif_table *dst, const char *level, const char *section, const char *key, const char *val)  entry 
)
static

Parse the INI file and store the key/value pairs

Parameters
[in]fileINI file open for reading
[out]dstwhere to store the key/value pairs
[in]levelName of the level these entries correspond to (can be NULL)
[in]entryFunction to call to insert/replace the new entry into the table
Returns
0 on success or a line number where it fails
Todo:

Add support for boolean parameters (e.g. keywords without an rvalue)

Remove size restrictions for line length and name length

◆ is_ini_file()

static bool is_ini_file ( const char *  file_name)
static

Check if the name is a ini file (simple duck test)

Parameters
[in]file_nameFile's name to check
Return values
trueIf the naming convention is fullfilled
falseElse

Naming convention is "*.ini" (not "*.INI")

◆ destroy_list_files_in_dir()

static void destroy_list_files_in_dir ( struct dirent **  e,
size_t  cnt 
)
static

Free the memory allocated by 'list_ini_files_in_dir()'

Parameters
[in]eThe array with directory entries
[in]cntNumber of entries in the array 'e'

◆ ini_file_filter()

static int ini_file_filter ( const struct dirent *  e)
static

Select only files which seem an INI file

Parameters
[in]eDirectory entry to check
Return values
0Not an INI file
1Seems an INI file

We sort out:

  • all files beginning with a dot
  • all files which doesn't end with '.ini' (refer is_ini_file())

◆ list_ini_files_in_dir()

static int list_ini_files_in_dir ( const char *  dir,
struct dirent ***  e,
size_t *  cnt 
)
static

Create an array of filenames in a directory which seems to be ini files

Parameters
[in]dirDirectory path where to search for ini files
[out]eWhere to store the array pointer
[out]cntWhere to store how many entries the array has
Return values
0on succes
-ENOENTThe path in dir doesn't exist
-ENOTDIRThe path in dir isn't a directory
Todo:

Grump when the directory is empty

Be able to say what kind of INI files where expected in the directory. Currently the error message is for the base INI files the same than for the printer's INI files!

◆ get_file_name()

static char * get_file_name ( const char *  file_name)
static

Extract the name of the given filename, e.g. remove the extension if exist

Parameters
[in]file_nameFull filename (for example 'printer.ini')
Returns
Extracted name (like 'printer')
Note
the string buffer must be freed after use
Does not return in case of memory failure

◆ caps_inif_dir_process()

static int caps_inif_dir_process ( struct caps_inif_table dst,
const char *  level,
const char *  dir,
int  merge 
)
static

Read/merge in all ini files within the given directory into one single table

Parameters
[in,out]dstThe INI table to store the result to
[in]levelName of the level these entries correspond to (can be NULL)
[in]dirDirectory to read the entries from
[in]merge0 = read all directory entries into an empty table, or 1 = merge further directory entries into a filles table
Return values
0On success
-ENOENTThe path in dir doesn't exist
-ENOTDIRThe path in dir isn't a directory
-EACCESAccess to dir or it's file(s) is not allowed
-EINVALOne of the INI file(s) in dir contain an error

◆ dump_ini_table()

static int dump_ini_table ( FILE *  file,
const struct caps_inif_table dst 
)
static

Write the table content into a INI

Parameters
[in]fileThe stream to write the content to
[in]dstPointer to INI table
Return values
0On success
Note
This function does not return if the dst seems broken.
Todo:
dump_ini_table(): add error handling for fprintf().

◆ caps_inif_free_lock_info()

static void caps_inif_free_lock_info ( struct caps_inif_lock li)
static

Unlock the file and free related resouces

Parameters
[in]liThe lock information to destroy

Variable Documentation

◆ data_set_desc

const struct caps_table_desc data_set_desc
static
Initial value:
= {
.element_sz = sizeof(struct data_set),
.increment = 10 ,
}
INI data set.
Definition: libcapsinif.c:74