ini.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * ini.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <ctype.h>
  11. /*--------------------------------------------------------- */
  12. void lub_ini_init(lub_ini_t *this)
  13. {
  14. assert(this);
  15. memset(this, 0, sizeof(*this));
  16. this->list = lub_list_new(lub_pair_compare, lub_pair_free);
  17. }
  18. /*--------------------------------------------------------- */
  19. lub_ini_t *lub_ini_new(void)
  20. {
  21. lub_ini_t *this;
  22. this = malloc(sizeof(*this));
  23. if (this)
  24. lub_ini_init(this);
  25. return this;
  26. }
  27. /*--------------------------------------------------------- */
  28. void lub_ini_fini(lub_ini_t *this)
  29. {
  30. lub_list_free_all(this->list);
  31. }
  32. /*--------------------------------------------------------- */
  33. void lub_ini_free(lub_ini_t *this)
  34. {
  35. assert(this);
  36. lub_ini_fini(this);
  37. free(this);
  38. }
  39. /*--------------------------------------------------------- */
  40. void lub_ini_add(lub_ini_t *this, lub_pair_t *pair)
  41. {
  42. assert(this);
  43. lub_list_add(this->list, pair);
  44. }
  45. /*--------------------------------------------------------- */
  46. /* Find pair by name */
  47. lub_pair_t *lub_ini_find_pair(const lub_ini_t *this, const char *name)
  48. {
  49. lub_list_node_t *iter;
  50. lub_pair_t *pair;
  51. if (!this || !name)
  52. return NULL;
  53. /* Iterate elements */
  54. for(iter = lub_list__get_head(this->list);
  55. iter; iter = lub_list_node__get_next(iter)) {
  56. int res;
  57. pair = (lub_pair_t *)lub_list_node__get_data(iter);
  58. res = strcmp(lub_pair__get_name(pair), name);
  59. if (!res)
  60. return pair;
  61. if (res > 0) /* No chance to find name */
  62. break;
  63. }
  64. return NULL;
  65. }
  66. /*--------------------------------------------------------- */
  67. /* Find pair by name */
  68. const char *lub_ini_find(const lub_ini_t *this, const char *name)
  69. {
  70. lub_pair_t *pair = lub_ini_find_pair(this, name);
  71. if (!pair)
  72. return NULL;
  73. return lub_pair__get_value(pair);
  74. }
  75. /*--------------------------------------------------------- */
  76. int lub_ini_parse_str(lub_ini_t *this, const char *ini)
  77. {
  78. char *buffer = NULL;
  79. char *saveptr = NULL;
  80. char *line = NULL;
  81. buffer = lub_string_dup(ini);
  82. /* Now loop though each line */
  83. for (line = strtok_r(buffer, "\n", &saveptr);
  84. line; line = strtok_r(NULL, "\n", &saveptr)) {
  85. char *str = NULL;
  86. char *name = NULL;
  87. char *value = NULL;
  88. char *savestr = NULL;
  89. char *ns = line;
  90. const char *begin = NULL;
  91. size_t len = 0;
  92. char *rname = NULL;
  93. char *rvalue = NULL;
  94. lub_pair_t *pair = NULL;
  95. if (!*ns) /* Empty */
  96. continue;
  97. while (*ns && isspace(*ns))
  98. ns++;
  99. if ('#' == *ns) /* Comment */
  100. continue;
  101. if ('=' == *ns) /* Broken string */
  102. continue;
  103. str = lub_string_dup(ns);
  104. name = strtok_r(str, "=", &savestr);
  105. if (!name) {
  106. lub_string_free(str);
  107. continue;
  108. }
  109. value = strtok_r(NULL, "=", &savestr);
  110. begin = lub_string_nextword(name, &len, NULL, NULL, NULL);
  111. rname = lub_string_dupn(begin, len);
  112. if (!value) /* Empty value */
  113. rvalue = NULL;
  114. else {
  115. begin = lub_string_nextword(value, &len, NULL, NULL, NULL);
  116. rvalue = lub_string_dupn(begin, len);
  117. }
  118. pair = lub_pair_new(rname, rvalue);
  119. lub_ini_add(this, pair);
  120. lub_string_free(rname);
  121. lub_string_free(rvalue);
  122. lub_string_free(str);
  123. }
  124. lub_string_free(buffer);
  125. return 0;
  126. }
  127. /*--------------------------------------------------------- */
  128. int lub_ini_parse_file(lub_ini_t *this, const char *fn)
  129. {
  130. int ret = -1;
  131. FILE *f;
  132. char *buf;
  133. unsigned int p = 0;
  134. const int chunk_size = 128;
  135. int size = chunk_size;
  136. if (!fn || !*fn)
  137. return -1;
  138. f = fopen(fn, "r");
  139. if (!f)
  140. return -1;
  141. buf = malloc(size);
  142. while (fgets(buf + p, size - p, f)) {
  143. char *tmp;
  144. if (feof(f) || strchr(buf + p, '\n') || strchr(buf + p, '\r')) {
  145. lub_ini_parse_str(this, buf);
  146. p = 0;
  147. continue;
  148. }
  149. p = size - 1;
  150. size += chunk_size;
  151. tmp = realloc(buf, size);
  152. if (!tmp)
  153. goto error;
  154. buf = tmp;
  155. }
  156. ret = 0;
  157. error:
  158. free(buf);
  159. fclose(f);
  160. return ret;
  161. }
  162. /*--------------------------------------------------------- */
  163. lub_ini_node_t *lub_ini__get_head(lub_ini_t *this)
  164. {
  165. return lub_list__get_head(this->list);
  166. }
  167. /*--------------------------------------------------------- */
  168. lub_ini_node_t *lub_ini__get_tail(lub_ini_t *this)
  169. {
  170. return lub_list__get_tail(this->list);
  171. }
  172. /*--------------------------------------------------------- */
  173. lub_ini_node_t *lub_ini__get_next(lub_ini_node_t *node)
  174. {
  175. return lub_list_node__get_next(node);
  176. }
  177. /*--------------------------------------------------------- */
  178. lub_ini_node_t *lub_ini__get_prev(lub_ini_node_t *node)
  179. {
  180. return lub_list_node__get_next(node);
  181. }
  182. /*--------------------------------------------------------- */
  183. lub_pair_t *lub_ini__iter_data(lub_ini_node_t *node)
  184. {
  185. return (lub_pair_t *)lub_list_node__get_data(node);
  186. }
  187. /*--------------------------------------------------------- */