Skip to content

Commit

Permalink
Refactor librcc use so that initialisation and cleanup occur only once.
Browse files Browse the repository at this point in the history
git-svn-id: svn://svn.daper.net/moc/trunk@2430 910807d9-36e0-0310-a014-e9ea483e2ba4
  • Loading branch information
jcf committed Jul 8, 2012
1 parent e89da1c commit 0905063
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 111 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ mocp_SOURCES = log.c \
tags_cache.h \
utf8.c \
utf8.h \
rcc.c \
rcc.h \
audio_helper.h \
audio_helper.c \
softmixer.c \
Expand Down
15 changes: 15 additions & 0 deletions configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ if test "x$am_cv_func_iconv" != xyes; then
fi
EXTRA_LIBS="$EXTRA_LIBS $LIBICONV"

dnl librcc
COMPILE_RCC=no
AC_ARG_WITH(rcc, AS_HELP_STRING([--without-rcc],
[Compile without LIBRCC support]))
if test "x$with_rcc" != "xno"
then
AC_CHECK_HEADERS([librcc.h],
[AC_DEFINE([HAVE_RCC], 1, [Define if you have librcc.h])
AC_CHECK_LIB(rcc, rccInit,
[RCC_LIBS="-lrcc"
AC_SUBST([RCC_LIBS])
COMPILE_RCC=yes])
])
fi

AC_C_CONST
AC_C_INLINE
AC_TYPE_OFF_T
Expand Down
49 changes: 2 additions & 47 deletions decoder_plugins/mp3/mp3.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
#ifdef HAVE_ICONV
# include <iconv.h>
#endif
#ifdef HAVE_RCC
# include <librcc.h>
#endif

#define DEBUG

Expand All @@ -48,6 +45,7 @@
#include "options.h"
#include "files.h"
#include "utf8.h"
#include "rcc.h"
#include "options.h"

#define INPUT_BUFFER (32 * 1024)
Expand Down Expand Up @@ -117,28 +115,6 @@ static size_t fill_buff (struct mp3_data *data)
return read_size;
}

#ifdef HAVE_RCC
static char *do_rcc (char *str)
{
rcc_string rccstring;
char *reencoded;

assert (str != NULL);

rccstring = rccFrom(NULL, 0, str);
if (rccstring) {
if (*rccstring && (reencoded = rccToCharset(NULL, "UTF-8", rccstring))) {
free(str);
free(rccstring);
return reencoded;
}

free (rccstring);
}
return str;
}
#endif

