Tag Parser  9.1.3
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
id3v2tag.h
Go to the documentation of this file.
1 #ifndef TAG_PARSER_ID3V2TAG_H
2 #define TAG_PARSER_ID3V2TAG_H
3 
4 #include "./id3v2frame.h"
5 
6 #include "../fieldbasedtag.h"
7 
8 #include <map>
9 
10 namespace TagParser {
11 
12 class Id3v2Tag;
13 
15  bool operator()(std::uint32_t lhs, std::uint32_t rhs) const;
16 };
17 
19  friend class Id3v2Tag;
20 
21 public:
22  void make(std::ostream &stream, std::uint32_t padding, Diagnostics &diag);
23  const Id3v2Tag &tag() const;
24  std::uint64_t requiredSize() const;
25 
26 private:
27  Id3v2TagMaker(Id3v2Tag &tag, Diagnostics &diag);
28 
29  Id3v2Tag &m_tag;
30  std::uint32_t m_framesSize;
31  std::uint32_t m_requiredSize;
32  std::vector<Id3v2FrameMaker> m_maker;
33 };
34 
38 inline const Id3v2Tag &Id3v2TagMaker::tag() const
39 {
40  return m_tag;
41 }
42 
47 inline std::uint64_t Id3v2TagMaker::requiredSize() const
48 {
49  return m_requiredSize;
50 }
51 
56 public:
59 };
60 
61 class TAG_PARSER_EXPORT Id3v2Tag : public FieldMapBasedTag<Id3v2Tag> {
62  friend class FieldMapBasedTag<Id3v2Tag>;
63 
64 public:
65  Id3v2Tag();
66 
67  static constexpr TagType tagType = TagType::Id3v2Tag;
68  static constexpr const char *tagName = "ID3v2 tag";
69  static constexpr TagTextEncoding defaultTextEncoding = TagTextEncoding::Utf16LittleEndian;
70  TagTextEncoding proposedTextEncoding() const override;
71  bool canEncodingBeUsed(TagTextEncoding encoding) const override;
72  bool supportsDescription(KnownField field) const override;
73  bool supportsMimeType(KnownField field) const override;
74  bool supportsMultipleValues(KnownField field) const override;
75  void ensureTextValuesAreProperlyEncoded() override;
76 
77  void parse(std::istream &sourceStream, const std::uint64_t maximalSize, Diagnostics &diag);
78  Id3v2TagMaker prepareMaking(Diagnostics &diag);
79  void make(std::ostream &targetStream, std::uint32_t padding, Diagnostics &diag);
80 
81  std::uint8_t majorVersion() const;
82  std::uint8_t revisionVersion() const;
83  void setVersion(std::uint8_t majorVersion, std::uint8_t revisionVersion);
84  bool isVersionSupported() const;
85  std::uint8_t flags() const;
86  bool isUnsynchronisationUsed() const;
87  bool hasExtendedHeader() const;
88  bool isExperimental() const;
89  bool hasFooter() const;
90  std::uint32_t extendedHeaderSize() const;
91  std::uint32_t paddingSize() const;
92 
93 protected:
94  IdentifierType internallyGetFieldId(KnownField field) const;
95  KnownField internallyGetKnownField(const IdentifierType &id) const;
96  TagDataType internallyGetProposedDataType(const std::uint32_t &id) const;
97  std::vector<const TagValue *> internallyGetValues(const IdentifierType &id) const;
98  bool internallySetValues(const IdentifierType &id, const std::vector<TagValue> &values);
99 
100 private:
101  std::uint8_t m_majorVersion;
102  std::uint8_t m_revisionVersion;
103  std::uint8_t m_flags;
104  std::uint32_t m_sizeExcludingHeader;
105  std::uint32_t m_extendedHeaderSize;
106  std::uint32_t m_paddingSize;
107 };
108 
113  : m_majorVersion(4)
114  , m_revisionVersion(0)
115  , m_flags(0)
116  , m_sizeExcludingHeader(0)
117  , m_extendedHeaderSize(0)
118  , m_paddingSize(0)
119 {
120 }
121 
123 {
124  return m_majorVersion > 3 ? TagTextEncoding::Utf8 : TagTextEncoding::Utf16LittleEndian;
125 }
126 
127 inline bool Id3v2Tag::canEncodingBeUsed(TagTextEncoding encoding) const
128 {
129  return encoding == TagTextEncoding::Latin1 || (encoding == TagTextEncoding::Utf8 && m_majorVersion > 3)
131 }
132 
134 {
135  switch (field) {
136  case KnownField::Cover:
137  case KnownField::Lyrics:
139  return true;
140  default:
141  return false;
142  }
143 }
144 
145 inline bool Id3v2Tag::supportsMimeType(KnownField field) const
146 {
147  return field == KnownField::Cover;
148 }
149 
153 inline std::uint8_t Id3v2Tag::majorVersion() const
154 {
155  return m_majorVersion;
156 }
157 
161 inline std::uint8_t Id3v2Tag::revisionVersion() const
162 {
163  return m_revisionVersion;
164 }
165 
172 inline bool Id3v2Tag::isVersionSupported() const
173 {
174  return m_majorVersion == 2 || m_majorVersion == 3 || m_majorVersion == 4;
175 }
176 
180 inline std::uint8_t Id3v2Tag::flags() const
181 {
182  return m_flags;
183 }
184 
189 {
190  return m_flags & 0x80;
191 }
192 
196 inline bool Id3v2Tag::hasExtendedHeader() const
197 {
198  return (m_majorVersion >= 3) && (m_flags & 0x40);
199 }
200 
204 inline bool Id3v2Tag::isExperimental() const
205 {
206  return (m_majorVersion >= 3) && (m_flags & 0x20);
207 }
208 
212 inline bool Id3v2Tag::hasFooter() const
213 {
214  return (m_majorVersion >= 3) && (m_flags & 0x10);
215 }
216 
220 inline std::uint32_t Id3v2Tag::extendedHeaderSize() const
221 {
222  return m_extendedHeaderSize;
223 }
224 
229 inline std::uint32_t Id3v2Tag::paddingSize() const
230 {
231  return m_paddingSize;
232 }
233 
234 } // namespace TagParser
235 
236 #endif // TAG_PARSER_ID3V2TAG_H
TagParser::Id3v2Tag::isVersionSupported
bool isVersionSupported() const
Returns an indication whether the version is supported by the Id3v2Tag class.
Definition: id3v2tag.h:172
TagParser::Id3v2Tag::hasExtendedHeader
bool hasExtendedHeader() const
Returns an indication whether an extended header is used.
Definition: id3v2tag.h:196
TagParser::TagTextEncoding::Utf8
@ Utf8
TagParser::Id3v2Frame
The Id3v2Frame class is used by Id3v2Tag to store the fields.
Definition: id3v2frame.h:86
TagParser::Id3v2Tag::isExperimental
bool isExperimental() const
Returns an indication whether the tag is labeled as experimental.
Definition: id3v2tag.h:204
TagParser::TagTextEncoding
TagTextEncoding
Specifies the text encoding.
Definition: tagvalue.h:25
TagParser::Id3v2Tag::flags
std::uint8_t flags() const
Returns the flags read from the ID3v2 header.
Definition: id3v2tag.h:180
TagParser::TagTextEncoding::Utf16BigEndian
@ Utf16BigEndian
TagParser::Id3v2Tag::hasFooter
bool hasFooter() const
Returns an indication whether a footer is present.
Definition: id3v2tag.h:212
TagParser::KnownField::SynchronizedLyrics
@ SynchronizedLyrics
TagParser::Diagnostics
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:156
TagParser::Id3v2Tag::supportsDescription
bool supportsDescription(KnownField field) const override
Returns an indications whether the specified field supports descriptions.
Definition: id3v2tag.h:133
TagParser::KnownField::Lyrics
@ Lyrics
TagParser
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:10
TagParser::TagDataType
TagDataType
Specifies the data type.
Definition: tagvalue.h:54
TagParser::Id3v2Tag::extendedHeaderSize
std::uint32_t extendedHeaderSize() const
Returns the size of the extended header if one is present; otherwise returns 0.
Definition: id3v2tag.h:220
TagParser::Id3v2Tag::isUnsynchronisationUsed
bool isUnsynchronisationUsed() const
Returns an indication whether unsynchronisation is used.
Definition: id3v2tag.h:188
TagParser::Id3v2Tag::majorVersion
std::uint8_t majorVersion() const
Returns the major version if known; otherwise returns 0.
Definition: id3v2tag.h:153
TagParser::KnownField::Cover
@ Cover
TagParser::TagTextEncoding::Latin1
@ Latin1
TagParser::Id3v2Tag::canEncodingBeUsed
bool canEncodingBeUsed(TagTextEncoding encoding) const override
Returns an indication whether the specified encoding can be used to provide string values for the tag...
Definition: id3v2tag.h:127
TagParser::Id3v2Tag::proposedTextEncoding
TagTextEncoding proposedTextEncoding() const override
Returns the proposed text encoding.
Definition: id3v2tag.h:122
TagParser::Id3v2Tag
Implementation of TagParser::Tag for ID3v2 tags.
Definition: id3v2tag.h:61
TagParser::KnownField
KnownField
Specifies the field.
Definition: tag.h:42
TagParser::Id3v2Tag::Id3v2Tag
Id3v2Tag()
Constructs a new tag.
Definition: id3v2tag.h:112
TagParser::FieldMapBasedTag
The FieldMapBasedTag provides a generic implementation of Tag which stores the tag fields using std::...
Definition: fieldbasedtag.h:31
TagParser::TagTextEncoding::Utf16LittleEndian
@ Utf16LittleEndian
TagParser::TagType::Id3v2Tag
@ Id3v2Tag
TAG_PARSER_EXPORT
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
TagParser::Id3v2Tag::revisionVersion
std::uint8_t revisionVersion() const
Returns the revision version if known; otherwise returns 0.
Definition: id3v2tag.h:161
TagParser::FrameComparer
Defines the order which is used to store ID3v2 frames.
Definition: id3v2tag.h:14
TagParser::Id3v2TagMaker::requiredSize
std::uint64_t requiredSize() const
Returns the number of bytes which will be written when making the tag.
Definition: id3v2tag.h:47
TagParser::FieldMapBasedTagTraits
Defines traits for the specified ImplementationType.
Definition: fieldbasedtag.h:17
id3v2frame.h
TagParser::Id3v2Tag::paddingSize
std::uint32_t paddingSize() const
Returns the size of the padding between the tag and the first MPEG frame if one is present; otherwise...
Definition: id3v2tag.h:229
TagParser::Id3v2Tag::supportsMimeType
bool supportsMimeType(KnownField field) const override
Returns an indications whether the specified field supports mime types.
Definition: id3v2tag.h:145
TagParser::Id3v2TagMaker
The Id3v2TagMaker class helps writing ID3v2 tags.
Definition: id3v2tag.h:18
TagParser::TagType
TagType
Specifies the tag type.
Definition: tag.h:20
TagParser::Id3v2TagMaker::tag
const Id3v2Tag & tag() const
Returns the associated tag.
Definition: id3v2tag.h:38