kptype.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/kptype.h>
  8. struct kptype_s {
  9. bool_t is_static;
  10. iptype_t info;
  11. };
  12. static kptype_t *kptype_new_internal(iptype_t info, bool_t is_static)
  13. {
  14. kptype_t *ptype = NULL;
  15. ptype = faux_zmalloc(sizeof(*ptype));
  16. assert(ptype);
  17. if (!ptype)
  18. return NULL;
  19. // Initialize
  20. ptype->is_static = is_static;
  21. ptype->info = info;
  22. return ptype;
  23. }
  24. kptype_t *kptype_new(iptype_t info)
  25. {
  26. return kptype_new_internal(info, BOOL_FALSE);
  27. }
  28. kptype_t *kptype_new_static(iptype_t info)
  29. {
  30. return kptype_new_internal(info, BOOL_TRUE);
  31. }
  32. void kptype_free(kptype_t *ptype)
  33. {
  34. if (!ptype)
  35. return;
  36. if (!ptype->is_static) {
  37. faux_str_free(ptype->info.name);
  38. faux_str_free(ptype->info.help);
  39. }
  40. faux_free(ptype);
  41. }
  42. const char *kptype_name(const kptype_t *ptype)
  43. {
  44. assert(ptype);
  45. if (!ptype)
  46. return NULL;
  47. return ptype->info.name;
  48. }
  49. const char *kptype_help(const kptype_t *ptype)
  50. {
  51. assert(ptype);
  52. if (!ptype)
  53. return NULL;
  54. return ptype->info.help;
  55. }