static char *id3v1_fix (const char *str)
{
if (iconv_id3_fix != (iconv_t)-1)
Expand Down Expand Up @@ -191,7 +167,7 @@ static char *get_tag (struct id3_tag *tag, const char *what)

#ifdef HAVE_RCC
if (options_get_int("UseRCC"))
comm = do_rcc (comm);
comm = rcc_reencode (comm);
else {
#endif /* HAVE_RCC */
t = comm;
Expand Down Expand Up @@ -795,23 +771,6 @@ static int mp3_can_decode (struct io_stream *stream)

static void mp3_init ()
{
#ifdef HAVE_RCC
rcc_class classes[] = {
{ "input", RCC_CLASS_STANDARD, NULL, NULL, "Input Encoding",
0 },
{ "output", RCC_CLASS_KNOWN, NULL, NULL,
"Output Encoding", 0 },
{ NULL, 0, NULL, NULL, NULL, 0 }
};

rccInit ();
rccInitDefaultContext(NULL, 0, 0, classes, 0);
rccLoad(NULL, "moc");
rccSetOption(NULL, RCC_OPTION_TRANSLATE,
RCC_OPTION_TRANSLATE_SKIP_PARRENT);
rccSetOption(NULL, RCC_OPTION_AUTODETECT_LANGUAGE, 1);
#endif

iconv_id3_fix = iconv_open ("UTF-8",
options_get_str("ID3v1TagsEncoding"));
if (iconv_id3_fix == (iconv_t)(-1))
Expand All @@ -820,10 +779,6 @@ static void mp3_init ()

static void mp3_destroy ()
{
#ifdef HAVE_RCC
rccFree ();
#endif

if (iconv_close(iconv_id3_fix) == -1)
logit ("iconv_close() failed: %s", strerror(errno));
}
Expand Down
15 changes: 0 additions & 15 deletions decoder_plugins/mp3/mp3.m4
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,13 @@ dnl libmad (mp3)
AC_ARG_WITH(mp3, AS_HELP_STRING([--without-mp3],
[Compile without mp3 support (libmad)]))

COMPILE_RCC=no
if test "x$with_mp3" != "xno"
then
AC_CHECK_LIB(mad, mad_stream_init, [
AC_CHECK_HEADER([mad.h], ,)])

if test "$ac_cv_lib_mad_mad_stream_init" = "yes" -a "$HAVE_ID3TAG" = "yes"
then
dnl librcc
AC_ARG_WITH(rcc, AS_HELP_STRING([--without-rcc],
[Compile without LIBRCC support]))
if test "x$with_rcc" != "xno"
then
AC_CHECK_HEADERS([librcc.h],
[AC_DEFINE([HAVE_RCC], 1, [Define if you have librcc.h])
AC_CHECK_LIB(rcc, rccInit,
[RCC_LIBS="-lrcc"
AC_SUBST([RCC_LIBS])
COMPILE_RCC=yes])
])
fi

want_mp3="yes"
DECODER_PLUGINS="$DECODER_PLUGINS mp3"
fi
Expand Down
3 changes: 2 additions & 1 deletion interface_elements.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include "protocol.h"
#include "interface.h"
#include "utf8.h"
#include "rcc.h"
#include "lyrics.h"

#ifndef PACKAGE_REVISION
Expand Down Expand Up @@ -1142,7 +1143,7 @@ static void side_menu_make_list_content (struct side_menu *m,
strcpy (title, strrchr (lists_strs_at (dirs, i), '/') + 1);
strcat (title, "/");
t_str = xstrdup (title);
t_str = iconv_rcc (t_str);
t_str = rcc_reencode (t_str);
snprintf(title, PATH_MAX, "%s", t_str);
free(t_str);
}
Expand Down
5 changes: 5 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "compat.h"
#include "decoder.h"
#include "lists.h"
#include "rcc.h"

struct parameters
{
Expand Down Expand Up @@ -174,6 +175,7 @@ static void start_moc (const struct parameters *params, lists_t_strs *args)
signal (SIGCHLD, sig_chld);
server_loop (list_sock);
options_free ();
rcc_cleanup ();
exit (0);
case -1:
fatal ("fork() failed: %s", strerror(errno));
Expand Down Expand Up @@ -832,6 +834,7 @@ int main (int argc, char *argv[])

check_moc_dir ();

rcc_init ();
decoder_init (params.debug);
srand (time(NULL));

Expand All @@ -840,5 +843,7 @@ int main (int argc, char *argv[])
else
start_moc (&params, args);

rcc_cleanup ();

return 0;
}
3 changes: 2 additions & 1 deletion playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "files.h"
#include "rbtree.h"
#include "utf8.h"
#include "rcc.h"

/* Initial size of the table */
#define INIT_SIZE 64
Expand Down Expand Up @@ -679,7 +680,7 @@ void plist_set_title_file (struct plist *plist, const int num,
#ifdef HAVE_RCC
if (options_get_int("UseRCCForFilesystem")) {
char *t_str = xstrdup (title);
plist->items[num].title_file = iconv_rcc(t_str);
plist->items[num].title_file = rcc_reencode (t_str);
return;
}
#endif
Expand Down
77 changes: 77 additions & 0 deletions rcc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* MOC - music on console
* Copyright (C) 2005,2011 Damian Pietras <[email protected]>
*
* 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.
*
*/

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

#include <stdlib.h>

#ifdef HAVE_RCC
# include <librcc.h>
#endif

#include <assert.h>

#include "rcc.h"

char *rcc_reencode (char *str)
{
char *result = str;

assert (str != NULL);

#ifdef HAVE_RCC
rcc_string rccstring;

rccstring = rccFrom (NULL, 0, str);
if (rccstring) {
if (*rccstring) {
char *reencoded;

reencoded = rccToCharset (NULL, "UTF-8", rccstring);
if (reencoded) {
free (result);
result = reencoded;
}
}

free (rccstring);
}
#endif /* HAVE_RCC */

return result;
}

void rcc_init ()
{
#ifdef HAVE_RCC
rcc_class classes[] = {
{"input", RCC_CLASS_STANDARD, NULL, NULL, "Input Encoding", 0},
{"output", RCC_CLASS_KNOWN, NULL, NULL, "Output Encoding", 0},
{NULL, 0, NULL, NULL, NULL, 0}
};

rccInit ();
rccInitDefaultContext (NULL, 0, 0, classes, 0);
rccLoad (NULL, "moc");
rccSetOption (NULL, RCC_OPTION_TRANSLATE,
RCC_OPTION_TRANSLATE_SKIP_PARRENT);
rccSetOption (NULL, RCC_OPTION_AUTODETECT_LANGUAGE, 1);
#endif /* HAVE_RCC */
}

void rcc_cleanup ()
{
#ifdef HAVE_RCC
rccFree ();
#endif /* HAVE_RCC */
}
16 changes: 16 additions & 0 deletions rcc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef RCC_H
#define RCC_H

#ifdef __cplusplus
extern "C" {
#endif

char *rcc_reencode (char *);
void rcc_init ();
void rcc_cleanup ();

#ifdef __cplusplus
}
#endif

#endif
47 changes: 1 addition & 46 deletions utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
#ifdef HAVE_ICONV
# include <iconv.h>
#endif
#ifdef HAVE_RCC
# include <librcc.h>
#endif
#ifdef HAVE_NL_TYPES_H
# include <nl_types.h>
#endif
Expand All @@ -49,6 +46,7 @@
#include "log.h"
#include "options.h"
#include "utf8.h"
#include "rcc.h"

static char *terminal_charset = NULL;
static int using_utf8 = 0;
Expand All @@ -58,30 +56,6 @@ static iconv_t files_iconv_desc = (iconv_t)(-1);
static iconv_t xterm_iconv_desc = (iconv_t)(-1);


char *iconv_rcc (char *str)
{
#ifdef HAVE_RCC
rcc_string rccstring;
char *reencoded;

assert (str != NULL);

rccstring = rccFrom(NULL, 0, str);
if (rccstring) {
if (*rccstring && (reencoded = rccToCharset(NULL, "UTF-8", rccstring))) {
free(str);
free(rccstring);
return reencoded;
}

free (rccstring);
}
return str;
#endif /* HAVE_RCC */
return xstrdup (str);
}


/* Return a malloc()ed string converted using iconv().
* If for_file_name is not 0, use the conversion defined for file names.
* For NULL returns NULL. */
Expand Down Expand Up @@ -330,9 +304,6 @@ static void iconv_cleanup ()
if (iconv_desc != (iconv_t)(-1)
&& iconv_close(iconv_desc) == -1)
logit ("iconv_close() failed: %s", strerror(errno));
#ifdef HAVE_RCC
rccFree ();
#endif
}

void utf8_init ()
Expand Down Expand Up @@ -365,22 +336,6 @@ void utf8_init ()
if (iconv_desc == (iconv_t)(-1))
logit ("iconv_open() failed: %s", strerror(errno));
}
#ifdef HAVE_RCC
rcc_class classes[] = {
{ "input", RCC_CLASS_STANDARD, NULL, NULL, "Input Encoding",
0 },
{ "output", RCC_CLASS_KNOWN, NULL, NULL,
"Output Encoding", 0 },
{ NULL, 0, NULL, NULL, NULL, 0 }
};

rccInit ();
rccInitDefaultContext(NULL, 0, 0, classes, 0);
rccLoad(NULL, "moc");
rccSetOption(NULL, RCC_OPTION_TRANSLATE,
RCC_OPTION_TRANSLATE_SKIP_PARRENT);
rccSetOption(NULL, RCC_OPTION_AUTODETECT_LANGUAGE, 1);
#endif /* HAVE_RCC */

if (options_get_int ("FileNamesIconv"))
{
Expand Down
Loading

0 comments on commit 0905063

Please sign in to comment.