Example #1
0
        /// <summary>
        ///    Reads a tag starting at a specified position and moves the
        ///    cursor to its start position.
        /// </summary>
        /// <param name="start">
        ///    A <see cref="long" /> value reference specifying at what
        ///    position the potential tag starts. If a tag is found,
        ///    this value will be updated to the position at which the
        ///    found tag ends.
        /// </param>
        /// <returns>
        ///    A <see cref="TagLibSharp.Tag" /> object representing the tag
        ///    found at the specified position, or <see langword="null"
        ///    /> if no tag was found.
        /// </returns>
        private TagLibSharp.Tag ReadTag(ref long start)
        {
            long     end  = start;
            TagTypes type = ReadTagInfo(ref end);

            TagLibSharp.Tag tag = null;

            try {
                switch (type)
                {
                case TagTypes.Ape:
                    tag = new Ape.Tag(file, start);
                    break;

                case TagTypes.Id3v2:
                    tag = new Id3v2.Tag(file, start);
                    break;
                }
            } catch (CorruptFileException e) {
                Console.Error.WriteLine("taglib-sharp caught exception creating tag: {0}", e);
            }

            start = end;
            return(tag);
        }
Example #2
0
        /// <summary>
        ///    Adds a tag of a specified type to the current instance,
        ///    optionally copying values from an existing type.
        /// </summary>
        /// <param name="type">
        ///    A <see cref="TagTypes" /> value specifying the type of
        ///    tag to add to the current instance. At the time of this
        ///    writing, this is limited to <see cref="TagTypes.Ape" />
        ///    and <see cref="TagTypes.Id3v2" />.
        /// </param>
        /// <param name="copy">
        ///    A <see cref="TagLibSharp.Tag" /> to copy values from using
        ///    <see cref="TagLibSharp.Tag.CopyTo" />, or <see
        ///    langword="null" /> if no tag is to be copied.
        /// </param>
        /// <returns>
        ///    The <see cref="TagLibSharp.Tag" /> object added to the current
        ///    instance, or <see langword="null" /> if it couldn't be
        ///    created.
        /// </returns>
        /// <remarks>
        ///    ID3v2 tags are added at the end of the current instance,
        ///    while other tags are added to the beginning.
        /// </remarks>
        public TagLibSharp.Tag AddTag(TagTypes type, TagLibSharp.Tag copy)
        {
            TagLibSharp.Tag tag = null;

            if (type == TagTypes.Id3v2)
            {
                tag = new Id3v2.Tag();
            }
            else if (type == TagTypes.Ape)
            {
                tag = new Ape.Tag();
                (tag as Ape.Tag).HeaderPresent = true;
            }

            if (tag != null)
            {
                if (copy != null)
                {
                    copy.CopyTo(tag, true);
                }

                AddTag(tag);
            }

            return(tag);
        }
Example #3
0
        /// <summary>
        ///    Reads a tag ending at a specified position and moves the
        ///    cursor to its start position.
        /// </summary>
        /// <param name="end">
        ///    A <see cref="long" /> value reference specifying at what
        ///    position the potential tag ends at. If a tag is found,
        ///    this value will be updated to the position at which the
        ///    found tag starts.
        /// </param>
        /// <returns>
        ///    A <see cref="TagLibSharp.Tag" /> object representing the tag
        ///    found at the specified position, or <see langword="null"
        ///    /> if no tag was found.
        /// </returns>
        private TagLibSharp.Tag ReadTag(ref long end)
        {
            long     start = end;
            TagTypes type  = ReadTagInfo(ref start);

            TagLibSharp.Tag tag = null;

            try {
                switch (type)
                {
                case TagTypes.Ape:
                    tag = new Ape.Tag(file, end - Footer.Size);
                    break;

                case TagTypes.Id3v2:
                    tag = new Id3v2.Tag(file, start);
                    break;

                case TagTypes.Id3v1:
                    tag = new Id3v1.Tag(file, start);
                    break;
                }

                end = start;
            } catch (CorruptFileException) {
            }

            return(tag);
        }
Example #4
0
        /// <summary>
        ///    Gets a tag of a specified type from the current instance,
        ///    optionally creating a new tag if possible.
        /// </summary>
        /// <param name="type">
        ///    A <see cref="TagTypes" /> value indicating the
        ///    type of tag to read.
        /// </param>
        /// <param name="create">
        ///    A <see cref="bool" /> value specifying whether or not to
        ///    try and create the tag if one is not found.
        /// </param>
        /// <returns>
        ///    A <see cref="Tag" /> object containing the tag that was
        ///    found in or added to the current instance. If no
        ///    matching tag was found and none was created, <see
        ///    langword="null" /> is returned.
        /// </returns>
        /// <remarks>
        ///    If a <see cref="Id3v2.Tag" /> is added to the
        ///    current instance, it will be placed at the start of the
        ///    file. On the other hand, <see cref="Id3v1.Tag" />
        ///    <see cref="Tag" /> will be added to the end of
        ///    the file. All other tag types will be ignored.
        /// </remarks>
        public override TagLibSharp.Tag GetTag(TagTypes type, bool create)
        {
            TagLibSharp.Tag t = (Tag as NonContainer.Tag)
                                .GetTag(type);

            if (t != null || !create)
            {
                return(t);
            }

            switch (type)
            {
            case TagTypes.Id3v1:
                return(EndTag.AddTag(type, Tag));

            case TagTypes.Id3v2:
                return(StartTag.AddTag(type, Tag));

            case TagTypes.Ape:
                return(EndTag.AddTag(type, Tag));

            default:
                return(null);
            }
        }
Example #5
0
        /// <summary>
        ///    Adds a tag of a specified type to the current instance,
        ///    optionally copying values from an existing type.
        /// </summary>
        /// <param name="type">
        ///    A <see cref="TagTypes" /> value specifying the type of
        ///    tag to add to the current instance. At the time of this
        ///    writing, this is limited to <see cref="TagTypes.Ape" />,
        ///    <see cref="TagTypes.Id3v1" />, and <see
        ///    cref="TagTypes.Id3v2" />.
        /// </param>
        /// <param name="copy">
        ///    A <see cref="TagLibSharp.Tag" /> to copy values from using
        ///    <see cref="TagLibSharp.Tag.CopyTo" />, or <see
        ///    langword="null" /> if no tag is to be copied.
        /// </param>
        /// <returns>
        ///    The <see cref="TagLibSharp.Tag" /> object added to the current
        ///    instance, or <see langword="null" /> if it couldn't be
        ///    created.
        /// </returns>
        /// <remarks>
        ///    ID3v2 tags are added at the end of the current instance,
        ///    while other tags are added to the beginning.
        /// </remarks>
        public TagLibSharp.Tag AddTag(TagTypes type, TagLibSharp.Tag copy)
        {
            TagLibSharp.Tag tag = null;

            if (type == TagTypes.Id3v1)
            {
                tag = new Id3v1.Tag();
            }
            else if (type == TagTypes.Id3v2)
            {
                Id3v2.Tag tag32 = new Id3v2.Tag();
                tag32.Version = 4;
                tag32.Flags  |= Id3v2.HeaderFlags.FooterPresent;
                tag           = tag32;
            }
            else if (type == TagTypes.Ape)
            {
                tag = new Ape.Tag();
            }

            if (tag != null)
            {
                if (copy != null)
                {
                    copy.CopyTo(tag, true);
                }

                if (type == TagTypes.Id3v1)
                {
                    AddTag(tag);
                }
                else
                {
                    InsertTag(0, tag);
                }
            }

            return(tag);
        }