Tag Parser  6.2.1
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
tagvalue.h
Go to the documentation of this file.
1 #ifndef TAGVALUE_H
2 #define TAGVALUE_H
3 
4 #include "./positioninset.h"
5 
6 #include <c++utilities/conversion/binaryconversion.h>
7 #include <c++utilities/chrono/timespan.h>
8 #include <c++utilities/chrono/datetime.h>
9 
10 #include <iosfwd>
11 #include <string>
12 #include <memory>
13 
14 namespace Media {
15 
16 class Tag;
17 
21 enum class TagTextEncoding : unsigned int
22 {
23  Latin1,
24  Utf8,
28 };
29 
34 inline int characterSize(TagTextEncoding encoding) {
35  switch(encoding) {
38  return 1;
41  return 2;
42  default:
43  return 0;
44  }
45 }
46 
50 enum class TagDataType : unsigned int
51 {
52  Text,
53  Integer,
56  TimeSpan,
57  DateTime,
58  Picture,
59  Binary,
60  Undefined
61 };
62 
64 {
65 public:
66  // constructor, destructor
67  TagValue();
68  TagValue(const char *text, std::size_t textSize, TagTextEncoding textEncoding = TagTextEncoding::Latin1, TagTextEncoding convertTo = TagTextEncoding::Unspecified);
69  TagValue(const std::string &text, TagTextEncoding textEncoding = TagTextEncoding::Latin1, TagTextEncoding convertTo = TagTextEncoding::Unspecified);
70  TagValue(int value);
71  TagValue(const char *data, size_t length, TagDataType type = TagDataType::Undefined, TagTextEncoding encoding = TagTextEncoding::Latin1);
72  TagValue(std::unique_ptr<char[]> &&data, size_t length, TagDataType type = TagDataType::Binary, TagTextEncoding encoding = TagTextEncoding::Latin1);
73  TagValue(const PositionInSet &value);
74  TagValue(const TagValue &other);
75  TagValue(TagValue &&other) = default;
76  ~TagValue();
77 
78  // operators
79  TagValue &operator=(const TagValue &other);
80  TagValue &operator=(TagValue &&other) = default;
81  bool operator==(const TagValue &other) const;
82  bool operator!=(const TagValue &other) const;
83 
84  // methods
85  bool isEmpty() const;
86  void clearData();
87  void clearMetadata();
88  void clearDataAndMetadata();
89  TagDataType type() const;
90  std::string toString(TagTextEncoding encoding = TagTextEncoding::Unspecified) const;
91  void toString(std::string &result, TagTextEncoding encoding = TagTextEncoding::Unspecified) const;
92  std::u16string toWString(TagTextEncoding encoding = TagTextEncoding::Unspecified) const;
93  void toWString(std::u16string &result, TagTextEncoding encoding = TagTextEncoding::Unspecified) const;
94  int32 toInteger() const;
95  int toStandardGenreIndex() const;
96  PositionInSet toPositionInSet() const;
97  ChronoUtilities::TimeSpan toTimeSpan() const;
98  ChronoUtilities::DateTime toDateTime() const;
99  size_t dataSize() const;
100  char *dataPointer() const;
101  const std::string &description() const;
102  void setDescription(const std::string &value, TagTextEncoding encoding = TagTextEncoding::Latin1);
103  const std::string &mimeType() const;
104  void setMimeType(const std::string &value);
105  const std::string &language() const;
106  void setLanguage(const std::string &value);
107  bool isLabeledAsReadonly() const;
108  void setReadonly(bool value);
109  TagTextEncoding dataEncoding() const;
110  void convertDataEncoding(TagTextEncoding encoding);
111  void convertDataEncodingForTag(const Tag *tag);
112  TagTextEncoding descriptionEncoding() const;
113  static const TagValue &empty();
114 
115  void assignText(const char *text, std::size_t textSize, TagTextEncoding textEncoding = TagTextEncoding::Latin1, TagTextEncoding convertTo = TagTextEncoding::Unspecified);
116  void assignText(const std::string &text, TagTextEncoding textEncoding = TagTextEncoding::Latin1, TagTextEncoding convertTo = TagTextEncoding::Unspecified);
117  void assignInteger(int value);
118  void assignStandardGenreIndex(int index);
119  void assignData(const char *data, size_t length, TagDataType type = TagDataType::Binary, TagTextEncoding encoding = TagTextEncoding::Latin1);
120  void assignData(std::unique_ptr<char[]> &&data, size_t length, TagDataType type = TagDataType::Binary, TagTextEncoding encoding = TagTextEncoding::Latin1);
121  void assignPosition(PositionInSet value);
122  void assignTimeSpan(ChronoUtilities::TimeSpan value);
123  void assignDateTime(ChronoUtilities::DateTime value);
124 
125 
126 private:
127  std::unique_ptr<char[]> m_ptr;
128  std::string::size_type m_size;
129  TagDataType m_type;
130  std::string m_desc;
131  std::string m_mimeType;
132  std::string m_lng;
133  bool m_labeledAsReadonly;
134  TagTextEncoding m_encoding;
135  TagTextEncoding m_descEncoding;
136 };
137 
142  m_size(0),
143  m_type(TagDataType::Undefined),
144  m_labeledAsReadonly(false),
145  m_encoding(TagTextEncoding::Latin1),
146  m_descEncoding(TagTextEncoding::Latin1)
147 {}
148 
158 inline TagValue::TagValue(const char *text, std::size_t textSize, TagTextEncoding textEncoding, TagTextEncoding convertTo) :
159  m_labeledAsReadonly(false),
160  m_descEncoding(TagTextEncoding::Latin1)
161 {
162  assignText(text, textSize, textEncoding, convertTo);
163 }
164 
173 inline TagValue::TagValue(const std::string &text, TagTextEncoding textEncoding, TagTextEncoding convertTo) :
174  m_labeledAsReadonly(false),
175  m_descEncoding(TagTextEncoding::Latin1)
176 {
177  assignText(text, textEncoding, convertTo);
178 }
179 
183 inline TagValue::TagValue(int value) :
184  TagValue(reinterpret_cast<const char *>(&value), sizeof(value), TagDataType::Integer)
185 {}
186 
196 inline TagValue::TagValue(const char *data, size_t length, TagDataType type, TagTextEncoding encoding) :
197  m_size(length),
198  m_type(type),
199  m_labeledAsReadonly(false),
200  m_encoding(encoding),
201  m_descEncoding(TagTextEncoding::Latin1)
202 {
203  if(length) {
204  m_ptr = std::make_unique<char []>(m_size);
205  std::copy(data, data + length, m_ptr.get());
206  }
207 }
208 
220 inline TagValue::TagValue(std::unique_ptr<char[]> &&data, size_t length, TagDataType type, TagTextEncoding encoding) :
221  m_size(length),
222  m_type(type),
223  m_labeledAsReadonly(false),
224  m_encoding(encoding),
225  m_descEncoding(TagTextEncoding::Latin1)
226 {
227  if(length) {
228  m_ptr = move(data);
229  }
230 }
231 
236 inline TagValue::TagValue(const PositionInSet &value) :
237  TagValue(reinterpret_cast<const char *>(&value), sizeof(value), TagDataType::PositionInSet)
238 {}
239 
244 inline bool TagValue::operator!=(const TagValue &other) const
245 {
246  return !(*this == other);
247 }
248 
257 inline void TagValue::assignText(const std::string &text, TagTextEncoding textEncoding, TagTextEncoding convertTo)
258 {
259  assignText(text.data(), text.size(), textEncoding, convertTo);
260 }
261 
266 {
267  if(value.isNull()) {
269  clearData();
270  } else {
271  assignData(reinterpret_cast<const char *>(&value), sizeof(value), TagDataType::PositionInSet);
272  }
273 }
274 
279 {
280  assignData(reinterpret_cast<const char *>(&value), sizeof(value), TagDataType::TimeSpan);
281 }
282 
287 {
288  assignData(reinterpret_cast<const char *>(&value), sizeof(value), TagDataType::DateTime);
289 }
290 
295 {
296  return m_type;
297 }
298 
306 inline std::string TagValue::toString(TagTextEncoding encoding) const
307 {
308  std::string res;
309  toString(res, encoding);
310  return res;
311 }
312 
319 inline std::u16string TagValue::toWString(TagTextEncoding encoding) const
320 {
321  std::u16string res;
322  toWString(res, encoding);
323  return res;
324 }
325 
330 inline bool TagValue::isEmpty() const
331 {
332  return m_ptr == nullptr || m_size == 0;
333 }
334 
341 inline void TagValue::clearData()
342 {
343  m_size = 0;
344  m_ptr.reset();
345 }
346 
351 inline size_t TagValue::dataSize() const
352 {
353  return m_size;
354 }
355 
362 inline char *TagValue::dataPointer() const
363 {
364  return m_ptr.get();
365 }
366 
373 inline const std::string &TagValue::description() const
374 {
375  return m_desc;
376 }
377 
386 inline void TagValue::setDescription(const std::string &value, TagTextEncoding encoding)
387 {
388  m_desc = value;
389  m_descEncoding = encoding;
390 }
391 
397 inline const std::string &TagValue::mimeType() const
398 {
399  return m_mimeType;
400 }
401 
408 inline void TagValue::setMimeType(const std::string &value)
409 {
410  m_mimeType = value;
411 }
412 
418 inline const std::string &TagValue::language() const
419 {
420  return m_lng;
421 }
422 
429 inline void TagValue::setLanguage(const std::string &value)
430 {
431  m_lng = value;
432 }
433 
442 inline bool TagValue::isLabeledAsReadonly() const
443 {
444  return m_labeledAsReadonly;
445 }
446 
455 inline void TagValue::setReadonly(bool value)
456 {
457  m_labeledAsReadonly = value;
458 }
459 
466 {
467  return m_encoding;
468 }
469 
476 {
477  return m_descEncoding;
478 }
479 
480 }
481 
482 #endif // TAGVALUE_H
The TagValue class wraps values of different types.
Definition: tagvalue.h:63
void setDescription(const std::string &value, TagTextEncoding encoding=TagTextEncoding::Latin1)
Sets the description.
Definition: tagvalue.h:386
TAG_PARSER_EXPORT const char * language()
bool isEmpty() const
Returns an indication whether an value is assigned.
Definition: tagvalue.h:330
TagTextEncoding dataEncoding() const
Returns the data encoding.
Definition: tagvalue.h:465
void assignText(const char *text, std::size_t textSize, TagTextEncoding textEncoding=TagTextEncoding::Latin1, TagTextEncoding convertTo=TagTextEncoding::Unspecified)
Assigns a copy of the given text.
Definition: tagvalue.cpp:548
char * dataPointer() const
Returns a pointer to the raw data assigned to the current instance.
Definition: tagvalue.h:362
std::u16string toWString(TagTextEncoding encoding=TagTextEncoding::Unspecified) const
Converts the value of the current TagValue object to its equivalent std::wstring representation.
Definition: tagvalue.h:319
TagTextEncoding
Specifies the text encoding.
Definition: tagvalue.h:21
constexpr bool operator!=(byte lhs, FlacMetaDataBlockType type)
Definition: flacmetadata.h:33
void assignTimeSpan(ChronoUtilities::TimeSpan value)
Assigns the given TimeSpan value.
Definition: tagvalue.h:278
void assignDateTime(ChronoUtilities::DateTime value)
Assigns the given DateTime value.
Definition: tagvalue.h:286
The PositionInSet class describes the position of an element in a set which consists of a certain num...
Definition: positioninset.h:20
const std::string & language() const
Returns the language.
Definition: tagvalue.h:418
TagValue()
Constructs an empty TagValue.
Definition: tagvalue.h:141
TagDataType type() const
Returns the type of the assigned value.
Definition: tagvalue.h:294
TagTextEncoding descriptionEncoding() const
Returns the description encoding.
Definition: tagvalue.h:475
void clearData()
Clears the assigned data.
Definition: tagvalue.h:341
The Tag class is used to store, read and write tag information.
Definition: tag.h:98
bool operator!=(const TagValue &other) const
Returns whether both instances are not equal.
Definition: tagvalue.h:244
void assignPosition(PositionInSet value)
Assigns the given PositionInSet value.
Definition: tagvalue.h:265
void assignData(const char *data, size_t length, TagDataType type=TagDataType::Binary, TagTextEncoding encoding=TagTextEncoding::Latin1)
Assigns a copy of the given data.
Definition: tagvalue.cpp:624
int characterSize(TagTextEncoding encoding)
Returns the size of one character for the specified encoding in bytes.
Definition: tagvalue.h:34
TAG_PARSER_EXPORT const char * description()
constexpr bool operator==(byte lhs, FlacMetaDataBlockType type)
Definition: flacmetadata.h:28
TagDataType
Specifies the data type.
Definition: tagvalue.h:50
bool isLabeledAsReadonly() const
Returns an indication whether the value is labeled as read-only.
Definition: tagvalue.h:442
Contains all classes and functions of the TagInfo library.
Definition: exceptions.h:9
void setLanguage(const std::string &value)
Sets the language.
Definition: tagvalue.h:429
void setMimeType(const std::string &value)
Sets the MIME type.
Definition: tagvalue.h:408
const std::string & mimeType() const
Returns the MIME type.
Definition: tagvalue.h:397
const std::string & description() const
Returns the description.
Definition: tagvalue.h:373
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
size_t dataSize() const
Returns the size of the assigned value in bytes.
Definition: tagvalue.h:351
std::string toString(TagTextEncoding encoding=TagTextEncoding::Unspecified) const
Converts the value of the current TagValue object to its equivalent std::string representation.
Definition: tagvalue.h:306
void setReadonly(bool value)
Sets whether the TagValue is labeled as read-only.
Definition: tagvalue.h:455
constexpr bool isNull() const
Returns an indication whether both the element position and total element count is 0...
Definition: positioninset.h:91