kparam.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 <klish/khelper.h>
  8. #include <klish/kptype.h>
  9. #include <klish/kparam.h>
  10. struct kparam_s {
  11. char *name;
  12. char *help;
  13. char *ptype_ref; // Text reference to PTYPE
  14. kptype_t *ptype; // Resolved PARAM's PTYPE
  15. faux_list_t *params; // Nested parameters
  16. };
  17. // Simple methods
  18. // Name
  19. KGET_STR(param, name);
  20. // Help
  21. KGET_STR(param, help);
  22. KSET_STR(param, help);
  23. // PTYPE reference (must be resolved later)
  24. KGET_STR(param, ptype_ref);
  25. KSET_STR(param, ptype_ref);
  26. // PTYPE (resolved)
  27. KGET(param, kptype_t *, ptype);
  28. KSET(param, kptype_t *, ptype);
  29. // PARAM list
  30. static KCMP_NESTED(param, param, name);
  31. static KCMP_NESTED_BY_KEY(param, param, name);
  32. KADD_NESTED(param, param);
  33. KFIND_NESTED(param, param);
  34. kparam_t *kparam_new(const char *name)
  35. {
  36. kparam_t *param = NULL;
  37. if (faux_str_is_empty(name))
  38. return NULL;
  39. param = faux_zmalloc(sizeof(*param));
  40. assert(param);
  41. if (!param)
  42. return NULL;
  43. // Initialize
  44. param->name = faux_str_dup(name);
  45. param->help = NULL;
  46. param->ptype_ref = NULL;
  47. param->ptype = NULL;
  48. param->params = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  49. kparam_param_compare, kparam_param_kcompare,
  50. (void (*)(void *))kparam_free);
  51. assert(param->params);
  52. return param;
  53. }
  54. void kparam_free(kparam_t *param)
  55. {
  56. if (!param)
  57. return;
  58. faux_str_free(param->name);
  59. faux_str_free(param->help);
  60. faux_str_free(param->ptype_ref);
  61. faux_list_free(param->params);
  62. faux_free(param);
  63. }