1 /* plugin_common - Routines common to several plugins
2 * Copyright (C) 2002 Daisuke Shimamura
4 * Almost from id3_tag.c - 2001/02/16
5 * EasyTAG - Tag editor for MP3 and OGG files
6 * Copyright (C) 2001-2002 Jerome Couderc <j.couderc@ifrance.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include "configure.h"
40 #define ID3V2_MAX_STRING_LEN 4096
41 #define NUMBER_TRACK_FORMATED 1
46 static size_t ID3Tag_Link_1 (ID3Tag *id3tag, const char *filename);
47 static size_t ID3Field_GetASCII_1 (const ID3Field *field, char *buffer, size_t maxChars, index_t itemNum);
49 static gchar *Id3tag_Genre_To_String (unsigned char genre_code);
50 static void Strip_String (gchar *string);
58 * Read id3v1.x / id3v2 tag and load data into the File_Tag structure using id3lib functions.
59 * Returns TRUE on success, else FALSE.
60 * If a tag entry exists (ex: title), we allocate memory, else value stays to NULL
62 gboolean Id3tag_Read_File_Tag (gchar *filename, File_Tag *FileTag)
65 ID3Tag *id3_tag = NULL; /* Tag defined by the id3lib */
66 gchar *string, *string1, *string2;
67 gboolean USE_CHARACTER_SET_TRANSLATION;
69 USE_CHARACTER_SET_TRANSLATION = flac_cfg.convert_char_set;
71 if (!filename || !FileTag)
74 if ( (file=fopen(filename,"r"))==NULL )
76 g_print(_("ERROR while opening file: '%s' (%s).\n\a"),filename,g_strerror(errno));
79 fclose(file); // We close it cause id3lib opens/closes file itself
82 /* Get data from tag */
83 if ( (id3_tag = ID3Tag_New()) )
89 guint field_num = 0; // First field
91 /* Link the file to the tag */
92 frm_size = ID3Tag_Link_1(id3_tag,filename);
94 string = g_malloc(ID3V2_MAX_STRING_LEN+1);
99 if ( (id3_frame = ID3Tag_FindFrameWithID(id3_tag,ID3FID_TITLE)) )
101 if ( (id3_field = ID3Frame_GetField(id3_frame,ID3FN_TEXT)) )
103 // Note: if 'num_chars' is equal to 0, then the field is empty or corrupted!
104 if ( (num_chars=ID3Field_GetASCII_1(id3_field,string,ID3V2_MAX_STRING_LEN,field_num)) > 0
107 if (USE_CHARACTER_SET_TRANSLATION)
109 string1 = convert_from_file_to_user(string);
110 Strip_String(string1);
111 FileTag->title = g_strdup(string1);
115 Strip_String(string);
116 FileTag->title = g_strdup(string);
126 if ( (id3_frame = ID3Tag_FindFrameWithID(id3_tag,ID3FID_LEADARTIST)) )
128 if ( (id3_field = ID3Frame_GetField(id3_frame,ID3FN_TEXT)) )
130 if ( (num_chars=ID3Field_GetASCII_1(id3_field,string,ID3V2_MAX_STRING_LEN,field_num)) > 0
133 if (USE_CHARACTER_SET_TRANSLATION)
135 string1 = convert_from_file_to_user(string);
136 Strip_String(string1);
137 FileTag->artist = g_strdup(string1);
141 Strip_String(string);
142 FileTag->artist = g_strdup(string);
152 if ( (id3_frame = ID3Tag_FindFrameWithID(id3_tag,ID3FID_ALBUM)) )
154 if ( (id3_field = ID3Frame_GetField(id3_frame,ID3FN_TEXT)) )
156 if ( (num_chars=ID3Field_GetASCII_1(id3_field,string,ID3V2_MAX_STRING_LEN,field_num)) > 0
159 if (USE_CHARACTER_SET_TRANSLATION)
161 string1 = convert_from_file_to_user(string);
162 Strip_String(string1);
163 FileTag->album = g_strdup(string1);
167 Strip_String(string);
168 FileTag->album = g_strdup(string);
178 if ( (id3_frame = ID3Tag_FindFrameWithID(id3_tag,ID3FID_YEAR)) )
180 if ( (id3_field = ID3Frame_GetField(id3_frame,ID3FN_TEXT)) )
182 if ( (num_chars=ID3Field_GetASCII_1(id3_field,string,ID3V2_MAX_STRING_LEN,field_num)) > 0
187 Strip_String(string);
189 /* Fix for id3lib 3.7.x: if the id3v1.x tag was filled with spaces
190 * instead of zeroes, then the year field contains garbages! */
192 while (isdigit(*tmp_str)) tmp_str++;
194 /* End of fix for id3lib 3.7.x */
196 if (USE_CHARACTER_SET_TRANSLATION)
198 string1 = convert_from_file_to_user(string);
199 Strip_String(string1);
200 FileTag->year = g_strdup(string1);
204 Strip_String(string);
205 FileTag->year = g_strdup(string);
212 /*************************
213 * Track and Total Track *
214 *************************/
215 if ( (id3_frame = ID3Tag_FindFrameWithID(id3_tag,ID3FID_TRACKNUM)) )
217 if ( (id3_field = ID3Frame_GetField(id3_frame,ID3FN_TEXT)) )
219 if ( (num_chars=ID3Field_GetASCII_1(id3_field,string,ID3V2_MAX_STRING_LEN,field_num)) > 0
223 Strip_String(string);
225 if (USE_CHARACTER_SET_TRANSLATION)
227 string1 = convert_from_file_to_user(string);
228 string2 = strchr(string1,'/');
229 if (NUMBER_TRACK_FORMATED)
233 FileTag->track_total = g_strdup_printf("%.2d",atoi(string2+1)); // Just to have numbers like this : '01', '05', '12', ...
236 FileTag->track = g_strdup_printf("%.2d",atoi(string1)); // Just to have numbers like this : '01', '05', '12', ...
241 FileTag->track_total = g_strdup(string2+1);
244 FileTag->track = g_strdup(string1);
249 string2 = strchr(string,'/');
250 if (NUMBER_TRACK_FORMATED)
254 FileTag->track_total = g_strdup_printf("%.2d",atoi(string2+1)); // Just to have numbers like this : '01', '05', '12', ...
257 FileTag->track = g_strdup_printf("%.2d",atoi(string)); // Just to have numbers like this : '01', '05', '12', ...
262 FileTag->track_total = g_strdup(string2+1);
265 FileTag->track = g_strdup(string);
276 if ( (id3_frame = ID3Tag_FindFrameWithID(id3_tag,ID3FID_CONTENTTYPE)) )
278 if ( (id3_field = ID3Frame_GetField(id3_frame,ID3FN_TEXT)) )
281 * We manipulate only the name of the genre
283 if ( (num_chars=ID3Field_GetASCII_1(id3_field,string,ID3V2_MAX_STRING_LEN,field_num)) > 0
288 Strip_String(string);
290 if ( (string[0]=='(') && (tmp=strchr(string,')')) && (strlen((tmp+1))>0) )
293 /* Convert a genre written as '(3)Dance' into 'Dance' */
294 if (USE_CHARACTER_SET_TRANSLATION)
296 string1 = convert_from_file_to_user(tmp+1);
297 FileTag->genre = g_strdup(string1);
301 FileTag->genre = g_strdup(tmp+1);
304 }else if ( (string[0]=='(') && (tmp=strchr(string,')')) )
307 /* Convert a genre written as '(3)' into 'Dance' */
309 if (USE_CHARACTER_SET_TRANSLATION)
311 string1 = convert_from_file_to_user(Id3tag_Genre_To_String(atoi(string+1)));
312 FileTag->genre = g_strdup(string1);
316 FileTag->genre = g_strdup(Id3tag_Genre_To_String(atoi(string+1)));
322 /* Genre is already written as 'Dance' */
323 if (USE_CHARACTER_SET_TRANSLATION)
325 string1 = convert_from_file_to_user(string);
326 FileTag->genre = g_strdup(string1);
330 FileTag->genre = g_strdup(string);
342 if ( (id3_frame = ID3Tag_FindFrameWithID(id3_tag,ID3FID_COMMENT)) )
344 if ( (id3_field = ID3Frame_GetField(id3_frame,ID3FN_TEXT)) )
346 if ( (num_chars=ID3Field_GetASCII_1(id3_field,string,ID3V2_MAX_STRING_LEN,field_num)) > 0
349 if (USE_CHARACTER_SET_TRANSLATION)
351 string1 = convert_from_file_to_user(string);
352 Strip_String(string1);
353 FileTag->comment = g_strdup(string1);
357 Strip_String(string);
358 FileTag->comment = g_strdup(string);
362 /*if ( (id3_field = ID3Frame_GetField(id3_frame,ID3FN_DESCRIPTION)) )
364 gchar *comment1 = g_malloc0(MAX_STRING_LEN+1);
365 num_chars = ID3Field_GetASCII(id3_field,comment1,MAX_STRING_LEN,Item_Num);
368 if ( (id3_field = ID3Frame_GetField(id3_frame,ID3FN_LANGUAGE)) )
370 gchar *comment2 = g_malloc0(MAX_STRING_LEN+1);
371 num_chars = ID3Field_GetASCII(id3_field,comment2,MAX_STRING_LEN,Item_Num);
377 /* Free allocated data */
378 ID3Tag_Delete(id3_tag);
385 * Returns the name of a genre code if found
386 * Three states for genre code :
387 * - defined (0 to GENRE_MAX)
388 * - undefined/unknown (GENRE_MAX+1 to ID3_INVALID_GENRE-1)
389 * - invalid (>ID3_INVALID_GENRE)
391 static gchar *Id3tag_Genre_To_String (unsigned char genre_code)
393 if (genre_code>=ID3_INVALID_GENRE) /* empty */
395 else if (genre_code>GENRE_MAX) /* unknown tag */
398 return id3_genres[genre_code];
404 * As the ID3Tag_Link function of id3lib-3.8.0pre2 returns the ID3v1 tags
405 * when a file has both ID3v1 and ID3v2 tags, we first try to explicitely
406 * get the ID3v2 tags with ID3Tag_LinkWithFlags and, if we cannot get them,
407 * fall back to the ID3v1 tags.
408 * (Written by Holger Schemel).
410 static size_t ID3Tag_Link_1 (ID3Tag *id3tag, const char *filename)
414 # if ( (ID3LIB_MAJOR >= 3) && (ID3LIB_MINOR >= 8) )
415 /* First, try to get the ID3v2 tags */
416 offset = ID3Tag_LinkWithFlags(id3tag,filename,ID3TT_ID3V2);
419 /* No ID3v2 tags available => try to get the ID3v1 tags */
420 offset = ID3Tag_LinkWithFlags(id3tag,filename,ID3TT_ID3V1);
423 /* Function 'ID3Tag_LinkWithFlags' is not defined up to id3lib-.3.7.13 */
424 offset = ID3Tag_Link(id3tag,filename);
426 //g_print("ID3 TAG SIZE: %d\t%s\n",offset,g_basename(filename));
432 * As the ID3Field_GetASCII function differs with the version of id3lib, we must redefine it.
434 static size_t ID3Field_GetASCII_1(const ID3Field *field, char *buffer, size_t maxChars, index_t itemNum)
437 /* Defined by id3lib: ID3LIB_MAJOR_VERSION, ID3LIB_MINOR_VERSION, ID3LIB_PATCH_VERSION
438 * Defined by autoconf: ID3LIB_MAJOR, ID3LIB_MINOR, ID3LIB_PATCH
440 * <= 3.7.12 : first item num is 1 for ID3Field_GetASCII
441 * = 3.7.13 : first item num is 0 for ID3Field_GetASCII
442 * >= 3.8.0 : doesn't need item num for ID3Field_GetASCII
444 //g_print("id3lib version: %d.%d.%d\n",ID3LIB_MAJOR,ID3LIB_MINOR,ID3LIB_PATCH);
445 # if (ID3LIB_MAJOR >= 3)
447 # if (ID3LIB_MINOR <= 7)
449 # if (ID3LIB_PATCH >= 13)
451 return ID3Field_GetASCII(field,buffer,maxChars,itemNum);
453 return ID3Field_GetASCII(field,buffer,maxChars,itemNum+1);
457 //return ID3Field_GetASCII(field,buffer,maxChars);
458 return ID3Field_GetASCIIItem(field,buffer,maxChars,itemNum);
461 // Not tested (< 3.x.x)
462 return ID3Field_GetASCII(field,buffer,maxChars,itemNum+1);
467 * Delete spaces at the end and the beginning of the string
469 static void Strip_String (gchar *string)
472 string = g_strstrip(string);