6 #include <c++utilities/conversion/binaryconversion.h> 7 #include <c++utilities/conversion/conversionexception.h> 8 #include <c++utilities/conversion/stringconversion.h> 32 : m_size(other.m_size)
33 , m_desc(other.m_desc)
34 , m_mimeType(other.m_mimeType)
35 , m_language(other.m_language)
36 , m_type(other.m_type)
37 , m_encoding(other.m_encoding)
38 , m_descEncoding(other.m_descEncoding)
39 , m_labeledAsReadonly(other.m_labeledAsReadonly)
42 m_ptr = make_unique<char[]>(m_size);
43 std::copy(other.m_ptr.get(), other.m_ptr.get() + other.m_size, m_ptr.get());
53 m_size = other.m_size;
54 m_type = other.m_type;
55 m_desc = other.m_desc;
56 m_mimeType = other.m_mimeType;
57 m_language = other.m_language;
58 m_labeledAsReadonly = other.m_labeledAsReadonly;
59 m_encoding = other.m_encoding;
60 m_descEncoding = other.m_descEncoding;
64 m_ptr = make_unique<char[]>(m_size);
65 std::copy(other.m_ptr.get(), other.m_ptr.get() + other.m_size, m_ptr.get());
81 if (m_desc != other.m_desc || (!m_desc.empty() && m_descEncoding != other.m_descEncoding) || m_mimeType != other.m_mimeType
82 || m_language != other.m_language || m_labeledAsReadonly != other.m_labeledAsReadonly) {
85 if (m_type == other.m_type) {
88 if (m_size != other.m_size && m_encoding != other.m_encoding) {
92 return strncmp(m_ptr.get(), other.m_ptr.get(), m_size) == 0;
106 if (m_size != other.m_size) {
109 return strncmp(m_ptr.get(), other.m_ptr.get(), m_size) == 0;
119 }
catch (
const ConversionException &) {
137 m_labeledAsReadonly =
false;
155 switch (m_encoding) {
159 return ConversionUtilities::bufferToNumber<int32>(m_ptr.get(), m_size);
162 u16string u16str(reinterpret_cast<char16_t *>(m_ptr.get()), m_size / 2);
164 return ConversionUtilities::stringToNumber<int32>(u16str);
169 if (m_size ==
sizeof(int32)) {
170 return *
reinterpret_cast<int32 *
>(m_ptr.get());
172 throw ConversionException(
"Can not convert assigned data to integer because the data size is not appropriate.");
174 throw ConversionException(
"Can not convert binary data/picture/time span/date time to integer.");
194 }
catch (
const ConversionException &) {
206 if (m_size !=
sizeof(int32)) {
207 throw ConversionException(
"The assigned data is of unappropriate size.");
209 index =
static_cast<int>(*
reinterpret_cast<int32 *
>(m_ptr.get()));
212 throw ConversionException(
"It is not possible to convert assigned data to a number because of its incompatible type.");
217 throw ConversionException(
"The assigned number is not a valid standard genre index.");
232 switch (m_encoding) {
239 u16string u16str(reinterpret_cast<char16_t *>(m_ptr.get()), m_size / 2);
247 return PositionInSet(*(reinterpret_cast<int32 *>(m_ptr.get())));
248 case 2 *
sizeof(int32):
249 return PositionInSet(*(reinterpret_cast<int32 *>(m_ptr.get())), *(
reinterpret_cast<int32 *
>(m_ptr.get() +
sizeof(int32))));
251 throw ConversionException(
"The size of the assigned data is not appropriate.");
254 throw ConversionException(
"Can not convert binary data/genre index/picture to \"position in set\".");
270 return TimeSpan::fromString(
string(m_ptr.get(), m_size));
275 return TimeSpan(*(reinterpret_cast<int32 *>(m_ptr.get())));
277 return TimeSpan(*(reinterpret_cast<int64 *>(m_ptr.get())));
279 throw ConversionException(
"The size of the assigned data is not appropriate.");
282 throw ConversionException(
"Can not convert binary data/genre index/position in set/picture to time span.");
298 return DateTime::fromString(
string(m_ptr.get(), m_size));
301 if (m_size ==
sizeof(int32)) {
302 return DateTime(*(reinterpret_cast<uint32 *>(m_ptr.get())));
303 }
else if (m_size ==
sizeof(int64)) {
304 return DateTime(*(reinterpret_cast<uint64 *>(m_ptr.get())));
306 throw ConversionException(
"The assigned data is of unappropriate size.");
309 throw ConversionException(
"Can not convert binary data/genre index/position in set/picture to date time.");
318 switch (tagTextEncoding) {
320 return make_pair(
"ISO-8859-1", 1.0f);
322 return make_pair(
"UTF-8", 1.0f);
324 return make_pair(
"UTF-16LE", 2.0f);
326 return make_pair(
"UTF-16BE", 2.0f);
328 return make_pair(
nullptr, 0.0f);
343 if (m_encoding == encoding) {
347 StringData encodedData;
353 encodedData = convertLatin1ToUtf8(m_ptr.get(), m_size);
356 encodedData = convertUtf16LEToUtf8(m_ptr.get(), m_size);
359 encodedData = convertUtf16BEToUtf8(m_ptr.get(), m_size);
369 = convertString(inputParameter.first, outputParameter.first, m_ptr.get(), m_size, outputParameter.second / inputParameter.second);
373 m_ptr = make_unique<char[]>(m_size = encodedData.second);
374 copy(encodedData.first.get(), encodedData.first.get() + encodedData.second, m_ptr.get());
376 m_encoding = encoding;
409 result.assign(m_ptr.get(), m_size);
411 StringData encodedData;
417 encodedData = convertLatin1ToUtf8(m_ptr.get(), m_size);
420 encodedData = convertUtf16LEToUtf8(m_ptr.get(), m_size);
423 encodedData = convertUtf16BEToUtf8(m_ptr.get(), m_size);
433 = convertString(inputParameter.first, outputParameter.first, m_ptr.get(), m_size, outputParameter.second / inputParameter.second);
436 result.assign(encodedData.first.get(), encodedData.second);
440 result = ConversionUtilities::numberToString(
toInteger());
447 result.assign(genreName);
450 throw ConversionException(
"No string representation for the assigned standard genre index available.");
459 throw ConversionException(
"Can not convert binary data/picture to string.");
463 : convertUtf8ToUtf16BE(result.data(), result.size());
464 result.assign(encodedData.first.get(), encodedData.second);
482 string regularStrRes;
486 result.assign(reinterpret_cast<const char16_t *>(m_ptr.get()), m_size /
sizeof(char16_t));
488 StringData encodedData;
494 encodedData = convertLatin1ToUtf8(m_ptr.get(), m_size);
497 encodedData = convertUtf16LEToUtf8(m_ptr.get(), m_size);
500 encodedData = convertUtf16BEToUtf8(m_ptr.get(), m_size);
510 = convertString(inputParameter.first, outputParameter.first, m_ptr.get(), m_size, outputParameter.second / inputParameter.second);
513 result.assign(reinterpret_cast<const char16_t *>(encodedData.first.get()), encodedData.second /
sizeof(char16_t));
517 regularStrRes = ConversionUtilities::numberToString(
toInteger());
524 regularStrRes.assign(genreName);
527 throw ConversionException(
"No string representation for the assigned standard genre index available.");
533 throw ConversionException(
"Can not convert binary data/picture to string.");
537 : convertUtf8ToUtf16BE(regularStrRes.data(), result.size());
538 result.assign(reinterpret_cast<const char16_t *>(encodedData.first.get()), encodedData.second /
sizeof(
const char16_t));
557 stripBom(text, textSize, textEncoding);
565 m_ptr = make_unique<char[]>(m_size = textSize);
566 copy(text, text + textSize, m_ptr.get());
570 StringData encodedData;
571 switch (textEncoding) {
576 encodedData = convertUtf8ToLatin1(text, textSize);
579 encodedData = convertUtf8ToUtf16LE(text, textSize);
582 encodedData = convertUtf8ToUtf16BE(text, textSize);
591 encodedData = convertString(inputParameter.first, outputParameter.first, text, textSize, outputParameter.second / inputParameter.second);
595 m_ptr = make_unique<char[]>(m_size = encodedData.second);
596 copy(encodedData.first.get(), encodedData.first.get() + encodedData.second, m_ptr.get());
605 m_size =
sizeof(value);
606 m_ptr = make_unique<char[]>(m_size);
607 std::copy(reinterpret_cast<const char *>(&value), reinterpret_cast<const char *>(&value) + m_size, m_ptr.get());
625 if (length > m_size) {
626 m_ptr = make_unique<char[]>(length);
629 std::copy(data, data + length, m_ptr.get());
635 m_encoding = encoding;
654 m_encoding = encoding;
665 if ((length >= 3) && (ConversionUtilities::BE::toUInt24(text) == 0x00EFBBBF)) {
671 if ((length >= 2) && (ConversionUtilities::LE::toUInt16(text) == 0xFEFF)) {
677 if ((length >= 2) && (ConversionUtilities::BE::toUInt16(text) == 0xFEFF)) {
692 if (currentEncoding !=
693 #
if defined(CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN)
695 #elif defined(CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN)
698 #error
"Host byte order not supported" 701 for (
auto &c : u16str) {
702 c = swapOrder(static_cast<uint16>(c));
713 return emptyTagValue;
TagTextEncoding dataEncoding() const
Returns the data encoding.
StringType toString() const
Returns the string representation of the current PositionInSet.
virtual TagTextEncoding proposedTextEncoding() const
Returns the proposed text encoding.
pair< const char *, float > encodingParameter(TagTextEncoding tagTextEncoding)
Returns the encoding parameter (name of the character set and bytes per character) for the specified ...
std::u16string toWString(TagTextEncoding encoding=TagTextEncoding::Unspecified) const
Converts the value of the current TagValue object to its equivalent std::wstring representation.
TagDataType
Specifies the data type.
static const char * stringFromIndex(int index)
Returns the genre name for the specified numerical denotation as C-style string.
int toStandardGenreIndex() const
Converts the value of the current TagValue object to its equivalent standard genre index...
static int indexFromString(const std::string &genre)
Returns the numerical denotation of the specified genre or -1 if genre is unknown.
virtual bool canEncodingBeUsed(TagTextEncoding encoding) const
Returns an indication whether the specified encoding can be used to provide string values for the tag...
bool isEmpty() const
Returns an indication whether an value is assigned.
void clearMetadata()
Wipes assigned meta data.
ChronoUtilities::TimeSpan toTimeSpan() const
Converts the value of the current TagValue object to its equivalent TimeSpan representation.
static const TagValue & empty()
Returns an empty TagValue.
PositionInSet toPositionInSet() const
Converts the value of the current TagValue object to its equivalent PositionInSet representation...
bool operator==(const TagValue &other) const
Returns whether both instances are equal.
void convertDataEncoding(TagTextEncoding encoding)
Converts the currently assigned text value to the specified encoding.
TagDataType type() const
Returns the type of the assigned value.
ChronoUtilities::DateTime toDateTime() const
Converts the value of the current TagValue object to its equivalent DateTime representation.
int32 toInteger() const
Converts the value of the current TagValue object to its equivalent integer representation.
void assignInteger(int value)
Assigns the given integer value.
void assignText(const char *text, std::size_t textSize, TagTextEncoding textEncoding=TagTextEncoding::Latin1, TagTextEncoding convertTo=TagTextEncoding::Unspecified)
Assigns a copy of the given text.
TagValue & operator=(const TagValue &other)
Assigns the value of another TagValue to the current instance.
static constexpr bool isIndexSupported(int index)
Returns an indication whether the specified numerical denotation is supported by this class...
std::string toString(TagTextEncoding encoding=TagTextEncoding::Unspecified) const
Converts the value of the current TagValue object to its equivalent std::string representation.
static void stripBom(const char *&text, size_t &length, TagTextEncoding encoding)
Strips the byte order mask from the specified text.
void convertDataEncodingForTag(const Tag *tag)
Ensures the encoding of the currently assigned text value is supported by the specified tag...
void assignData(const char *data, size_t length, TagDataType type=TagDataType::Binary, TagTextEncoding encoding=TagTextEncoding::Latin1)
Assigns a copy of the given data.
TagTextEncoding
Specifies the text encoding.
static void ensureHostByteOrder(std::u16string &u16str, TagTextEncoding currentEncoding)
Ensures the byte-order of the specified UTF-16 string matches the byte-order of the machine...