Example #1
0
        public void Save(TagTypes types, bool stripOthers)
        {
            if (types == TagTypes.None && stripOthers)
            {
                if (!Strip(TagTypes.AllTags))
                {
                    throw new TagLibException(TagLibError.MpegCouldNotStripTags);
                }

                return;
            }

            if (id3v2_tag == null && id3v1_tag == null && ape_tag == null)
            {
                if (stripOthers)
                {
                    if (!Strip(TagTypes.AllTags))
                    {
                        throw new TagLibException(TagLibError.MpegCouldNotStripTags);
                    }
                }

                return;
            }

            if (IsReadOnly)
            {
                throw new ReadOnlyException();
            }

            Mode = FileAccessMode.Write;

            // Create the tags if we've been asked to.  Copy the values from the tag that
            // does exist into the new tag.

            if ((types & TagTypes.Id3v2) != 0 && id3v1_tag != null)
            {
                TagLib.Tag.Duplicate(id3v1_tag, FindTag(TagTypes.Id3v2, true), false);
            }

            if ((types & TagTypes.Id3v1) != 0 && id3v2_tag != null)
            {
                TagLib.Tag.Duplicate(id3v2_tag, FindTag(TagTypes.Id3v1, true), false);
            }

            bool success = true;

            if ((TagTypes.Id3v2 & types) != 0 && id3v2_tag != null && !id3v2_tag.IsEmpty)
            {
                long id3v2_location = FindId3v2();
                int  id3v2_size     = 0;

                if (id3v2_location < 0)
                {
                    id3v2_location = 0;
                }
                else
                {
                    Seek(id3v2_location);
                    Id3v2Header header = new Id3v2Header(ReadBlock((int)Id3v2Header.Size));

                    if (header.TagSize == 0)
                    {
                        TagLibDebugger.Debug("Mpc.File.Save() -- Id3v2 header is broken. Ignoring.");
                    }
                    else
                    {
                        id3v2_size = (int)header.CompleteTagSize;
                    }
                }

                Insert(id3v2_tag.Render(), id3v2_location, id3v2_size);
            }
            else if (stripOthers)
            {
                success = Strip(TagTypes.Id3v2) && success;
            }

            if ((TagTypes.Id3v1 & types) != 0 && id3v1_tag != null && !id3v1_tag.IsEmpty)
            {
                long id3v1_location = FindId3v1();
                if (id3v1_location < 0)
                {
                    Seek(0, System.IO.SeekOrigin.End);
                }
                else
                {
                    Seek(id3v1_location);
                }

                WriteBlock(id3v1_tag.Render());
            }
            else if (stripOthers)
            {
                success = (Strip(TagTypes.Id3v1, false) && success);
            }

            // Dont save an APE-tag unless one has been created
            if ((TagTypes.Ape & types) != 0 && ape_tag != null && !ape_tag.IsEmpty)
            {
                long ape_location = FindApe(FindId3v1() >= 0);
                long ape_size     = 0;

                if (ape_location < 0)
                {
                    ape_location = Length;
                }
                else
                {
                    Seek(ape_location);
                    ape_size     = (new ApeFooter(ReadBlock((int)ApeFooter.Size))).CompleteTagSize;
                    ape_location = ape_location + ApeFooter.Size - ape_size;
                }

                Insert(ape_tag.Render(), ape_location, ape_size);
            }
            else if (stripOthers)
            {
                success = (Strip(TagTypes.Ape, false) && success);
            }

            Mode = FileAccessMode.Closed;

            if (!success)
            {
                throw new TagLibException(TagLibError.MpegCouldNotWriteTags);
            }
        }
