strlist.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
  3. *
  4. * Licensed under the GPLv2.
  5. */
  6. #include "strlist.h"
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. static struct str_node *str_node__new(const char *s, bool dupstr)
  12. {
  13. struct str_node *self = malloc(sizeof(*self));
  14. if (self != NULL) {
  15. if (dupstr) {
  16. s = strdup(s);
  17. if (s == NULL)
  18. goto out_delete;
  19. }
  20. self->s = s;
  21. }
  22. return self;
  23. out_delete:
  24. free(self);
  25. return NULL;
  26. }
  27. static void str_node__delete(struct str_node *self, bool dupstr)
  28. {
  29. if (dupstr)
  30. free((void *)self->s);
  31. free(self);
  32. }
  33. int strlist__add(struct strlist *self, const char *new_entry)
  34. {
  35. struct rb_node **p = &self->entries.rb_node;
  36. struct rb_node *parent = NULL;
  37. struct str_node *sn;
  38. while (*p != NULL) {
  39. int rc;
  40. parent = *p;
  41. sn = rb_entry(parent, struct str_node, rb_node);
  42. rc = strcmp(sn->s, new_entry);
  43. if (rc > 0)
  44. p = &(*p)->rb_left;
  45. else if (rc < 0)
  46. p = &(*p)->rb_right;
  47. else
  48. return -EEXIST;
  49. }
  50. sn = str_node__new(new_entry, self->dupstr);
  51. if (sn == NULL)
  52. return -ENOMEM;
  53. rb_link_node(&sn->rb_node, parent, p);
  54. rb_insert_color(&sn->rb_node, &self->entries);
  55. ++self->nr_entries;
  56. return 0;
  57. }
  58. int strlist__load(struct strlist *self, const char *filename)
  59. {
  60. char entry[1024];
  61. int err;
  62. FILE *fp = fopen(filename, "r");
  63. if (fp == NULL)
  64. return errno;
  65. while (fgets(entry, sizeof(entry), fp) != NULL) {
  66. const size_t len = strlen(entry);
  67. if (len == 0)
  68. continue;
  69. entry[len - 1] = '\0';
  70. err = strlist__add(self, entry);
  71. if (err != 0)
  72. goto out;
  73. }
  74. err = 0;
  75. out:
  76. fclose(fp);
  77. return err;
  78. }
  79. void strlist__remove(struct strlist *self, struct str_node *sn)
  80. {
  81. rb_erase(&sn->rb_node, &self->entries);
  82. str_node__delete(sn, self->dupstr);
  83. }
  84. struct str_node *strlist__find(struct strlist *self, const char *entry)
  85. {
  86. struct rb_node **p = &self->entries.rb_node;
  87. struct rb_node *parent = NULL;
  88. while (*p != NULL) {
  89. struct str_node *sn;
  90. int rc;
  91. parent = *p;
  92. sn = rb_entry(parent, struct str_node, rb_node);
  93. rc = strcmp(sn->s, entry);
  94. if (rc > 0)
  95. p = &(*p)->rb_left;
  96. else if (rc < 0)
  97. p = &(*p)->rb_right;
  98. else
  99. return sn;
  100. }
  101. return NULL;
  102. }
  103. static int strlist__parse_list_entry(struct strlist *self, const char *s)
  104. {
  105. if (strncmp(s, "file://", 7) == 0)
  106. return strlist__load(self, s + 7);
  107. return strlist__add(self, s);
  108. }
  109. int strlist__parse_list(struct strlist *self, const char *s)
  110. {
  111. char *sep;
  112. int err;
  113. while ((sep = strchr(s, ',')) != NULL) {
  114. *sep = '\0';
  115. err = strlist__parse_list_entry(self, s);
  116. *sep = ',';
  117. if (err != 0)
  118. return err;
  119. s = sep + 1;
  120. }
  121. return *s ? strlist__parse_list_entry(self, s) : 0;
  122. }
  123. struct strlist *strlist__new(bool dupstr, const char *slist)
  124. {
  125. struct strlist *self = malloc(sizeof(*self));
  126. if (self != NULL) {
  127. self->entries = RB_ROOT;
  128. self->dupstr = dupstr;
  129. self->nr_entries = 0;
  130. if (slist && strlist__parse_list(self, slist) != 0)
  131. goto out_error;
  132. }
  133. return self;
  134. out_error:
  135. free(self);
  136. return NULL;
  137. }
  138. void strlist__delete(struct strlist *self)
  139. {
  140. if (self != NULL) {
  141. struct str_node *pos;
  142. struct rb_node *next = rb_first(&self->entries);
  143. while (next) {
  144. pos = rb_entry(next, struct str_node, rb_node);
  145. next = rb_next(&pos->rb_node);
  146. strlist__remove(self, pos);
  147. }
  148. self->entries = RB_ROOT;
  149. free(self);
  150. }
  151. }
  152. struct str_node *strlist__entry(const struct strlist *self, unsigned int idx)
  153. {
  154. struct rb_node *nd;
  155. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  156. struct str_node *pos = rb_entry(nd, struct str_node, rb_node);
  157. if (!idx--)
  158. return pos;
  159. }
  160. return NULL;
  161. }