view libaudacious/formatter.c @ 1982:cf33371e7ac3 trunk

[svn] - enhancements to the NewVFS probing layer.
author nenolod
date Sat, 18 Nov 2006 19:44:58 -0800
parents cb178e5ad177
children 04961c9a5242
line wrap: on
line source

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <glib.h>
#include <string.h>
#include "formatter.h"


Formatter *
xmms_formatter_new(void)
{
    Formatter *formatter = g_new0(Formatter, 1);

    xmms_formatter_associate(formatter, '%', "%");
    return formatter;
}

void
xmms_formatter_destroy(Formatter * formatter)
{
    int i;

    for (i = 0; i < 256; i++)
        if (formatter->values[i])
            g_free(formatter->values[i]);
    g_free(formatter);
}

void
xmms_formatter_associate(Formatter * formatter, guchar id, char *value)
{
    xmms_formatter_dissociate(formatter, id);
    formatter->values[id] = g_strdup(value);
}

void
xmms_formatter_dissociate(Formatter * formatter, guchar id)
{
    if (formatter->values[id])
        g_free(formatter->values[id]);
    formatter->values[id] = 0;
}

gchar *
xmms_formatter_format(Formatter * formatter, char *format)
{
    char *p, *q, *buffer;
    int 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]) {
                strcpy(q, formatter->values[(int) *p]);
                q += strlen(q);
            }
            else {
                *q++ = '%';
                if (*p != '\0')
                    *q++ = *p;
                else
                    p--;
            }
        }
        else
            *q++ = *p;
    *q = 0;
    return buffer;
}