# HG changeset patch # User Matti Hamalainen # Date 1206940449 -10800 # Node ID f88dd72c9156bc66bb2c502823c0db7dd2419d5b # Parent 11f7c096f7e6d69e20f2af801dfd2ea7b4038923 Moved formatter stuff to the only plugin that actually used it. diff -r 11f7c096f7e6 -r f88dd72c9156 src/song_change/Makefile --- a/src/song_change/Makefile Sun Mar 30 08:17:21 2008 +0300 +++ b/src/song_change/Makefile Mon Mar 31 08:14:09 2008 +0300 @@ -1,6 +1,6 @@ PLUGIN = song_change${PLUGIN_SUFFIX} -SRCS = song_change.c +SRCS = formatter.c song_change.c include ../../buildsys.mk include ../../extra.mk diff -r 11f7c096f7e6 -r f88dd72c9156 src/song_change/formatter.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/song_change/formatter.c Mon Mar 31 08:14:09 2008 +0300 @@ -0,0 +1,105 @@ +/* Audacious + * Copyright (C) 2005-2007 Audacious team + * + * XMMS - Cross-platform multimedia player + * Copyright (C) 1998-2003 Peter Alm, Mikael Alm, Olle Hallnas, + * Thomas Nilsson and 4Front Technologies + * Copyright (C) 1999-2003 Haavard Kvaalen + * + * 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; under version 3 of the License. + * + * 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 . + * + * The Audacious team does not consider modular code linking to + * Audacious or using our public API to be a derived work. + */ + +#include "config.h" + +#include +#include +#include "formatter.h" + +Formatter * +formatter_new(void) +{ + Formatter *formatter = g_slice_new0(Formatter); + + formatter_associate(formatter, '%', "%"); + return formatter; +} + +void +formatter_destroy(Formatter * formatter) +{ + int i; + + for (i = 0; i < 256; i++) + if (formatter->values[i]) + g_free(formatter->values[i]); + + g_slice_free(Formatter, formatter); +} + +void +formatter_associate(Formatter * formatter, const guchar id, const gchar *value) +{ + formatter_dissociate(formatter, id); + formatter->values[id] = g_strdup(value); +} + +void +formatter_dissociate(Formatter * formatter, const guchar id) +{ + if (formatter->values[id]) + g_free(formatter->values[id]); + formatter->values[id] = 0; +} + +gchar * +formatter_format(Formatter * formatter, const gchar *format) +{ + gchar *p, *q, *buffer; + gint len; + + for (p = format, len = 0; *p; p++) + if (*p == '%') { + if (formatter->values[(int) *++p]) + len += strlen(formatter->values[(int) *p]); + else if (!*p) { + len += 1; + p--; + } + else + len += 2; + } + else + len++; + buffer = g_malloc(len + 1); + for (p = format, q = buffer; *p; p++) + if (*p == '%') { + if (formatter->values[(int) *++p]) { + g_strlcpy(q, formatter->values[(int) *p], len - 1); + q += strlen(q); + } + else { + *q++ = '%'; + if (*p != '\0') + *q++ = *p; + else + p--; + } + } + else + *q++ = *p; + *q = 0; + return buffer; +} diff -r 11f7c096f7e6 -r f88dd72c9156 src/song_change/formatter.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/song_change/formatter.h Mon Mar 31 08:14:09 2008 +0300 @@ -0,0 +1,16 @@ +#ifndef FORMATTER_H +#define FORMATTER_H + +#include + +typedef struct { + gchar *values[256]; +} Formatter; + +Formatter *formatter_new(void); +void formatter_destroy(Formatter * formatter); +void formatter_associate(Formatter * formatter, const guchar id, const gchar * value); +void formatter_dissociate(Formatter * formatter, const guchar id); +gchar *formatter_format(Formatter * formatter, const gchar * format); + +#endif diff -r 11f7c096f7e6 -r f88dd72c9156 src/song_change/song_change.c --- a/src/song_change/song_change.c Sun Mar 30 08:17:21 2008 +0300 +++ b/src/song_change/song_change.c Mon Mar 31 08:14:09 2008 +0300 @@ -19,7 +19,7 @@ #include #include #include -#include +#include "formatter.h" #include #include #include @@ -99,58 +99,58 @@ do_command(char *cmd, const char *current_file, int pos) { int length, rate, freq, nch; - char *str, *shstring = NULL, *temp, numbuf[16]; + char *str, *shstring = NULL, *temp, numbuf[32]; gboolean playing; Formatter *formatter; if (cmd && strlen(cmd) > 0) { - formatter = aud_formatter_new(); + formatter = formatter_new(); str = audacious_drct_pl_get_title(pos); if (str) { temp = aud_escape_shell_chars(str); - aud_formatter_associate(formatter, 's', temp); - aud_formatter_associate(formatter, 'n', temp); + formatter_associate(formatter, 's', temp); + formatter_associate(formatter, 'n', temp); g_free(str); g_free(temp); } else { - aud_formatter_associate(formatter, 's', ""); - aud_formatter_associate(formatter, 'n', ""); + formatter_associate(formatter, 's', ""); + formatter_associate(formatter, 'n', ""); } if (current_file) { temp = aud_escape_shell_chars(current_file); - aud_formatter_associate(formatter, 'f', temp); + formatter_associate(formatter, 'f', temp); g_free(temp); } else - aud_formatter_associate(formatter, 'f', ""); - sprintf(numbuf, "%02d", pos + 1); - aud_formatter_associate(formatter, 't', numbuf); + formatter_associate(formatter, 'f', ""); + g_snprintf(numbuf, sizeof(numbuf), "%02d", pos + 1); + formatter_associate(formatter, 't', numbuf); length = audacious_drct_pl_get_time(pos); if (length != -1) { - sprintf(numbuf, "%d", length); - aud_formatter_associate(formatter, 'l', numbuf); + g_snprintf(numbuf, sizeof(numbuf), "%d", length); + formatter_associate(formatter, 'l', numbuf); } else - aud_formatter_associate(formatter, 'l', "0"); + formatter_associate(formatter, 'l', "0"); audacious_drct_get_info(&rate, &freq, &nch); - sprintf(numbuf, "%d", rate); - aud_formatter_associate(formatter, 'r', numbuf); - sprintf(numbuf, "%d", freq); - aud_formatter_associate(formatter, 'F', numbuf); - sprintf(numbuf, "%d", nch); - aud_formatter_associate(formatter, 'c', numbuf); + g_snprintf(numbuf, sizeof(numbuf), "%d", rate); + formatter_associate(formatter, 'r', numbuf); + g_snprintf(numbuf, sizeof(numbuf), "%d", freq); + formatter_associate(formatter, 'F', numbuf); + g_snprintf(numbuf, sizeof(numbuf), "%d", nch); + formatter_associate(formatter, 'c', numbuf); playing = audacious_drct_get_playing(); - sprintf(numbuf, "%d", playing); - aud_formatter_associate(formatter, 'p', numbuf); - shstring = aud_formatter_format(formatter, cmd); - aud_formatter_destroy(formatter); + g_snprintf(numbuf, sizeof(numbuf), "%d", playing); + formatter_associate(formatter, 'p', numbuf); + shstring = formatter_format(formatter, cmd); + formatter_destroy(formatter); if (shstring) { @@ -538,8 +538,8 @@ static void songchange_playlist_eof(gpointer unused, gpointer unused2) { - int pos; - char *current_file; + gint pos; + gchar *current_file; pos = audacious_drct_pl_get_pos(); current_file = audacious_drct_pl_get_file(pos);