Discussion:
[PATCH 2/2] Add 'icecast_default_charset' option to allow cmus to properly display non-UTF-8 streaming metadata.
Pyotr Berezhkov
2012-08-31 08:32:07 UTC
Permalink
---
Doc/cmus.txt | 5 +++++
convert.c | 14 ++++++++++++++
convert.h | 2 ++
input.c | 7 ++++---
options.c | 14 ++++++++++++++
options.h | 1 +
ui_curses.c | 7 ++++++-
7 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/Doc/cmus.txt b/Doc/cmus.txt
index c1b4654..a1f2d5f 100644
--- a/Doc/cmus.txt
+++ b/Doc/cmus.txt
@@ -869,6 +869,11 @@ id3_default_charset (ISO-8859-1)

NOTE: This is used only if the tag is not valid UTF-8.

+icecast_default_charset (ISO-8859-1)
+ Default character set to use for non-UTF-8 icecast stream metadata.
+
+ NOTE: This is used only if the metadata is not valid UTF-8.
+
lib_sort (artist album discnumber tracknumber title filename) [`Sort Keys`]
Sort keys for the sorted library view (2).

diff --git a/convert.c b/convert.c
index 7b18ab3..cfb07e8 100644
--- a/convert.c
+++ b/convert.c
@@ -18,6 +18,7 @@

#include "convert.h"
#include "xmalloc.h"
+#include "uchar.h"
#ifdef HAVE_CONFIG
#include "config/iconv.h"
#endif
@@ -113,3 +114,16 @@ int utf8_encode(const char *inbuf, const char *encoding, char **outbuf)

return rc < 0 ? -1 : 0;
}
+
+char *to_utf8(const char *str, const char *enc)
+{
+ char *outbuf = NULL;
+ int rc;
+
+ if (u_is_valid(str)) {
+ return xstrdup(str);
+ } else {
+ rc = utf8_encode(str, enc, &outbuf);
+ return rc < 0 ? xstrdup(str) : outbuf;
+ }
+}
diff --git a/convert.h b/convert.h
index 86358e7..08b0c70 100644
--- a/convert.h
+++ b/convert.h
@@ -28,4 +28,6 @@ ssize_t convert(const char *inbuf, ssize_t inbuf_size,

int utf8_encode(const char *inbuf, const char *encoding, char **outbuf);

+char *to_utf8(const char *str, const char *enc);
+
#endif
diff --git a/input.c b/input.c
index 1813164..e5b668a 100644
--- a/input.c
+++ b/input.c
@@ -16,6 +16,7 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

+#include "convert.h"
#include "input.h"
#include "ip.h"
#include "pcm.h"
@@ -272,15 +273,15 @@ static int setup_remote(struct input_plugin *ip, const struct keyval *headers, i

val = keyvals_get_val(headers, "icy-name");
if (val)
- ip->data.icy_name = xstrdup(val);
+ ip->data.icy_name = to_utf8(val, icecast_default_charset);

val = keyvals_get_val(headers, "icy-genre");
if (val)
- ip->data.icy_genre = xstrdup(val);
+ ip->data.icy_genre = to_utf8(val, icecast_default_charset);

val = keyvals_get_val(headers, "icy-url");
if (val)
- ip->data.icy_url = xstrdup(val);
+ ip->data.icy_url = to_utf8(val, icecast_default_charset);

return 0;
}
diff --git a/options.c b/options.c
index 8325c05..3823f4a 100644
--- a/options.c
+++ b/options.c
@@ -116,6 +116,7 @@ char *current_alt_format = NULL;
char *window_title_format = NULL;
char *window_title_alt_format = NULL;
char *id3_default_charset = NULL;
+char *icecast_default_charset = NULL;

static void buf_int(char *buf, int val)
{
@@ -214,12 +215,23 @@ static void get_id3_default_charset(unsigned int id, char *buf)
strcpy(buf, id3_default_charset);
}

+static void get_icecast_default_charset(unsigned int id, char *buf)
+{
+ strcpy(buf, icecast_default_charset);
+}
+
static void set_id3_default_charset(unsigned int id, const char *buf)
{
free(id3_default_charset);
id3_default_charset = xstrdup(buf);
}

+static void set_icecast_default_charset(unsigned int id, const char *buf)
+{
+ free(icecast_default_charset);
+ icecast_default_charset = xstrdup(buf);
+}
+
static const struct {
const char *str;
sort_key_t key;
@@ -970,6 +982,7 @@ static const struct {
DT(continue)
DT(smart_artist_sort)
DN(id3_default_charset)
+ DN(icecast_default_charset)
DN(lib_sort)
DN(output_plugin)
DN(passwd)
@@ -1041,6 +1054,7 @@ static const struct {
{ "lib_sort" , "albumartist date album discnumber tracknumber title filename" },
{ "pl_sort", "" },
{ "id3_default_charset","ISO-8859-1" },
+ { "icecast_default_charset","ISO-8859-1" },
{ NULL, NULL }
};

diff --git a/options.h b/options.h
index 3fa6a6f..aeba6ec 100644
--- a/options.h
+++ b/options.h
@@ -143,6 +143,7 @@ extern char *window_title_format;
extern char *window_title_alt_format;

extern char *id3_default_charset;
+extern char *icecast_default_charset;

/* build option list */
void options_add(void);
diff --git a/ui_curses.c b/ui_curses.c
index 77d5f5b..1287a2a 100644
--- a/ui_curses.c
+++ b/ui_curses.c
@@ -16,6 +16,7 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

+#include "convert.h"
#include "ui_curses.h"
#include "cmdline.h"
#include "search_mode.h"
@@ -143,6 +144,8 @@ static int show_cursor;
static int cursor_x;
static int cursor_y;

+static char *title_buf = NULL;
+
enum {
CURSED_WIN,
CURSED_WIN_CUR,
@@ -1284,11 +1287,13 @@ static void do_update_titleline(void)
const char *title = get_stream_title();

if (title != NULL) {
+ free(title_buf);
+ title_buf = to_utf8(title, icecast_default_charset);
/*
* StreamTitle overrides radio station name
*/
use_alt_format = 0;
- fopt_set_str(&track_fopts[TF_TITLE], title);
+ fopt_set_str(&track_fopts[TF_TITLE], title_buf);
}
}
--
1.7.9.5


--FL5UXtIhxfXey3p5--
Loading...