2
* Copyright (C) 2003-2009 The Music Player Daemon Project
3
* http://www.musicpd.org
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
#include "buffer2array.h"
26
int buffer2array(char *buffer, char *array[], const int max)
31
while (*c != '\0' && i < max) {
39
else if (*(c++) == '\\' && *c != '\0') {
40
memmove(c - 1, c, strlen(c) + 1);
50
while (!g_ascii_isspace(*c) && *c != '\0')
70
char *a[4] = { NULL };
74
b = strdup("lsinfo \"/some/dir/name \\\"test\\\"\"");
76
max = buffer2array(b, a, 4);
77
assert( !strcmp("lsinfo", a[0]) );
78
assert( !strcmp("/some/dir/name \"test\"", a[1]) );
81
b = strdup("lsinfo \"/some/dir/name \\\"test\\\" something else\"");
83
max = buffer2array(b, a, 4);
84
assert( !strcmp("lsinfo", a[0]) );
85
assert( !strcmp("/some/dir/name \"test\" something else", a[1]) );
88
b = strdup("lsinfo \"/some/dir\\\\name\"");
90
max = buffer2array(b, a, 4);
91
assert( !strcmp("lsinfo", a[0]) );
92
assert( !strcmp("/some/dir\\name", a[1]) );
95
b = strdup("lsinfo \"/some/dir name\"");
97
max = buffer2array(b, a, 4);
98
assert( !strcmp("lsinfo", a[0]) );
99
assert( !strcmp("/some/dir name", a[1]) );
102
b = strdup("lsinfo \"\\\"/some/dir\\\"\"");
104
max = buffer2array(b, a, 4);
105
assert( !strcmp("lsinfo", a[0]) );
106
assert( !strcmp("\"/some/dir\"", a[1]) );
109
b = strdup("lsinfo \"\\\"/some/dir\\\" x\"");
111
max = buffer2array(b, a, 4);
112
assert( !strcmp("lsinfo", a[0]) );
113
assert( !strcmp("\"/some/dir\" x", a[1]) );
116
b = strdup("lsinfo \"single quote\\'d from php magicquotes\"");
118
max = buffer2array(b, a, 4);
119
assert( !strcmp("lsinfo", a[0]) );
120
assert( !strcmp("single quote\'d from php magicquotes", a[1]) );
123
b = strdup("lsinfo \"double quote\\\"d from php magicquotes\"");
125
max = buffer2array(b, a, 4);
126
assert( !strcmp("lsinfo", a[0]) );
127
assert( !strcmp("double quote\"d from php magicquotes", a[1]) );