Tag Parser  7.0.3
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 unsigned int insertValues(const Tag &from, bool overwrite);
125  virtual void ensureTextValuesAreProperlyEncoded() = 0;
126 
127 protected:
128  Tag();
129 
130  std::string m_version;
131  uint32 m_size;
133 };
134 
141 inline TagType Tag::type() const
142 {
143  return TagType::Unspecified;
144 }
145 
152 inline const char *Tag::typeName() const
153 {
154  return "unspecified";
155 }
156 
171 {
173 }
174 
189 inline bool Tag::canEncodingBeUsed(TagTextEncoding encoding) const
190 {
191  return encoding == proposedTextEncoding();
192 }
193 
198 inline const std::string &Tag::version() const
199 {
200  return m_version;
201 }
202 
207 inline uint32 Tag::size() const
208 {
209  return m_size;
210 }
211 
221 inline bool Tag::supportsTarget() const
222 {
223  return false;
224 }
225 
232 inline const TagTarget &Tag::target() const
233 {
234  return m_target;
235 }
236 
246 inline void Tag::setTarget(const TagTarget &target)
247 {
248  m_target = target;
249 }
250 
256 {
258 }
259 
264 inline const char *Tag::targetLevelName() const
265 {
266  return supportsTarget() ? tagTargetLevelName(targetLevel()) : nullptr;
267 }
268 
274 inline bool Tag::isTargetingLevel(TagTargetLevel tagTargetLevel) const
275 {
276  return !supportsTarget() || static_cast<byte>(targetLevel()) >= static_cast<byte>(tagTargetLevel);
277 }
278 
282 inline std::string Tag::targetString() const
283 {
284  return target().toString(targetLevel());
285 }
286 
302 {
303  switch (field) {
304  case KnownField::Bpm:
305  case KnownField::Bps:
306  case KnownField::Rating:
309  return TagDataType::Integer;
310  case KnownField::Cover:
311  return TagDataType::Picture;
312  case KnownField::Length:
313  return TagDataType::TimeSpan;
317  case KnownField::Genre:
319  default:
320  return TagDataType::Text;
321  }
322 }
323 
334 {
335  return false;
336 }
337 
348 {
349  return false;
350 }
351 
352 } // namespace TagParser
353 
354 #endif // TAG_PARSER_TAG_H
virtual TagDataType proposedDataType(KnownField field) const
Returns the proposed data type for the specified field as TagDataType.
Definition: tag.h:301
virtual TagTextEncoding proposedTextEncoding() const
Returns the proposed text encoding.
Definition: tag.h:170
std::string toString(const std::function< TagTargetLevel(uint64)> &tagTargetMapping) const
Returns the string representation of the current instance.
Definition: tagtarget.h:201
std::string targetString() const
Returns the string representation for the assigned tag target.
Definition: tag.h:282
TagTargetLevel
The TagTargetLevel enum specifies tag target levels.
Definition: tagtarget.h:17
virtual bool supportsTarget() const
Returns an indication whether a target is supported by the tag.
Definition: tag.h:221
TAG_PARSER_EXPORT const char * version()
const char * targetLevelName() const
Returns the name of the current target level.
Definition: tag.h:264
TagDataType
Specifies the data type.
Definition: tagvalue.h:51
KnownField
Specifies the field.
Definition: tag.h:40
constexpr KnownField firstKnownField
The first valid entry in the Media::KnownField enum.
Definition: tag.h:75
const TagTarget & target() const
Returns the target of tag.
Definition: tag.h:232
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:189
TAG_PARSER_EXPORT const char * tagTargetLevelName(TagTargetLevel tagTargetLevel)
Returns a string representation for the specified tagTargetLevel.
Definition: tagtarget.cpp:17
const std::string & version() const
Returns the version of the tag as std::string.
Definition: tag.h:198
bool isTargetingLevel(TagTargetLevel tagTargetLevel) const
Returns whether the tag is targeting the specified tagTargetLevel.
Definition: tag.h:274
virtual bool supportsMimeType(KnownField field) const
Returns an indications whether the specified field supports mime types.
Definition: tag.h:347
uint32 size() const
Returns the size of the tag in bytes.
Definition: tag.h:207
constexpr KnownField lastKnownField
The last valid entry in the Media::KnownField enum.
Definition: tag.h:80
uint32 m_size
Definition: tag.h:131
constexpr unsigned int knownFieldArraySize
The number of valid entries in the Media::KnownField enum.
Definition: tag.h:85
virtual TagType type() const
Returns the type of the tag as Media::TagType.
Definition: tag.h:141
TagTarget m_target
Definition: tag.h:132
virtual bool supportsDescription(KnownField field) const
Returns an indications whether the specified field supports descriptions.
Definition: tag.h:333
constexpr KnownField nextKnownField(KnownField field)
Returns the next known field.
Definition: tag.h:90
void setTarget(const TagTarget &target)
Sets the target of tag.
Definition: tag.h:246
virtual TagTargetLevel targetLevel() const
Returns the name of the current tag target level.
Definition: tag.h:255
TagType
Specifies the tag type.
Definition: tag.h:20
TagTextEncoding
Specifies the text encoding.
Definition: tagvalue.h:22
std::string m_version
Definition: tag.h:130
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
virtual const char * typeName() const
Returns the type name of the tag as C-style string.
Definition: tag.h:152