2
0

str_list.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* str_list.c - the opkg package management system
  2. Carl D. Worth
  3. Copyright (C) 2001 University of Southern California
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 2, or (at
  7. your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. */
  13. #include "str_list.h"
  14. #include "libbb/libbb.h"
  15. void str_list_elt_init(str_list_elt_t * elt, char *data)
  16. {
  17. void_list_elt_init((void_list_elt_t *) elt, data);
  18. }
  19. void str_list_elt_deinit(str_list_elt_t * elt)
  20. {
  21. if (elt->data)
  22. free(elt->data);
  23. void_list_elt_deinit((void_list_elt_t *) elt);
  24. }
  25. str_list_t *str_list_alloc()
  26. {
  27. str_list_t *list = xcalloc(1, sizeof(str_list_t));
  28. str_list_init(list);
  29. return list;
  30. }
  31. void str_list_init(str_list_t * list)
  32. {
  33. void_list_init((void_list_t *) list);
  34. }
  35. void str_list_deinit(str_list_t * list)
  36. {
  37. str_list_elt_t *elt;
  38. while (!void_list_empty(list)) {
  39. elt = str_list_first(list);
  40. if (!elt)
  41. return;
  42. list_del_init(&elt->node);
  43. free(elt->data);
  44. elt->data = NULL;
  45. free(elt);
  46. }
  47. }
  48. void str_list_append(str_list_t * list, char *data)
  49. {
  50. void_list_append((void_list_t *) list, xstrdup(data));
  51. }
  52. str_list_elt_t *str_list_pop(str_list_t * list)
  53. {
  54. return (str_list_elt_t *) void_list_pop((void_list_t *) list);
  55. }
  56. void str_list_remove(str_list_t * list, str_list_elt_t ** iter)
  57. {
  58. char *str = void_list_remove((void_list_t *) list,
  59. (void_list_elt_t **) iter);
  60. if (str)
  61. free(str);
  62. }
  63. void str_list_remove_elt(str_list_t * list, const char *target_str)
  64. {
  65. char *str = void_list_remove_elt((void_list_t *) list,
  66. (void *)target_str,
  67. (void_list_cmp_t) strcmp);
  68. if (str)
  69. free(str);
  70. }
  71. str_list_elt_t *str_list_first(str_list_t * list)
  72. {
  73. return (str_list_elt_t *) void_list_first((void_list_t *) list);
  74. }
  75. str_list_elt_t *str_list_next(str_list_t * list, str_list_elt_t * node)
  76. {
  77. return (str_list_elt_t *) void_list_next((void_list_t *) list,
  78. (void_list_elt_t *) node);
  79. }
  80. void str_list_purge(str_list_t * list)
  81. {
  82. str_list_deinit(list);
  83. free(list);
  84. }