[brlcad-commits] SF.net SVN: brlcad:[36403] brlcad/trunk
Open Source Solid Modeling CAD
Brought to you by:
brlcad
From: <br...@us...> - 2009-11-04 05:22:59
|
Revision: 36403 http://brlcad.svn.sourceforge.net/brlcad/?rev=36403&view=rev Author: brlcad Date: 2009-11-04 05:22:50 +0000 (Wed, 04 Nov 2009) Log Message: ----------- change the signature of bu_argv_from_string() to take a const string instead of modifying the input string. this requires the caller to deallocate the array elements (with bu_free_argv() or bu_free_array()), but is safer than relying on them to not modify/delete what the gave as input that the argv was referring to. still doesn't decode yet. Modified Paths: -------------- brlcad/trunk/include/bu.h brlcad/trunk/src/libbu/vls.c Modified: brlcad/trunk/include/bu.h =================================================================== --- brlcad/trunk/include/bu.h 2009-11-04 04:40:25 UTC (rev 36402) +++ brlcad/trunk/include/bu.h 2009-11-04 05:22:50 UTC (rev 36403) @@ -4722,11 +4722,11 @@ * 'lim' indicates the maximum number of elements that can be stored * in the argv[] array not including a terminating NULL. * - * The input buffer is altered by this process. The argv[] array - * points into the input buffer. The argv[] array needs to have at + * The input buffer is NOT altered by this process. The argv[] array + * points newly allocated strings. The argv[] array needs to have at * least lim+1 pointers allocated for lim items plus a terminating - * pointer to NULL. The input buffer should not be freed until argv - * has been freed or passes out of scope. + * pointer to NULL. The argv array should be released with + * bu_free_array(). * * Returns - * 0 no words in input @@ -4735,7 +4735,7 @@ BU_EXPORT BU_EXTERN(int bu_argv_from_string, (char *argv[], int lim, - char *lp)); + const char *lp)); /** * b u _ v l s _ f w r i t e Modified: brlcad/trunk/src/libbu/vls.c =================================================================== --- brlcad/trunk/src/libbu/vls.c 2009-11-04 04:40:25 UTC (rev 36402) +++ brlcad/trunk/src/libbu/vls.c 2009-11-04 05:22:50 UTC (rev 36403) @@ -607,11 +607,13 @@ int -bu_argv_from_string(char *argv[], int lim, char *lp) +bu_argv_from_string(char *argv[], int lim, const char *lp) { int argc = 0; /* number of words seen */ int skip = 0; + struct bu_vls item; + if (!argv) { /* do this instead of crashing */ bu_bomb("bu_argv_from_string received a NULL argv\n"); @@ -634,25 +636,23 @@ return 0; } - /* some non-space string has been encountered, set argv[0] */ - argc = 0; - argv[argc] = lp; + bu_vls_init(&item); for (; *lp != '\0'; lp++) { - /* skip over current word */ - if (!isspace(*lp)) + skip = 0; + + /* stash current word */ + if (!isspace(*lp)) { + bu_vls_putc(&item, *lp); continue; + } - skip = 0; - - /* terminate current word, skip space until we find the start - * of the next word nulling out the spaces as we go along. + /* done with current word, skip whitespace until we find start + * of the next word or end of string. */ - while (*(lp+skip) != '\0' && isspace(*(lp+skip))) { - lp[skip] = '\0'; + while (*(lp+skip) != '\0' && isspace(*(lp+skip))) skip++; - } if (*(lp + skip) == '\0') break; @@ -662,16 +662,24 @@ break; /* start of next word */ - argc++; - argv[argc] = lp + skip; + argv[argc++] = bu_vls_strdup(&item); + bu_vls_trunc(&item, 0); /* jump over the spaces, remember the loop's lp++ */ lp += skip - 1; } + /* stash the last word encountered */ + if ((*(lp + skip) == '\0') && (argc < lim)) { + argv[argc++] = bu_vls_strdup(&item); + } + /* always NULL-terminate the array */ argc++; argv[argc] = (char *)NULL; + + bu_vls_free(&item); + return argc; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |