buffer2array.c 3.32 KB
Newer Older
Warren Dukes's avatar
Warren Dukes committed
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
Warren Dukes's avatar
Warren Dukes committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * This project's homepage is: http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "buffer2array.h"
20
#include "os_compat.h"
21

22
static inline
23 24 25 26 27
int
isWhiteSpace(char c)
{
	return (c == ' ' || c == '\t');
}
28

29 30 31 32 33 34 35 36 37 38 39 40
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;
41
				}
42 43
				else if (*(c++) == '\\' && *c != '\0') {
					memmove(c - 1, c, strlen(c) + 1);
44
				}
Warren Dukes's avatar
Warren Dukes committed
45
			}
Avuton Olrich's avatar
Avuton Olrich committed
46
		} else {
47 48 49 50 51 52 53
			while (isWhiteSpace(*c))
				++c;
			array[i++] = c++;
			if (*c == '\0')
				return i;
			while (!isWhiteSpace(*c) && *c != '\0')
				++c;
Warren Dukes's avatar
Warren Dukes committed
54
		}
55 56 57 58 59
		if (*c == '\0')
			return i;
		*(c++) = '\0';
		while (isWhiteSpace(*c))
			++c;
Warren Dukes's avatar
Warren Dukes committed
60
	}
61
	return i;
Warren Dukes's avatar
Warren Dukes committed
62
}
63 64 65 66 67 68 69 70 71

#ifdef UNIT_TEST

#include <stdio.h>
#include <string.h>
#include <assert.h>

int main()
{
72
	char *a[4] = { NULL };
73
	char *b;
74
	int max;
75

76
	b = strdup("lsinfo \"/some/dir/name \\\"test\\\"\"");
Eric Wong's avatar
Eric Wong committed
77
	assert(b);
78
	max = buffer2array(b, a, 4);
79 80
	assert( !strcmp("lsinfo", a[0]) );
	assert( !strcmp("/some/dir/name \"test\"", a[1]) );
81
	assert( !a[2] );
82

83
	b = strdup("lsinfo \"/some/dir/name \\\"test\\\" something else\"");
Eric Wong's avatar
Eric Wong committed
84
	assert(b);
85
	max = buffer2array(b, a, 4);
86 87
	assert( !strcmp("lsinfo", a[0]) );
	assert( !strcmp("/some/dir/name \"test\" something else", a[1]) );
88
	assert( !a[2] );
89

90
	b = strdup("lsinfo \"/some/dir\\\\name\"");
Eric Wong's avatar
Eric Wong committed
91
	assert(b);
92
	max = buffer2array(b, a, 4);
93 94
	assert( !strcmp("lsinfo", a[0]) );
	assert( !strcmp("/some/dir\\name", a[1]) );
95 96
	assert( !a[2] );

97
	b = strdup("lsinfo \"/some/dir name\"");
Eric Wong's avatar
Eric Wong committed
98
	assert(b);
99
	max = buffer2array(b, a, 4);
100 101 102 103
	assert( !strcmp("lsinfo", a[0]) );
	assert( !strcmp("/some/dir name", a[1]) );
	assert( !a[2] );

104
	b = strdup("lsinfo \"\\\"/some/dir\\\"\"");
Eric Wong's avatar
Eric Wong committed
105
	assert(b);
106
	max = buffer2array(b, a, 4);
107 108 109 110
	assert( !strcmp("lsinfo", a[0]) );
	assert( !strcmp("\"/some/dir\"", a[1]) );
	assert( !a[2] );

111
	b = strdup("lsinfo \"\\\"/some/dir\\\" x\"");
Eric Wong's avatar
Eric Wong committed
112
	assert(b);
113
	max = buffer2array(b, a, 4);
114 115 116
	assert( !strcmp("lsinfo", a[0]) );
	assert( !strcmp("\"/some/dir\" x", a[1]) );
	assert( !a[2] );
117

118
	b = strdup("lsinfo \"single quote\\'d from php magicquotes\"");
Eric Wong's avatar
Eric Wong committed
119
	assert(b);
120 121 122 123 124 125
	max = buffer2array(b, a, 4);
	assert( !strcmp("lsinfo", a[0]) );
	assert( !strcmp("single quote\'d from php magicquotes", a[1]) );
	assert( !a[2] );

	b = strdup("lsinfo \"double quote\\\"d from php magicquotes\"");
Eric Wong's avatar
Eric Wong committed
126
	assert(b);
127 128 129 130 131
	max = buffer2array(b, a, 4);
	assert( !strcmp("lsinfo", a[0]) );
	assert( !strcmp("double quote\"d from php magicquotes", a[1]) );
	assert( !a[2] );

132 133 134 135
	return 0;
}

#endif