comparison src/libid3tag/file.c @ 2513:1e6d2d719876 trunk

[svn] - delete id3 tag feature has been added. - debug code in id3_render_paddedstring() which had prevented proper id3v1 rendering has been removed.
author yaz
date Tue, 13 Feb 2007 05:48:33 -0800
parents 10692383c103
children 6eb30e919b6e
comparison
equal deleted inserted replaced
2512:3a1fc6f7c187 2513:1e6d2d719876
347 clearerr(file->iofile); 347 clearerr(file->iofile);
348 #endif 348 #endif
349 349
350 /* restore seek position */ 350 /* restore seek position */
351 351
352 // if (fsetpos(file->iofile, &save_position) == -1)
353 if (vfs_fseek(file->iofile, save_position, SEEK_SET) == -1) 352 if (vfs_fseek(file->iofile, save_position, SEEK_SET) == -1)
354 return -1; 353 return -1;
355 354
356 /* set primary tag options and target padded length for convenience */ 355 /* set primary tag options and target padded length for convenience */
357 356
544 */ 543 */
545 static 544 static
546 int v1_write(struct id3_file *file, 545 int v1_write(struct id3_file *file,
547 id3_byte_t const *data, id3_length_t length) 546 id3_byte_t const *data, id3_length_t length)
548 { 547 {
549 assert(!data || length == 128); 548 // assert(!data || length == 128);
550 549
551 if (data) { 550 if (data) {
552 long location; 551 long location;
553 552
554 if (vfs_fseek(file->iofile, (file->flags & ID3_FILE_FLAG_ID3V1) ? -128 : 0, 553 if (vfs_fseek(file->iofile, (file->flags & ID3_FILE_FLAG_ID3V1) ? -128 : 0,
587 length = vfs_ftell(file->iofile); 586 length = vfs_ftell(file->iofile);
588 if (length == -1 || 587 if (length == -1 ||
589 (length >= 0 && length < 128)) 588 (length >= 0 && length < 128))
590 return -1; 589 return -1;
591 590
592 // if (ftruncate(fileno(file->iofile), length - 128) == -1) //XXX
593 if (vfs_truncate(file->iofile, length - 128) == -1) 591 if (vfs_truncate(file->iofile, length - 128) == -1)
594 return -1; 592 return -1;
595 593
596 /* delete file tag reference */ 594 /* delete file tag reference */
597 595
610 */ 608 */
611 static 609 static
612 int v2_write(struct id3_file *file, 610 int v2_write(struct id3_file *file,
613 id3_byte_t const *data, id3_length_t length) 611 id3_byte_t const *data, id3_length_t length)
614 { 612 {
615 assert(!data || length > 0); 613 // assert(!data || length > 0);
614
615 // delete tag request
616 if(!data && length == 0){
617 int file_size;
618 int remainder_size;
619 char *remainder;
620
621 /* read in the remainder of the file */
622 vfs_fseek(file->iofile, 0, SEEK_END);
623 file_size = vfs_ftell(file->iofile);
624 remainder_size = file_size - file->tags[0].location - file->tags[0].length;
625 remainder = (char*)malloc(remainder_size);
626
627 if (vfs_fseek(file->iofile, file->tags[0].location + file->tags[0].length, SEEK_SET) == -1 ||
628 vfs_fread(remainder, remainder_size, 1, file->iofile) != 1) {
629 free(remainder);
630 return -1;
631 }
632
633 /* write the remainder where the old tag was */
634 if (vfs_fseek(file->iofile, file->tags[0].location, SEEK_SET) == -1 ||
635 vfs_fwrite(remainder, remainder_size, 1, file->iofile) != 1) {
636 free(remainder);
637 return -1;
638 }
639
640 free(remainder);
641
642 /* flush the FILE */
643 #ifndef AUDACIOUS
644 if (fflush(file->iofile) == EOF)
645 return -1;
646 #endif
647 /* truncate if required */
648 if (vfs_ftell(file->iofile) < file_size)
649 vfs_truncate(file->iofile, vfs_ftell(file->iofile));
650
651 }
616 652
617 // append a new id3v2 tag to the file which doesn't have any tag or only have v1tag. 653 // append a new id3v2 tag to the file which doesn't have any tag or only have v1tag.
618 if(data && 654 if(data &&
619 ((file->ntags == 0) || // no tag 655 ((file->ntags == 0) || // no tag
620 (file->ntags == 1 && (file->flags & ID3_FILE_FLAG_ID3V1))) ) { // only v1 tag exists 656 (file->ntags == 1 && (file->flags & ID3_FILE_FLAG_ID3V1))) ) { // only v1 tag exists
667 file_size = vfs_ftell(file->iofile); 703 file_size = vfs_ftell(file->iofile);
668 remainder_size = file_size - file->tags[0].location - file->tags[0].length; 704 remainder_size = file_size - file->tags[0].location - file->tags[0].length;
669 remainder = (char*)malloc(remainder_size); 705 remainder = (char*)malloc(remainder_size);
670 if (vfs_fseek(file->iofile, file->tags[0].location + file->tags[0].length, SEEK_SET) == -1 || 706 if (vfs_fseek(file->iofile, file->tags[0].location + file->tags[0].length, SEEK_SET) == -1 ||
671 vfs_fread(remainder, remainder_size, 1, file->iofile) != 1) { 707 vfs_fread(remainder, remainder_size, 1, file->iofile) != 1) {
672 free(remainder); 708 free(remainder);
673 return -1; 709 return -1;
674 } 710 }
675 711
676 /* write the tag where the old one was */ 712 /* write the tag where the old one was */
677 if (vfs_fseek(file->iofile, file->tags[0].location, SEEK_SET) == -1 || 713 if (vfs_fseek(file->iofile, file->tags[0].location, SEEK_SET) == -1 ||
678 vfs_fwrite(data, length, 1, file->iofile) != 1) { 714 vfs_fwrite(data, length, 1, file->iofile) != 1) {
679 free(remainder); 715 free(remainder);
680 return -1; 716 return -1;
681 } 717 }
682 718
683 /* write the reaminder */ 719 /* write the reaminder */
684 if (vfs_fwrite(remainder, remainder_size, 1, file->iofile) != 1) { 720 if (vfs_fwrite(remainder, remainder_size, 1, file->iofile) != 1) {
751 } 787 }
752 } 788 }
753 789
754 /* write tags */ 790 /* write tags */
755 791
756 if (v2_write(file, id3v2, v2size) == -1 || 792 if (v2_write(file, id3v2, v2size) == -1 &&
757 v1_write(file, id3v1, v1size) == -1) 793 v1_write(file, id3v1, v1size) == -1)
758 goto fail; 794 goto fail;
759 795
760 vfs_rewind(file->iofile); 796 vfs_rewind(file->iofile);
761 797