DTS Application Library  0.2.3
Application library containing referenced objects and interfaces to common libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
INI Style config file Interface

Reads a ini config file into grouped hashed buckets. More...

Files

file  config.c
 INI style config file interface.
 

Data Structures

struct  config_category
 Configuration file category. More...
 
struct  config_file
 Config file. More...
 
struct  config_entry
 Configuration category entry. More...
 

Typedefs

typedef void(* config_filecb )(struct bucket_list *, const char *, const char *)
 Calback used when processing config files. More...
 
typedef void(* config_catcb )(struct bucket_list *, const char *)
 Calback used when processing a category. More...
 
typedef void(* config_entrycb )(const char *, const char *)
 Callback used when processing a entry. More...
 

Functions

void unrefconfigfiles (void)
 Empty out and unreference config files. More...
 
int process_config (const char *configname, const char *configfile)
 Process a configfile into buckets. More...
 
struct bucket_listget_config_file (const char *configname)
 Returns the catergories bucket for a config file. More...
 
struct bucket_listget_config_category (const char *configname, const char *category)
 Return a single category. More...
 
struct bucket_listget_category_next (struct bucket_loop *cloop, char *name, int len)
 Iterate through categories returning the entries bucket. More...
 
struct bucket_loopget_category_loop (const char *configname)
 Return a bucket loop to allow iterating over categories. More...
 
void config_entry_callback (struct bucket_list *entries, config_entrycb entry_cb)
 Callback Wraper that iterates through all items calling a callback for each item. More...
 
void config_cat_callback (struct bucket_list *categories, config_catcb cat_cb)
 Callback wrapper that iterates through categories calling a callback on each category. More...
 
void config_file_callback (config_filecb file_cb)
 Callback wrapper to iterate over all configfiles calling a callback on each file. More...
 
struct config_entryget_config_entry (struct bucket_list *categories, const char *item)
 Find the entry in a config file. More...
 

Detailed Description

Reads a ini config file into grouped hashed buckets.

Typedef Documentation

typedef void(* config_catcb)(struct bucket_list *, const char *)

Calback used when processing a category.

Parameters
entriesBucket list containing entries.
nameCategory name.

Definition at line 291 of file dtsapp.h.

typedef void(* config_entrycb)(const char *, const char *)

Callback used when processing a entry.

Parameters
itemName of the entry.
valueValue of the entry.

Definition at line 297 of file dtsapp.h.

typedef void(* config_filecb)(struct bucket_list *, const char *, const char *)

Calback used when processing config files.

Parameters
categoriesBucket list of categories.
filenameThe filename.
filepathThe filepath.

Definition at line 285 of file dtsapp.h.

Function Documentation

void config_cat_callback ( struct bucket_list categories,
config_catcb  cat_cb 
)

Callback wrapper that iterates through categories calling a callback on each category.

See Also
config_catcb
Parameters
categoriesBucketlist from a config file containing categories.
cat_cbCallback to call on each category.

Definition at line 383 of file config.c.

References bucketlist_callback().

383  {
384  bucketlist_callback(categories, category_callback, &cat_cb);
385 }
void bucketlist_callback(struct bucket_list *blist, blist_cb callback, void *data2)
Run a callback function on all items in the list.
Definition: refobj.c:613
void config_entry_callback ( struct bucket_list entries,
config_entrycb  entry_cb 
)

Callback Wraper that iterates through all items calling a callback for each item.

See Also
config_entrycb
Parameters
entriesBucketlist of entries (from a category).
entry_cbThe callback to call on each entry.

Definition at line 365 of file config.c.

References bucketlist_callback().

365  {
366  bucketlist_callback(entries, entry_callback, &entry_cb);
367 }
void bucketlist_callback(struct bucket_list *blist, blist_cb callback, void *data2)
Run a callback function on all items in the list.
Definition: refobj.c:613
void config_file_callback ( config_filecb  file_cb)

Callback wrapper to iterate over all configfiles calling a callback on each file.

See Also
config_filecb
Parameters
file_cbCallback to call.

Definition at line 400 of file config.c.

References bucketlist_callback().

400  {
401  bucketlist_callback(configfiles, file_callback, &file_cb);
402 }
void bucketlist_callback(struct bucket_list *blist, blist_cb callback, void *data2)
Run a callback function on all items in the list.
Definition: refobj.c:613
struct bucket_loop* get_category_loop ( const char *  configname)

Return a bucket loop to allow iterating over categories.

Parameters
confignameName assigned to the config file when calling process_config().
Returns
Bucket loop iterator.

Definition at line 341 of file config.c.

References get_config_file(), init_bucket_loop(), and objunref().

341  {
342  struct bucket_loop *cloop;
343  struct bucket_list *file;
344 
345  file = get_config_file(configname);
346  cloop = init_bucket_loop(file);
347  objunref(file);
348  return (cloop);
349 }
Bucket iterator.
Definition: refobj.c:97
struct bucket_list * get_config_file(const char *configname)
Returns the catergories bucket for a config file.
Definition: config.c:263
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
struct bucket_loop * init_bucket_loop(struct bucket_list *blist)
Create a bucket list iterator to safely iterate the list.
Definition: refobj.c:640
Bucket list, hold hashed objects in buckets.
Definition: refobj.c:75
struct bucket_list* get_category_next ( struct bucket_loop cloop,
char *  name,
int  len 
)

Iterate through categories returning the entries bucket.

As well as the entries returned name will be filled upto len bytes with the category name

Parameters
cloopIterator created with get_category_loop.
nameBuffer where the category name is copied.
lenlimit the number of characters copied to len.
Returns
Entries list for category returned in paramaater name.

Definition at line 317 of file config.c.

References config_category::entries, config_category::name, next_bucket_loop(), objref(), objunref(), and strlenzero().

317  {
318  struct config_category *category;
319 
320  if (cloop && (category = next_bucket_loop(cloop))) {
321  if (category->entries) {
322  if (!objref(category->entries)) {
323  objunref(category);
324  return (NULL);
325  }
326  if (!strlenzero(name)) {
327  strncpy(name, category->name, len);
328  }
329  objunref(category);
330  return (category->entries);
331  } else {
332  objunref(category);
333  }
334  }
335  return (NULL);
336 }
int strlenzero(const char *str)
Check if a string is zero length.
Definition: util.c:341
int objref(void *data)
Reference a object.
Definition: refobj.c:153
const char * name
Category name.
Definition: config.c:34
void * next_bucket_loop(struct bucket_loop *bloop)
Return a reference to the next item in the list this could be the first item.
Definition: refobj.c:662
struct bucket_list * entries
Entries in category.
Definition: config.c:36
Configuration file category.
Definition: config.c:32
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
struct bucket_list* get_config_category ( const char *  configname,
const char *  category 
)

Return a single category.

If category is NULL the category "default" is returned.

Parameters
confignameName assigned to the config file when calling process_config().
categoryConfiguration category to return or "default" if NULL.
Returns
Bucket list containing the category.

Definition at line 286 of file config.c.

References bucket_list_find_key(), config_category::entries, get_config_file(), objref(), and objunref().

286  {
287  struct bucket_list *file;
288  struct config_category *cat;
289 
290  file = get_config_file(configname);
291  if (category) {
292  cat = bucket_list_find_key(file, category);
293  } else {
294  cat = bucket_list_find_key(file, "default");
295  }
296 
297  objunref(file);
298  if (cat) {
299  if (!objref(cat->entries)) {
300  objunref(cat);
301  return (NULL);
302  }
303  objunref(cat);
304  return (cat->entries);
305  } else {
306  return (NULL);
307  }
308 }
int objref(void *data)
Reference a object.
Definition: refobj.c:153
struct bucket_list * entries
Entries in category.
Definition: config.c:36
struct bucket_list * get_config_file(const char *configname)
Returns the catergories bucket for a config file.
Definition: config.c:263
Configuration file category.
Definition: config.c:32
void * bucket_list_find_key(struct bucket_list *list, const void *key)
Find and return a reference to a item matching supplied key.
Definition: refobj.c:572
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
Bucket list, hold hashed objects in buckets.
Definition: refobj.c:75
struct config_entry* get_config_entry ( struct bucket_list categories,
const char *  item 
)

Find the entry in a config file.

Parameters
categoriesCategories bucketlist.
itemItem to search for.
Returns
Reference to a entry.

