vec.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /** @file vec.c
  2. *
  3. */
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include "private.h"
  10. #include "faux/str.h"
  11. #if 0
  12. /*--------------------------------------------------------- */
  13. static void lub_argv_init(lub_argv_t * this, const char *line, size_t off)
  14. {
  15. size_t len = 0;
  16. const char *word = NULL;
  17. lub_arg_t *arg = NULL;
  18. bool_t quoted = BOOL_FALSE;
  19. bool_t alt_quoted = BOOL_FALSE;
  20. const char *str = line + off; // Start on specified offset
  21. const char *offset = NULL;
  22. this->argv = NULL;
  23. this->argc = 0;
  24. if (!line)
  25. return;
  26. /* first of all count the words in the line */
  27. this->argc = lub_string_wordcount(line);
  28. if (0 == this->argc)
  29. return;
  30. /* allocate space to hold the vector */
  31. arg = this->argv = malloc(sizeof(lub_arg_t) * this->argc);
  32. assert(arg);
  33. /* then fill out the array with the words */
  34. for (word = lub_string_nextword(str, &len, &offset, &quoted, NULL, &alt_quoted);
  35. word && (*word != '\0');
  36. word = lub_string_nextword(str, &len, &offset, &quoted, NULL, &alt_quoted)) {
  37. if (alt_quoted)
  38. (*arg).arg = lub_string_dupn(word, len);
  39. else
  40. (*arg).arg = lub_string_ndecode(word, len);
  41. (*arg).offset = offset - line;
  42. (*arg).quoted = quoted;
  43. str = offset;
  44. arg++;
  45. }
  46. }
  47. /*--------------------------------------------------------- */
  48. lub_argv_t *lub_argv_new(const char *line, size_t offset)
  49. {
  50. lub_argv_t *this;
  51. this = malloc(sizeof(lub_argv_t));
  52. if (this)
  53. lub_argv_init(this, line, offset);
  54. return this;
  55. }
  56. /*--------------------------------------------------------- */
  57. void lub_argv_add(lub_argv_t * this, const char *text)
  58. {
  59. lub_arg_t * arg;
  60. if (!text)
  61. return;
  62. /* allocate space to hold the vector */
  63. arg = realloc(this->argv, sizeof(lub_arg_t) * (this->argc + 1));
  64. assert(arg);
  65. this->argv = arg;
  66. (this->argv[this->argc++]).arg = strdup(text);
  67. }
  68. /*--------------------------------------------------------- */
  69. static void lub_argv_fini(lub_argv_t * this)
  70. {
  71. unsigned i;
  72. for (i = 0; i < this->argc; i++)
  73. free(this->argv[i].arg);
  74. free(this->argv);
  75. this->argv = NULL;
  76. }
  77. /*--------------------------------------------------------- */
  78. void lub_argv_delete(lub_argv_t * this)
  79. {
  80. lub_argv_fini(this);
  81. free(this);
  82. }
  83. /*--------------------------------------------------------- */
  84. char *lub_argv__get_line(const lub_argv_t * this)
  85. {
  86. int space = 0;
  87. const char *p;
  88. unsigned i;
  89. char *line = NULL;
  90. for (i = 0; i < this->argc; i++) {
  91. if (i != 0)
  92. lub_string_cat(&line, " ");
  93. space = 0;
  94. /* Search for spaces */
  95. for (p = this->argv[i].arg; *p; p++) {
  96. if (isspace(*p)) {
  97. space = 1;
  98. break;
  99. }
  100. }
  101. if (space)
  102. lub_string_cat(&line, "\"");
  103. lub_string_cat(&line, this->argv[i].arg);
  104. if (space)
  105. lub_string_cat(&line, "\"");
  106. }
  107. return line;
  108. }
  109. /*--------------------------------------------------------- */
  110. char **lub_argv__get_argv(const lub_argv_t * this, const char *argv0)
  111. {
  112. char **result = NULL;
  113. unsigned i;
  114. unsigned a = 0;
  115. if (argv0)
  116. a = 1;
  117. result = malloc(sizeof(char *) * (this->argc + 1 + a));
  118. if (argv0)
  119. result[0] = strdup(argv0);
  120. for (i = 0; i < this->argc; i++)
  121. result[i + a] = strdup(this->argv[i].arg);
  122. result[i + a] = NULL;
  123. return result;
  124. }
  125. /*--------------------------------------------------------- */
  126. void lub_argv__free_argv(char **argv)
  127. {
  128. unsigned i;
  129. if (!argv)
  130. return;
  131. for (i = 0; argv[i]; i++)
  132. free(argv[i]);
  133. free(argv);
  134. }
  135. /*--------------------------------------------------------- */
  136. const char *lub_argv__get_arg(const lub_argv_t *this, unsigned int index)
  137. {
  138. const char *result = NULL;
  139. if (!this)
  140. return NULL;
  141. if (this->argc > index)
  142. result = this->argv[index].arg;
  143. return result;
  144. }
  145. /*--------------------------------------------------------- */
  146. unsigned lub_argv__get_count(const lub_argv_t * this)
  147. {
  148. return this->argc;
  149. }
  150. /*--------------------------------------------------------- */
  151. size_t lub_argv__get_offset(const lub_argv_t * this, unsigned index)
  152. {
  153. size_t result = 0;
  154. if (this->argc > index)
  155. result = this->argv[index].offset;
  156. return result;
  157. }
  158. /*--------------------------------------------------------- */
  159. bool_t lub_argv__get_quoted(const lub_argv_t * this, unsigned index)
  160. {
  161. bool_t result = BOOL_FALSE;
  162. if (this->argc > index)
  163. result = this->argv[index].quoted;
  164. return result;
  165. }
  166. /*--------------------------------------------------------- */
  167. #endif