Commit 162b8a36 authored by Josh Coalson's avatar Josh Coalson
Browse files

initial import

parent fe9d2b4e
/* plugin_common - Routines common to several plugins
* Copyright (C) 2002 Josh Coalson
*
* Only slightly modified charset.c from:
* EasyTAG - Tag editor for MP3 and OGG files
* Copyright (C) 1999-2001 Hvard Kvlen <havardk@xmms.org>
*
* 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.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_ICONV
#include <iconv.h>
#endif
#ifdef HAVE_LANGINFO_CODESET
#include <langinfo.h>
#endif
#include "charset.h"
/*************
* Functions *
*************/
char* FLAC_plugin__charset_get_current (void)
{
char *charset = getenv("CHARSET");
#ifdef HAVE_LANGINFO_CODESET
if (!charset)
charset = nl_langinfo(CODESET);
#endif
if (!charset)
charset = "ISO-8859-1";
return charset;
}
#ifdef HAVE_ICONV
char* FLAC_plugin__charset_convert_string (const char *string, char *from, char *to)
{
size_t outleft, outsize, length;
iconv_t cd;
char *out, *outptr;
const char *input = string;
if (!string)
return NULL;
length = strlen(string);
if ((cd = iconv_open(to, from)) == (iconv_t)-1)
{
#ifdef DEBUG
fprintf(stderr, "convert_string(): Conversion not supported. Charsets: %s -> %s", from, to);
#endif
return strdup(string);
}
/* Due to a GLIBC bug, round outbuf_size up to a multiple of 4 */
/* + 1 for nul in case len == 1 */
outsize = ((length + 3) & ~3) + 1;
out = malloc(outsize);
outleft = outsize - 1;
outptr = out;
retry:
if (iconv(cd, &input, &length, &outptr, &outleft) == -1)
{
int used;
switch (errno)
{
case E2BIG:
used = outptr - out;
outsize = (outsize - 1) * 2 + 1;
out = realloc(out, outsize);
outptr = out + used;
outleft = outsize - 1 - used;
goto retry;
case EINVAL:
break;
case EILSEQ:
/* Invalid sequence, try to get the rest of the string */
input++;
length = strlen(input);
goto retry;
default:
#ifdef DEBUG
fprintf(stderr, "convert_string(): Conversion failed. Inputstring: %s; Error: %s", string, strerror(errno));
#endif
break;
}
}
*outptr = '\0';
iconv_close(cd);
return out;
}
#else
char* FLAC_plugin__charset_convert_string (const char *string, char *from, char *to)
{
(void)from, (void)to;
if (!string)
return NULL;
return strdup(string);
}
#endif
#ifdef HAVE_ICONV
int FLAC_plugin__charset_test_conversion (char *from, char *to)
{
iconv_t cd;
if ((cd=iconv_open(to,from)) == (iconv_t)-1)
{
/* Conversion not supported */
return 0;
}
iconv_close(cd);
return ;
}
#else
int FLAC_plugin__charset_test_conversion (char *from, char *to)
{
(void)from, (void)to;
return 1;
}
#endif
/* plugin_common - Routines common to several plugins
* Copyright (C) 2002 Josh Coalson
*
* Only slightly modified charset.h from:
* charset.h - 2001/12/04
* EasyTAG - Tag editor for MP3 and OGG files
* Copyright (C) 1999-2001 H蛆ard Kv虱en <havardk@xmms.org>
*
* 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.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef FLAC__PLUGIN_COMMON__CHARSET_H
#define FLAC__PLUGIN_COMMON__CHARSET_H
/**************
* Prototypes *
**************/
char* FLAC_plugin__charset_get_current (void);
char* FLAC_plugin__charset_convert_string (const char *string, char *from, char *to);
/* returns 1 for success, 0 for failure or no iconv */
int FLAC_plugin__charset_test_conversion (char *from, char *to);
#endif
/* plugin_common - Routines common to several plugins
* Copyright (C) 2002 Josh Coalson
*
* 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.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include "FLAC/assert.h"
#include "id3v1.h"
#include "locale_hack.h"
/*
* Do not sort genres!!
* Last Update: 2000/04/30
*/
static const char * const FLAC_plugin__id3v1_tag_genre_table[] =
{
"Blues", /* 0 */
"Classic Rock",
"Country",
"Dance",
"Disco",
"Funk", /* 5 */
"Grunge",
"Hip-Hop",
"Jazz",
"Metal",
"New Age", /* 10 */
"Oldies",
"Other",
"Pop",
"R&B",
"Rap", /* 15 */
"Reggae",
"Rock",
"Techno",
"Industrial",
"Alternative", /* 20 */
"Ska",
"Death Metal",
"Pranks",
"Soundtrack",
"Euro-Techno", /* 25 */
"Ambient",
"Trip-Hop",
"Vocal",
"Jazz+Funk",
"Fusion", /* 30 */
"Trance",
"Classical",
"Instrumental",
"Acid",
"House", /* 35 */
"Game",
"Sound Clip",
"Gospel",
"Noise",
"Altern Rock", /* 40 */
"Bass",
"Soul",
"Punk",
"Space",
"Meditative", /* 45 */
"Instrumental Pop",
"Instrumental Rock",
"Ethnic",
"Gothic",
"Darkwave", /* 50 */
"Techno-Industrial",
"Electronic",
"Pop-Folk",
"Eurodance",
"Dream", /* 55 */
"Southern Rock",
"Comedy",
"Cult",
"Gangsta",
"Top 40", /* 60 */
"Christian Rap",
"Pop/Funk",
"Jungle",
"Native American",
"Cabaret", /* 65 */
"New Wave",
"Psychadelic",
"Rave",
"Showtunes",
"Trailer", /* 70 */
"Lo-Fi",
"Tribal",
"Acid Punk",
"Acid Jazz",
"Polka", /* 75 */
"Retro",
"Musical",
"Rock & Roll",
"Hard Rock",
"Folk", /* 80 */
"Folk/Rock",
"National Folk",
"Fast Fusion",
"Swing",
"Bebob", /* 85 */
"Latin",
"Revival",
"Celtic",
"Bluegrass",
"Avantgarde", /* 90 */
"Gothic Rock",
"Progressive Rock",
"Psychedelic Rock",
"Symphonic Rock",
"Slow Rock", /* 95 */
"Big Band",
"Chorus",
"Easy Listening",
"Acoustic",
"Humour", /* 100 */
"Speech",
"Chanson",
"Opera",
"Chamber Music",
"Sonata", /* 105 */
"Symphony",
"Booty Bass",
"Primus",
"Porn Groove",
"Satire", /* 110 */
"Slow Jam",
"Club",
"Tango",
"Samba",
"Folklore", /* 115 */
"Ballad",
"Power Ballad",
"Rhythmic Soul",
"Freestyle",
"Duet", /* 120 */
"Punk Rock",
"Drum Solo",
"A Capella",
"Euro-House",
"Dance Hall", /* 125 */
"Goa",
"Drum & Bass",
"Club-House",
"Hardcore",
"Terror", /* 130 */
"Indie",
"BritPop",
"Negerpunk",
"Polsk Punk",
"Beat", /* 135 */
"Christian Gangsta Rap",
"Heavy Metal",
"Black Metal",
"Crossover",
"Contemporary Christian",/* 140 */
"Christian Rock",
"Merengue",
"Salsa",
"Thrash Metal",
"Anime", /* 145 */
"JPop",
"Synthpop"
};
FLAC__bool FLAC_plugin__id3v1_tag_get(const char *filename, FLAC_Plugin__Id3v1_Tag *tag)
{
char raw[128];
FILE *f;
FLAC__ASSERT(0 != filename);
FLAC__ASSERT(0 != tag);
memset(tag, 0, sizeof(FLAC_Plugin__Id3v1_Tag));
if(0 == (f = fopen(filename, "rb")))
return false;
if(-1 == fseek(f, -128, SEEK_END)) {
fclose(f);
return false;
}
if(fread(raw, 1, 128, f) < 128) {
fclose(f);
return false;
}
fclose(f);
if(strncmp(raw, "TAG", 3))
return false;
else {
memcpy(tag->tag, raw, 3);
memcpy(tag->title, raw+3, 30);
memcpy(tag->artist, raw+33, 30);
memcpy(tag->album, raw+63, 30);
memcpy(tag->year, raw+93, 4);
memcpy(tag->comment.v1_0.comment, raw+97, 30);
tag->genre = raw[127];
return true;
}
}
const char *FLAC_plugin__id3v1_tag_get_genre_as_string(unsigned char genre_code)
{
if (genre_code < FLAC_plugin__id3v1_tag_genre_table_max())
return gettext(FLAC_plugin__id3v1_tag_genre_table[genre_code]);
return "";
}
unsigned FLAC_plugin__id3v1_tag_genre_table_max()
{
return sizeof(FLAC_plugin__id3v1_tag_genre_table) / sizeof(FLAC_plugin__id3v1_tag_genre_table[0]) - 1;
}
/* plugin_common - Routines common to several plugins
* Copyright (C) 2002 Josh Coalson
*
* 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.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef FLAC__PLUGIN_COMMON__ID3V1_H
#define FLAC__PLUGIN_COMMON__ID3V1_H
#include <string.h>
#include "FLAC/ordinals.h"
typedef struct {
char tag[3];
char title[30];
char artist[30];
char album[30];
char year[4];
union {
struct {
char comment[30];
} v1_0;
struct {
char comment[28];
char zero;
unsigned char track;
} v1_1;
} comment;
unsigned char genre;
} FLAC_Plugin__Id3v1_Tag;
FLAC__bool FLAC_plugin__id3v1_tag_get(const char *filename, FLAC_Plugin__Id3v1_Tag *tag);
#define ID3_INVALID_GENRE 255
const char *FLAC_plugin__id3v1_tag_get_genre_as_string(unsigned char genre_code);
unsigned FLAC_plugin__id3v1_tag_genre_table_max();
#endif
/* plugin_common - Routines common to several plugins
* Copyright (C) 2002 Daisuke Shimamura
*
* Almost from id3_tag.c - 2001/02/16
* EasyTAG - Tag editor for MP3 and OGG files
* Copyright (C) 2001-2002 Jerome Couderc <j.couderc@ifrance.com>
*
* 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.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <id3.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include "configure.h"
#include "genres.h"
#include "charset.h"
#include "mylocale.h"
#include "id3_tag.h"
/****************
* Declarations *
****************/
#define ID3V2_MAX_STRING_LEN 4096
#define NUMBER_TRACK_FORMATED 1
/**************
* Prototypes *
**************/
static size_t ID3Tag_Link_1 (ID3Tag *id3tag, const char *filename);
static size_t ID3Field_GetASCII_1 (const ID3Field *field, char *buffer, size_t maxChars, index_t itemNum);
static gchar *Id3tag_Genre_To_String (unsigned char genre_code);
static void Strip_String (gchar *string);
@@@@
/*************
* Functions *
*************/
/*
* Read id3v1.x / id3v2 tag and load data into the File_Tag structure using id3lib functions.
* Returns TRUE on success, else FALSE.
* If a tag entry exists (ex: title), we allocate memory, else value stays to NULL
*/
gboolean Id3tag_Read_File_Tag (gchar *filename, File_Tag *FileTag)
{
FILE *file;
ID3Tag *id3_tag = NULL; /* Tag defined by the id3lib */
gchar *string, *string1, *string2;
gboolean USE_CHARACTER_SET_TRANSLATION;
USE_CHARACTER_SET_TRANSLATION = flac_cfg.convert_char_set;
if (!filename || !FileTag)
return FALSE;
if ( (file=fopen(filename,"r"))==NULL )
{
g_print(_("ERROR while opening file: '%s' (%s).\n\a"),filename,g_strerror(errno));
return FALSE;
}
fclose(file); // We close it cause id3lib opens/closes file itself
/* Get data from tag */
if ( (id3_tag = ID3Tag_New()) )
{
ID3Frame *id3_frame;
ID3Field *id3_field;
luint frm_size;
luint num_chars;
guint field_num = 0; // First field
/* Link the file to the tag */
frm_size = ID3Tag_Link_1(id3_tag,filename);
string = g_malloc(ID3V2_MAX_STRING_LEN+1);
/*********
* Title *
*********/
if ( (id3_frame = ID3Tag_FindFrameWithID(id3_tag,ID3FID_TITLE)) )
{
if ( (id3_field = ID3Frame_GetField(id3_frame,ID3FN_TEXT)) )
{
// Note: if 'num_chars' is equal to 0, then the field is empty or corrupted!
if ( (num_chars=ID3Field_GetASCII_1(id3_field,string,ID3V2_MAX_STRING_LEN,field_num)) > 0
&& string != NULL )
{
if (USE_CHARACTER_SET_TRANSLATION)
{
string1 = convert_from_file_to_user(string);
Strip_String(string1);
FileTag->title = g_strdup(string1);
g_free(string1);
}else
{
Strip_String(string);
FileTag->title = g_strdup(string);
}
}
}
}
/**********
* Artist *
**********/
if ( (id3_frame = ID3Tag_FindFrameWithID(id3_tag,ID3FID_LEADARTIST)) )
{
if ( (id3_field = ID3Frame_GetField(id3_frame,ID3FN_TEXT)) )
{
if ( (num_chars=ID3Field_GetASCII_1(id3_field,string,ID3V2_MAX_STRING_LEN,field_num)) > 0
&& string != NULL )
{
if (USE_CHARACTER_SET_TRANSLATION)
{
string1 = convert_from_file_to_user(string);
Strip_String(string1);
FileTag->artist = g_strdup(string1);
g_free(string1);
}else
{
Strip_String(string);
FileTag->artist = g_strdup(string);
}
}
}
}
/*********
* Album *
*********/
if ( (id3_frame = ID3Tag_FindFrameWithID(id3_tag,ID3FID_ALBUM)) )
{
if ( (id3_field = ID3Frame_GetField(id3_frame,ID3FN_TEXT)) )
{
if ( (num_chars=ID3Field_GetASCII_1(id3_field,string,ID3V2_MAX_STRING_LEN,field_num)) > 0
&& string != NULL )
{
if (USE_CHARACTER_SET_TRANSLATION)
{
string1 = convert_from_file_to_user(string);