/* * argv_new.c */ #include "private.h" #include "lub/string.h" #include #include /*--------------------------------------------------------- */ static void lub_argv_init(lub_argv_t * this, const char *line, size_t offset) { size_t len; const char *word; lub_arg_t *arg; bool_t quoted; /* Save the whole line */ this->line = lub_string_dup(line); /* first of all count the words in the line */ this->argc = lub_argv_wordcount(line); /* allocate space to hold the vector */ arg = this->argv = malloc(sizeof(lub_arg_t) * this->argc); if (arg) { /* then fill out the array with the words */ for (word = lub_argv_nextword(line, &len, &offset, "ed); *word; word = lub_argv_nextword(word + len, &len, &offset, "ed)) { char *tmp = lub_string_dupn(word, len); (*arg).arg = lub_string_decode(tmp); lub_string_free(tmp); (*arg).offset = offset; (*arg).quoted = quoted; offset += len; if (BOOL_TRUE == quoted) { len += 1; /* account for terminating quotation mark */ offset += 2; /* account for quotation marks */ } arg++; } } else { /* failed to get memory so don't pretend otherwise */ this->argc = 0; } } /*--------------------------------------------------------- */ lub_argv_t *lub_argv_new(const char *line, size_t offset) { lub_argv_t *this; this = malloc(sizeof(lub_argv_t)); if (NULL != this) { lub_argv_init(this, line, offset); } return this; } /*--------------------------------------------------------- */