FOSSology  4.4.0
Open Source License Compliance by Open Source Software
fossconfig.c File Reference

FOSSology library to read config file. More...

#include <fossconfig.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
Include dependency graph for fossconfig.c:

Go to the source code of this file.

Macros

#define BUFFER_SIZE   4096
 Maximum buffer length.
 
#define yynext()   (c = next()) != EOF
 
#define throw_error(error, domain, code, ...)
 

Functions

static gint str_comp (gconstpointer a, gconstpointer b, gpointer user_data)
 
static gboolean collect_keys (char *key, gpointer *value, char **data)
 
static gboolean fo_config_sub (const GMatchInfo *match, GString *ret, gpointer data)
 
static int fo_config_key (GTree *group, gchar *key, gchar *val, gboolean list, gchar *fname, guint line, GError **error)
 Inserts a new Key/Value pair into the mapping of keys to values. More...
 
static gboolean fo_config_eval (const GMatchInfo *match, GTree **g_current, fo_conf *dest, gchar *yyfile, guint yyline, GError **error)
 Decides what to do with any one line of an input file. More...
 
fo_conffo_config_load (char *rawname, GError **error)
 Load the configuration information from the provided file. More...
 
char * fo_config_get (fo_conf *conf, const char *group, const char *key, GError **error)
 Gets an element based on its group name and key name. If the group or key is not found, the error object is set and NULL is returned. More...
 
char * fo_config_get_list (fo_conf *conf, char *group, char *key, int idx, GError **error)
 
int fo_config_is_list (fo_conf *conf, char *group, char *key, GError **error)
 Checks if a particular value is a list or just a normal value. More...
 
int fo_config_list_length (fo_conf *conf, char *group, char *key, GError **error)
 Gets the length of the list associated with a particular list key. More...
 
void fo_config_free (fo_conf *conf)
 Frees the memory associated with the internal configuration data structures. More...
 
void fo_config_join (fo_conf *dst, fo_conf *src, GError **error)
 Takes all groups and key from a fo_conf and adds them to another. More...
 
char ** fo_config_group_set (fo_conf *conf, int *length)
 Gets the set of group names. More...
 
char ** fo_config_key_set (fo_conf *conf, char *group, int *length)
 Gets the set of key names for a particular group. More...
 
int fo_config_has_group (fo_conf *conf, char *group)
 Checks if the currently parsed configuration file has a specific group. More...
 
int fo_config_has_key (fo_conf *conf, char *group, char *key)
 Checks if the a specific group in the currently parsed configuration file has a specific key. More...
 
char * trim (char *ptext)
 Trimming whitespace. More...
 

Variables

static const gchar * fo_conf_pattern
 
static const gchar * fo_conf_variable = "\\$(\\w+)"
 
GRegex * fo_conf_parse
 Regex for parsing.
 
GRegex * fo_conf_replace
 Regex for replace.
 

Detailed Description

FOSSology library to read config file.

This library provides various methods to handle the FOSSology configuration file.

Definition in file fossconfig.c.

Macro Definition Documentation

◆ throw_error

#define throw_error (   error,
  domain,
  code,
  ... 
)
Value:
{ g_set_error(error, domain, code, __VA_ARGS__); \
return 0; }

Create a GError and return

Definition at line 104 of file fossconfig.c.

Function Documentation

◆ collect_keys()

static gboolean collect_keys ( char *  key,
gpointer *  value,
char **  data 
)
static

Function that collects all of the keys for a GTree into a single array of strings. This is used to grab the sets of keys for the fo_config_key_set and fo_config_group_set functions printf("%s\n", (*error)->message);

Parameters
keythe key for this particular key/value pair
valuethe value for this particular key/value pair
datanot used
Returns
always return 0 so that the traversal continues

Definition at line 85 of file fossconfig.c.

◆ fo_config_eval()

static gboolean fo_config_eval ( const GMatchInfo *  match,
GTree **  g_current,
fo_conf dest,
gchar *  yyfile,
guint  yyline,
GError **  error 
)
static

Decides what to do with any one line of an input file.

Based upon what part of the regex matches, this chooses what to do with the line from the input file.

