changeset 2487:729134979b7b

Moved formatter stuff to the only plugin that actually used it.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 31 Mar 2008 08:14:09 +0300
parents b8fb86dd3c66
children 70d12fddc27c
files src/song_change/Makefile src/song_change/formatter.c src/song_change/formatter.h src/song_change/song_change.c
diffstat 4 files changed, 148 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/song_change/Makefile	Mon Mar 31 08:16:11 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
--- /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 <http://www.gnu.org/licenses>.
+ *
+ *  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 <glib.h>
+#include <string.h>
+#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;
+}
--- /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 <glib.h>
+
+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
--- a/src/song_change/song_change.c	Mon Mar 31 08:16:11 2008 +0300
+++ b/src/song_change/song_change.c	Mon Mar 31 08:14:09 2008 +0300
@@ -19,7 +19,7 @@
 #include <audacious/ui_preferences.h>
 #include <audacious/configdb.h>
 #include <audacious/auddrct.h>
-#include <audacious/formatter.h>
+#include "formatter.h"
 #include <audacious/i18n.h>
 #include <audacious/hook.h>
 #include <audacious/playlist.h>
@@ -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);