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