6 #include <c++utilities/conversion/binaryconversion.h> 7 #include <c++utilities/conversion/stringconversion.h> 8 #include <c++utilities/conversion/conversionexception.h> 35 m_mimeType(other.m_mimeType),
37 m_labeledAsReadonly(other.m_labeledAsReadonly),
38 m_encoding(other.m_encoding),
39 m_descEncoding(other.m_descEncoding)
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;
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)
82 || m_mimeType != other.m_mimeType || m_lng != other.m_lng || 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 &) {
143 m_labeledAsReadonly =
false;
173 return ConversionUtilities::bufferToNumber<int32>(m_ptr.get(), m_size);
176 u16string u16str(reinterpret_cast<char16_t *>(m_ptr.get()), m_size / 2);
177 ensureHostByteOrder(u16str, m_encoding);
178 return ConversionUtilities::stringToNumber<int32>(u16str);
183 if(m_size ==
sizeof(int32)) {
184 return *
reinterpret_cast<int32 *
>(m_ptr.get());
186 throw ConversionException(
"Can not convert assigned data to integer because the data size is not appropriate.");
189 throw ConversionException(
"Can not convert binary data/picture/time span/date time to integer.");
209 }
catch (
const ConversionException &) {
220 if(m_size ==
sizeof(int32)) {
221 index =
static_cast<int>(*
reinterpret_cast<int32 *
>(m_ptr.get()));
223 throw ConversionException(
"The assigned data is of unappropriate size.");
227 throw ConversionException(
"It is not possible to convert assigned data to a number because of its incompatible type.");
232 throw ConversionException(
"The assigned number is not a valid standard genre index.");
255 u16string u16str(reinterpret_cast<char16_t *>(m_ptr.get()), m_size / 2);
256 ensureHostByteOrder(u16str, m_encoding);
263 return PositionInSet(*(reinterpret_cast<int32 *>(m_ptr.get())));
264 case 2 *
sizeof(int32):
265 return PositionInSet(*(reinterpret_cast<int32 *>(m_ptr.get())), *(
reinterpret_cast<int32 *
>(m_ptr.get() +
sizeof(int32))));
267 throw ConversionException(
"The size of the assigned data is not appropriate.");
270 throw ConversionException(
"Can not convert binary data/genre index/picture to \"position in set\".");
286 return TimeSpan::fromString(
string(m_ptr.get(), m_size));
291 return TimeSpan(*(reinterpret_cast<int32 *>(m_ptr.get())));
293 return TimeSpan(*(reinterpret_cast<int64 *>(m_ptr.get())));
295 throw ConversionException(
"The size of the assigned data is not appropriate.");
298 throw ConversionException(
"Can not convert binary data/genre index/position in set/picture to time span.");
314 return DateTime::fromString(
string(m_ptr.get(), m_size));
317 if(m_size ==
sizeof(int32)) {
318 return DateTime(*(reinterpret_cast<int32 *>(m_ptr.get())));
319 }
else if(m_size ==
sizeof(int64)) {
320 return DateTime(*(reinterpret_cast<int64 *>(m_ptr.get())));
322 throw ConversionException(
"The assigned data is of unappropriate size.");
325 throw ConversionException(
"Can not convert binary data/genre index/position in set/picture to date time.");
336 switch(tagTextEncoding) {
338 return make_pair(
"ISO-8859-1", 1.0f);
340 return make_pair(
"UTF-8", 1.0f);
342 return make_pair(
"UTF-16LE", 2.0f);
344 return make_pair(
"UTF-16BE", 2.0f);
346 return make_pair(
nullptr, 0.0f);
361 if(m_encoding != encoding) {
363 StringData encodedData;
369 encodedData = convertLatin1ToUtf8(m_ptr.get(), m_size);
372 encodedData = convertUtf16LEToUtf8(m_ptr.get(), m_size);
375 encodedData = convertUtf16BEToUtf8(m_ptr.get(), m_size);
385 encodedData = convertString(inputParameter.first, outputParameter.first, m_ptr.get(), m_size, outputParameter.second / inputParameter.second);
389 m_ptr = make_unique<char []>(m_size = encodedData.second);
390 copy(encodedData.first.get(), encodedData.first.get() + encodedData.second, m_ptr.get());
392 m_encoding = encoding;
422 result.assign(m_ptr.get(), m_size);
424 StringData encodedData;
430 encodedData = convertLatin1ToUtf8(m_ptr.get(), m_size);
433 encodedData = convertUtf16LEToUtf8(m_ptr.get(), m_size);
436 encodedData = convertUtf16BEToUtf8(m_ptr.get(), m_size);
446 encodedData = convertString(inputParameter.first, outputParameter.first, m_ptr.get(), m_size, outputParameter.second / inputParameter.second);
449 result.assign(encodedData.first.get(), encodedData.second);
453 result = ConversionUtilities::numberToString(
toInteger());
460 result.assign(genreName);
463 throw ConversionException(
"No string representation for the assigned standard genre index available.");
472 throw ConversionException(
"Can not convert binary data/picture to string.");
476 ? convertUtf8ToUtf16LE(result.data(), result.size())
477 : convertUtf8ToUtf16BE(result.data(), result.size());
478 result.assign(encodedData.first.get(), encodedData.second);
495 string regularStrRes;
499 result.assign(reinterpret_cast<const char16_t *>(m_ptr.get()), m_size /
sizeof(char16_t));
501 StringData encodedData;
507 encodedData = convertLatin1ToUtf8(m_ptr.get(), m_size);
510 encodedData = convertUtf16LEToUtf8(m_ptr.get(), m_size);
513 encodedData = convertUtf16BEToUtf8(m_ptr.get(), m_size);
523 encodedData = convertString(inputParameter.first, outputParameter.first, m_ptr.get(), m_size, outputParameter.second / inputParameter.second);
526 result.assign(reinterpret_cast<const char16_t *>(encodedData.first.get()), encodedData.second /
sizeof(char16_t));
530 regularStrRes = ConversionUtilities::numberToString(
toInteger());
537 regularStrRes.assign(genreName);
540 throw ConversionException(
"No string representation for the assigned standard genre index available.");
546 throw ConversionException(
"Can not convert binary data/picture to string.");
550 ? convertUtf8ToUtf16LE(regularStrRes.data(), result.size())
551 : convertUtf8ToUtf16BE(regularStrRes.data(), result.size());
552 result.assign(reinterpret_cast<const char16_t *>(encodedData.first.get()), encodedData.second /
sizeof(
const char16_t));
574 stripBom(text, textSize, textEncoding);
582 m_ptr = make_unique<char []>(m_size = textSize);
583 copy(text, text + textSize, m_ptr.get());
585 StringData encodedData;
586 switch(textEncoding) {
591 encodedData = convertUtf8ToLatin1(text, textSize);
594 encodedData = convertUtf8ToUtf16LE(text, textSize);
597 encodedData = convertUtf8ToUtf16BE(text, textSize);
607 encodedData = convertString(inputParameter.first, outputParameter.first, text, textSize, outputParameter.second / inputParameter.second);
611 m_ptr = make_unique<char []>(m_size = encodedData.second);
612 copy(encodedData.first.get(), encodedData.first.get() + encodedData.second, m_ptr.get());
622 m_size =
sizeof(value);
623 m_ptr = make_unique<char []>(m_size);
624 std::copy(reinterpret_cast<const char *>(&value), reinterpret_cast<const char *>(&value) + m_size, m_ptr.get());
651 stripBom(data, length, encoding);
653 if(length > m_size) {
654 m_ptr = make_unique<char[]>(length);
657 std::copy(data, data + length, m_ptr.get());
663 m_encoding = encoding;
682 m_encoding = encoding;
689 void TagValue::stripBom(
const char *&text,
size_t &length,
TagTextEncoding encoding)
693 if((length >= 3) && (ConversionUtilities::BE::toUInt24(text) == 0x00EFBBBF)) {
699 if((length >= 2) && (ConversionUtilities::LE::toUInt16(text) == 0xFEFF)) {
705 if((length >= 2) && (ConversionUtilities::BE::toUInt16(text) == 0xFEFF)) {
719 void TagValue::ensureHostByteOrder(u16string &u16str,
TagTextEncoding currentEncoding)
721 if(currentEncoding !=
722 #
if defined(CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN)
724 #elif defined(CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN)
727 # error
"Host byte order not supported" 730 for(
auto &c : u16str) {
731 c = swapOrder(static_cast<uint16>(c));
742 return emptyTagValue;