Tag Parser  8.0.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
tag.h
Go to the documentation of this file.
1 #ifndef TAG_PARSER_TAG_H
2 #define TAG_PARSER_TAG_H
3 
4 #include "./tagtarget.h"
5 #include "./tagvalue.h"
6 
7 #include <c++utilities/conversion/types.h>
8 #include <c++utilities/io/binaryreader.h>
9 
10 #include <string>
11 #include <type_traits>
12 
13 namespace TagParser {
14 
20 enum class TagType : unsigned int {
21  Unspecified = 0x00,
22  Id3v1Tag = 0x01,
23  Id3v2Tag = 0x02,
24  Mp4Tag = 0x04,
25  MatroskaTag = 0x08,
26  VorbisComment = 0x10,
27  OggVorbisComment = 0x20
28 };
29 
40 enum class KnownField : unsigned int {
41  Invalid = std::numeric_limits<unsigned int>::max(),
42  Title = 0,
43  Album,
44  Artist,
45  Genre,
46  Year,
47  Comment,
48  Bpm,
49  Bps,
50  Lyricist,
52  DiskPosition,
53  PartNumber,
54  TotalParts,
55  Encoder,
56  RecordDate,
57  Performers,
58  Length,
59  Language,
61  Lyrics,
63  Grouping,
64  RecordLabel,
65  Cover,
66  Composer,
67  Rating,
68  Description,
69  Vendor
70 };
71 
76 
81 
85 constexpr unsigned int knownFieldArraySize = static_cast<unsigned int>(lastKnownField) + 1;
86 
91 {
92  return field == lastKnownField ? KnownField::Invalid : static_cast<KnownField>(static_cast<int>(field) + 1);
93 }
94 
96 public:
97  virtual ~Tag();
98 
99  virtual TagType type() const;
100  virtual const char *typeName() const;
101  std::string toString() const;
102  virtual TagTextEncoding proposedTextEncoding() const;
103  virtual bool canEncodingBeUsed(TagTextEncoding encoding) const;
104  virtual const TagValue &value(KnownField field) const = 0;
105  virtual std::vector<const TagValue *> values(KnownField field) const;
106  virtual bool setValue(KnownField field, const TagValue &value) = 0;
107  virtual bool setValues(KnownField field, const std::vector<TagValue> &values);
108  virtual bool hasField(KnownField field) const = 0;
109  virtual void removeAllFields() = 0;
110  const std::string &version() const;
111  uint32 size() const;
112  virtual bool supportsTarget() const;
113  const TagTarget &target() const;
114  void setTarget(const TagTarget &target);
115  virtual TagTargetLevel targetLevel() const;
116  const char *targetLevelName() const;
117  bool isTargetingLevel(TagTargetLevel tagTargetLevel) const;
118  std::string targetString() const;
119  virtual unsigned int fieldCount() const = 0;
120  virtual bool supportsField(KnownField field) const = 0;
121  virtual TagDataType proposedDataType(KnownField field) const;
122  virtual bool supportsDescription(KnownField field) const;
123  virtual bool supportsMimeType(KnownField field) const;
124  virtual bool supportsMultipleValues(KnownField field) const;
125  virtual unsigned int insertValues(const Tag &from, bool overwrite);
126  virtual void ensureTextValuesAreProperlyEncoded() = 0;
127 
128 protected:
129  Tag();
130 
131  std::string m_version;
132  uint32 m_size;
134 };
135 
136 inline TagType Tag::type() const
137 {
138  return TagType::Unspecified;
139 }
140 
141 inline const char *Tag::typeName() const
142 {
143  return "unspecified";
144 }
145 
147 {
149 }
150 
151 inline bool Tag::canEncodingBeUsed(TagTextEncoding encoding) const
152 {
153  return encoding == proposedTextEncoding();
154 }
155 
156 inline const std::string &Tag::version() const
157 {
158  return m_version;
159 }
160 
161 inline uint32 Tag::size() const
162 {
163  return m_size;
164 }
165 
166 inline bool Tag::supportsTarget() const
167 {
168  return false;
169 }
170 
171 inline const TagTarget &Tag::target() const
172 {
173  return m_target;
174 }
175 
176 inline void Tag::setTarget(const TagTarget &target)
177 {
178  m_target = target;
179 }
180 
182 {
184 }
185 
186 inline const char *Tag::targetLevelName() const
187 {
188  return supportsTarget() ? tagTargetLevelName(targetLevel()) : nullptr;
189 }
190 
191 inline bool Tag::isTargetingLevel(TagTargetLevel tagTargetLevel) const
192 {
193  return !supportsTarget() || static_cast<byte>(targetLevel()) >= static_cast<byte>(tagTargetLevel);
194 }
195 
196 inline std::string Tag::targetString() const
197 {
198  return target().toString(targetLevel());
199 }
200 
202 {
203  switch (field) {
204  case KnownField::Bpm:
205  case KnownField::Bps:
206  case KnownField::Rating:
209  return TagDataType::Integer;
210  case KnownField::Cover:
211  return TagDataType::Picture;
212  case KnownField::Length:
213  return TagDataType::TimeSpan;
217  case KnownField::Genre:
219  default:
220  return TagDataType::Text;
221  }
222 }
223 
225 {
226  return false;
227 }
228 
230 {
231  return false;
232 }
233 
235 {
236  return false;
237 }
238 
239 } // namespace TagParser
240 
241 #endif // TAG_PARSER_TAG_H
virtual bool supportsMultipleValues(KnownField field) const
Returns an indications whether the specified field supports multiple values.
Definition: tag.h:234
Implementation of TagParser::Tag for the MP4 container.
Definition: mp4tag.h:97
Specialization of TagParser::VorbisComment for Vorbis comments inside an OGG stream.
Definition: oggcontainer.h:67
std::string toString(const std::function< TagTargetLevel(uint64)> &tagTargetMapping) const
Returns the string representation of the current instance.
Definition: tagtarget.h:201
TagTargetLevel
The TagTargetLevel enum specifies tag target levels.
Definition: tagtarget.h:17
std::string targetString() const
Returns the string representation for the assigned tag target.
Definition: tag.h:196
The Tag class is used to store, read and write tag information.
Definition: tag.h:95
constexpr TAG_PARSER_EXPORT const char * version()
bool isTargetingLevel(TagTargetLevel tagTargetLevel) const
Returns whether the tag is targeting the specified tagTargetLevel.
Definition: tag.h:191
virtual TagTextEncoding proposedTextEncoding() const
Returns the proposed text encoding.
Definition: tag.h:146
virtual const char * typeName() const
Returns the type name of the tag as C-style string.
Definition: tag.h:141
TagDataType
Specifies the data type.
Definition: tagvalue.h:53
virtual bool supportsMimeType(KnownField field) const
Returns an indications whether the specified field supports mime types.
Definition: tag.h:229
KnownField
Specifies the field.
Definition: tag.h:40
virtual TagType type() const
Returns the type of the tag as TagParser::TagType.
Definition: tag.h:136
constexpr KnownField firstKnownField
The first valid entry in the TagParser::KnownField enum.
Definition: tag.h:75
virtual bool supportsTarget() const
Returns an indication whether a target is supported by the tag.
Definition: tag.h:166
TAG_PARSER_EXPORT const char * tagTargetLevelName(TagTargetLevel tagTargetLevel)
Returns a string representation for the specified tagTargetLevel.
Definition: tagtarget.cpp:17
Implementation of TagParser::Tag for Vorbis comments.
Definition: vorbiscomment.h:25
const char * targetLevelName() const
Returns the name of the current target level.
Definition: tag.h:186
The TagTarget class specifies the target of a tag.
Definition: tagtarget.h:21
virtual TagTargetLevel targetLevel() const
Returns the name of the current tag target level.
Definition: tag.h:181
constexpr KnownField lastKnownField
The last valid entry in the TagParser::KnownField enum.
Definition: tag.h:80
const std::string & version() const
Returns the version of the tag as std::string.
Definition: tag.h:156
Implementation of TagParser::Tag for the Matroska container.
Definition: matroskatag.h:58
uint32 m_size
Definition: tag.h:132
constexpr unsigned int knownFieldArraySize
The number of valid entries in the TagParser::KnownField enum.
Definition: tag.h:85
Implementation of TagParser::Tag for ID3v2 tags.
Definition: id3v2tag.h:61
Implementation of TagParser::Tag for ID3v1 tags.
Definition: id3v1tag.h:10
TagTarget m_target
Definition: tag.h:133
uint32 size() const
Returns the size of the tag in bytes.
Definition: tag.h:161
void setTarget(const TagTarget &target)
Sets the target of tag.
Definition: tag.h:176
virtual TagDataType proposedDataType(KnownField field) const
Returns the proposed data type for the specified field as TagDataType.
Definition: tag.h:201
virtual bool supportsDescription(KnownField field) const
Returns an indications whether the specified field supports descriptions.
Definition: tag.h:224
constexpr KnownField nextKnownField(KnownField field)
Returns the next known field.
Definition: tag.h:90
The TagValue class wraps values of different types.
Definition: tagvalue.h:65
TagType
Specifies the tag type.
Definition: tag.h:20
const TagTarget & target() const
Returns the target of tag.
Definition: tag.h:171
TagTextEncoding
Specifies the text encoding.
Definition: tagvalue.h:24
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:9
std::string m_version
Definition: tag.h:131
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
virtual bool canEncodingBeUsed(TagTextEncoding encoding) const
Returns an indication whether the specified encoding can be used to provide string values for the tag...
Definition: tag.h:151