diff options
author | 2009-12-23 20:31:50 +0100 | |
---|---|---|
committer | 2009-12-23 20:31:50 +0100 | |
commit | 78d4bc3bba3942072c4b061ec21339df957e4d71 (patch) | |
tree | 37a61bd1b4514a5c5e797ebdd9d3975a97c4f4a5 | |
parent | e56dcdc361562b2e0c8ed719f2b2b50e279445ed (diff) |
improved/fixed column config parser
-rw-r--r-- | plugins/gtkui/Makefile.am | 3 | ||||
-rw-r--r-- | plugins/gtkui/callbacks.c | 79 | ||||
-rw-r--r-- | plugins/gtkui/gtkplaylist.c | 134 | ||||
-rw-r--r-- | plugins/gtkui/parser.c | 107 | ||||
-rw-r--r-- | plugins/gtkui/parser.h | 39 |
5 files changed, 179 insertions, 183 deletions
diff --git a/plugins/gtkui/Makefile.am b/plugins/gtkui/Makefile.am index e6fe0485..504984e0 100644 --- a/plugins/gtkui/Makefile.am +++ b/plugins/gtkui/Makefile.am @@ -7,7 +7,8 @@ gtkui_la_SOURCES = gtkui.c gtkui.h\ drawing.h gdkdrawing.c\ progress.c progress.h\ search.c search.h\ - fileman.c + fileman.c\ + parser.c parser.h gtkui_la_LDFLAGS = -module diff --git a/plugins/gtkui/callbacks.c b/plugins/gtkui/callbacks.c index b34f4b08..4150e3fc 100644 --- a/plugins/gtkui/callbacks.c +++ b/plugins/gtkui/callbacks.c @@ -37,6 +37,7 @@ #include "progress.h" #include "session.h" #include "gtkui.h" +#include "parser.h" #define SELECTED(it) (deadbeef->pl_is_selected(it)) #define SELECT(it, sel) (deadbeef->pl_set_selected(it,sel)) @@ -1905,84 +1906,6 @@ on_addlocation_key_press_event (GtkWidget *widget, } -// very basic parser, ripped from psynth, optimized, and extended to support -// quoted strings and extra special chars -#define MAX_TOKEN 256 -static int parser_line; - -static const char * -skipws (const char *p) { - while (*p <= ' ' && *p) { - if (*p == '\n') { - parser_line++; - } - p++; - } - if (!*p) { - return NULL; - } - return p; -} - -const char * -gettoken (const char *p, char *tok) { - const char *c; - assert (p); - assert (tok); - int n = MAX_TOKEN-1; - char specialchars[] = "{}();"; - if (!(p = skipws (p))) { - return NULL; - } - if (*p == '"') { - p++; - c = p; - while (n > 0 && *c && *c != '"') { - if (*c == '\n') { - parser_line++; - } - *tok++ = *c++; - n--; - } - if (*c) { - c++; - } - *tok = 0; - return c; - } - if (strchr (specialchars, *p)) { - *tok = *p; - tok[1] = 0; - return p+1; - } - c = p; - while (n > 0 && *c > ' ' && !strchr (specialchars, *c)) { - *tok++ = *c++; - n--; - } - *tok = 0; - return c; -} - -const char * -gettoken_warn_eof (const char *p, char *tok) { - p = gettoken (p, tok); - if (!p) { - fprintf (stderr, "parser: unexpected eof at line %d", parser_line); - } - return p; -} - -const char * -gettoken_err_eof (const char *p, char *tok) { - p = gettoken (p, tok); - if (!p) { - fprintf (stderr, "parser: unexpected eof at line %d", parser_line); - exit (-1); - } - return p; -} - void on_prop_entry_changed(GtkEditable *editable, gpointer user_data) { const char *key = g_object_get_data (G_OBJECT (editable), "key"); diff --git a/plugins/gtkui/gtkplaylist.c b/plugins/gtkui/gtkplaylist.c index 023c917c..b3984481 100644 --- a/plugins/gtkui/gtkplaylist.c +++ b/plugins/gtkui/gtkplaylist.c @@ -39,6 +39,7 @@ #include "drawing.h" #include "session.h" #include "deadbeef.h" +#include "parser.h" #define min(x,y) ((x)<(y)?(x):(y)) #define max(x,y) ((x)>(y)?(x):(y)) @@ -1722,122 +1723,47 @@ gtkpl_column_remove (gtkplaylist_t *pl, gtkpl_column_t *c) { void gtkpl_append_column_from_textdef (gtkplaylist_t *pl, const uint8_t *def) { // syntax: "title" "format" id width alignright - char title[128]; - char format[128]; + char token[MAX_TOKEN]; + const char *p = def; + char title[MAX_TOKEN]; int id; + char fmt[MAX_TOKEN]; int width; - int align_right; - // title - if (*def != '"') { - return; - } - def++; - if (*def == 0) { - return; - } - const uint8_t *e = def; - e++; - while (*e && *e != '"') { - e++; - } - if (*e == 0) { - return; - } - memcpy (title, def, e-def); - title[e-def] = 0; - // skip whitespace - def = e; - def++; - while (*def && *def <= ' ') { - def++; - } - if (*def == 0) { - return; - } - // format - if (*def != '"') { - return; - } - def++; - if (*def == 0) { - return; - } - e = def; - while (*e && *e != '"') { - e++; - } - if (*e == 0) { - return; - } - memcpy (format, def, e-def); - format[e-def] = 0; - // skip whitespace - def = e; - def++; - while (*def && *def <= ' ') { - def++; - } - if (*def == 0) { + int align; + + parser_init (); + + p = gettoken_warn_eof (p, token); + if (!p) { return; } - // id - e = def; - while (*e && (isdigit (*e) || *e == '-')) { - e++; - } - if (*e == 0) { + strcpy (title, token); + + p = gettoken_warn_eof (p, token); + if (!p) { return; } - { - char s[e-def+1]; - memcpy (s, def, e-def); - s[e-def] = 0; - id = atoi (s); - } - // skip whitespace - def = e; - def++; - while (*def && *def <= ' ') { - def++; - } - if (*def == 0) { + strcpy (fmt, token); + + p = gettoken_warn_eof (p, token); + if (!p) { return; } - // width - e = def; - while (*e && isdigit (*e)) { - e++; - } - if (*e == 0) { + id = atoi (token); + + p = gettoken_warn_eof (p, token); + if (!p) { return; } - { - char s[e-def+1]; - memcpy (s, def, e-def); - s[e-def] = 0; - width = atoi (s); - } - // skip whitespace - def = e; - def++; - while (*def && *def <= ' ') { - def++; - } - if (*def == 0) { + width = atoi (token); + + p = gettoken_warn_eof (p, token); + if (!p) { return; } - // align_right - e = def; - while (*e && isdigit (*e)) { - e++; - } - { - char s[e-def+1]; - memcpy (s, def, e-def); - s[e-def] = 0; - align_right = atoi (s); - } - gtkpl_column_append (pl, gtkpl_column_alloc (title, width, id, format[0] ? format : NULL, align_right)); + align = atoi (token); + + gtkpl_column_append (pl, gtkpl_column_alloc (title, width, id, fmt, align)); } void diff --git a/plugins/gtkui/parser.c b/plugins/gtkui/parser.c new file mode 100644 index 00000000..4cbd2da4 --- /dev/null +++ b/plugins/gtkui/parser.c @@ -0,0 +1,107 @@ +/* + DeaDBeeF - ultimate music player for GNU/Linux systems with X11 + Copyright (C) 2009 Alexey Yakovenko + + 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, see <http://www.gnu.org/licenses/>. +*/ + +#include <stdlib.h> +#include <assert.h> +#include <string.h> +#include <stdio.h> +#include "parser.h" + +// very basic parser, ripped from psynth, optimized, and extended to support +// quoted strings and extra special chars +int parser_line; + +void +parser_init (void) { + parser_line = 1; +} + +const char * +skipws (const char *p) { + while (*p <= ' ' && *p) { + if (*p == '\n') { + parser_line++; + } + p++; + } + if (!*p) { + return NULL; + } + return p; +} + +const char * +gettoken (const char *p, char *tok) { + const char *c; + assert (p); + assert (tok); + int n = MAX_TOKEN-1; + char specialchars[] = "{}();"; + if (!(p = skipws (p))) { + return NULL; + } + if (*p == '"') { + p++; + c = p; + while (n > 0 && *c && *c != '"') { + if (*c == '\n') { + parser_line++; + } + *tok++ = *c++; + n--; + } + if (*c) { + c++; + } + *tok = 0; + return c; + } + if (strchr (specialchars, *p)) { + *tok = *p; + tok[1] = 0; + return p+1; + } + c = p; + while (n > 0 && *c > ' ' && !strchr (specialchars, *c)) { + *tok++ = *c++; + n--; + } + *tok = 0; + return c; +} + +const char * +gettoken_warn_eof (const char *p, char *tok) { + p = gettoken (p, tok); + if (!p) { + fprintf (stderr, "parser: unexpected eof at line %d", parser_line); + } + return p; +} + +const char * +gettoken_err_eof (const char *p, char *tok) { + p = gettoken (p, tok); + if (!p) { + fprintf (stderr, "parser: unexpected eof at line %d", parser_line); + exit (-1); + } + return p; +} + + diff --git a/plugins/gtkui/parser.h b/plugins/gtkui/parser.h new file mode 100644 index 00000000..61955bcf --- /dev/null +++ b/plugins/gtkui/parser.h @@ -0,0 +1,39 @@ +/* + DeaDBeeF - ultimate music player for GNU/Linux systems with X11 + Copyright (C) 2009 Alexey Yakovenko + + 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, see <http://www.gnu.org/licenses/>. +*/ +#ifndef __PARSER_H +#define __PARSER_H + +#define MAX_TOKEN 256 +extern int parser_line; + +void +parser_init (void); + +const char * +skipws (const char *p); + +const char * +gettoken (const char *p, char *tok); + +const char * +gettoken_warn_eof (const char *p, char *tok); + +const char * +gettoken_err_eof (const char *p, char *tok); + +#endif |