diff options
author | Warren Dukes <warren.dukes@gmail.com> | 2006-10-06 10:33:27 +0000 |
---|---|---|
committer | Warren Dukes <warren.dukes@gmail.com> | 2006-10-06 10:33:27 +0000 |
commit | e19882660677baf8ba01c3006cf1a8ae5bee83ef (patch) | |
tree | 998b69d48f4db57d4e3d0540ae808f1722612528 /src/buffer2array.c | |
parent | e3222d807a6178e0a4a5254b8443c3432b663efb (diff) | |
download | mpd-e19882660677baf8ba01c3006cf1a8ae5bee83ef.tar.gz mpd-e19882660677baf8ba01c3006cf1a8ae5bee83ef.tar.xz mpd-e19882660677baf8ba01c3006cf1a8ae5bee83ef.zip |
put back np's supperrior buffer2array, and fix my bug fix from change 4872
git-svn-id: https://svn.musicpd.org/mpd/trunk@4875 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to 'src/buffer2array.c')
-rw-r--r-- | src/buffer2array.c | 142 |
1 files changed, 42 insertions, 100 deletions
diff --git a/src/buffer2array.c b/src/buffer2array.c index ef08bc32a..fdcef790f 100644 --- a/src/buffer2array.c +++ b/src/buffer2array.c @@ -17,111 +17,53 @@ */ #include "buffer2array.h" -#ifdef UNIT_TEST -# define xstrdup(x) strdup(x) -# define xmalloc(x) malloc(x) -#else -# include "utils.h" -#endif #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <ctype.h> -int buffer2array(char *origBuffer, char ***array) -{ - int quotes = 0; - int count = 0; - int i; - int curr; - int *beginArray; - char *buffer = xstrdup(origBuffer); - int bufferLength = strlen(buffer); - char *markArray = xmalloc(sizeof(char) * (bufferLength + 1)); - - for (curr = 0; curr < bufferLength; curr++) { - if (!quotes && (buffer[curr] == ' ' || buffer[curr] == '\t')) { - markArray[curr] = '0'; - } else if (buffer[curr] == '\"') { - if (curr > 0 && buffer[curr - 1] != '\\') { - quotes = quotes ? 0 : 1; - markArray[curr] = '0'; - } else { - markArray[curr] = '1'; - } - } else { - markArray[curr] = '1'; - } - if (markArray[curr] == '1') { - if (curr > 0) { - if (markArray[curr - 1] == '0') { - count++; - } - } else { - count++; - } - } - } - markArray[bufferLength] = '\0'; - - if (!count) { - free(buffer); - free(markArray); - return count; - } - beginArray = xmalloc(sizeof(int) * count); - (*array) = xmalloc(sizeof(char *) * count); - - count = 0; +inline static +int +isWhiteSpace(char c) +{ + return (c == ' ' || c == '\t'); +} - for (curr = 0; curr < bufferLength; curr++) { - if (markArray[curr] == '1') { - if (curr > 0) { - if (markArray[curr - 1] == '0') { - beginArray[count++] = curr; +int buffer2array(char *buffer, char *array[], const int max) +{ + int i = 0; + char *c = buffer; + + while (*c != '\0' && i < max) { + if (*c == '\"') { + array[i++] = ++c; + while (*c != '\0') { + if (*c == '\"') { + *(c++) = '\0'; + break; + } + else if (*(c++) == '\\' && *c != '\0') { + memmove(c - 1, c, strlen(c) + 1); } - } else { - beginArray[count++] = curr; } } else { - buffer[curr] = '\0'; + while (isWhiteSpace(*c)) + ++c; + array[i++] = c++; + if (*c == '\0') + return i; + while (!isWhiteSpace(*c) && *c != '\0') + ++c; } + if (*c == '\0') + return i; + *(c++) = '\0'; + while (isWhiteSpace(*c)) + ++c; } - - for (i = 0; i < count; i++) { - int len = strlen(buffer + beginArray[i]) + 1; - int arrayCurr = 0; - (*array)[i] = xmalloc(sizeof(char) * len); - for (curr = beginArray[i]; buffer[curr] != '\0'; curr++) { - if (buffer[curr] == '\\') { - if (buffer[curr + 1] != '\0') { - curr++; - } - } - (*array)[i][arrayCurr++] = buffer[curr]; - } - (*array)[i][arrayCurr] = '\0'; - } - - free(markArray); - free(beginArray); - free(buffer); - - return count; -} - -void freeArgArray(char **array, int argArrayLength) -{ - int i; - - if (argArrayLength == 0) - return; - - for (i = 0; i < argArrayLength; i++) { - free(array[i]); - } - free(array); + return i; } #ifdef UNIT_TEST @@ -132,42 +74,42 @@ void freeArgArray(char **array, int argArrayLength) int main() { - char **a; + char *a[4] = { NULL }; char *b; int i, max; b = xstrdup("lsinfo \"/some/dir/name \\\"test\\\"\""); - max = buffer2array(b, &a); + max = buffer2array(b, a, 4); assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("/some/dir/name \"test\"", a[1]) ); assert( !a[2] ); b = xstrdup("lsinfo \"/some/dir/name \\\"test\\\" something else\""); - max = buffer2array(b, &a); + max = buffer2array(b, a, 4); assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("/some/dir/name \"test\" something else", a[1]) ); assert( !a[2] ); b = xstrdup("lsinfo \"/some/dir\\\\name\""); - max = buffer2array(b, &a); + max = buffer2array(b, a, 4); assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("/some/dir\\name", a[1]) ); assert( !a[2] ); b = xstrdup("lsinfo \"/some/dir name\""); - max = buffer2array(b, &a); + max = buffer2array(b, a, 4); assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("/some/dir name", a[1]) ); assert( !a[2] ); b = xstrdup("lsinfo \"\\\"/some/dir\\\"\""); - max = buffer2array(b, &a); + max = buffer2array(b, a, 4); assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("\"/some/dir\"", a[1]) ); assert( !a[2] ); b = xstrdup("lsinfo \"\\\"/some/dir\\\" x\""); - max = buffer2array(b, &a); + max = buffer2array(b, a, 4); assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("\"/some/dir\" x", a[1]) ); assert( !a[2] ); |