123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- /*
- * ini.c
- */
- #include "private.h"
- #include "lub/string.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include <ctype.h>
- /*--------------------------------------------------------- */
- void lub_ini_init(lub_ini_t *this)
- {
- assert(this);
- memset(this, 0, sizeof(*this));
- this->list = lub_list_new(lub_pair_compare, lub_pair_free);
- }
- /*--------------------------------------------------------- */
- lub_ini_t *lub_ini_new(void)
- {
- lub_ini_t *this;
- this = malloc(sizeof(*this));
- if (this)
- lub_ini_init(this);
- return this;
- }
- /*--------------------------------------------------------- */
- void lub_ini_fini(lub_ini_t *this)
- {
- lub_list_free_all(this->list);
- }
- /*--------------------------------------------------------- */
- void lub_ini_free(lub_ini_t *this)
- {
- assert(this);
- lub_ini_fini(this);
- free(this);
- }
- /*--------------------------------------------------------- */
- void lub_ini_add(lub_ini_t *this, lub_pair_t *pair)
- {
- lub_list_node_t *node = NULL;
- lub_pair_t *pair_in_list = NULL;
- assert(this);
- node = lub_list_find_add(this->list, pair);
- if (!node)
- return; // Strange error
- // INI file can contain multiple strings with the same keys. We will
- // consider last entry as actual.
- pair_in_list = (lub_pair_t *)lub_list_node__get_data(node);
- // Check for duplicate entry
- if (pair_in_list != pair) {
- // Set actual value
- lub_pair__set_value(pair_in_list, lub_pair__get_value(pair));
- lub_pair_free(pair);
- }
- }
- /*--------------------------------------------------------- */
- /* Find pair by name */
- lub_pair_t *lub_ini_find_pair(const lub_ini_t *this, const char *name)
- {
- lub_list_node_t *iter;
- lub_pair_t *pair;
- if (!this || !name)
- return NULL;
- /* Iterate elements */
- for(iter = lub_list__get_head(this->list);
- iter; iter = lub_list_node__get_next(iter)) {
- int res;
- pair = (lub_pair_t *)lub_list_node__get_data(iter);
- res = strcmp(lub_pair__get_name(pair), name);
- if (!res)
- return pair;
- if (res > 0) /* No chance to find name */
- break;
- }
- return NULL;
- }
- /*--------------------------------------------------------- */
- /* Find pair by name */
- const char *lub_ini_find(const lub_ini_t *this, const char *name)
- {
- lub_pair_t *pair = lub_ini_find_pair(this, name);
- if (!pair)
- return NULL;
- return lub_pair__get_value(pair);
- }
- /*--------------------------------------------------------- */
- int lub_ini_parse_str(lub_ini_t *this, const char *ini)
- {
- char *buffer = NULL;
- char *saveptr = NULL;
- char *line = NULL;
- buffer = lub_string_dup(ini);
- /* Now loop though each line */
- for (line = strtok_r(buffer, "\n", &saveptr);
- line; line = strtok_r(NULL, "\n", &saveptr)) {
- char *str = NULL;
- char *name = NULL;
- char *value = NULL;
- char *savestr = NULL;
- char *ns = line;
- const char *begin = NULL;
- size_t len = 0;
- char *rname = NULL;
- char *rvalue = NULL;
- lub_pair_t *pair = NULL;
- if (!*ns) /* Empty */
- continue;
- while (*ns && isspace(*ns))
- ns++;
- if ('#' == *ns) /* Comment */
- continue;
- if ('=' == *ns) /* Broken string */
- continue;
- str = lub_string_dup(ns);
- name = strtok_r(str, "=", &savestr);
- if (!name) {
- lub_string_free(str);
- continue;
- }
- value = strtok_r(NULL, "=", &savestr);
- begin = lub_string_nextword(name, &len, NULL, NULL, NULL, NULL);
- rname = lub_string_dupn(begin, len);
- if (!value) /* Empty value */
- rvalue = NULL;
- else {
- begin = lub_string_nextword(value, &len, NULL, NULL,
- NULL, NULL);
- rvalue = lub_string_dupn(begin, len);
- }
- pair = lub_pair_new(rname, rvalue);
- lub_ini_add(this, pair);
- lub_string_free(rname);
- lub_string_free(rvalue);
- lub_string_free(str);
- }
- lub_string_free(buffer);
- return 0;
- }
- /*--------------------------------------------------------- */
- int lub_ini_parse_file(lub_ini_t *this, const char *fn)
- {
- int ret = -1;
- FILE *f;
- char *buf;
- unsigned int p = 0;
- const int chunk_size = 128;
- int size = chunk_size;
- if (!fn || !*fn)
- return -1;
- f = fopen(fn, "r");
- if (!f)
- return -1;
- buf = malloc(size);
- while (fgets(buf + p, size - p, f)) {
- char *tmp;
- if (feof(f) || strchr(buf + p, '\n') || strchr(buf + p, '\r')) {
- lub_ini_parse_str(this, buf);
- p = 0;
- continue;
- }
- p = size - 1;
- size += chunk_size;
- tmp = realloc(buf, size);
- if (!tmp)
- goto error;
- buf = tmp;
- }
- ret = 0;
- error:
- free(buf);
- fclose(f);
- return ret;
- }
- /*--------------------------------------------------------- */
- lub_ini_node_t *lub_ini__get_head(lub_ini_t *this)
- {
- return lub_list__get_head(this->list);
- }
- /*--------------------------------------------------------- */
- lub_ini_node_t *lub_ini__get_tail(lub_ini_t *this)
- {
- return lub_list__get_tail(this->list);
- }
- /*--------------------------------------------------------- */
- lub_ini_node_t *lub_ini__get_next(lub_ini_node_t *node)
- {
- return lub_list_node__get_next(node);
- }
- /*--------------------------------------------------------- */
- lub_ini_node_t *lub_ini__get_prev(lub_ini_node_t *node)
- {
- return lub_list_node__get_next(node);
- }
- /*--------------------------------------------------------- */
- lub_pair_t *lub_ini__iter_data(lub_ini_node_t *node)
- {
- return (lub_pair_t *)lub_list_node__get_data(node);
- }
- /*--------------------------------------------------------- */
|