Example #2
0
        public override void Save()
        {
            if (IsReadOnly)
            {
                throw new ReadOnlyException();
            }

            Mode = FileAccessMode.Write;

            // Update ID3v2 tag
            long id3v2_location = FindId3v2();
            int  id3v2_size     = 0;

            if (id3v2_location != -1)
            {
                Seek(id3v2_location);
                Id3v2Header header = new Id3v2Header(ReadBlock((int)Id3v2Header.Size));
                if (header.TagSize == 0)
                {
                    TagLibDebugger.Debug("Mpc.File.Save() -- Id3v2 header is broken. Ignoring.");
                    id3v2_location = -1;
                }
                else
                {
                    id3v2_size = (int)header.CompleteTagSize;
                }
            }

            if (id3v2Tag != null)
            {
                if (id3v2_location >= 0)
                {
                    Insert(id3v2Tag.Render(), id3v2_location, id3v2_size);
                }
                else
                {
                    Insert(id3v2Tag.Render(), 0, 0);
                }
            }
            else if (id3v2_location >= 0)
            {
                RemoveBlock(id3v2_location, id3v2_size);
            }


            // Update ID3v1 tag
            long id3v1_location = FindId3v1();

            if (id3v1Tag != null)
            {
                if (id3v1_location >= 0)
                {
                    Insert(id3v1Tag.Render(), id3v1_location, 128);
                }
                else
                {
                    Seek(0, System.IO.SeekOrigin.End);
                    id3v1_location = Tell;
                    WriteBlock(id3v1Tag.Render());
                }
            }
            else if (id3v1_location >= 0)
            {
                RemoveBlock(id3v1_location, 128);
                id3v1_location = -1;
            }


            // Update APE tag
            long ape_location = FindApe(id3v1_location != -1);
            long ape_size     = 0;

            if (ape_location >= 0)
            {
                Seek(ape_location);
                ape_size     = (new ApeFooter(ReadBlock((int)ApeFooter.Size))).CompleteTagSize;
                ape_location = ape_location + ApeFooter.Size - ape_size;
            }

            if (apeTag != null)
            {
                if (ape_location >= 0)
                {
                    Insert(apeTag.Render(), ape_location, ape_size);
                }
                else
                {
                    if (id3v1_location >= 0)
                    {
                        Insert(apeTag.Render(), id3v1_location, 0);
                    }
                    else
                    {
                        Seek(0, System.IO.SeekOrigin.End);
                        WriteBlock(apeTag.Render());
                    }
                }
            }
            else if (ape_location >= 0)
            {
                RemoveBlock(ape_location, ape_size);
            }

            Mode = FileAccessMode.Closed;
        }
Example #3
0
        public override void Save()
        {
            if (IsReadOnly)
            {
                throw new ReadOnlyException();
            }

            Mode = FileAccessMode.Write;

            // Update ID3v1 tag
            long id3v1_location = FindId3v1();

            if (id3v1Tag != null)
            {
                if (id3v1_location >= 0)
                {
                    Insert(id3v1Tag.Render(), id3v1_location, 128);
                }
                else
                {
                    Seek(0, System.IO.SeekOrigin.End);
                    id3v1_location = Tell;
                    WriteBlock(id3v1Tag.Render());
                }
            }
            else if (id3v1_location >= 0)
            {
                RemoveBlock(id3v1_location, 128);
                id3v1_location = -1;
            }


            // Update APE tag
            long ape_location = FindApe(id3v1_location != -1);
            long ape_size     = 0;

            if (ape_location != -1)
            {
                Seek(ape_location);
                ape_size     = (new ApeFooter(ReadBlock((int)ApeFooter.Size))).CompleteTagSize;
                ape_location = ape_location + ApeFooter.Size - ape_size;
            }

            if (apeTag != null)
            {
                if (ape_location >= 0)
                {
                    Insert(apeTag.Render(), ape_location, ape_size);
                }
                else
                {
                    if (id3v1_location >= 0)
                    {
                        Insert(apeTag.Render(), id3v1_location, 0);
                    }
                    else
                    {
                        Seek(0, System.IO.SeekOrigin.End);
                        WriteBlock(apeTag.Render());
                    }
                }
            }
            else if (ape_location >= 0)
            {
                RemoveBlock(ape_location, ape_size);
            }

            Mode = FileAccessMode.Closed;
        }