1
0

pargv.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. clish_pargv_t *clish_pargv_clone(const clish_pargv_t *src)
  70. {
  71. clish_pargv_t *dst;
  72. unsigned int i;
  73. if (!src)
  74. return NULL;
  75. dst = clish_pargv_new();
  76. for (i = 0; i < src->pargc; i++) {
  77. clish_pargv_insert(dst, src->pargv[i]->param, src->pargv[i]->value);
  78. }
  79. return dst;
  80. }
  81. /*--------------------------------------------------------- */
  82. static void clish_pargv_fini(clish_pargv_t * this)
  83. {
  84. unsigned int i;
  85. /* cleanup time */
  86. for (i = 0; i < this->pargc; i++) {
  87. lub_string_free(this->pargv[i]->value);
  88. this->pargv[i]->value = NULL;
  89. free(this->pargv[i]);
  90. }
  91. free(this->pargv);
  92. }
  93. /*--------------------------------------------------------- */
  94. void clish_pargv_delete(clish_pargv_t * this)
  95. {
  96. if (!this)
  97. return;
  98. clish_pargv_fini(this);
  99. free(this);
  100. }
  101. /*--------------------------------------------------------- */
  102. unsigned clish_pargv__get_count(clish_pargv_t * this)
  103. {
  104. if (!this)
  105. return 0;
  106. return this->pargc;
  107. }
  108. /*--------------------------------------------------------- */
  109. clish_parg_t *clish_pargv__get_parg(clish_pargv_t * this, unsigned int index)
  110. {
  111. if (!this)
  112. return NULL;
  113. if (index >= this->pargc)
  114. return NULL;
  115. return this->pargv[index];
  116. }
  117. /*--------------------------------------------------------- */
  118. const clish_param_t *clish_pargv__get_param(clish_pargv_t * this,
  119. unsigned index)
  120. {
  121. clish_parg_t *tmp;
  122. if (!this)
  123. return NULL;
  124. if (index >= this->pargc)
  125. return NULL;
  126. tmp = this->pargv[index];
  127. return tmp->param;
  128. }
  129. /*--------------------------------------------------------- */
  130. const clish_parg_t *clish_pargv_find_arg(clish_pargv_t * this, const char *name)
  131. {
  132. if (!this)
  133. return NULL;
  134. return find_parg(this, name);
  135. }
  136. /*--------------------------------------------------------- */