Skip to content

Commit

Permalink
reduced amount of copy-pasted iconv code in junklib;
Browse files Browse the repository at this point in the history
added junk_iconv to DB_functions_t
  • Loading branch information
Oleksiy-Yakovenko committed Apr 5, 2010
1 parent eaee7c8 commit d3138fa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 141 deletions.
3 changes: 2 additions & 1 deletion deadbeef.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,8 @@ typedef struct {
int (*junk_get_leading_size_stdio) (FILE *fp);
void (*junk_copy) (DB_playItem_t *from, DB_playItem_t *first, DB_playItem_t *last);
const char * (*junk_detect_charset) (const char *s);
void (*junk_recode) (const char *in, int inlen, char *out, int outlen, const char *cs);
int (*junk_recode) (const char *in, int inlen, char *out, int outlen, const char *cs);
int (*junk_iconv) (const char *in, int inlen, char *out, int outlen, const char *cs_in, const char *cs_out);
// vfs
DB_FILE* (*fopen) (const char *fname);
void (*fclose) (DB_FILE *f);
Expand Down
157 changes: 18 additions & 139 deletions junklib.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@

#define UTF8 "utf-8"

//#define trace(...) { fprintf(stderr, __VA_ARGS__); }
#define trace(fmt,...)
#define trace(...) { fprintf(stderr, __VA_ARGS__); }
//#define trace(fmt,...)

#define min(x,y) ((x)<(y)?(x):(y))
#define max(x,y) ((x)>(y)?(x):(y))
Expand Down Expand Up @@ -312,67 +312,10 @@ can_be_russian (const signed char *str) {
return 0;
}

#if 0
static char *
convstr_id3v2_2to3 (const unsigned char* str, int sz) {
static char out[2048];
const char *enc = "iso8859-1";
char *ret = out;

// hack to add limited cp1251 recoding support
if (*str == 1) {
if (str[1] == 0xff && str[2] == 0xfe) {
enc = "UCS-2LE";
str += 2;
sz -= 2;
}
else if (str[2] == 0xff && str[1] == 0xfe) {
enc = "UCS-2BE";
str += 2;
sz -= 2;
}
else {
trace ("invalid ucs-2 signature %x %x\n", (int)str[1], (int)str[2]);
return NULL;
}
}
else {
if (can_be_russian (&str[1])) {
enc = "cp1251";
}
}
str++;
sz--;
iconv_t cd = iconv_open (UTF8, enc);
if (cd == (iconv_t)-1) {
trace ("iconv can't recoode from %s to utf8", enc);
return strdup ("-");
}
else {
size_t inbytesleft = sz;
size_t outbytesleft = 2047;
#ifdef __linux__
char *pin = (char*)str;
#else
const char *pin = str;
#endif
char *pout = out;
memset (out, 0, sizeof (out));
/*size_t res = */iconv (cd, &pin, &inbytesleft, &pout, &outbytesleft);
iconv_close (cd);
ret = out;
}
return strdup (ret);
}
#endif

static char *
convstr_id3v2 (int version, uint8_t encoding, const unsigned char* str, int sz) {
static char out[2048];
const char *enc = "iso8859-1";
char *ret = out;

// hack to add limited cp1251 recoding support

if (version == 4 && encoding == 3) {
// utf8
Expand Down Expand Up @@ -415,32 +358,20 @@ convstr_id3v2 (int version, uint8_t encoding, const unsigned char* str, int sz)
enc = "iso8859-1";
}
#endif
else {
else if (encoding == 0) {
// hack to add limited cp1251 recoding support
if (can_be_russian (str)) {
enc = "cp1251";
}
}
iconv_t cd = iconv_open (UTF8, enc);
if (cd == (iconv_t)-1) {
trace ("iconv can't recode from %s to utf8\n", enc);
return strdup ("-");
}
else {
size_t inbytesleft = sz;
size_t outbytesleft = 2047;
#ifdef __linux__
char *pin = (char*)str;
#else
const char *pin = str;
#endif
char *pout = out;
memset (out, 0, sizeof (out));
/*size_t res = */iconv (cd, &pin, &inbytesleft, &pout, &outbytesleft);
iconv_close (cd);
ret = out;
}
// trace ("decoded %s\n", out+3);
return strdup (ret);
return 0; // invalid encoding
}
if (junk_iconv (str, sz, out, sizeof (out), enc, UTF8) > 0) {
trace ("converted: %s\n", out);
return strdup (out);
}
return NULL;
}

static const char *
Expand All @@ -458,52 +389,18 @@ convstr_id3v1 (const char* str, int sz) {
}

// check for utf8 (hack)
iconv_t cd;
cd = iconv_open (UTF8, UTF8);
if (cd == (iconv_t)-1) {
trace ("iconv doesn't support utf8\n");
return str;
}
size_t inbytesleft = sz;
size_t outbytesleft = 2047;
#ifdef __linux__
char *pin = (char*)str;
#else
const char *pin = str;
#endif
char *pout = out;
memset (out, 0, sizeof (out));
size_t res = iconv (cd, &pin, &inbytesleft, &pout, &outbytesleft);
iconv_close (cd);
if (res == 0) {
strncpy (out, str, 2047);
out[min (sz, 2047)] = 0;
if (junk_iconv (str, sz, out, sizeof (out), UTF8, UTF8) > 0) {
return out;
}

const char *enc = "iso8859-1";
if (can_be_russian (str)) {
enc = "cp1251";
}
cd = iconv_open (UTF8, enc);
if (cd == (iconv_t)-1) {
trace ("iconv can't recode from %s to utf8\n", enc);
return str;
}
else {
size_t inbytesleft = sz;
size_t outbytesleft = 2047;
#ifdef __linux__
char *pin = (char*)str;
#else
const char *pin = str;
#endif
char *pout = out;
memset (out, 0, sizeof (out));
/*size_t res = */iconv (cd, &pin, &inbytesleft, &pout, &outbytesleft);
iconv_close (cd);

if (junk_iconv (str, sz, out, sizeof (out), enc, UTF8) > 0) {
return out;
}
return out;
return NULL;
}

static void
Expand Down Expand Up @@ -2849,26 +2746,8 @@ junk_detect_charset (const char *s) {
return "iso8859-1";
}

void
int
junk_recode (const char *in, int inlen, char *out, int outlen, const char *cs) {
iconv_t cd = iconv_open (UTF8, cs);
if (cd == (iconv_t)-1) {
trace ("iconv can't recode from %s to utf8\n", cs);
memcpy (out, in, min(inlen, outlen));
return;
}
else {
size_t inbytesleft = inlen;
size_t outbytesleft = outlen;
#ifdef __linux__
char *pin = (char*)in;
#else
const char *pin = in;
#endif
char *pout = out;
memset (out, 0, outlen);
size_t res = iconv (cd, &pin, &inbytesleft, &pout, &outbytesleft);
iconv_close (cd);
}
return junk_iconv (in, inlen, out, outlen, cs, UTF8);
}

5 changes: 4 additions & 1 deletion junklib.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ junk_get_leading_size (DB_FILE *fp);
const char *
junk_detect_charset (const char *s);

void
int
junk_iconv (const char *in, int inlen, char *out, int outlen, const char *cs_in, const char *cs_out);

int
junk_recode (const char *in, int inlen, char *out, int outlen, const char *cs);

#endif // __JUNKLIB_H
1 change: 1 addition & 0 deletions plugins.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ static DB_functions_t deadbeef_api = {
.junk_get_leading_size_stdio = junk_get_leading_size_stdio,
.junk_detect_charset = junk_detect_charset,
.junk_recode = junk_recode,
.junk_iconv = junk_iconv,
// vfs
.fopen = vfs_fopen,
.fclose = vfs_fclose,
Expand Down

0 comments on commit d3138fa

Please sign in to comment.