1
0

argv_new.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * argv_new.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include <stdlib.h>
  7. #include <string.h>
  8. /*--------------------------------------------------------- */
  9. static void
  10. lub_argv_init(lub_argv_t *this,
  11. const char *line,
  12. size_t offset)
  13. {
  14. size_t len;
  15. const char *word;
  16. lub_arg_t *arg;
  17. bool_t quoted;
  18. /* Save the whole line */
  19. this->line = lub_string_dup(line);
  20. /* first of all count the words in the line */
  21. this->argc = lub_argv_wordcount(line);
  22. /* allocate space to hold the vector */
  23. arg = this->argv = malloc(sizeof(lub_arg_t) * this->argc);
  24. if(arg)
  25. {
  26. /* then fill out the array with the words */
  27. for(word = lub_argv_nextword(line,&len,&offset,&quoted);
  28. *word;
  29. word = lub_argv_nextword(word+len,&len,&offset,&quoted))
  30. {
  31. char * tmp = lub_string_dupn(word,len);
  32. (*arg).arg = lub_string_decode(tmp);
  33. lub_string_free(tmp);
  34. (*arg).offset = offset;
  35. (*arg).quoted = quoted;
  36. offset += len;
  37. if(BOOL_TRUE == quoted)
  38. {
  39. len += 1; /* account for terminating quotation mark */
  40. offset += 2; /* account for quotation marks */
  41. }
  42. arg++;
  43. }
  44. }
  45. else
  46. {
  47. /* failed to get memory so don't pretend otherwise */
  48. this->argc = 0;
  49. }
  50. }
  51. /*--------------------------------------------------------- */
  52. lub_argv_t *
  53. lub_argv_new(const char *line,
  54. size_t offset)
  55. {
  56. lub_argv_t *this;
  57. this = malloc(sizeof(lub_argv_t));
  58. if(NULL != this)
  59. {
  60. lub_argv_init(this,line,offset);
  61. }
  62. return this;
  63. }
  64. /*--------------------------------------------------------- */