-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtagvalue.cpp
1554 lines (1483 loc) · 61.5 KB
/
tagvalue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "./tagvalue.h"
#include "./caseinsensitivecomparer.h"
#include "./tag.h"
#include "./id3/id3genres.h"
#include <c++utilities/conversion/binaryconversion.h>
#include <c++utilities/conversion/conversionexception.h>
#include <c++utilities/conversion/stringbuilder.h>
#include <c++utilities/conversion/stringconversion.h>
#include <c++utilities/io/binaryreader.h>
#include <c++utilities/io/binarywriter.h>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <utility>
using namespace std;
using namespace CppUtilities;
namespace TagParser {
/// \brief The TagValuePrivate struct contains private fields of the TagValue class.
struct TagValuePrivate {};
/*!
* \brief Returns the string representation of the specified \a dataType.
*/
std::string_view tagDataTypeString(TagDataType dataType)
{
switch (dataType) {
case TagDataType::Text:
return "text";
case TagDataType::Integer:
return "integer";
case TagDataType::PositionInSet:
return "position in set";
case TagDataType::StandardGenreIndex:
return "genre index";
case TagDataType::TimeSpan:
return "time span";
case TagDataType::DateTime:
return "date time";
case TagDataType::Picture:
return "picture";
case TagDataType::Binary:
return "binary";
case TagDataType::Popularity:
return "popularity";
case TagDataType::UnsignedInteger:
return "unsigned integer";
case TagDataType::DateTimeExpression:
return "date time expression";
default:
return "undefined";
}
}
/*!
* \brief Returns the encoding parameter (name of the character set and bytes per character) for the specified \a tagTextEncoding.
*/
pair<const char *, float> encodingParameter(TagTextEncoding tagTextEncoding)
{
switch (tagTextEncoding) {
case TagTextEncoding::Latin1:
return make_pair("ISO-8859-1", 1.0f);
case TagTextEncoding::Utf8:
return make_pair("UTF-8", 1.0f);
case TagTextEncoding::Utf16LittleEndian:
return make_pair("UTF-16LE", 2.0f);
case TagTextEncoding::Utf16BigEndian:
return make_pair("UTF-16BE", 2.0f);
default:
return make_pair(nullptr, 0.0f);
}
}
/*!
* \class TagParser::Popularity
* \brief The Popularity class contains a value for ID3v2's "Popularimeter" field.
* \remarks It can also be used for other formats than ID3v2.
* \sa See documentation of TagParser::Popularity for scaling.
*/
/*!
* \class TagParser::TagValue
* \brief The TagValue class wraps values of different types. It is meant to be assigned to a tag field.
*
* For a list of supported types see TagParser::TagDataType.
*
* When constructing a TagValue choose the type which suites the value you want to store best. If the
* tag format uses a different type the serializer will take care of the neccassary conversion (eg. convert
* an integer to a string).
*
* When consuming a TagValue read from a tag one should not expect that a particular type is used. The
* type depends on what the particular tag format uses. However, the conversion functions provided by the
* TagValue class take care of neccassary conversions, eg. TagValue::toInteger() will attempt to convert a
* string to a number (an possibly throw a ConversionException on failure).
*
* Values of the type TagDataType::Text can be differently encoded.
* - See TagParser::TagTextEncoding for a list of encodings supported by this library.
* - Tag formats usually only support a subset of these encodings. The serializers for the various tag
* formats provided by this library will keep the encoding if possible and otherwise convert the assigned
* text to an encoding supported by the tag format on the fly. Note that ID3v1 does not specify which
* encodings are supported (or unsupported) so the serializer will just write text data as-is.
* - The deserializers will store text data in the encoding that is used in the tag.
* - The functions Tag::canEncodingBeUsed() and Tag::proposedTextEncoding() can be used to check
* whether an encoding can be used by a certain tag format to avoid any unnecessary character set
* conversions.
* - There's also the function Tag::ensureTextValuesAreProperlyEncoded() which can be used to convert all
* text values currently assigned to a tag to the encoding which is deemed best for the current tag format.
* This function is a bit more agressive than the implict conversions, e.g. it ensures no UTF-16 encoded
* text ends up in ID3v1 tags.
* - If you want to use UTF-8 everywhere, simply always assign UTF-8 text and use
* TagValue::toString(TagTextEncoding::Utf8) when reading text.
*
* Values of the type TagDataType::Popularity might use different rating scales depending on the tag
* format.
* - You can assign a Popularity object of any scale. Tag implementations will convert it accordingly.
* - You can use TagValue::toScaledPopularity() to retrieve a Popularity object of the desired scale.
* - When just working with text data (via TagValue::toString() and TagValue::assignText()), no scaling
* of internally assigned Popularity objects is done; so you're working with raw rating values in this
* case.
*
* Values of the type TagDataType::Text are not supposed to contain Byte-Order-Marks. Before assigning text
* which might be prepended by a Byte-Order-Mark the helper function TagValue::stripBom() can be used.
*/
/*!
* \brief Constructs a new TagValue holding a copy of the given TagValue instance.
* \param other Specifies another TagValue instance.
*/
TagValue::TagValue(const TagValue &other)
: m_size(other.m_size)
, m_desc(other.m_desc)
, m_mimeType(other.m_mimeType)
, m_locale(other.m_locale)
, m_type(other.m_type)
, m_encoding(other.m_encoding)
, m_descEncoding(other.m_descEncoding)
, m_flags(TagValueFlags::None)
{
if (!other.isEmpty()) {
m_ptr = make_unique<char[]>(m_size);
std::copy(other.m_ptr.get(), other.m_ptr.get() + other.m_size, m_ptr.get());
}
}
TagValue::TagValue(TagValue &&other) = default;
/*!
* \brief Constructs an empty TagValue.
*/
TagValue::TagValue()
: m_size(0)
, m_type(TagDataType::Undefined)
, m_encoding(TagTextEncoding::Latin1)
, m_descEncoding(TagTextEncoding::Latin1)
, m_flags(TagValueFlags::None)
{
}
/*!
* \brief Constructs a new TagValue holding a copy of the given \a text.
* \param text Specifies the text to be assigned.
* \param textSize Specifies the size of \a text. (The actual number of bytes, not the number of characters.)
* \param textEncoding Specifies the encoding of the given \a text.
* \param convertTo Specifies the encoding to convert \a text to; set to TagTextEncoding::Unspecified to
* use \a textEncoding without any character set conversions.
* \throws Throws a ConversionException if the conversion the specified character set fails.
* \remarks Strips the BOM of the specified \a text.
*/
TagValue::TagValue(const char *text, std::size_t textSize, TagTextEncoding textEncoding, TagTextEncoding convertTo)
: m_descEncoding(TagTextEncoding::Latin1)
, m_flags(TagValueFlags::None)
{
assignText(text, textSize, textEncoding, convertTo);
}
/*!
* \brief Constructs a new TagValue holding a copy of the given \a text.
* \param text Specifies the text to be assigned. This string must be null-terminated.
* \param textEncoding Specifies the encoding of the given \a text.
* \param convertTo Specifies the encoding to convert \a text to; set to TagTextEncoding::Unspecified to
* use \a textEncoding without any character set conversions.
* \throws Throws a ConversionException if the conversion the specified character set fails.
* \remarks Strips the BOM of the specified \a text.
*/
TagValue::TagValue(const char *text, TagTextEncoding textEncoding, TagTextEncoding convertTo)
{
assignText(text, std::strlen(text), textEncoding, convertTo);
}
/*!
* \brief Constructs a new TagValue holding a copy of the given \a text.
* \param text Specifies the text to be assigned.
* \param textEncoding Specifies the encoding of the given \a text.
* \param convertTo Specifies the encoding to convert \a text to; set to TagTextEncoding::Unspecified to
* use \a textEncoding without any character set conversions.
* \throws Throws a ConversionException if the conversion the specified character set fails.
* \remarks Strips the BOM of the specified \a text.
*/
TagValue::TagValue(const std::string &text, TagTextEncoding textEncoding, TagTextEncoding convertTo)
: m_descEncoding(TagTextEncoding::Latin1)
, m_flags(TagValueFlags::None)
{
assignText(text, textEncoding, convertTo);
}
/*!
* \brief Constructs a new TagValue holding a copy of the given \a text.
* \param text Specifies the text to be assigned.
* \param textEncoding Specifies the encoding of the given \a text.
* \param convertTo Specifies the encoding to convert \a text to; set to TagTextEncoding::Unspecified to
* use \a textEncoding without any character set conversions.
* \throws Throws a ConversionException if the conversion the specified character set fails.
* \remarks Strips the BOM of the specified \a text.
*/
TagValue::TagValue(std::string_view text, TagTextEncoding textEncoding, TagTextEncoding convertTo)
: m_descEncoding(TagTextEncoding::Latin1)
, m_flags(TagValueFlags::None)
{
assignText(text, textEncoding, convertTo);
}
/*!
* \brief Destroys the TagValue.
*/
TagValue::~TagValue()
{
}
/*!
* \brief Constructs a new TagValue with a copy of the given \a data.
*
* \param data Specifies a pointer to the data.
* \param length Specifies the length of the data.
* \param type Specifies the type of the data as TagDataType.
* \param encoding Specifies the encoding of the data as TagTextEncoding. The
* encoding will only be considered if a text is assigned.
* \remarks Strips the BOM of the specified \a data if \a type is TagDataType::Text.
*/
TagValue::TagValue(const char *data, std::size_t length, TagDataType type, TagTextEncoding encoding)
: m_size(length)
, m_type(type)
, m_encoding(encoding)
, m_descEncoding(TagTextEncoding::Latin1)
, m_flags(TagValueFlags::None)
{
if (length) {
if (type == TagDataType::Text) {
stripBom(data, m_size, encoding);
}
m_ptr = std::make_unique<char[]>(m_size);
std::copy(data, data + m_size, m_ptr.get());
}
}
/*!
* \brief Constructs a new TagValue holding with the given \a data.
*
* The \a data is not copied. It is moved.
*
* \param data Specifies a pointer to the data.
* \param length Specifies the length of the data.
* \param type Specifies the type of the data as TagDataType.
* \param encoding Specifies the encoding of the data as TagTextEncoding. The
* encoding will only be considered if a text is assigned.
* \remarks Does not strip the BOM so for consistency the caller must ensure there is no BOM present.
*/
TagValue::TagValue(std::unique_ptr<char[]> &&data, std::size_t length, TagDataType type, TagTextEncoding encoding)
: m_size(length)
, m_type(type)
, m_encoding(encoding)
, m_descEncoding(TagTextEncoding::Latin1)
, m_flags(TagValueFlags::None)
{
if (length) {
m_ptr = std::move(data);
}
}
/*!
* \brief Assigns the value of another TagValue to the current instance.
*/
TagValue &TagValue::operator=(const TagValue &other)
{
if (this == &other) {
return *this;
}
m_size = other.m_size;
m_type = other.m_type;
m_desc = other.m_desc;
m_mimeType = other.m_mimeType;
m_locale = other.m_locale;
m_flags = other.m_flags;
m_encoding = other.m_encoding;
m_descEncoding = other.m_descEncoding;
if (other.isEmpty()) {
m_ptr.reset();
} else {
m_ptr = make_unique<char[]>(m_size);
std::copy(other.m_ptr.get(), other.m_ptr.get() + other.m_size, m_ptr.get());
}
return *this;
}
TagValue &TagValue::operator=(TagValue &&other) = default;
/// \cond
TagTextEncoding pickUtfEncoding(TagTextEncoding encoding1, TagTextEncoding encoding2)
{
switch (encoding1) {
case TagTextEncoding::Utf8:
case TagTextEncoding::Utf16LittleEndian:
case TagTextEncoding::Utf16BigEndian:
return encoding1;
default:
switch (encoding2) {
case TagTextEncoding::Utf8:
case TagTextEncoding::Utf16LittleEndian:
case TagTextEncoding::Utf16BigEndian:
return encoding2;
default:;
}
}
return TagTextEncoding::Utf8;
}
/// \endcond
/*!
* \brief Returns whether both instances are equal. Meta-data like description and MIME-type is taken into
* account as well.
* \arg other Specifies the other instance.
* \arg options Specifies options to alter the behavior. See TagValueComparisionFlags for details.
* \remarks
* - If the data types are not equal, two instances are still considered equal if the string representation
* is identical. For instance the text "2" is considered equal to the integer 2. This also means that an empty
* TagValue and the integer 0 are *not* considered equal.
* - The choice to allow implicit conversions was made because different tag formats use different types and
* usually one does not care about those internals when comparing values.
* - If any of the differently typed values can not be converted to a string (eg. it is binary data) the values
* are *not* considered equal. So the text "foo" and the binary value "foo" are not considered equal although
* the raw data is identical.
* - If the type is TagDataType::Text and the encoding differs values might still be considered equal if they
* represent the same characters. The same counts for the description.
* - This might be a costly operation due to possible conversions.
* \sa
* - TagValue::compareData() to compare raw data without any conversions
* - TagValueTests::testEqualityOperator() for examples
*/
bool TagValue::compareTo(const TagValue &other, TagValueComparisionFlags options) const
{
// check whether meta-data is equal (except description)
if (!(options & TagValueComparisionFlags::IgnoreMetaData)) {
// check meta-data which always uses UTF-8 (everything but description)
if (m_mimeType != other.m_mimeType || m_locale != other.m_locale || m_flags != other.m_flags) {
return false;
}
// check description which might use different encodings
if (m_descEncoding == other.m_descEncoding || m_descEncoding == TagTextEncoding::Unspecified
|| other.m_descEncoding == TagTextEncoding::Unspecified || m_desc.empty() || other.m_desc.empty()) {
if (!compareData(m_desc, other.m_desc, options & TagValueComparisionFlags::CaseInsensitive)) {
return false;
}
} else {
const auto utfEncodingToUse = pickUtfEncoding(m_descEncoding, other.m_descEncoding);
StringData str1, str2;
const char *data1, *data2;
size_t size1, size2;
if (m_descEncoding != utfEncodingToUse) {
const auto inputParameter = encodingParameter(m_descEncoding), outputParameter = encodingParameter(utfEncodingToUse);
str1 = convertString(
inputParameter.first, outputParameter.first, m_desc.data(), m_desc.size(), outputParameter.second / inputParameter.second);
data1 = str1.first.get();
size1 = str1.second;
} else {
data1 = m_desc.data();
size1 = m_desc.size();
}
if (other.m_descEncoding != utfEncodingToUse) {
const auto inputParameter = encodingParameter(other.m_descEncoding), outputParameter = encodingParameter(utfEncodingToUse);
str2 = convertString(inputParameter.first, outputParameter.first, other.m_desc.data(), other.m_desc.size(),
outputParameter.second / inputParameter.second);
data2 = str2.first.get();
size2 = str2.second;
} else {
data2 = other.m_desc.data();
size2 = other.m_desc.size();
}
if (!compareData(data1, size1, data2, size2, options & TagValueComparisionFlags::CaseInsensitive)) {
return false;
}
}
}
try {
// check for equality if both types are identical
if (m_type == other.m_type) {
switch (m_type) {
case TagDataType::Text: {
// compare raw data directly if the encoding is the same
if (m_size != other.m_size && m_encoding == other.m_encoding) {
return false;
}
if (m_encoding == other.m_encoding || m_encoding == TagTextEncoding::Unspecified
|| other.m_encoding == TagTextEncoding::Unspecified) {
return compareData(other, options & TagValueComparisionFlags::CaseInsensitive);
}
// compare UTF-8 or UTF-16 representation of strings avoiding unnecessary conversions
const auto utfEncodingToUse = pickUtfEncoding(m_encoding, other.m_encoding);
string str1, str2;
const char *data1, *data2;
size_t size1, size2;
if (m_encoding != utfEncodingToUse) {
str1 = toString(utfEncodingToUse);
data1 = str1.data();
size1 = str1.size();
} else {
data1 = m_ptr.get();
size1 = m_size;
}
if (other.m_encoding != utfEncodingToUse) {
str2 = other.toString(utfEncodingToUse);
data2 = str2.data();
size2 = str2.size();
} else {
data2 = other.m_ptr.get();
size2 = other.m_size;
}
return compareData(data1, size1, data2, size2, options & TagValueComparisionFlags::CaseInsensitive);
}
case TagDataType::PositionInSet:
return toPositionInSet() == other.toPositionInSet();
case TagDataType::StandardGenreIndex:
return toStandardGenreIndex() == other.toStandardGenreIndex();
case TagDataType::TimeSpan:
return toTimeSpan() == other.toTimeSpan();
case TagDataType::DateTime:
return toDateTime() == other.toDateTime();
case TagDataType::DateTimeExpression:
return toDateTimeExpression() == other.toDateTimeExpression();
case TagDataType::Picture:
case TagDataType::Binary:
case TagDataType::Undefined:
return compareData(other);
default:;
}
}
// do not attempt implicit conversions for certain types
// TODO: Maybe it would actually make sense for some of these types (at least when the other type is
// string)?
for (const auto dataType : { m_type, other.m_type }) {
switch (dataType) {
case TagDataType::TimeSpan:
case TagDataType::DateTime:
case TagDataType::DateTimeExpression:
case TagDataType::Picture:
case TagDataType::Binary:
case TagDataType::Undefined:
return false;
default:;
}
}
// handle types where an implicit conversion to the specific type can be done
if (m_type == TagDataType::Integer || other.m_type == TagDataType::Integer) {
return toInteger() == other.toInteger();
} else if (m_type == TagDataType::UnsignedInteger || other.m_type == TagDataType::UnsignedInteger) {
return toUnsignedInteger() == other.toUnsignedInteger();
} else if (m_type == TagDataType::Popularity || other.m_type == TagDataType::Popularity) {
if (options & TagValueComparisionFlags::CaseInsensitive) {
const auto lhs = toPopularity(), rhs = other.toPopularity();
return lhs.rating == rhs.rating && lhs.playCounter == rhs.playCounter && lhs.scale == rhs.scale
&& compareData(lhs.user, rhs.user, true);
} else {
return toPopularity() == other.toPopularity();
}
}
// handle other types where an implicit conversion to string can be done by comparing the string representation
return compareData(toString(), other.toString(m_encoding), options & TagValueComparisionFlags::CaseInsensitive);
} catch (const ConversionException &) {
return false;
}
}
/*!
* \brief Wipes assigned meta data.
* - Clears description, mime type, language and flags.
* - Resets the encoding to TagTextEncoding::Latin1.
* - Resets the data type to TagDataType::Undefined.
*/
void TagValue::clearMetadata()
{
m_desc.clear();
m_mimeType.clear();
m_locale.clear();
m_flags = TagValueFlags::None;
m_encoding = TagTextEncoding::Latin1;
m_descEncoding = TagTextEncoding::Latin1;
m_type = TagDataType::Undefined;
}
/*!
* \brief Returns a "display string" for the specified value.
* \returns
* - Returns the just the type if no displayable string can be made of it, eg. "picture", otherwise
* returns the string representation.
* - Returns "invalid …" if a conversion error returned when making the string representation but
* never throws a ConversionException (unlike toString()).
*/
std::string TagParser::TagValue::toDisplayString() const
{
switch (m_type) {
case TagDataType::Undefined:
case TagDataType::Binary:
case TagDataType::Picture:
return std::string(tagDataTypeString(m_type));
default:
try {
return toString(TagTextEncoding::Utf8);
} catch (const ConversionException &e) {
return argsToString("invalid ", tagDataTypeString(m_type), ':', ' ', e.what());
}
}
}
/*!
* \brief Converts the value of the current TagValue object to its equivalent
* integer representation.
* \throws Throws ConversionException on failure.
*/
std::int32_t TagValue::toInteger() const
{
if (isEmpty()) {
return 0;
}
switch (m_type) {
case TagDataType::Text:
switch (m_encoding) {
case TagTextEncoding::Utf16LittleEndian:
case TagTextEncoding::Utf16BigEndian: {
auto u16str = u16string(reinterpret_cast<char16_t *>(m_ptr.get()), m_size / 2);
ensureHostByteOrder(u16str, m_encoding);
return stringToNumber<std::int32_t>(u16str);
}
default:
return bufferToNumber<std::int32_t>(m_ptr.get(), m_size);
}
case TagDataType::PositionInSet:
if (m_size == sizeof(PositionInSet)) {
return *reinterpret_cast<std::int32_t *>(m_ptr.get());
}
throw ConversionException("Can not convert assigned data to integer because the data size is not appropriate.");
case TagDataType::Integer:
case TagDataType::StandardGenreIndex:
if (m_size == sizeof(std::int32_t)) {
return *reinterpret_cast<std::int32_t *>(m_ptr.get());
}
throw ConversionException("Can not convert assigned data to integer because the data size is not appropriate.");
case TagDataType::Popularity:
return static_cast<std::int32_t>(toPopularity().rating);
case TagDataType::UnsignedInteger: {
const auto unsignedInteger = toUnsignedInteger();
if (unsignedInteger > std::numeric_limits<std::int32_t>::max()) {
throw ConversionException(argsToString("Unsigned integer too big for conversion to integer."));
}
return static_cast<std::int32_t>(unsignedInteger);
}
default:
throw ConversionException(argsToString("Can not convert ", tagDataTypeString(m_type), " to integer."));
}
}
std::uint64_t TagValue::toUnsignedInteger() const
{
if (isEmpty()) {
return 0;
}
switch (m_type) {
case TagDataType::Text:
switch (m_encoding) {
case TagTextEncoding::Utf16LittleEndian:
case TagTextEncoding::Utf16BigEndian: {
auto u16str = u16string(reinterpret_cast<char16_t *>(m_ptr.get()), m_size / 2);
ensureHostByteOrder(u16str, m_encoding);
return stringToNumber<std::uint64_t>(u16str);
}
default:
return bufferToNumber<std::uint64_t>(m_ptr.get(), m_size);
}
case TagDataType::PositionInSet:
case TagDataType::Integer:
case TagDataType::StandardGenreIndex: {
const auto integer = toInteger();
if (integer < 0) {
throw ConversionException(argsToString("Can not convert negative value to unsigned integer."));
}
return static_cast<std::uint64_t>(integer);
}
case TagDataType::Popularity:
return static_cast<std::uint64_t>(toPopularity().rating);
case TagDataType::UnsignedInteger:
if (m_size == sizeof(std::uint64_t)) {
return *reinterpret_cast<std::uint64_t *>(m_ptr.get());
}
throw ConversionException("Can not convert assigned data to unsigned integer because the data size is not appropriate.");
default:
throw ConversionException(argsToString("Can not convert ", tagDataTypeString(m_type), " to unsigned integer."));
}
}
/*!
* \brief Converts the value of the current TagValue object to its equivalent
* standard genre index.
* \throws Throws ConversionException on failure.
*/
int TagValue::toStandardGenreIndex() const
{
if (isEmpty()) {
return Id3Genres::emptyGenreIndex();
}
int index = 0;
switch (m_type) {
case TagDataType::Text: {
try {
index = toInteger();
} catch (const ConversionException &) {
TagTextEncoding encoding = TagTextEncoding::Utf8;
if (m_encoding == TagTextEncoding::Latin1) {
// no need to convert Latin-1 to UTF-8 (makes no difference in case of genre strings)
encoding = TagTextEncoding::Unspecified;
}
index = Id3Genres::indexFromString(toString(encoding));
}
break;
}
case TagDataType::StandardGenreIndex:
case TagDataType::Integer:
case TagDataType::UnsignedInteger:
if (m_size == sizeof(std::int32_t)) {
index = static_cast<int>(*reinterpret_cast<std::int32_t *>(m_ptr.get()));
} else if (m_size == sizeof(std::uint64_t)) {
const auto unsignedInt = *reinterpret_cast<std::uint64_t *>(m_ptr.get());
if (unsignedInt <= std::numeric_limits<int>::max()) {
index = static_cast<int>(unsignedInt);
} else {
index = Id3Genres::genreCount();
}
} else {
throw ConversionException("The assigned index/integer is of unappropriate size.");
}
break;
default:
throw ConversionException(argsToString("Can not convert ", tagDataTypeString(m_type), " to genre index."));
}
if (!Id3Genres::isEmptyGenre(index) && !Id3Genres::isIndexSupported(index)) {
throw ConversionException("The assigned number is not a valid standard genre index.");
}
return index;
}
/*!
* \brief Converts the value of the current TagValue object to its equivalent
* PositionInSet representation.
* \throws Throws ConversionException on failure.
*/
PositionInSet TagValue::toPositionInSet() const
{
if (isEmpty()) {
return PositionInSet();
}
switch (m_type) {
case TagDataType::Text:
switch (m_encoding) {
case TagTextEncoding::Utf16LittleEndian:
case TagTextEncoding::Utf16BigEndian: {
auto u16str = u16string(reinterpret_cast<char16_t *>(m_ptr.get()), m_size / 2);
ensureHostByteOrder(u16str, m_encoding);
return PositionInSet(u16str);
}
default:
return PositionInSet(string(m_ptr.get(), m_size));
}
case TagDataType::Integer:
case TagDataType::PositionInSet:
switch (m_size) {
case sizeof(std::int32_t):
return PositionInSet(*(reinterpret_cast<std::int32_t *>(m_ptr.get())));
case 2 * sizeof(std::int32_t):
return PositionInSet(
*(reinterpret_cast<std::int32_t *>(m_ptr.get())), *(reinterpret_cast<std::int32_t *>(m_ptr.get() + sizeof(std::int32_t))));
default:
throw ConversionException("The size of the assigned data is not appropriate.");
}
case TagDataType::UnsignedInteger:
switch (m_size) {
case sizeof(std::uint64_t): {
const auto track = *(reinterpret_cast<std::uint64_t *>(m_ptr.get()));
if (track < std::numeric_limits<std::int32_t>::max()) {
return PositionInSet(static_cast<std::int32_t>(track));
}
}
default:;
}
throw ConversionException("The size of the assigned data is not appropriate.");
default:
throw ConversionException(argsToString("Can not convert ", tagDataTypeString(m_type), " to position in set."));
}
}
/*!
* \brief Converts the value of the current TagValue object to its equivalent
* TimeSpan representation.
* \throws Throws ConversionException on failure.
*/
TimeSpan TagValue::toTimeSpan() const
{
if (isEmpty()) {
return TimeSpan();
}
switch (m_type) {
case TagDataType::Text:
return TimeSpan::fromString(toString(m_encoding == TagTextEncoding::Utf8 ? TagTextEncoding::Utf8 : TagTextEncoding::Latin1));
case TagDataType::Integer:
case TagDataType::TimeSpan:
switch (m_size) {
case sizeof(std::int32_t):
return TimeSpan(*(reinterpret_cast<std::int32_t *>(m_ptr.get())));
case sizeof(std::int64_t):
return TimeSpan(*(reinterpret_cast<std::int64_t *>(m_ptr.get())));
default:
throw ConversionException("The size of the assigned data is not appropriate for conversion to time span.");
}
case TagDataType::UnsignedInteger:
switch (m_size) {
case sizeof(std::uint64_t): {
const auto ticks = *(reinterpret_cast<std::uint64_t *>(m_ptr.get()));
if (ticks < static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max())) {
return TimeSpan(static_cast<std::int64_t>(ticks));
}
}
default:;
}
throw ConversionException("The size of the assigned data is not appropriate.");
default:
throw ConversionException(argsToString("Can not convert ", tagDataTypeString(m_type), " to time span."));
}
}
/*!
* \brief Converts the value of the current TagValue object to its equivalent
* DateTime representation (using the UTC timezone).
* \throws Throws ConversionException on failure.
*/
DateTime TagValue::toDateTime() const
{
if (isEmpty()) {
return DateTime();
}
switch (m_type) {
case TagDataType::Text: {
const auto str = toString(m_encoding == TagTextEncoding::Utf8 ? TagTextEncoding::Utf8 : TagTextEncoding::Latin1);
try {
return DateTime::fromIsoStringGmt(str.data());
} catch (const ConversionException &) {
return DateTime::fromString(str);
}
}
case TagDataType::Integer:
case TagDataType::DateTime:
case TagDataType::UnsignedInteger:
if (m_size == sizeof(std::int32_t)) {
return DateTime(*(reinterpret_cast<std::uint32_t *>(m_ptr.get())));
} else if (m_size == sizeof(std::uint64_t)) {
return DateTime(*(reinterpret_cast<std::uint64_t *>(m_ptr.get())));
} else {
throw ConversionException("The size of the assigned data is not appropriate for conversion to date time.");
}
case TagDataType::DateTimeExpression:
return toDateTimeExpression().gmt();
default:
throw ConversionException(argsToString("Can not convert ", tagDataTypeString(m_type), " to date time."));
}
}
/*!
* \brief Converts the value of the current TagValue object to its equivalent
* DateTimeExpression representation.
* \throws Throws ConversionException on failure.
*/
CppUtilities::DateTimeExpression TagParser::TagValue::toDateTimeExpression() const
{
if (isEmpty()) {
return DateTimeExpression();
}
switch (m_type) {
case TagDataType::Text: {
const auto str = toString(m_encoding == TagTextEncoding::Utf8 ? TagTextEncoding::Utf8 : TagTextEncoding::Latin1);
try {
return DateTimeExpression::fromIsoString(str.data());
} catch (const ConversionException &) {
return DateTimeExpression::fromString(str.data());
}
}
case TagDataType::Integer:
case TagDataType::DateTime:
case TagDataType::UnsignedInteger:
return DateTimeExpression{ .value = toDateTime(), .delta = TimeSpan(), .parts = DateTimeParts::DateTime };
case TagDataType::DateTimeExpression:
if (m_size == sizeof(DateTimeExpression)) {
return *reinterpret_cast<DateTimeExpression *>(m_ptr.get());
} else {
throw ConversionException("The size of the assigned data is not appropriate for conversion to date time expression.");
}
default:
throw ConversionException(argsToString("Can not convert ", tagDataTypeString(m_type), " to date time."));
}
}
/*!
* \brief Converts the value of the current TagValue object to its equivalent
* Popularity representation.
* \throws Throws ConversionException on failure.
* \remarks
* - If text is assigned, the returned popularity's scale will always be TagType::Unspecified
* as the text representation does not preserve the scale. Assign the correct scale if needed
* manually. Note that tag field implementations provided by this library take care to assign a
* popularity (and not just text) when parsing the popularity/rating fields to preserve the
* scale information.
* - Use TagValue::toScaledPopularity() if you want to convert the rating to a certain scale (to
* use that scale consistently without having to deal with multiple scales yourself).
*/
Popularity TagValue::toPopularity() const
{
auto popularity = Popularity();
if (isEmpty()) {
return popularity;
}
switch (m_type) {
case TagDataType::Text:
popularity = Popularity::fromString(std::string_view(toString(TagTextEncoding::Utf8)));
break;
case TagDataType::Integer:
popularity.rating = static_cast<double>(toInteger());
break;
case TagDataType::Popularity: {
auto s = std::stringstream(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
auto reader = BinaryReader(&s);
try {
s.exceptions(std::ios_base::failbit | std::ios_base::badbit);
#if defined(__GLIBCXX__) && !defined(_LIBCPP_VERSION)
s.rdbuf()->pubsetbuf(m_ptr.get(), static_cast<std::streamsize>(m_size));
#else
s.write(m_ptr.get(), static_cast<std::streamsize>(m_size));
#endif
popularity.user = reader.readLengthPrefixedString();
popularity.rating = reader.readFloat64LE();
popularity.playCounter = reader.readUInt64LE();
popularity.scale = static_cast<TagType>(reader.readUInt64LE());
} catch (const std::ios_base::failure &) {
throw ConversionException(argsToString("Assigned popularity is invalid"));
}
break;
}
case TagDataType::UnsignedInteger:
popularity.rating = static_cast<double>(toUnsignedInteger());
break;
default:
throw ConversionException(argsToString("Can not convert ", tagDataTypeString(m_type), " to date time."));
}
return popularity;
}
/*!
* \brief Converts the value of the current TagValue object to its equivalent
* Popularity representation using the specified \a scale.
* \throws Throws ConversionException on failure, e.g. when Popularity::scaleTo() fails.
* \remarks
* 1. See Popularity::scaleTo() for details about scaling.
* 2. If text is assigned, it is converted like with TagValue::toPopularity(). However,
* the specified \a a scale is *assigned* as the popularity's scale assuming that the
* text representation already contains a rating with the desired \a scale. That means
* if you assign text to a TagValue, the tag implementations (which use this function
* internally) will use that text as-is when serializing the popularity/rating field.
* 3. Since TagValue::toString() also does not do any scaling the previous point means that
* if you only ever use TagValue::assignText() (or equivalent c'tors) and TagValue::toString()
* you will always work with raw rating values consistently.
* 4. Since tag implementations provided by this library always take care to assign the
* popularity/rating as such (and not just as text) you do not need to care about point 2. if
* you want to use a certain scale consistently. Just call this function with the desired scal
* when reading and assign a popularity object with that scale before saving changes.
*/
Popularity TagValue::toScaledPopularity(TagType scale) const
{
auto popularity = toPopularity();
if (m_type == TagDataType::Text) {
popularity.scale = scale;
} else if (!popularity.scaleTo(scale)) {
throw ConversionException(argsToString("Assigned popularity cannot be scaled accordingly"));
}
return popularity;
}
/*!
* \brief Converts the currently assigned text value to the specified \a encoding.
* \throws Throws CppUtilities::ConversionException() if the conversion fails.
* \remarks
* - Does nothing if dataEncoding() equals \a encoding.
* - Sets dataEncoding() to the specified \a encoding if the conversion succeeds.
* - Does not do any conversion if the current type() is not TagDataType::Text.
* \sa convertDataEncodingForTag()
*/
void TagValue::convertDataEncoding(TagTextEncoding encoding)
{
if (m_encoding == encoding) {
return;
}
if (type() == TagDataType::Text) {
StringData encodedData;
switch (encoding) {
case TagTextEncoding::Utf8:
// use pre-defined methods when encoding to UTF-8
switch (dataEncoding()) {
case TagTextEncoding::Latin1:
encodedData = convertLatin1ToUtf8(m_ptr.get(), m_size);
break;
case TagTextEncoding::Utf16LittleEndian:
encodedData = convertUtf16LEToUtf8(m_ptr.get(), m_size);
break;
case TagTextEncoding::Utf16BigEndian:
encodedData = convertUtf16BEToUtf8(m_ptr.get(), m_size);
break;
default:;
}
break;
default: {
// otherwise, determine input and output parameter to use general covertString method
const auto inputParameter = encodingParameter(dataEncoding());
const auto outputParameter = encodingParameter(encoding);
encodedData
= convertString(inputParameter.first, outputParameter.first, m_ptr.get(), m_size, outputParameter.second / inputParameter.second);
}
}
// can't just move the encoded data because it needs to be deleted with free
m_ptr = make_unique<char[]>(m_size = encodedData.second);
copy(encodedData.first.get(), encodedData.first.get() + encodedData.second, m_ptr.get());
}
m_encoding = encoding;
}
/*!
* \brief Ensures the encoding of the currently assigned text value is supported by the specified \a tag.
* \sa This is a convenience method for convertDataEncoding().
*/
void TagValue::convertDataEncodingForTag(const Tag *tag)
{
if (type() == TagDataType::Text && !tag->canEncodingBeUsed(dataEncoding())) {
convertDataEncoding(tag->proposedTextEncoding());
}
}
/*!
* \brief Converts the assigned description to use the specified \a encoding.
*/
void TagValue::convertDescriptionEncoding(TagTextEncoding encoding)
{
if (encoding == m_descEncoding) {
return;
}
if (m_desc.empty()) {
m_descEncoding = encoding;
return;
}
StringData encodedData;
switch (encoding) {
case TagTextEncoding::Utf8:
// use pre-defined methods when encoding to UTF-8
switch (descriptionEncoding()) {
case TagTextEncoding::Latin1:
encodedData = convertLatin1ToUtf8(m_desc.data(), m_desc.size());
break;
case TagTextEncoding::Utf16LittleEndian:
encodedData = convertUtf16LEToUtf8(m_desc.data(), m_desc.size());
break;
case TagTextEncoding::Utf16BigEndian:
encodedData = convertUtf16BEToUtf8(m_desc.data(), m_desc.size());
break;
default:;
}
break;
default: {
// otherwise, determine input and output parameter to use general covertString method