Tag Parser  6.1.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 #include <c++utilities/misc/memory.h>
10 
11 #include <iosfwd>
12 #include <string>
13 #include <memory>
14 
15 namespace Media {
16 
17 class Tag;
18 
22 enum class TagTextEncoding : unsigned int
23 {
24  Latin1,
25  Utf8,
29 };
30 
35 inline int characterSize(TagTextEncoding encoding) {
36  switch(encoding) {
39  return 1;
42  return 2;
43  default:
44  return 0;
45  }
46 }
47 
51 enum class TagDataType : unsigned int
52 {
53  Text,
54  Integer,
57  TimeSpan,
58  DateTime,
59  Picture,
60  Binary,
61  Undefined
62 };
63 
65 {
66 public:
67  // constructor, destructor
68  TagValue();
69  TagValue(const char *text, std::size_t textSize, TagTextEncoding textEncoding = TagTextEncoding::Latin1, TagTextEncoding convertTo = TagTextEncoding::Unspecified);
70  TagValue(const std::string &text, TagTextEncoding textEncoding = TagTextEncoding::Latin1, TagTextEncoding convertTo = TagTextEncoding::Unspecified);
71  TagValue(int value);
72  TagValue(const char *data, size_t length, TagDataType type = TagDataType::Undefined, TagTextEncoding encoding = TagTextEncoding::Latin1);
73  TagValue(std::unique_ptr<char[]> &&data, size_t length, TagDataType type = TagDataType::Binary, TagTextEncoding encoding = TagTextEncoding::Latin1);
74  TagValue(const PositionInSet &value);
75  TagValue(const TagValue &other);
76  TagValue(TagValue &&other) = default;
77  ~TagValue();
78 
79  // operators
80  TagValue &operator=(const TagValue &other);
81  TagValue &operator=(TagValue &&other) = default;
82  bool operator==(const TagValue &other) const;
83  bool operator!=(const TagValue &other) const;
84 
85  // methods
86  bool isEmpty() const;
87  void clearData();
88  void clearMetadata();
89  void clearDataAndMetadata();
90  TagDataType type() const;
91  std::string toString(TagTextEncoding encoding = TagTextEncoding::Unspecified) const;
92  void toString(std::string &result, TagTextEncoding encoding = TagTextEncoding::Unspecified) const;
93  std::u16string toWString(TagTextEncoding encoding = TagTextEncoding::Unspecified) const;
94  void toWString(std::u16string &result, TagTextEncoding encoding = TagTextEncoding::Unspecified) const;
95  int32 toInteger() const;
96  int toStandardGenreIndex() const;
97  PositionInSet toPositionInSet() const;
98  ChronoUtilities::TimeSpan toTimeSpan() const;
99  ChronoUtilities::DateTime toDateTime() const;
100  size_t dataSize() const;
101  char *dataPointer() const;
102  const std::string &description() const;
103  void setDescription(const std::string &value, TagTextEncoding encoding = TagTextEncoding::Latin1);
104  const std::string &mimeType() const;
105  void setMimeType(const std::string &value);
106  const std::string &language() const;
107  void setLanguage(const std::string &value);
108  bool isLabeledAsReadonly() const;
109  void setReadonly(bool value);
110  TagTextEncoding dataEncoding() const;
111  void convertDataEncoding(TagTextEncoding encoding);
112  void convertDataEncodingForTag(const Tag *tag);
113  TagTextEncoding descriptionEncoding() const;
114  static const TagValue &empty();
115 
116  void assignText(const char *text, std::size_t textSize, TagTextEncoding textEncoding = TagTextEncoding::Latin1, TagTextEncoding convertTo = TagTextEncoding::Unspecified);
117  void assignText(const std::string &text, TagTextEncoding textEncoding = TagTextEncoding::Latin1, TagTextEncoding convertTo = TagTextEncoding::Unspecified);
118  void assignInteger(int value);
119  void assignStandardGenreIndex(int index);
120  void assignData(const char *data, size_t length, TagDataType type = TagDataType::Binary, TagTextEncoding encoding = TagTextEncoding::Latin1);
121  void assignData(std::unique_ptr<char[]> &&data, size_t length, TagDataType type = TagDataType::Binary, TagTextEncoding encoding = TagTextEncoding::Latin1);
122  void assignPosition(PositionInSet value);
123  void assignTimeSpan(ChronoUtilities::TimeSpan value);
124  void assignDateTime(ChronoUtilities::DateTime value);
125 
126 
127 private:
128  std::unique_ptr<char[]> m_ptr;
129  std::string::size_type m_size;
130  TagDataType m_type;
131  std::string m_desc;
132  std::string m_mimeType;
133  std::string m_lng;
134  bool m_labeledAsReadonly;
135  TagTextEncoding m_encoding;
136  TagTextEncoding m_descEncoding;
137 };
138 
143  m_size(0),
144  m_type(TagDataType::Undefined),
145  m_labeledAsReadonly(false),
146  m_encoding(TagTextEncoding::Latin1),
147  m_descEncoding(TagTextEncoding::Latin1)
148 {}
149 
159 inline TagValue::TagValue(const char *text, std::size_t textSize, TagTextEncoding textEncoding, TagTextEncoding convertTo) :
160  m_labeledAsReadonly(false),
161  m_descEncoding(TagTextEncoding::Latin1)
162 {
163  assignText(text, textSize, textEncoding, convertTo);
164 }
165 
174 inline TagValue::TagValue(const std::string &text, TagTextEncoding textEncoding, TagTextEncoding convertTo) :
175  m_labeledAsReadonly(false),
176  m_descEncoding(TagTextEncoding::Latin1)
177 {
178  assignText(text, textEncoding, convertTo);
179 }
180 
184 inline TagValue::TagValue(int value) :
185  TagValue(reinterpret_cast<const char *>(&value), sizeof(value), TagDataType::Integer)
186 {}
187 
197 inline TagValue::TagValue(const char *data, size_t length, TagDataType type, TagTextEncoding encoding) :
198  m_size(length),
199  m_type(type),
200  m_labeledAsReadonly(false),
201  m_encoding(encoding),
202  m_descEncoding(TagTextEncoding::Latin1)
203 {
204  if(length) {
205  m_ptr = std::make_unique<char []>(m_size);
206  std::copy(data, data + length, m_ptr.get());
207  }
208 }
209 
221 inline TagValue::TagValue(std::unique_ptr<char[]> &&data, size_t length, TagDataType type, TagTextEncoding encoding) :
222  m_size(length),
223  m_type(type),
224  m_labeledAsReadonly(false),
225  m_encoding(encoding),
226  m_descEncoding(TagTextEncoding::Latin1)
227 {
228  if(length) {
229  m_ptr = move(data);
230  }
231 }
232 
237 inline TagValue::TagValue(const PositionInSet &value) :
238  TagValue(reinterpret_cast<const char *>(&value), sizeof(value), TagDataType::PositionInSet)
239 {}
240 
245 inline bool TagValue::operator!=(const TagValue &other) const
246 {
247  return !(*this == other);
248 }
249 
258 inline void TagValue::assignText(const std::string &text, TagTextEncoding textEncoding, TagTextEncoding convertTo)
259 {
260  assignText(text.data(), text.size(), textEncoding, convertTo);
261 }
262 
267 {
268  if(value.isNull()) {
270  clearData();
271  } else {
272  assignData(reinterpret_cast<const char *>(&value), sizeof(value), TagDataType::PositionInSet);
273  }
274 }
275 
280 {
281  assignData(reinterpret_cast<const char *>(&value), sizeof(value), TagDataType::TimeSpan);
282 }
283 
288 {
289  assignData(reinterpret_cast<const char *>(&value), sizeof(value), TagDataType::DateTime);
290 }
291 
296 {
297  return m_type;
298 }
299 
307 inline std::string TagValue::toString(TagTextEncoding encoding) const
308 {
309  std::string res;
310  toString(res, encoding);
311  return res;
312 }
313 
320 inline std::u16string TagValue::toWString(TagTextEncoding encoding) const
321 {
322  std::u16string res;
323  toWString(res, encoding);
324  return res;
325 }
326 
331 inline bool TagValue::isEmpty() const
332 {
333  return m_ptr == nullptr || m_size == 0;
334 }
335 
342 inline void TagValue::clearData()
343 {
344  m_size = 0;
345  m_ptr.reset();
346 }
347 
352 inline size_t TagValue::dataSize() const
353 {
354  return m_size;
355 }
356 
363 inline char *TagValue::dataPointer() const
364 {
365  return m_ptr.get();
366 }
367 
374 inline const std::string &TagValue::description() const
375 {
376  return m_desc;
377 }
378 
387 inline void TagValue::setDescription(const std::string &value, TagTextEncoding encoding)
388 {
389  m_desc = value;
390  m_descEncoding = encoding;
391 }
392 
398 inline const std::string &TagValue::mimeType() const
399 {
400  return m_mimeType;
401 }
402 
409 inline void TagValue::setMimeType(const std::string &value)
410 {
411  m_mimeType = value;
412 }
413 
419 inline const std::string &TagValue::language() const
420 {
421  return m_lng;
422 }
423 
430 inline void TagValue::setLanguage(const std::string &value)
431 {
432  m_lng = value;
433 }
434 
443 inline bool TagValue::isLabeledAsReadonly() const
444 {
445  return m_labeledAsReadonly;
446 }
447 
456 inline void TagValue::setReadonly(bool value)
457 {
458  m_labeledAsReadonly = value;
459 }
460 
467 {
468  return m_encoding;
469 }
470 
477 {
478  return m_descEncoding;
479 }
480 
481 }
482 
483 #endif // TAGVALUE_H
The TagValue class wraps values of different types.
Definition: tagvalue.h:64
void setDescription(const std::string &value, TagTextEncoding encoding=TagTextEncoding::Latin1)
Sets the description.
Definition: tagvalue.h:387
TAG_PARSER_EXPORT const char * language()
bool isEmpty() const
Returns an indication whether an value is assigned.
Definition: tagvalue.h:331
TagTextEncoding dataEncoding() const
Returns the data encoding.
Definition: tagvalue.h:466
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:363
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:320
TagTextEncoding
Specifies the text encoding.
Definition: tagvalue.h:22
constexpr bool operator!=(byte lhs, FlacMetaDataBlockType type)
Definition: flacmetadata.h:33
void assignTimeSpan(ChronoUtilities::TimeSpan value)
Assigns the given TimeSpan value.
Definition: tagvalue.h:279
void assignDateTime(ChronoUtilities::DateTime value)
Assigns the given DateTime value.
Definition: tagvalue.h:287
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:419
TagValue()
Constructs an empty TagValue.
Definition: tagvalue.h:142
TagDataType type() const
Returns the type of the assigned value.
Definition: tagvalue.h:295
TagTextEncoding descriptionEncoding() const
Returns the description encoding.
Definition: tagvalue.h:476
void clearData()
Clears the assigned data.
Definition: tagvalue.h:342
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:245
void assignPosition(PositionInSet value)
Assigns the given PositionInSet value.
Definition: tagvalue.h:266
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:35
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:51
bool isLabeledAsReadonly() const
Returns an indication whether the value is labeled as read-only.
Definition: tagvalue.h:443
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:430
void setMimeType(const std::string &value)
Sets the MIME type.
Definition: tagvalue.h:409
const std::string & mimeType() const
Returns the MIME type.
Definition: tagvalue.h:398
const std::string & description() const
Returns the description.
Definition: tagvalue.h:374
#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:352
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:307
void setReadonly(bool value)
Sets whether the TagValue is labeled as read-only.
Definition: tagvalue.h:456
constexpr bool isNull() const
Returns an indication whether both the element position and total element count is 0...
Definition: positioninset.h:91