1
0

argv_new.c 1.6 KB

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