kview.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <faux/str.h>
  6. #include <faux/list.h>
  7. #include <faux/error.h>
  8. #include <klish/khelper.h>
  9. #include <klish/kcommand.h>
  10. #include <klish/kview.h>
  11. struct kview_s {
  12. char *name;
  13. faux_list_t *commands;
  14. };
  15. // Simple attributes
  16. // Name
  17. KGET_STR(view, name);
  18. // COMMAND list
  19. KGET(view, faux_list_t *, commands);
  20. KCMP_NESTED(view, command, name);
  21. KCMP_NESTED_BY_KEY(view, command, name);
  22. KADD_NESTED(view, command);
  23. KFIND_NESTED(view, command);
  24. KNESTED_LEN(view, command);
  25. KNESTED_ITER(view, command);
  26. KNESTED_EACH(view, command);
  27. kview_t *kview_new(const char *name)
  28. {
  29. kview_t *view = NULL;
  30. if (faux_str_is_empty(name))
  31. return NULL;
  32. view = faux_zmalloc(sizeof(*view));
  33. assert(view);
  34. if (!view)
  35. return NULL;
  36. // Initialize
  37. view->name = faux_str_dup(name);
  38. view->commands = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  39. kview_command_compare, kview_command_kcompare,
  40. (void (*)(void *))kcommand_free);
  41. assert(view->commands);
  42. return view;
  43. }
  44. void kview_free(kview_t *view)
  45. {
  46. if (!view)
  47. return;
  48. faux_str_free(view->name);
  49. faux_list_free(view->commands);
  50. faux_free(view);
  51. }