kpath.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. }