~ubuntu-branches/ubuntu/saucy/mpd/saucy

« back to all changes in this revision

Viewing changes to src/buffer2array.c

  • Committer: Bazaar Package Importer
  • Author(s): Angel Abad
  • Date: 2011-02-02 12:26:30 UTC
  • mfrom: (1.5.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20110202122630-bdyx8w4k94doz4fs
Tags: 0.16.1-1ubuntu1
* Merge from debian unstable. Remaining changes:
  - debian/control:
    + Don't build-depend on libmikmod2-dev (Debian bug #510675).
    + Move avahi-daemon from Suggests field to Recommends field.
  - debian/mpd.init.d:
    + Read mpd user from mpd.conf.
  - debian/control, debian/rules:
    + Add libmp3lame-dev to the build dependencies and enable lame.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2003-2009 The Music Player Daemon Project
3
 
 * http://www.musicpd.org
4
 
 *
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.
9
 
 *
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.
14
 
 *
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.
18
 
 */
19
 
 
20
 
#include "buffer2array.h"
21
 
 
22
 
#include <glib.h>
23
 
 
24
 
#include <string.h>
25
 
 
26
 
int buffer2array(char *buffer, char *array[], const int max)
27
 
{
28
 
        int i = 0;
29
 
        char *c = buffer;
30
 
 
31
 
        while (*c != '\0' && i < max) {
32
 
                if (*c == '\"') {
33
 
                        array[i++] = ++c;
34
 
                        while (*c != '\0') {
35
 
                                if (*c == '\"') {
36
 
                                        *(c++) = '\0';
37
 
                                        break;
38
 
                                }
39
 
                                else if (*(c++) == '\\' && *c != '\0') {
40
 
                                        memmove(c - 1, c, strlen(c) + 1);
41
 
                                }
42
 
                        }
43
 
                } else {
44
 
                        c = g_strchug(c);
45
 
                        if (*c == '\0')
46
 
                                return i;
47
 
 
48
 
                        array[i++] = c++;
49
 
 
50
 
                        while (!g_ascii_isspace(*c) && *c != '\0')
51
 
                                ++c;
52
 
                }
53
 
                if (*c == '\0')
54
 
                        return i;
55
 
                *(c++) = '\0';
56
 
 
57
 
                c = g_strchug(c);
58
 
        }
59
 
        return i;
60
 
}
61
 
 
62
 
#ifdef UNIT_TEST
63
 
 
64
 
#include <stdio.h>
65
 
#include <string.h>
66
 
#include <assert.h>
67
 
 
68
 
int main()
69
 
{
70
 
        char *a[4] = { NULL };
71
 
        char *b;
72
 
        int max;
73
 
 
74
 
        b = strdup("lsinfo \"/some/dir/name \\\"test\\\"\"");
75
 
        assert(b);
76
 
        max = buffer2array(b, a, 4);
77
 
        assert( !strcmp("lsinfo", a[0]) );
78
 
        assert( !strcmp("/some/dir/name \"test\"", a[1]) );
79
 
        assert( !a[2] );
80
 
 
81
 
        b = strdup("lsinfo \"/some/dir/name \\\"test\\\" something else\"");
82
 
        assert(b);
83
 
        max = buffer2array(b, a, 4);
84
 
        assert( !strcmp("lsinfo", a[0]) );
85
 
        assert( !strcmp("/some/dir/name \"test\" something else", a[1]) );
86
 
        assert( !a[2] );
87
 
 
88
 
        b = strdup("lsinfo \"/some/dir\\\\name\"");
89
 
        assert(b);
90
 
        max = buffer2array(b, a, 4);
91
 
        assert( !strcmp("lsinfo", a[0]) );
92
 
        assert( !strcmp("/some/dir\\name", a[1]) );
93
 
        assert( !a[2] );
94
 
 
95
 
        b = strdup("lsinfo \"/some/dir name\"");
96
 
        assert(b);
97
 
        max = buffer2array(b, a, 4);
98
 
        assert( !strcmp("lsinfo", a[0]) );
99
 
        assert( !strcmp("/some/dir name", a[1]) );
100
 
        assert( !a[2] );
101
 
 
102
 
        b = strdup("lsinfo \"\\\"/some/dir\\\"\"");
103
 
        assert(b);
104
 
        max = buffer2array(b, a, 4);
105
 
        assert( !strcmp("lsinfo", a[0]) );
106
 
        assert( !strcmp("\"/some/dir\"", a[1]) );
107
 
        assert( !a[2] );
108
 
 
109
 
        b = strdup("lsinfo \"\\\"/some/dir\\\" x\"");
110
 
        assert(b);
111
 
        max = buffer2array(b, a, 4);
112
 
        assert( !strcmp("lsinfo", a[0]) );
113
 
        assert( !strcmp("\"/some/dir\" x", a[1]) );
114
 
        assert( !a[2] );
115
 
 
116
 
        b = strdup("lsinfo \"single quote\\'d from php magicquotes\"");
117
 
        assert(b);
118
 
        max = buffer2array(b, a, 4);
119
 
        assert( !strcmp("lsinfo", a[0]) );
120
 
        assert( !strcmp("single quote\'d from php magicquotes", a[1]) );
121
 
        assert( !a[2] );
122
 
 
123
 
        b = strdup("lsinfo \"double quote\\\"d from php magicquotes\"");
124
 
        assert(b);
125
 
        max = buffer2array(b, a, 4);
126
 
        assert( !strcmp("lsinfo", a[0]) );
127
 
        assert( !strcmp("double quote\"d from php magicquotes", a[1]) );
128
 
        assert( !a[2] );
129
 
 
130
 
        return 0;
131
 
}
132
 
 
133
 
#endif