pargv.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * pargv.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include "lub/argv.h"
  7. #include "lub/system.h"
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <assert.h>
  12. /*--------------------------------------------------------- */
  13. /*
  14. * Search for the specified parameter and return its value
  15. */
  16. static clish_parg_t *find_parg(clish_pargv_t * this, const char *name)
  17. {
  18. unsigned i;
  19. clish_parg_t *result = NULL;
  20. if (!this || !name)
  21. return NULL;
  22. /* scan the parameters in this instance */
  23. for (i = 0; i < this->pargc; i++) {
  24. clish_parg_t *parg = this->pargv[i];
  25. const char *pname = clish_param__get_name(parg->param);
  26. if (0 == strcmp(pname, name)) {
  27. result = parg;
  28. break;
  29. }
  30. }
  31. return result;
  32. }
  33. /*--------------------------------------------------------- */
  34. int clish_pargv_insert(clish_pargv_t * this,
  35. const clish_param_t * param, const char *value)
  36. {
  37. if (!this || !param)
  38. return -1;
  39. clish_parg_t *parg = find_parg(this, clish_param__get_name(param));
  40. if (parg) {
  41. /* release the current value */
  42. lub_string_free(parg->value);
  43. } else {
  44. size_t new_size = ((this->pargc + 1) * sizeof(clish_parg_t *));
  45. clish_parg_t **tmp;
  46. /* resize the parameter vector */
  47. tmp = realloc(this->pargv, new_size);
  48. this->pargv = tmp;
  49. /* insert reference to the parameter */
  50. parg = malloc(sizeof(*parg));
  51. this->pargv[this->pargc++] = parg;
  52. parg->param = param;
  53. }
  54. parg->value = NULL;
  55. if (value)
  56. parg->value = lub_string_dup(value);
  57. return 0;
  58. }
  59. /*--------------------------------------------------------- */
  60. clish_pargv_t *clish_pargv_new(void)
  61. {
  62. clish_pargv_t *this;
  63. this = malloc(sizeof(clish_pargv_t));
  64. this->pargc = 0;
  65. this->pargv = NULL;
  66. return this;
  67. }
  68. /*--------------------------------------------------------- */
  69. static void clish_pargv_fini(clish_pargv_t * this)
  70. {
  71. unsigned i;
  72. /* cleanup time */
  73. for (i = 0; i < this->pargc; i++) {
  74. lub_string_free(this->pargv[i]->value);
  75. this->pargv[i]->value = NULL;
  76. free(this->pargv[i]);
  77. }
  78. free(this->pargv);
  79. }
  80. /*--------------------------------------------------------- */
  81. void clish_pargv_delete(clish_pargv_t * this)
  82. {
  83. if (!this)
  84. return;
  85. clish_pargv_fini(this);
  86. free(this);
  87. }
  88. /*--------------------------------------------------------- */
  89. unsigned clish_pargv__get_count(clish_pargv_t * this)
  90. {
  91. if (!this)
  92. return 0;
  93. return this->pargc;
  94. }
  95. /*--------------------------------------------------------- */
  96. clish_parg_t *clish_pargv__get_parg(clish_pargv_t * this, unsigned int index)
  97. {
  98. if (!this)
  99. return NULL;
  100. if (index >= this->pargc)
  101. return NULL;
  102. return this->pargv[index];
  103. }
  104. /*--------------------------------------------------------- */
  105. const clish_param_t *clish_pargv__get_param(clish_pargv_t * this,
  106. unsigned index)
  107. {
  108. clish_parg_t *tmp;
  109. if (!this)
  110. return NULL;
  111. if (index >= this->pargc)
  112. return NULL;
  113. tmp = this->pargv[index];
  114. return tmp->param;
  115. }
  116. /*--------------------------------------------------------- */
  117. const char *clish_parg__get_value(const clish_parg_t * this)
  118. {
  119. if (!this)
  120. return NULL;
  121. return this->value;
  122. }
  123. /*--------------------------------------------------------- */
  124. const char *clish_parg__get_name(const clish_parg_t * this)
  125. {
  126. if (!this)
  127. return NULL;
  128. return clish_param__get_name(this->param);
  129. }
  130. /*--------------------------------------------------------- */
  131. const clish_ptype_t *clish_parg__get_ptype(const clish_parg_t * this)
  132. {
  133. if (!this)
  134. return NULL;
  135. return clish_param__get_ptype(this->param);
  136. }
  137. /*--------------------------------------------------------- */
  138. const clish_parg_t *clish_pargv_find_arg(clish_pargv_t * this, const char *name)
  139. {
  140. if (!this)
  141. return NULL;
  142. return find_parg(this, name);
  143. }
  144. /*--------------------------------------------------------- */