buffer2array.c 3.24 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

21 22 23
#include <glib.h>

#include <string.h>
24

25 26 27 28 29 30 31 32 33 34 35 36
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;
37
				}
38 39
				else if (*(c++) == '\\' && *c != '\0') {
					memmove(c - 1, c, strlen(c) + 1);
40
				}
Warren Dukes's avatar
Warren Dukes committed
41
			}
Avuton Olrich's avatar
Avuton Olrich committed
42
		} else {
43
			c = g_strchug(c);
44 45
			if (*c == '\0')
				return i;
46 47 48 49

			array[i++] = c++;

			while (!g_ascii_isspace(*c) && *c != '\0')
50
				++c;
Warren Dukes's avatar
Warren Dukes committed
51
		}
52 53 54
		if (*c == '\0')
			return i;
		*(c++) = '\0';
55 56

		c = g_strchug(c);
Warren Dukes's avatar
Warren Dukes committed
57
	}
58
	return i;
Warren Dukes's avatar
Warren Dukes committed
59
}
60 61 62 63 64 65 66 67 68

#ifdef UNIT_TEST

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

int main()
{
69
	char *a[4] = { NULL };
70
	char *b;
71
	int max;
72

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

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

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

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

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

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

115
	b = strdup("lsinfo \"single quote\\'d from php magicquotes\"");
Eric Wong's avatar
Eric Wong committed
116
	assert(b);
117 118 119 120 121 122
	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
123
	assert(b);
124 125 126 127 128
	max = buffer2array(b, a, 4);
	assert( !strcmp("lsinfo", a[0]) );
	assert( !strcmp("double quote\"d from php magicquotes", a[1]) );
	assert( !a[2] );

129 130 131 132
	return 0;
}

#endif