paramv.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * paramv.c
  3. *
  4. */
  5. #include "private.h"
  6. #include "lub/string.h"
  7. #include "clish/types.h"
  8. #include <assert.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. /*--------------------------------------------------------- */
  13. static void clish_paramv_init(clish_paramv_t * this)
  14. {
  15. this->paramc = 0;
  16. this->paramv = NULL;
  17. }
  18. /*--------------------------------------------------------- */
  19. static void clish_paramv_fini(clish_paramv_t * this)
  20. {
  21. unsigned i;
  22. /* finalize each of the parameter instances */
  23. for (i = 0; i < this->paramc; i++) {
  24. clish_param_delete(this->paramv[i]);
  25. }
  26. /* free the parameter vector */
  27. free(this->paramv);
  28. this->paramc = 0;
  29. }
  30. /*--------------------------------------------------------- */
  31. clish_paramv_t *clish_paramv_new(void)
  32. {
  33. clish_paramv_t *this = malloc(sizeof(clish_paramv_t));
  34. if (this)
  35. clish_paramv_init(this);
  36. return this;
  37. }
  38. /*--------------------------------------------------------- */
  39. void clish_paramv_delete(clish_paramv_t * this)
  40. {
  41. clish_paramv_fini(this);
  42. free(this);
  43. }
  44. /*--------------------------------------------------------- */
  45. void clish_paramv_insert(clish_paramv_t * this, clish_param_t * param)
  46. {
  47. size_t new_size = ((this->paramc + 1) * sizeof(clish_param_t *));
  48. clish_param_t **tmp;
  49. /* resize the parameter vector */
  50. tmp = realloc(this->paramv, new_size);
  51. if (tmp) {
  52. this->paramv = tmp;
  53. /* insert reference to the parameter */
  54. this->paramv[this->paramc++] = param;
  55. }
  56. }
  57. /*--------------------------------------------------------- */
  58. int clish_paramv_remove(clish_paramv_t *this, unsigned int index)
  59. {
  60. size_t new_size;
  61. clish_param_t **tmp;
  62. clish_param_t **dst, **src;
  63. size_t n;
  64. if (this->paramc < 1)
  65. return -1;
  66. if (index >= this->paramc)
  67. return -1;
  68. new_size = ((this->paramc - 1) * sizeof(clish_param_t *));
  69. dst = this->paramv + index;
  70. src = dst + 1;
  71. n = this->paramc - index - 1;
  72. if (n)
  73. memmove(dst, src, n * sizeof(clish_param_t *));
  74. /* Resize the parameter vector */
  75. if (new_size) {
  76. tmp = realloc(this->paramv, new_size);
  77. if (!tmp)
  78. return -1;
  79. this->paramv = tmp;
  80. } else {
  81. free(this->paramv);
  82. this->paramv = NULL;
  83. }
  84. this->paramc--;
  85. return 0;
  86. }
  87. /*--------------------------------------------------------- */
  88. clish_param_t *clish_paramv__get_param(const clish_paramv_t * this,
  89. unsigned int index)
  90. {
  91. clish_param_t *result = NULL;
  92. if (index < this->paramc)
  93. result = this->paramv[index];
  94. return result;
  95. }
  96. /*--------------------------------------------------------- */
  97. clish_param_t *clish_paramv_find_param(const clish_paramv_t * this,
  98. const char *name)
  99. {
  100. clish_param_t *res = NULL;
  101. unsigned int i;
  102. for (i = 0; i < this->paramc; i++) {
  103. if (!strcmp(clish_param__get_name(this->paramv[i]), name))
  104. return this->paramv[i];
  105. if ((res = clish_paramv_find_param(
  106. clish_param__get_paramv(this->paramv[i]), name)))
  107. return res;
  108. }
  109. return res;
  110. }
  111. /*--------------------------------------------------------- */
  112. const char *clish_paramv_find_default(const clish_paramv_t * this,
  113. const char *name)
  114. {
  115. clish_param_t *res = clish_paramv_find_param(this, name);
  116. if (res)
  117. return clish_param__get_defval(res);
  118. return NULL;
  119. }
  120. /*--------------------------------------------------------- */
  121. unsigned int clish_paramv__get_count(const clish_paramv_t * this)
  122. {
  123. return this->paramc;
  124. }