/* Set a field (by pointer) to the requested id3 value */ static void set_field(char** out, ID3Tag *tag, ID3_FieldID id, ID3_FrameID fid) { ID3Frame *frame = NULL; ID3Field *field = NULL; size_t size; char *set; frame = ID3Tag_FindFrameWithID(tag, fid); if (frame == NULL) goto end; field = ID3Frame_GetField(frame, id); if (field == NULL) goto end; size = ID3Field_Size(field); size += 1; set = malloc(size); if (!set) goto end; ID3Field_GetASCII(field, set, size); *out = g_strescape(set, NULL); free(set); end: if (!frame || !field) *out = NULL; return; }
static int _id3_find_and_set_frame(ID3Tag *tag, ID3_FrameID id, const char *value) { ID3Frame *frame; ID3Field *field; /* Remove existing tag to avoid issues with trashed frames. */ while ((frame = ID3Tag_FindFrameWithID(tag, id))) ID3Tag_RemoveFrame(tag, frame); if (value && strlen(value) > 0) { frame = ID3Frame_NewID(id); g_assert(frame); ID3Tag_AttachFrame(tag, frame); field = ID3Frame_GetField(frame, ID3FN_TEXT); if (field) ID3Field_SetASCII(field, value); //TODO: UTF8 else return 1; } return 0; }
void set_TagField (ID3Tag * myTag, char *data, ID3_FrameID id) { ID3Frame *myFrame; ID3Frame *pFrame; char *conv_str = NULL; myFrame = ID3Frame_NewID (id); pFrame = ID3Tag_FindFrameWithID (myTag, id); if (pFrame != NULL) { ID3Tag_RemoveFrame (myTag, pFrame); } conv_str = g_locale_from_utf8 (data, -1, NULL, NULL, NULL); ID3Field_SetASCII (ID3Frame_GetField (myFrame, ID3FN_TEXT), conv_str); ID3Tag_AttachFrame (myTag, myFrame); g_free (conv_str); return; }
/* get the frame from a tag. */ static ID3Frame *get_id3_frame(ID3Tag *id3tag, ID3_FrameID frameid) { ID3Frame *frame = NULL; /* first get the v2 frame. if it is not there, then get the v1 * frame. */ frame = ID3Tag_FindFrameWithID(id3tag, frameid); return frame; }
/* * 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 */ static FLAC__bool local__get_tag(const char *filename, FLAC_Plugin__CanonicalTag *tag) { FILE *file; ID3Tag *id3_tag = 0; /* Tag defined by id3lib */ char *string, *string1; FLAC__ASSERT(0 != filename); FLAC__ASSERT(0 != tag); if(0 == (file = fopen(filename, "r"))) { #ifdef DEBUG fprintf(stderr, _("ERROR while opening file: '%s' (%s).\n\a"), filename, strerror(errno)); #endif return false; } fclose(file); /* We close it cause id3lib opens/closes file itself */ /* Get data from tag */ if(0 != (id3_tag = ID3Tag_New())) { ID3Frame *id3_frame; ID3Field *id3_field; luint frm_size; luint num_chars; size_t field_num = 0; /* First field */ /* Link the file to the tag */ frm_size = local__ID3Tag_Link_wrapper(id3_tag, filename); string = malloc(ID3V2_MAX_STRING_LEN+1); /********* * Title * *********/ if(0 != (id3_frame = ID3Tag_FindFrameWithID(id3_tag, ID3FID_TITLE))) { if(0 != (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 = local__ID3Field_GetASCII_wrapper(id3_field, string, ID3V2_MAX_STRING_LEN, field_num)) > 0 && string != NULL) { local__strip(string); tag->title = strdup(string); } } } /************ * Composer * ************/ if(0 != (id3_frame = ID3Tag_FindFrameWithID(id3_tag, ID3FID_COMPOSER))) { if(0 != (id3_field = ID3Frame_GetField(id3_frame, ID3FN_TEXT))) { if((num_chars = local__ID3Field_GetASCII_wrapper(id3_field, string, ID3V2_MAX_STRING_LEN, field_num)) > 0 && string != NULL) { local__strip(string); tag->composer = strdup(string); } } } /********** * Artist * **********/ if(0 != (id3_frame = ID3Tag_FindFrameWithID(id3_tag, ID3FID_LEADARTIST))) { if(0 != (id3_field = ID3Frame_GetField(id3_frame, ID3FN_TEXT))) { if((num_chars = local__ID3Field_GetASCII_wrapper(id3_field, string, ID3V2_MAX_STRING_LEN, field_num)) > 0 && string != NULL) { local__strip(string); tag->performer = strdup(string); } } } /********* * Album * *********/ if(0 != (id3_frame = ID3Tag_FindFrameWithID(id3_tag, ID3FID_ALBUM))) { if(0 != (id3_field = ID3Frame_GetField(id3_frame, ID3FN_TEXT))) { if((num_chars = local__ID3Field_GetASCII_wrapper(id3_field, string, ID3V2_MAX_STRING_LEN, field_num)) > 0 && string != NULL) { local__strip(string); tag->album = strdup(string); } } } /******** * Year * ********/ if(0 != (id3_frame = ID3Tag_FindFrameWithID(id3_tag, ID3FID_YEAR))) { if(0 != (id3_field = ID3Frame_GetField(id3_frame, ID3FN_TEXT))) { if((num_chars = local__ID3Field_GetASCII_wrapper(id3_field, string, ID3V2_MAX_STRING_LEN, field_num)) > 0 && string != NULL) { char *tmp_str; local__strip(string); /* Fix for id3lib 3.7.x: if the id3v1.x tag was filled with spaces * instead of zeroes, then the year field contains garbages! */ tmp_str = string; while (isdigit(*tmp_str)) tmp_str++; *tmp_str = 0; /* End of fix for id3lib 3.7.x */ local__strip(string); tag->year_recorded = strdup(string); tag->year_performed = strdup(string); } } } /************************* * Track and Total Track * *************************/ if(0 != (id3_frame = ID3Tag_FindFrameWithID(id3_tag, ID3FID_TRACKNUM))) { if(0 != (id3_field = ID3Frame_GetField(id3_frame, ID3FN_TEXT))) { if((num_chars = local__ID3Field_GetASCII_wrapper(id3_field, string, ID3V2_MAX_STRING_LEN, field_num)) > 0 && string != NULL) { local__strip(string); string1 = strchr(string, '/'); if (NUMBER_TRACK_FORMATED) { if (string1) { /* Just to have numbers like this : '01', '05', '12', ... */ tag->tracks_in_album = malloc(64); sprintf(tag->tracks_in_album, "%.2d", atoi(string1+1)); *string1 = '\0'; } /* Just to have numbers like this : '01', '05', '12', ... */ tag->track_number = malloc(64); sprintf(tag->track_number, "%.2d", atoi(string)); } else { if (string1) { tag->tracks_in_album = strdup(string1+1); *string1 = '\0'; } tag->track_number = strdup(string); } } } } /********* * Genre * *********/ if(0 != (id3_frame = ID3Tag_FindFrameWithID(id3_tag, ID3FID_CONTENTTYPE))) { if(0 != (id3_field = ID3Frame_GetField(id3_frame, ID3FN_TEXT))) { /* * We manipulate only the name of the genre */ if((num_chars = local__ID3Field_GetASCII_wrapper(id3_field, string, ID3V2_MAX_STRING_LEN, field_num)) > 0 && string != NULL) { char *tmp; local__strip(string); if((string[0]=='(') && (tmp=strchr(string, ')')) && (strlen((tmp+1))>0)) { /* Convert a genre written as '(3)Dance' into 'Dance' */ tag->genre = strdup(tmp+1); } else if((string[0]=='(') && (tmp=strchr(string, ')'))) { /* Convert a genre written as '(3)' into 'Dance' */ *tmp = 0; tag->genre = strdup(local__genre_to_string((unsigned)atoi(string+1))); } else { /* Genre is already written as 'Dance' */ tag->genre = strdup(string); } } } } /*********** * Comment * ***********/ if(0 != (id3_frame = ID3Tag_FindFrameWithID(id3_tag, ID3FID_COMMENT))) { if(0 != (id3_field = ID3Frame_GetField(id3_frame, ID3FN_TEXT))) { if((num_chars = local__ID3Field_GetASCII_wrapper(id3_field, string, ID3V2_MAX_STRING_LEN, field_num)) > 0 && string != NULL) { local__strip(string); tag->comment = strdup(string); } } #if 0 if(0 != (id3_field = ID3Frame_GetField(id3_frame, ID3FN_DESCRIPTION))) { char *comment1 = calloc(MAX_STRING_LEN+1); num_chars = ID3Field_GetASCII(id3_field, comment1, MAX_STRING_LEN, Item_Num); free(comment1); } if(0 != (id3_field = ID3Frame_GetField(id3_frame, ID3FN_LANGUAGE))) { char *comment2 = calloc(MAX_STRING_LEN+1); num_chars = ID3Field_GetASCII(id3_field, comment2, MAX_STRING_LEN, Item_Num); free(comment2); } #endif } free(string); /* Free allocated data */ ID3Tag_Delete(id3_tag); } return true; }
/* Get the title from the file */ void GetID3FileTitle(char *filename, char *title, char *format) { ID3Tag *tag; ID3Frame *frame; ID3Field *field; char buffer[255]; int some_info = 0; char *in = format; char *out = title; char *bound = out + (MAX_PATH - 10 - 1); if ((tag = ID3Tag_New()) != NULL) { ID3Tag_Link(tag, filename); while (*in && out < bound) { switch (*in) { case '%': ++in; break; default: *out++ = *in++; continue; } /* handle % escape sequence */ switch (*in++) { case '0': if ((frame = ID3Tag_FindFrameWithID(tag, ID3FID_TRACKNUM)) != NULL) { int size; field = ID3Frame_GetField(frame, ID3FN_TEXT); size = ID3Field_GetASCII(field, buffer, 255, 1); lstrcpy(out, buffer); out += size; some_info = 1; } break; case '1': if ((frame = ID3Tag_FindFrameWithID(tag, ID3FID_LEADARTIST)) != NULL) { int size; field = ID3Frame_GetField(frame, ID3FN_TEXT); size = ID3Field_GetASCII(field, buffer, 255, 1); lstrcpy(out, buffer); out += size; some_info = 1; } break; case '2': if ((frame = ID3Tag_FindFrameWithID(tag, ID3FID_TITLE)) != NULL) { int size; field = ID3Frame_GetField(frame, ID3FN_TEXT); size = ID3Field_GetASCII(field, buffer, 255, 1); lstrcpy(out, buffer); out += size; some_info = 1; } break; case '3': if ((frame = ID3Tag_FindFrameWithID(tag, ID3FID_ALBUM)) != NULL) { int size; field = ID3Frame_GetField(frame, ID3FN_TEXT); size = ID3Field_GetASCII(field, buffer, 255, 1); lstrcpy(out, buffer); out += size; some_info = 1; } break; case '4': if ((frame = ID3Tag_FindFrameWithID(tag, ID3FID_YEAR)) != NULL) { int size; field = ID3Frame_GetField(frame, ID3FN_TEXT); size = ID3Field_GetASCII(field, buffer, 255, 1); lstrcpy(out, buffer); out += size; some_info = 1; } break; case '5': if ((frame = ID3Tag_FindFrameWithID(tag, ID3FID_COMMENT)) != NULL) { int size; field = ID3Frame_GetField(frame, ID3FN_TEXT); size = ID3Field_GetASCII(field, buffer, 255, 1); lstrcpy(out, buffer); out += size; some_info = 1; } break; case '6': if ((frame = ID3Tag_FindFrameWithID(tag, ID3FID_CONTENTTYPE)) != NULL) { int size; char *tmp; field = ID3Frame_GetField(frame, ID3FN_TEXT); size = ID3Field_GetASCII(field, buffer, 255, 1); tmp = GetGenre(buffer); lstrcpy(out, tmp); out += size; some_info = 1; } break; case '7': { char *p=filename+lstrlen(filename); int len = 0; while (*p != '\\' && p >= filename) { p--; len++; } lstrcpy(out, ++p); out += len; some_info = 1; break; } } } ID3Tag_Delete(tag); } if (!some_info) { char *p=filename+lstrlen(filename); while (*p != '\\' && p >= filename) p--; lstrcpy(title,++p); } }
int main( int argc, char *argv[] ) { char *filename = NULL; ID3Tag *tag; if (argc != 2) { printf("*** Usage: %s file\n", argv[0]); exit (1); } filename = argv[1]; printf("*** Reading %s\n", filename); if ((tag = ID3Tag_New()) != NULL) { ID3Frame *frame; (void) ID3Tag_Link(tag, filename); if ((frame = ID3Tag_FindFrameWithID(tag, ID3FID_TITLE)) != NULL) { ID3Field *field; if ((field = ID3Frame_GetField(frame, ID3FN_TEXT)) != NULL) { char title[1024]; (void) ID3Field_GetASCII(field, title, 1024); printf("Title: %s\n", title); } else { printf("Didn't get the field\n"); } } else { printf("Didn't get the frame\n"); } if ((frame = ID3Tag_FindFrameWithID(tag, ID3FID_LEADARTIST)) != NULL) { ID3Field *field; if ((field = ID3Frame_GetField(frame, ID3FN_TEXT)) != NULL) { char artist[1024]; (void) ID3Field_GetASCII(field, artist, 1024); printf("Artist: %s\n", artist); } else { printf("Didn't get the field\n"); } } else { printf("Didn't get the frame\n"); } if ((frame = ID3Tag_FindFrameWithID(tag, ID3FID_ALBUM)) != NULL) { ID3Field *field; if ((field = ID3Frame_GetField(frame, ID3FN_TEXT)) != NULL) { char album[1024]; (void) ID3Field_GetASCII(field, album, 1024); printf("Album: %s\n", album); } else { printf("Didn't get the field\n"); } } else { printf("Didn't get the frame\n"); } } else { printf("Didn't get the tag\n"); } exit (0); }