portion/result:
group: create a new GTree* for the new group and move on
key: call fo_config_key with the key/value pair and list set to FALSE
klist: call fo_config_key with the key/value pair and list set to TRUE

Parameters
matchThe regex match that was found
g_currentThe current group that is being read from the file
destThe fo_conf struct that all this is being placed in
yyfileThe name of the file that is being parsed
yylineThe line of the file that is being parsed
errorGError struct used to pass errors out
Returns
FALSE if successful.

Definition at line 202 of file fossconfig.c.

◆ fo_config_free()

void fo_config_free ( fo_conf conf)

Frees the memory associated with the internal configuration data structures.

Parameters
confThe fo_conf struct to free

Definition at line 506 of file fossconfig.c.

◆ fo_config_get()

char* fo_config_get ( fo_conf conf,
const char *  group,
const char *  key,
GError **  error 
)

Gets an element based on its group name and key name. If the group or key is not found, the error object is set and NULL is returned.

Parameters
conffo_conf object to load from
groupC string that is the name of the group
keyC string that is the name of the key for the key/value pair
Returns
The C string representation of the value

Definition at line 336 of file fossconfig.c.

◆ fo_config_get_list()

char* fo_config_get_list ( fo_conf conf,
char *  group,
char *  key,
int  idx,
GError **  error 
)

Keys can be associated with multiple values. If this is the case for a particular key, use this function instead of fo_config_get(). This also takes the index of the element in the list. Index work identically to standard c-array indices. It is important to note event though keys will appear as "key[]" in the config file this function just takes "key" as the key

Parameters
conffo_conf object to load from
groupC string that is the name of the group
keyC string that is the name of the key for the key/value pair
idxThe index of the value in the value list
errorobject that allows errors to propagate up the stack
Returns
c string representation of the value, once returned the caller owns this pointer. make sure to call g_free on it

Definition at line 382 of file fossconfig.c.

◆ fo_config_group_set()

char** fo_config_group_set ( fo_conf conf,
int *  length 
)

Gets the set of group names.

This function returns an array of strings that the user can iterate to get all group names. The user does not own the return of this function and should not free any of the memory associated with it.

Parameters
conffo_conf object to load from
lengthPointer allowing the number of groups to be returned
Returns
Array of strings containing all the group names

Definition at line 572 of file fossconfig.c.

◆ fo_config_has_group()

int fo_config_has_group ( fo_conf conf,
char *  group 
)

Checks if the currently parsed configuration file has a specific group.

Parameters
conffo_conf object to load from
groupThe name of the group to check for
Returns
1 if the group exists, 0 if it does not

Definition at line 651 of file fossconfig.c.

◆ fo_config_has_key()

int fo_config_has_key ( fo_conf conf,
char *  group,
char *  key 
)

Checks if the a specific group in the currently parsed configuration file has a specific key.

Parameters
groupThe group to check for the key
keyThe key to check for
Returns
1 if the group has the key, 0 if it does not

Definition at line 668 of file fossconfig.c.

◆ fo_config_is_list()

int fo_config_is_list ( fo_conf conf,
char *  group,
char *  key,
GError **  error 
)

Checks if a particular value is a list or just a normal value.

Parameters
conffo_conf object to load from
groupC string name of the group
keyC string name of the key
Returns
0 if it isn't a list, 1 if it is

Definition at line 439 of file fossconfig.c.

◆ fo_config_join()

void fo_config_join ( fo_conf dst,
fo_conf src,
GError **  error 
)

Takes all groups and key from a fo_conf and adds them to another.

This will iterate across the groups of a configuration and add each to another configuration. This joins the two configurations, making it possible to see multiple configuration files as a single configuration.

Parameters
dstThe destination for all the information in src
srcThe information that will be duplicated into dst
errorA location to returns errors from

Definition at line 531 of file fossconfig.c.

◆ fo_config_key()

static int fo_config_key ( GTree *  group,
gchar *  key,
gchar *  val,
gboolean  list,
gchar *  fname,
guint  line,
GError **  error 
)
static

Inserts a new Key/Value pair into the mapping of keys to values.

Since the values need to be strings, if the key that is being is inserted is a list then this uses a system of "[value1][value2]...[valueN]" to store the value. This is done under the assumption that lists are not going to be extremely long.

