1
0

argv_new.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. /* first of all count the words in the line */
  19. this->argc = lub_argv_wordcount(line);
  20. /* allocate space to hold the vector */
  21. arg = this->argv = malloc(sizeof(lub_arg_t) * this->argc);
  22. if(arg)
  23. {
  24. /* then fill out the array with the words */
  25. for(word = lub_argv_nextword(line,&len,&offset,&quoted);
  26. *word;
  27. word = lub_argv_nextword(word+len,&len,&offset,&quoted))
  28. {
  29. char * tmp = lub_string_dupn(word,len);
  30. (*arg).arg = lub_string_decode(tmp);
  31. lub_string_free(tmp);
  32. (*arg).offset = offset;
  33. (*arg).quoted = quoted;
  34. offset += len;
  35. if(BOOL_TRUE == quoted)
  36. {
  37. len += 1; /* account for terminating quotation mark */
  38. offset += 2; /* account for quotation marks */
  39. }
  40. arg++;
  41. }
  42. }
  43. else
  44. {
  45. /* failed to get memory so don't pretend otherwise */
  46. this->argc = 0;
  47. }
  48. }
  49. /*--------------------------------------------------------- */
  50. lub_argv_t *
  51. lub_argv_new(const char *line,
  52. size_t offset)
  53. {
  54. lub_argv_t *this;
  55. this = malloc(sizeof(lub_argv_t));
  56. if(NULL != this)
  57. {
  58. lub_argv_init(this,line,offset);
  59. }
  60. return this;
  61. }
  62. /*--------------------------------------------------------- */