aboutsummaryrefslogtreecommitdiff
path: root/src/buffer2array.c
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2006-07-29 18:55:00 +0000
committerEric Wong <normalperson@yhbt.net>2006-07-29 18:55:00 +0000
commitf08342c11fb7f31b168902b4d89d8e543e03125a (patch)
treee56ebfd5f0b4376d10e4f02dba17f3dfef82b999 /src/buffer2array.c
parentf05166a6a0f39e6c6f8ce9675d7f6f6f58300577 (diff)
replace buffer2array() with cstrtok() from mpd-ke
This modifies the string in place, and does not allocate any memory from the heap. This is considerably smaller than the function it replaces, and will be instrumental in getting the commands/conf malloc reductions done. git-svn-id: https://svn.musicpd.org/mpd/trunk@4481 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to 'src/buffer2array.c')
-rw-r--r--src/buffer2array.c120
1 files changed, 32 insertions, 88 deletions
diff --git a/src/buffer2array.c b/src/buffer2array.c
index 4a0751ea..322036df 100644
--- a/src/buffer2array.c
+++ b/src/buffer2array.c
@@ -21,99 +21,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <ctype.h>
-int buffer2array(char *origBuffer, char ***array)
+int cstrtok(char *buffer, char *array[], const int max)
{
- int quotes = 0;
- int count = 0;
- int i;
- int curr;
- int *beginArray;
- char *buffer = strdup(origBuffer);
- int bufferLength = strlen(buffer);
- char *markArray = malloc(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 = malloc(sizeof(int) * count);
- (*array) = malloc(sizeof(char *) * count);
-
- count = 0;
-
- for (curr = 0; curr < bufferLength; curr++) {
- if (markArray[curr] == '1') {
- if (curr > 0) {
- if (markArray[curr - 1] == '0') {
- beginArray[count++] = curr;
+ int i = 0;
+ char *c = buffer;
+
+ while (*c != '\0' && i < max) {
+ if (*c == '\"') {
+ int escape = 0;
+ array[i++] = ++c;
+ while (*c != '\0') {
+ if (*c == '\"') {
+ if (escape)
+ memmove(c - 1, c,
+ strlen(c) + 1);
+ else {
+ *(c++) = '\0';
+ break;
+ }
}
- } else {
- beginArray[count++] = curr;
+ escape = (*(c++) != '\\') ? 0 : !escape;
}
} else {
- buffer[curr] = '\0';
+ while (isspace(*c))
+ ++c;
+ array[i++] = c++;
+ if (*c == '\0')
+ return i;
+ while (!isspace(*c) && *c != '\0')
+ ++c;
}
+ if (*c == '\0')
+ return i;
+ *(c++) = '\0';
+ while (isspace(*c))
+ ++c;
}
-
- for (i = 0; i < count; i++) {
- int len = strlen(buffer + beginArray[i]) + 1;
- int arrayCurr = 0;
- (*array)[i] = malloc(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;
}