Parameters
groupThe Group that this key belongs to
keyThe key that the value is associated with
valThe value that the key maps to
listIf the key/value pair is a list
fnameThe name of the file that this key was found in
lineThe line number in the file that this key was found on
errorGError struct that is used to pass errors out of this function
Returns
always returns 1

Definition at line 147 of file fossconfig.c.

◆ fo_config_key_set()

char** fo_config_key_set ( fo_conf conf,
char *  group,
int *  length 
)

Gets the set of key names for a particular group.

This returns an array of strings that the user can iterate to get all of the key's for a particular group. This is useful if the keys are not known for a particular group. The array returned by this is owned by the config library and should not be freed by the caller.

Parameters
conffo_conf object to load from
groupC string name of the group
lengthPointer allowing the number of keys to be returned
Returns
Array of string containing all the key names for a group

Definition at line 614 of file fossconfig.c.

◆ fo_config_list_length()

int fo_config_list_length ( fo_conf conf,
char *  group,
char *  key,
GError **  error 
)

Gets the length of the list associated with a particular list key.

Parameters
groupC string name of the group
keyC string name of the key
Returns
The number of elements in the list, on error returns 0

Definition at line 475 of file fossconfig.c.

◆ fo_config_load()

fo_conf* fo_config_load ( char *  rawname,
GError **  error 
)

Load the configuration information from the provided file.

If the user has not done a fo_config_free since the last fo_config_load, this will make sure to call that first. In other words, it is assumed that if this is called the configuration file has changed and the user would like to use the new copy.

Parameters
rawnameThe name of the configuration file
errorObject that allows errors to propagate up the stack
Returns
0 for failure, 1 for success

Definition at line 275 of file fossconfig.c.

◆ fo_config_sub()

static gboolean fo_config_sub ( const GMatchInfo *  match,
GString *  ret,
gpointer  data 
)
static

A glib regex replace callback function. This will get called from the fo_config_key when it call g_regex_replace_evel().

Parameters
matchThe regex match that was found in the text i.e. '$HI'
retA GString that is the return location for this function
dataUser data passed into the function, currently unused.
Returns
Always return FALSE to continue the traversal

Definition at line 117 of file fossconfig.c.

◆ str_comp()

static gint str_comp ( gconstpointer  a,
gconstpointer  b,
gpointer  user_data 
)
static

A wrapper function for the strcmp function that allows it to mascarade as a GCompareDataFunc

Parameters
aC string to be compared
bC string to be compare
user_datanot used
Returns
an integral value indicating the relationship between strings

Definition at line 69 of file fossconfig.c.

◆ trim()

char* trim ( char *  ptext)

Trimming whitespace.

Will trim the lead/trail space

Parameters
ptextThe string to trim
Returns
The string after trim

Definition at line 690 of file fossconfig.c.

Variable Documentation

◆ fo_conf_pattern

const gchar* fo_conf_pattern
static
Initial value:
= "\
(?<comment>;.*)\n|\
(?<group> \\[ (?:[ \t]*) (?<gname>[\\w\\d_]+) (?:[ \t]*) \\]) \n|\
(?<key> ([\\.\\w\\d_-]+)) (?:[ \t]*) = (?:[ \t]*)(?<value>.*)\n|\
(?<klist> ([\\.\\w\\d_-]+))\\[\\](?:[ \t]*) = (?:[ \t]*)(?<vlist>.*)\n|\
(?<error> (?:\\S+)(?:[ \t]*))\n"

Complicated regular expression for parsing the different elements of the ini file format.

The parts:

line 1: matches ini comments : "; something \n"
line 2: matches ini groups : "[group_name]\n"
line 3: matches ini key-value pairs : "key = value\n"
line 4: matches ini key-value list pairs : "key[] = value\n"
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308

Please refer to the glib regular expression syntax for more information about how the different parts of the regex work.

Definition at line 44 of file fossconfig.c.

◆ fo_conf_variable

const gchar* fo_conf_variable = "\\$(\\w+)"
static

Regular expression that is used to match variables in the configuration file. This simply matches a '$' followed by a set of alphabetic characters.

Definition at line 55 of file fossconfig.c.