2004-07-16 19:56:36 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2004-07-16 19:56:36 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
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.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-07-16 19:56:36 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-06-19 01:01:09 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/dynarray.h>
|
|
|
|
|
#include <pulsecore/gccmacro.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-06-19 01:01:09 +00:00
|
|
|
#include "tokenizer.h"
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
struct pa_tokenizer {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_dynarray *dynarray;
|
2004-06-19 01:01:09 +00:00
|
|
|
};
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void token_free(void *p, PA_GCC_UNUSED void *userdata) {
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(p);
|
2004-06-19 01:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void parse(pa_dynarray*a, const char *s, unsigned args) {
|
2004-06-19 01:01:09 +00:00
|
|
|
int infty = 0;
|
|
|
|
|
const char delimiter[] = " \t\n\r";
|
|
|
|
|
const char *p;
|
|
|
|
|
assert(a && s);
|
|
|
|
|
|
|
|
|
|
if (args == 0)
|
|
|
|
|
infty = 1;
|
|
|
|
|
|
|
|
|
|
p = s+strspn(s, delimiter);
|
|
|
|
|
while (*p && (infty || args >= 2)) {
|
|
|
|
|
size_t l = strcspn(p, delimiter);
|
2004-08-04 16:39:30 +00:00
|
|
|
char *n = pa_xstrndup(p, l);
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_dynarray_append(a, n);
|
2004-06-19 01:01:09 +00:00
|
|
|
p += l;
|
|
|
|
|
p += strspn(p, delimiter);
|
|
|
|
|
args--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (args && *p) {
|
2004-08-04 16:39:30 +00:00
|
|
|
char *n = pa_xstrdup(p);
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_dynarray_append(a, n);
|
2004-06-19 01:01:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_tokenizer* pa_tokenizer_new(const char *s, unsigned args) {
|
|
|
|
|
pa_tokenizer *t;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
t = pa_xmalloc(sizeof(pa_tokenizer));
|
2004-07-03 23:35:12 +00:00
|
|
|
t->dynarray = pa_dynarray_new();
|
2004-06-19 01:01:09 +00:00
|
|
|
assert(t->dynarray);
|
|
|
|
|
|
|
|
|
|
parse(t->dynarray, s, args);
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_tokenizer_free(pa_tokenizer *t) {
|
2004-06-19 01:01:09 +00:00
|
|
|
assert(t);
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_dynarray_free(t->dynarray, token_free, NULL);
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(t);
|
2004-06-19 01:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
const char *pa_tokenizer_get(pa_tokenizer *t, unsigned i) {
|
2004-06-19 01:01:09 +00:00
|
|
|
assert(t);
|
2004-07-03 23:35:12 +00:00
|
|
|
return pa_dynarray_get(t->dynarray, i);
|
2004-06-19 01:01:09 +00:00
|
|
|
}
|