kpath.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /** @file kpath.c
  2. */
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <faux/list.h>
  8. #include <klish/khelper.h>
  9. #include <klish/kpath.h>
  10. struct kpath_s {
  11. faux_list_t *levels;
  12. };
  13. kpath_t *kpath_new()
  14. {
  15. kpath_t *path = NULL;
  16. path = faux_zmalloc(sizeof(*path));
  17. assert(path);
  18. if (!path)
  19. return NULL;
  20. // User data blobs list
  21. path->levels = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  22. NULL, NULL, (void (*)(void *))klevel_free);
  23. assert(path->levels);
  24. return path;
  25. }
  26. void kpath_free(kpath_t *path)
  27. {
  28. if (!path)
  29. return;
  30. faux_list_free(path->levels);
  31. free(path);
  32. }
  33. size_t kpath_len(const kpath_t *path)
  34. {
  35. assert(path);
  36. if (!path)
  37. return 0;
  38. return faux_list_len(path->levels);
  39. }
  40. size_t kpath_is_empty(const kpath_t *path)
  41. {
  42. assert(path);
  43. if (!path)
  44. return 0;
  45. return faux_list_is_empty(path->levels);
  46. }
  47. bool_t kpath_push(kpath_t *path, klevel_t *level)
  48. {
  49. assert(path);
  50. assert(level);
  51. if (!path)
  52. return BOOL_FALSE;
  53. if (!level)
  54. return BOOL_FALSE;
  55. if (!faux_list_add(path->levels, level))
  56. return BOOL_FALSE;
  57. return BOOL_TRUE;
  58. }
  59. bool_t kpath_pop(kpath_t *path)
  60. {
  61. assert(path);
  62. if (!path)
  63. return BOOL_FALSE;
  64. if (kpath_is_empty(path))
  65. return BOOL_FALSE;
  66. return faux_list_del(path->levels, faux_list_tail(path->levels));
  67. }
  68. klevel_t *kpath_current(const kpath_t *path)
  69. {
  70. assert(path);
  71. if (!path)
  72. return NULL;
  73. if (kpath_is_empty(path))
  74. return NULL;
  75. return (klevel_t *)faux_list_data(faux_list_tail(path->levels));
  76. }
  77. kpath_levels_node_t *kpath_iterr(const kpath_t *path)
  78. {
  79. assert(path);
  80. if (!path)
  81. return NULL;
  82. return (kpath_levels_node_t *)faux_list_tail(path->levels);
  83. }
  84. klevel_t *kpath_eachr(kpath_levels_node_t **iterr)
  85. {
  86. return (klevel_t *)faux_list_eachr((faux_list_node_t **)iterr);
  87. }
  88. kpath_levels_node_t *kpath_iter(const kpath_t *path)
  89. {
  90. assert(path);
  91. if (!path)
  92. return NULL;
  93. return (kpath_levels_node_t *)faux_list_head(path->levels);
  94. }
  95. klevel_t *kpath_each(kpath_levels_node_t **iter)
  96. {
  97. return (klevel_t *)faux_list_each((faux_list_node_t **)iter);
  98. }
  99. kpath_t *kpath_clone(const kpath_t *path)
  100. {
  101. kpath_t *new_path = NULL;
  102. kpath_levels_node_t *iter = NULL;
  103. klevel_t *level = NULL;
  104. assert(path);
  105. if (!path)
  106. return NULL;
  107. new_path = kpath_new();
  108. iter = kpath_iter(path);
  109. while ((level = kpath_each(&iter)))
  110. kpath_push(new_path, klevel_clone(level));
  111. return new_path;
  112. }
  113. bool_t kpath_is_equal(const kpath_t *f, const kpath_t *s)
  114. {
  115. kpath_levels_node_t *iter_f = NULL;
  116. kpath_levels_node_t *iter_s = NULL;
  117. klevel_t *level_f = NULL;
  118. klevel_t *level_s = NULL;
  119. if (!f && !s)
  120. return BOOL_TRUE;
  121. if (!f || !s)
  122. return BOOL_FALSE;
  123. iter_f = kpath_iterr(f);
  124. iter_s = kpath_iterr(s);
  125. do {
  126. level_f = kpath_eachr(&iter_f);
  127. level_s = kpath_eachr(&iter_s);
  128. if (!klevel_is_equal(level_f, level_s))
  129. return BOOL_FALSE;
  130. } while(level_f);
  131. return BOOL_TRUE;
  132. }