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());
79 if(m_desc != other.m_desc || (!m_desc.empty() && m_descEncoding != other.m_descEncoding)
80 || m_mimeType != other.m_mimeType || m_lng != other.m_lng || m_labeledAsReadonly != other.m_labeledAsReadonly) {
83 if(m_type == other.m_type) {
86 if(m_size != other.m_size && m_encoding != other.m_encoding) {
90 return strncmp(m_ptr.get(), other.m_ptr.get(), m_size) == 0;
104 if(m_size != other.m_size) {
107 return strncmp(m_ptr.get(), other.m_ptr.get(), m_size) == 0;
117 }
catch(
const ConversionException &) {
141 m_labeledAsReadonly =
false;
167 return ConversionUtilities::stringToNumber<int32>(string(m_ptr.get(), m_size));
171 if(m_size ==
sizeof(int32)) {
172 auto res = *
reinterpret_cast<int32 *
>(m_ptr.get());
175 throw ConversionException(
"Can not convert assigned data to integer because the data size is not appropriate.");
179 throw ConversionException(
"Can not convert binary data/picture/time span/date time to integer.");
196 string s(m_ptr.get(), m_size);
198 index = ConversionUtilities::stringToNumber<int32>(s);
199 }
catch (ConversionException &) {
205 if(m_size ==
sizeof(
int)) {
206 index = *
reinterpret_cast<int *
>(m_ptr.get());
208 throw ConversionException(
"The assigned data is of unappropriate size.");
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.");
241 return PositionInSet(u16string(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<int32 *>(m_ptr.get())));
303 }
else if(m_size ==
sizeof(int64)) {
304 return DateTime(*(reinterpret_cast<int64 *>(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.");
320 switch(tagTextEncoding) {
322 return make_pair(
"ISO-8859-1", 1.0f);
324 return make_pair(
"UTF-8", 1.0f);
326 return make_pair(
"UTF-16LE", 2.0f);
328 return make_pair(
"UTF-16BE", 2.0f);
330 return make_pair(
nullptr, 0.0f);
345 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 encodedData = 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;
406 result.assign(m_ptr.get(), m_size);
408 StringData encodedData;
414 encodedData = convertLatin1ToUtf8(m_ptr.get(), m_size);
417 encodedData = convertUtf16LEToUtf8(m_ptr.get(), m_size);
420 encodedData = convertUtf16BEToUtf8(m_ptr.get(), m_size);
430 encodedData = convertString(inputParameter.first, outputParameter.first, m_ptr.get(), m_size, outputParameter.second / inputParameter.second);
433 result.assign(encodedData.first.get(), encodedData.second);
437 result = ConversionUtilities::numberToString(
toInteger());
444 result.assign(genreName);
447 throw ConversionException(
"No string representation for the assigned standard genre index available.");
453 throw ConversionException(
"Can not convert binary data/picture to string.");
457 ? convertUtf8ToUtf16LE(result.data(), result.size())
458 : convertUtf8ToUtf16BE(result.data(), result.size());
459 result.assign(encodedData.first.get(), encodedData.second);
476 string regularStrRes;
480 result.assign(reinterpret_cast<const char16_t *>(m_ptr.get()), m_size /
sizeof(char16_t));
482 StringData encodedData;
488 encodedData = convertLatin1ToUtf8(m_ptr.get(), m_size);
491 encodedData = convertUtf16LEToUtf8(m_ptr.get(), m_size);
494 encodedData = convertUtf16BEToUtf8(m_ptr.get(), m_size);
504 encodedData = convertString(inputParameter.first, outputParameter.first, m_ptr.get(), m_size, outputParameter.second / inputParameter.second);
507 result.assign(reinterpret_cast<const char16_t *>(encodedData.first.get()), encodedData.second /
sizeof(char16_t));
511 regularStrRes = ConversionUtilities::numberToString(
toInteger());
518 regularStrRes.assign(genreName);
521 throw ConversionException(
"No string representation for the assigned standard genre index available.");
527 throw ConversionException(
"Can not convert binary data/picture to string.");
531 ? convertUtf8ToUtf16LE(regularStrRes.data(), result.size())
532 : convertUtf8ToUtf16BE(regularStrRes.data(), result.size());
533 result.assign(reinterpret_cast<const char16_t *>(encodedData.first.get()), encodedData.second /
sizeof(
const char16_t));
555 stripBom(text, textSize, textEncoding);
563 m_ptr = make_unique<char []>(m_size = textSize);
564 copy(text, text + textSize, m_ptr.get());
566 StringData encodedData;
567 switch(textEncoding) {
572 encodedData = convertUtf8ToLatin1(text, textSize);
575 encodedData = convertUtf8ToUtf16LE(text, textSize);
578 encodedData = convertUtf8ToUtf16BE(text, textSize);
588 encodedData = convertString(inputParameter.first, outputParameter.first, text, textSize, outputParameter.second / inputParameter.second);
592 m_ptr = make_unique<char []>(m_size = encodedData.second);
593 copy(encodedData.first.get(), encodedData.first.get() + encodedData.second, m_ptr.get());
603 m_size =
sizeof(value);
604 m_ptr = make_unique<char []>(m_size);
605 std::copy(reinterpret_cast<const char *>(&value), reinterpret_cast<const char *>(&value) + m_size, m_ptr.get());
632 stripBom(data, length, encoding);
634 if(length > m_size) {
635 m_ptr = make_unique<char[]>(length);
638 std::copy(data, data + length, m_ptr.get());
644 m_encoding = encoding;
663 m_encoding = encoding;
670 void TagValue::stripBom(
const char *&text,
size_t &length,
TagTextEncoding encoding)
674 if((length >= 3) && (ConversionUtilities::BE::toUInt24(text) == 0x00EFBBBF)) {
680 if((length >= 2) && (ConversionUtilities::LE::toUInt16(text) == 0xFEFF)) {
686 if((length >= 2) && (ConversionUtilities::BE::toUInt16(text) == 0xFEFF)) {
702 return emptyTagValue;