46 (?<group> \\[ (?:[ \t]*) (?<gname>[\\w\\d_]+) (?:[ \t]*) \\]) \n|\
47 (?<key> ([\\.\\w\\d_-]+)) (?:[ \t]*) = (?:[ \t]*)(?<value>.*)\n|\
48 (?<klist> ([\\.\\w\\d_-]+))\\[\\](?:[ \t]*) = (?:[ \t]*)(?<vlist>.*)\n|\
49 (?<error> (?:\\S+)(?:[ \t]*))\n";
69 static gint
str_comp(gconstpointer a, gconstpointer b, gpointer user_data)
71 return strcmp((
char*) a, (
char*) b);
85 static gboolean
collect_keys(
char* key, gpointer* value,
char** data)
101 #define BUFFER_SIZE 4096
102 #define yynext() (c = next()) != EOF
104 #define throw_error(error, domain, code, ...) \
105 { g_set_error(error, domain, code, __VA_ARGS__); \
120 GTree* group = (GTree*) data;
121 gchar* key = g_match_info_fetch(
match, 1);
122 gchar* sub = g_tree_lookup(group, key);
124 g_string_append(ret, sub);
148 gchar* fname, guint line, GError** error)
158 "%s[line %d]: key \"%s\" does not have an associated group",
163 if ((val = g_tree_lookup(group, key)))
165 val = g_strdup_printf(
"%s[%s]", val, tmp);
170 val = g_strdup_printf(
"[%s]", tmp);
179 g_tree_insert(group, g_strdup(key), val);
203 fo_conf* dest, gchar* yyfile, guint yyline, GError** error)
205 gchar* error_t = NULL;
208 if ((error_t = g_match_info_fetch_named(
match,
"error")) != NULL)
210 if (strlen(error_t) > 0)
213 "%s[line %d]: incorrectly formated line \"%s\".",
214 yyfile, yyline, error_t);
224 gchar* group = g_match_info_fetch_named(
match,
"group");
225 gchar* gname = g_match_info_fetch_named(
match,
"gname");
226 gchar* key = g_match_info_fetch_named(
match,
"key");
227 gchar* value = g_match_info_fetch_named(
match,
"value");
228 gchar* klist = g_match_info_fetch_named(
match,
"klist");
229 gchar* vlist = g_match_info_fetch_named(
match,
"vlist");
230 gchar* wrong = g_match_info_fetch_named(
match,
"error");
232 if (group != NULL && group[0])
234 *g_current = g_tree_new_full(
str_comp, NULL, g_free, g_free);
235 g_tree_insert(dest->group_map, g_strdup(gname), *g_current);
237 else if (key != NULL && key[0])
239 if (!
fo_config_key(*g_current, key, value, FALSE, yyfile, yyline, error))
242 else if (klist != NULL && klist[0])
244 if (!
fo_config_key(*g_current, klist, vlist, TRUE, yyfile, yyline, error))
288 G_REGEX_EXTENDED | G_REGEX_OPTIMIZE, 0, NULL);
291 G_REGEX_EXTENDED | G_REGEX_OPTIMIZE, 0, NULL);
293 if ((fd = fopen(rawname,
"r")) == NULL)
295 "unable to open configuration file \"%s\"", rawname);
303 (GDestroyNotify) g_tree_unref);
305 GTree* g_current = NULL;
307 while (fgets(text,
sizeof(text), fd) != NULL)
317 g_match_info_free(
match);
347 "ERROR: invalid fo_conf object passed to fo_config_get");
354 "ERROR: unknown group \"%s\"", group);
356 if ((ret = g_tree_lookup(tree, key)) == NULL)
361 "ERROR: unknown key=\"%s\" for group=\"%s\"", key, group);
363 return g_tree_lookup(tree, key);
395 "ERROR: invalid fo_conf object passed to fo_config_get_list");
402 "ERROR: %s[%s] must be of type list to get list element", group, key);
409 "ERROR: %s[%s] %d is out of range", group, key, idx);
418 for (depth = 0; depth < idx;)
420 while (*(++curr) !=
'[');
425 while (*(++curr) !=
']');
426 val = g_strndup(val, curr - val);
449 "ERROR: invalid fo_conf object passed to fo_config_is_list");
456 "ERROR: unknown group \"%s\"", group);
458 if ((val = g_tree_lookup(tree, key)) == NULL)
463 "ERROR: unknown key/value expression \"%s\"", key);
465 return val[0] ==
'[';
486 "ERROR: %s[%s] must be of type list to get length", group, key);
493 for (curr = val; *curr; curr++)
538 for (i = 0; i < ngroups; i++)
543 "Cannot join configuration with conflicting group \"%s\"", groups[i]);
549 for (i = 0; i < ngroups; i++)
551 keys = g_tree_lookup(src->
group_map, groups[i]);
552 keys = g_tree_ref(keys);
553 g_tree_insert(dst->
group_map, g_strdup(groups[i]), keys);
631 *length = g_tree_nnodes(tree);
638 g_tree_foreach(tree, (GTraverseFunc)
collect_keys, ret);
678 return g_tree_lookup(tree, key) != NULL;
692 if (ptext && ptext[0])
694 int len = strlen(ptext);
695 while (isspace(ptext[len - 1])) ptext[--len] = 0;
696 while (isspace(*ptext)) ++ptext;
fo_conf * conf
The loaded configuration data.
char * fo_config_get_list(fo_conf *conf, char *group, char *key, int idx, GError **error)
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.
static gboolean fo_config_sub(const GMatchInfo *match, GString *ret, gpointer data)
char * trim(char *ptext)
Trimming whitespace.
void fo_config_free(fo_conf *conf)
Frees the memory associated with the internal configuration data structures.
char ** fo_config_group_set(fo_conf *conf, int *length)
Gets the set of group names.
#define BUFFER_SIZE
Maximum buffer length.
static const gchar * fo_conf_variable
GRegex * fo_conf_parse
Regex for parsing.
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.
fo_conf * fo_config_load(char *rawname, GError **error)
Load the configuration information from the provided file.
static gint str_comp(gconstpointer a, gconstpointer b, gpointer user_data)
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.
static const gchar * fo_conf_pattern
GRegex * fo_conf_replace
Regex for replace.
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,...
#define throw_error(error, domain, code,...)
int fo_config_has_group(fo_conf *conf, char *group)
Checks if the currently parsed configuration file has a specific group.
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.
static gboolean collect_keys(char *key, gpointer *value, char **data)
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.
char ** fo_config_key_set(fo_conf *conf, char *group, int *length)
Gets the set of key names for a particular group.
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.
FOSSology library to read config file.
@ fo_load_config
Unable to load config.
@ fo_missing_key
Required key is missing.
@ fo_invalid_join
Join is invalid.
@ fo_invalid_file
File is invalid.
@ fo_missing_file
File is missing.
@ fo_invalid_group
Requested group is invalid.
@ fo_missing_group
Required group is missing.
@ fo_invalid_key
Requested key is invalid.
char ** group_set
Array of groups.
int n_groups
Number of groups.
GTree * group_map
Tree of groups in conf file.
GTree * key_sets
Tree of sets of keys.
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Store the results of a regex match.