Definition at line 408 of file config.c.

References bucket_list_find_key().

408  {
409  struct config_entry *entry;
410 
411  entry = bucket_list_find_key(categories, item);
412 
413  return (entry);
414 }
const char * item
Definition: dtsapp.h:157
Configuration category entry.
Definition: dtsapp.h:155
void * bucket_list_find_key(struct bucket_list *list, const void *key)
Find and return a reference to a item matching supplied key.
Definition: refobj.c:572
struct bucket_list* get_config_file ( const char *  configname)

Returns the catergories bucket for a config file.

Parameters
confignameName assigned to the config file when calling process_config().
Returns
Categories bucketlist.

Definition at line 263 of file config.c.

References bucket_list_find_key(), config_file::cat, objref(), and objunref().

Referenced by get_category_loop(), and get_config_category().

263  {
264  struct config_file *file;
265 
266  if ((file = bucket_list_find_key(configfiles, configname))) {
267  if (file->cat) {
268  if (!objref(file->cat)) {
269  objunref(file);
270  return (NULL);
271  }
272  objunref(file);
273  return (file->cat);
274  }
275  objunref(file);
276  }
277  return (NULL);
278 }
struct bucket_list * cat
Categories.
Definition: config.c:46
int objref(void *data)
Reference a object.
Definition: refobj.c:153
Config file.
Definition: config.c:40
void * bucket_list_find_key(struct bucket_list *list, const void *key)
Find and return a reference to a item matching supplied key.
Definition: refobj.c:572
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int process_config ( const char *  configname,
const char *  configfile 
)

Process a configfile into buckets.

Parameters
confignameName of the configuration.
configfileFile to load into this configuration container.
Returns
Zero on success.

Definition at line 197 of file config.c.

References addtobucket(), config_file::cat, config_file::filepath, objunref(), strlenzero(), and trim().

197  {
198  struct config_file *file;
199  struct config_category *category = NULL;
200  FILE *config;
201  char line[256];
202  char item[128];
203  char value[128];
204  char *tmp = (char *)&line;
205  char *token;
206 
207  if (!configfiles) {
208  initconfigfiles();
209  }
210 
211  file = create_conf_file(configname, configfile);
212  addtobucket(configfiles, file);
213 
214  if (!(config = fopen(file->filepath, "r"))) {
215  return (-1);
216  }
217 
218  while(fgets(line, sizeof(line) - 1, config)) {
219  if (!(tmp = filterconf(line, 3))) {
220  continue;
221  }
222 
223  /*this is a new category*/
224  if ((token = strchr(tmp, '[')) && (token == tmp)) {
225  tmp++;
226  token = strrchr(tmp, ']');
227  token[0] = '\0';
228  tmp = trim(tmp);
229  if (!strlenzero(tmp)) {
230  if (category) {
231  objunref(category);
232  }
233  category = create_conf_category(tmp);
234  addtobucket(file->cat, category);
235  }
236  continue;
237  }
238 
239  if (sscanf(tmp, "%[^=] %*[=] %[^\n]", (char *)&item, (char *)&value) != 2) {
240  continue;
241  }
242 
243  if (!category) {
244  category = create_conf_category("default");
245  addtobucket(file->cat, category);
246  }
247 
248  add_conf_entry(category, trim(item), trim(value));
249  }
250  fclose(config);
251  if (category) {
252  objunref(category);
253  }
254  if (file) {
255  objunref(file);
256  }
257  return (0);
258 }
int strlenzero(const char *str)
Check if a string is zero length.
Definition: util.c:341
struct bucket_list * cat
Categories.
Definition: config.c:46
Config file.
Definition: config.c:40
char * trim(const char *str)
Trim whitesapce from the beggining and end of a string.
Definition: util.c:393
const char * filepath
File path.
Definition: config.c:44
Configuration file category.
Definition: config.c:32
int addtobucket(struct bucket_list *blist, void *data)
Add a reference to the bucketlist.
Definition: refobj.c:428
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
void unrefconfigfiles ( void  )

Empty out and unreference config files.

Definition at line 78 of file config.c.

References objunref().

Referenced by framework_init().

78  {
79  if (configfiles) {
80  objunref(configfiles);
81  }
82 }
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184