File: tag_c.cpp

package info (click to toggle)
taglib 2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,192 kB
  • sloc: cpp: 44,736; ansic: 137; makefile: 28
file content (885 lines) | stat: -rw-r--r-- 23,843 bytes parent folder | download
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
/***************************************************************************
    copyright            : (C) 2003 by Scott Wheeler
    email                : wheeler@kde.org
 ***************************************************************************/

/***************************************************************************
 *   This library is free software; you can redistribute it and/or modify  *
 *   it  under the terms of the GNU Lesser General Public License version  *
 *   2.1 as published by the Free Software Foundation.                     *
 *                                                                         *
 *   This library is distributed in the hope that it will be useful, but   *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the Free Software   *
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
 *   USA                                                                   *
 ***************************************************************************/

#include "tag_c.h"

#include <cstdlib>
#include <cstring>
#include <sstream>
#include <utility>

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "taglib_config.h"
#include "tstringlist.h"
#include "tbytevectorstream.h"
#include "tiostream.h"
#include "tfile.h"
#include "tpropertymap.h"
#include "fileref.h"
#include "mpegfile.h"
#include "tag.h"
#include "id3v2framefactory.h"
#ifdef TAGLIB_WITH_ASF
#include "asffile.h"
#endif
#ifdef TAGLIB_WITH_VORBIS
#include "vorbisfile.h"
#include "flacfile.h"
#include "oggflacfile.h"
#include "speexfile.h"
#include "opusfile.h"
#endif
#ifdef TAGLIB_WITH_APE
#include "mpcfile.h"
#include "wavpackfile.h"
#endif
#ifdef TAGLIB_WITH_TRUEAUDIO
#include "trueaudiofile.h"
#endif
#ifdef TAGLIB_WITH_MP4
#include "mp4file.h"
#endif
#ifdef TAGLIB_WITH_RIFF
#include "aifffile.h"
#include "wavfile.h"
#endif
#ifdef TAGLIB_WITH_APE
#include "apefile.h"
#endif
#ifdef TAGLIB_WITH_MOD
#include "itfile.h"
#include "modfile.h"
#include "s3mfile.h"
#include "xmfile.h"
#endif
#ifdef TAGLIB_WITH_DSF
#include "dsffile.h"
#include "dsdifffile.h"
#endif
#ifdef TAGLIB_WITH_SHORTEN
#include "shortenfile.h"
#endif
#ifdef TAGLIB_WITH_MATROSKA
#include "matroskafile.h"
#endif

using namespace TagLib;

namespace
{
  List<char *> strings;
  bool unicodeStrings = true;
  bool stringManagementEnabled = true;

  char *stringToCharArray(const String &s)
  {
    const std::string str = s.to8Bit(unicodeStrings);

#ifdef HAVE_ISO_STRDUP

    return ::_strdup(str.c_str());

#else

    return ::strdup(str.c_str());

#endif
  }

  String charArrayToString(const char *s)
  {
    return String(s, unicodeStrings ? String::UTF8 : String::Latin1);
  }
}  // namespace

void taglib_set_strings_unicode(BOOL unicode)
{
  unicodeStrings = (unicode != 0);
}

void taglib_set_string_management_enabled(BOOL management)
{
  stringManagementEnabled = (management != 0);
}

void taglib_free(void* pointer)
{
  free(pointer);
}

////////////////////////////////////////////////////////////////////////////////
// TagLib::IOStream wrapper
////////////////////////////////////////////////////////////////////////////////

TagLib_IOStream *taglib_memory_iostream_new(const char *data, unsigned int size)
{
  return reinterpret_cast<TagLib_IOStream *>(
    new ByteVectorStream(ByteVector(data, size)));
}

void taglib_iostream_free(TagLib_IOStream *stream)
{
  delete reinterpret_cast<IOStream *>(stream);
}

////////////////////////////////////////////////////////////////////////////////
// TagLib::FileRef wrapper
////////////////////////////////////////////////////////////////////////////////

TagLib_File *taglib_file_new(const char *filename)
{
  return reinterpret_cast<TagLib_File *>(new FileRef(filename));
}

#ifdef _WIN32
TagLib_File *taglib_file_new_wchar(const wchar_t *filename)
{
  return reinterpret_cast<TagLib_File *>(new FileRef(filename));
}
#endif

template<typename T>
TagLib_File *taglib_file_new_type_any_char(const T *filename, TagLib_File_Type type)
{
  File *file = NULL;
  switch(type) {
  case TagLib_File_MPEG:
    file = new MPEG::File(filename);
      break;
#ifdef TAGLIB_WITH_VORBIS
  case TagLib_File_OggVorbis:
    file = new Ogg::Vorbis::File(filename);
    break;
  case TagLib_File_FLAC:
    file = new FLAC::File(filename);
    break;
  case TagLib_File_OggFlac:
    file = new Ogg::FLAC::File(filename);
    break;
  case TagLib_File_Speex:
    file = new Ogg::Speex::File(filename);
    break;
  case TagLib_File_Opus:
    file = new Ogg::Opus::File(filename);
    break;
#endif
#ifdef TAGLIB_WITH_APE
  case TagLib_File_MPC:
    file = new MPC::File(filename);
    break;
  case TagLib_File_WavPack:
    file = new WavPack::File(filename);
    break;
  case TagLib_File_APE:
    file = new APE::File(filename);
    break;
#endif
#ifdef TAGLIB_WITH_TRUEAUDIO
  case TagLib_File_TrueAudio:
    file = new TrueAudio::File(filename);
    break;
#endif
#ifdef TAGLIB_WITH_MP4
  case TagLib_File_MP4:
    file = new MP4::File(filename);
    break;
#endif
#ifdef TAGLIB_WITH_ASF
  case TagLib_File_ASF:
    file = new ASF::File(filename);
    break;
#endif
#ifdef TAGLIB_WITH_RIFF
  case TagLib_File_AIFF:
    file = new RIFF::AIFF::File(filename);
    break;
  case TagLib_File_WAV:
    file = new RIFF::WAV::File(filename);
    break;
#endif
#ifdef TAGLIB_WITH_MOD
  case TagLib_File_IT:
    file = new IT::File(filename);
    break;
  case TagLib_File_Mod:
    file = new Mod::File(filename);
    break;
  case TagLib_File_S3M:
    file = new S3M::File(filename);
    break;
  case TagLib_File_XM:
    file = new XM::File(filename);
    break;
#endif
#ifdef TAGLIB_WITH_DSF
  case TagLib_File_DSF:
    file = new DSF::File(filename);
    break;
  case TagLib_File_DSDIFF:
    file = new DSDIFF::File(filename);
    break;
#endif
#ifdef TAGLIB_WITH_SHORTEN
  case TagLib_File_SHORTEN:
    file = new Shorten::File(filename);
    break;
#endif
#ifdef TAGLIB_WITH_MATROSKA
  case TagLib_File_MATROSKA:
    file = new Matroska::File(filename);
    break;
#endif
  default:
    break;
  }
  return file ? reinterpret_cast<TagLib_File *>(new FileRef(file)) : NULL;
}

TagLib_File *taglib_file_new_type(const char *filename, TagLib_File_Type type)
{
  return taglib_file_new_type_any_char(filename, type);
}

#ifdef _WIN32
TagLib_File *taglib_file_new_type_wchar(const wchar_t *filename, TagLib_File_Type type)
{
  return taglib_file_new_type_any_char(filename, type);
}
#endif

TagLib_File *taglib_file_new_iostream(TagLib_IOStream *stream)
{
  return reinterpret_cast<TagLib_File *>(
    new FileRef(reinterpret_cast<IOStream *>(stream)));
}

void taglib_file_free(TagLib_File *file)
{
  delete reinterpret_cast<FileRef *>(file);
}

BOOL taglib_file_is_valid(const TagLib_File *file)
{
  return !reinterpret_cast<const FileRef *>(file)->isNull();
}

TagLib_Tag *taglib_file_tag(const TagLib_File *file)
{
  auto f = reinterpret_cast<const FileRef *>(file);
  return reinterpret_cast<TagLib_Tag *>(f->tag());
}

const TagLib_AudioProperties *taglib_file_audioproperties(const TagLib_File *file)
{
  auto f = reinterpret_cast<const FileRef *>(file);
  return reinterpret_cast<const TagLib_AudioProperties *>(f->audioProperties());
}

BOOL taglib_file_save(TagLib_File *file)
{
  return reinterpret_cast<FileRef *>(file)->save();
}

////////////////////////////////////////////////////////////////////////////////
// TagLib::Tag wrapper
////////////////////////////////////////////////////////////////////////////////

char *taglib_tag_title(const TagLib_Tag *tag)
{
  auto t = reinterpret_cast<const Tag *>(tag);
  char *s = stringToCharArray(t->title());
  if(stringManagementEnabled)
    strings.append(s);
  return s;
}

char *taglib_tag_artist(const TagLib_Tag *tag)
{
  auto t = reinterpret_cast<const Tag *>(tag);
  char *s = stringToCharArray(t->artist());
  if(stringManagementEnabled)
    strings.append(s);
  return s;
}

char *taglib_tag_album(const TagLib_Tag *tag)
{
  auto t = reinterpret_cast<const Tag *>(tag);
  char *s = stringToCharArray(t->album());
  if(stringManagementEnabled)
    strings.append(s);
  return s;
}

char *taglib_tag_comment(const TagLib_Tag *tag)
{
  auto t = reinterpret_cast<const Tag *>(tag);
  char *s = stringToCharArray(t->comment());
  if(stringManagementEnabled)
    strings.append(s);
  return s;
}

char *taglib_tag_genre(const TagLib_Tag *tag)
{
  auto t = reinterpret_cast<const Tag *>(tag);
  char *s = stringToCharArray(t->genre());
  if(stringManagementEnabled)
    strings.append(s);
  return s;
}

unsigned int taglib_tag_year(const TagLib_Tag *tag)
{
  auto t = reinterpret_cast<const Tag *>(tag);
  return t->year();
}

unsigned int taglib_tag_track(const TagLib_Tag *tag)
{
  auto t = reinterpret_cast<const Tag *>(tag);
  return t->track();
}

void taglib_tag_set_title(TagLib_Tag *tag, const char *title)
{
  auto t = reinterpret_cast<Tag *>(tag);
  t->setTitle(charArrayToString(title));
}

void taglib_tag_set_artist(TagLib_Tag *tag, const char *artist)
{
  auto t = reinterpret_cast<Tag *>(tag);
  t->setArtist(charArrayToString(artist));
}

void taglib_tag_set_album(TagLib_Tag *tag, const char *album)
{
  auto t = reinterpret_cast<Tag *>(tag);
  t->setAlbum(charArrayToString(album));
}

void taglib_tag_set_comment(TagLib_Tag *tag, const char *comment)
{
  auto t = reinterpret_cast<Tag *>(tag);
  t->setComment(charArrayToString(comment));
}

void taglib_tag_set_genre(TagLib_Tag *tag, const char *genre)
{
  auto t = reinterpret_cast<Tag *>(tag);
  t->setGenre(charArrayToString(genre));
}

void taglib_tag_set_year(TagLib_Tag *tag, unsigned int year)
{
  auto t = reinterpret_cast<Tag *>(tag);
  t->setYear(year);
}

void taglib_tag_set_track(TagLib_Tag *tag, unsigned int track)
{
  auto t = reinterpret_cast<Tag *>(tag);
  t->setTrack(track);
}

void taglib_tag_free_strings()
{
  if(!stringManagementEnabled)
    return;

  for(auto &string : std::as_const(strings))
    free(string);
  strings.clear();
}

////////////////////////////////////////////////////////////////////////////////
// TagLib::AudioProperties wrapper
////////////////////////////////////////////////////////////////////////////////

int taglib_audioproperties_length(const TagLib_AudioProperties *audioProperties)
{
  auto p = reinterpret_cast<const AudioProperties *>(audioProperties);
  return p->lengthInSeconds();
}

int taglib_audioproperties_bitrate(const TagLib_AudioProperties *audioProperties)
{
  auto p = reinterpret_cast<const AudioProperties *>(audioProperties);
  return p->bitrate();
}

int taglib_audioproperties_samplerate(const TagLib_AudioProperties *audioProperties)
{
  auto p = reinterpret_cast<const AudioProperties *>(audioProperties);
  return p->sampleRate();
}

int taglib_audioproperties_channels(const TagLib_AudioProperties *audioProperties)
{
  auto p = reinterpret_cast<const AudioProperties *>(audioProperties);
  return p->channels();
}

void taglib_id3v2_set_default_text_encoding(TagLib_ID3v2_Encoding encoding)
{
  String::Type type = String::Latin1;

  switch(encoding)
  {
  case TagLib_ID3v2_Latin1:
    type = String::Latin1;
    break;
  case TagLib_ID3v2_UTF16:
    type = String::UTF16;
    break;
  case TagLib_ID3v2_UTF16BE:
    type = String::UTF16BE;
    break;
  case TagLib_ID3v2_UTF8:
    type = String::UTF8;
    break;
  }

  ID3v2::FrameFactory::instance()->setDefaultTextEncoding(type);
}


/******************************************************************************
 * Properties API
 ******************************************************************************/
namespace {

void _taglib_property_set(TagLib_File *file, const char* prop, const char* value, bool append)
{
  if(file == NULL || prop == NULL)
    return;

  auto tfile = reinterpret_cast<FileRef *>(file);
  auto propStr = charArrayToString(prop);
  PropertyMap map = tfile->tag()->properties();

  if(value) {
    auto property = map.find(propStr);
    if(property == map.end()) {
      map.insert(propStr, StringList(charArrayToString(value)));
    }
    else {
      if(append) {
        property->second.append(charArrayToString(value));
      }
      else {
        property->second = StringList(charArrayToString(value));
      }
    }
  }
  else {
    map.erase(propStr);
  }

  tfile->setProperties(map);
}

}  // namespace

void taglib_property_set(TagLib_File *file, const char *prop, const char *value)
{
  _taglib_property_set(file, prop, value, false);
}

void taglib_property_set_append(TagLib_File *file, const char *prop, const char *value)
{
  _taglib_property_set(file, prop, value, true);
}

char** taglib_property_keys(const TagLib_File *file)
{
  if(file == NULL)
    return NULL;

  const PropertyMap map = reinterpret_cast<const FileRef *>(file)->properties();
  if(map.isEmpty())
    return NULL;

  auto props = static_cast<char **>(malloc(sizeof(char *) * (map.size() + 1)));
  char **pp = props;

  for(const auto &i : map) {
    *pp++ = stringToCharArray(i.first);
  }
  *pp = NULL;

  return props;
}

char **taglib_property_get(const TagLib_File *file, const char *prop)
{
  if(file == NULL || prop == NULL)
    return NULL;

  const PropertyMap map = reinterpret_cast<const FileRef *>(file)->properties();

  auto property = map.find(charArrayToString(prop));
  if(property == map.end())
    return NULL;

  auto props = static_cast<char **>(malloc(sizeof(char *) * (property->second.size() + 1)));
  char **pp = props;

  for(const auto &i : property->second) {
    *pp++ = stringToCharArray(i);
  }
  *pp = NULL;

  return props;
}

void taglib_property_free(char **props)
{
  if(props == NULL)
    return;

  char **p = props;
  while(*p) {
    free(*p++);
  }
  free(props);
}


/******************************************************************************
 * Complex Properties API
 ******************************************************************************/

namespace {

bool _taglib_complex_property_set(
  TagLib_File *file, const char *key,
  const TagLib_Complex_Property_Attribute **value, bool append)
{
  if(file == NULL || key == NULL)
    return false;

  auto tfile = reinterpret_cast<FileRef *>(file);
  auto keyStr = charArrayToString(key);

  if(value == NULL) {
    return tfile->setComplexProperties(keyStr, {});
  }

  VariantMap map;
  const TagLib_Complex_Property_Attribute** attrPtr = value;
  while(*attrPtr) {
    const TagLib_Complex_Property_Attribute *attr = *attrPtr;
    auto attrKey = charArrayToString(attr->key);
    switch(attr->value.type) {
    case TagLib_Variant_Void:
      map.insert(attrKey, Variant());
      break;
    case TagLib_Variant_Bool:
      map.insert(attrKey, attr->value.value.boolValue != 0);
      break;
    case TagLib_Variant_Int:
      map.insert(attrKey, attr->value.value.intValue);
      break;
    case TagLib_Variant_UInt:
      map.insert(attrKey, attr->value.value.uIntValue);
      break;
    case TagLib_Variant_LongLong:
      map.insert(attrKey, attr->value.value.longLongValue);
      break;
    case TagLib_Variant_ULongLong:
      map.insert(attrKey, attr->value.value.uLongLongValue);
      break;
    case TagLib_Variant_Double:
      map.insert(attrKey, attr->value.value.doubleValue);
      break;
    case TagLib_Variant_String:
      map.insert(attrKey, charArrayToString(attr->value.value.stringValue));
      break;
    case TagLib_Variant_StringList: {
      StringList strs;
      if(attr->value.value.stringListValue) {
        char **s = attr->value.value.stringListValue;;
        while(*s) {
          strs.append(charArrayToString(*s++));
        }
      }
      map.insert(attrKey, strs);
      break;
    }
    case TagLib_Variant_ByteVector:
      map.insert(attrKey, ByteVector(attr->value.value.byteVectorValue,
                                     attr->value.size));
      break;
    }
    ++attrPtr;
  }

  return append ? tfile->setComplexProperties(keyStr, tfile->complexProperties(keyStr).append(map))
                : tfile->setComplexProperties(keyStr, {map});
}

}  // namespace

BOOL taglib_complex_property_set(
  TagLib_File *file, const char *key,
  const TagLib_Complex_Property_Attribute **value)
{
  return _taglib_complex_property_set(file, key, value, false);
}

BOOL taglib_complex_property_set_append(
  TagLib_File *file, const char *key,
  const TagLib_Complex_Property_Attribute **value)
{
  return _taglib_complex_property_set(file, key, value, true);
}

char** taglib_complex_property_keys(const TagLib_File *file)
{
  if(file == NULL) {
    return NULL;
  }

  const StringList strs = reinterpret_cast<const FileRef *>(file)->complexPropertyKeys();
  if(strs.isEmpty()) {
    return NULL;
  }

  auto keys = static_cast<char **>(malloc(sizeof(char *) * (strs.size() + 1)));
  char **keyPtr = keys;

  for(const auto &str : strs) {
    *keyPtr++ = stringToCharArray(str);
  }
  *keyPtr = NULL;

  return keys;
}

TagLib_Complex_Property_Attribute*** taglib_complex_property_get(
  const TagLib_File *file, const char *key)
{
  if(file == NULL || key == NULL) {
    return NULL;
  }

  const auto variantMaps = reinterpret_cast<const FileRef *>(file)->complexProperties(charArrayToString(key));
  if(variantMaps.isEmpty()) {
    return NULL;
  }

  auto props = static_cast<TagLib_Complex_Property_Attribute ***>(
    malloc(sizeof(TagLib_Complex_Property_Attribute **) * (variantMaps.size() + 1)));
  TagLib_Complex_Property_Attribute ***propPtr = props;

  for(const auto &variantMap : variantMaps) {
    if(!variantMap.isEmpty()) {
      auto attrs = static_cast<TagLib_Complex_Property_Attribute **>(
        malloc(sizeof(TagLib_Complex_Property_Attribute *) * (variantMap.size() + 1)));
      auto attr = static_cast<TagLib_Complex_Property_Attribute *>(
        malloc(sizeof(TagLib_Complex_Property_Attribute) * variantMap.size()));
      TagLib_Complex_Property_Attribute **attrPtr = attrs;
      // The next assignment is redundant to silence the clang analyzer,
      // it is done at the end of the loop, which must be entered because
      // variantMap is not empty.
      *attrPtr = attr;
      for(const auto &[k, v] : variantMap) {
        attr->key = stringToCharArray(k);
        attr->value.size = 0;
        switch(v.type()) {
        case Variant::Void:
          attr->value.type = TagLib_Variant_Void;
          attr->value.value.stringValue = NULL;
          break;
        case Variant::Bool:
          attr->value.type = TagLib_Variant_Bool;
          attr->value.value.boolValue = v.value<bool>();
          break;
        case Variant::Int:
          attr->value.type = TagLib_Variant_Int;
          attr->value.value.intValue = v.value<int>();
          break;
        case Variant::UInt:
          attr->value.type = TagLib_Variant_UInt;
          attr->value.value.uIntValue = v.value<unsigned int>();
          break;
        case Variant::LongLong:
          attr->value.type = TagLib_Variant_LongLong;
          attr->value.value.longLongValue = v.value<long long>();
          break;
        case Variant::ULongLong:
          attr->value.type = TagLib_Variant_ULongLong;
          attr->value.value.uLongLongValue = v.value<unsigned long long>();
          break;
        case Variant::Double:
          attr->value.type = TagLib_Variant_Double;
          attr->value.value.doubleValue = v.value<double>();
          break;
        case Variant::String: {
          attr->value.type = TagLib_Variant_String;
          auto str = v.value<String>();
          attr->value.value.stringValue = stringToCharArray(str);
          attr->value.size = str.size();
          break;
        }
        case Variant::StringList: {
          attr->value.type = TagLib_Variant_StringList;
          auto strs = v.value<StringList>();
          auto strPtr = static_cast<char **>(malloc(sizeof(char *) * (strs.size() + 1)));
          attr->value.value.stringListValue = strPtr;
          attr->value.size = strs.size();
          for(const auto &str : strs) {
            *strPtr++ = stringToCharArray(str);
          }
          *strPtr = NULL;
          break;
        }
        case Variant::ByteVector: {
          attr->value.type = TagLib_Variant_ByteVector;
          const ByteVector data = v.value<ByteVector>();
          auto bytePtr = static_cast<char *>(malloc(data.size()));
          attr->value.value.byteVectorValue = bytePtr;
          attr->value.size = data.size();
          ::memcpy(bytePtr, data.data(), data.size());
          break;
        }
        case Variant::ByteVectorList:
        case Variant::VariantList:
        case Variant::VariantMap: {
          attr->value.type = TagLib_Variant_String;
          std::stringstream ss;
          ss << v;
          attr->value.value.stringValue = stringToCharArray(ss.str());
          break;
        }
        }
        *attrPtr++ = attr++;
      }
      *attrPtr = NULL;
      *propPtr++ = attrs;
    }
  }
  *propPtr = NULL;
  return props;
}

void taglib_picture_from_complex_property(
  TagLib_Complex_Property_Attribute*** properties,
  TagLib_Complex_Property_Picture_Data *picture)
{
  if(!properties || !picture) {
    return;
  }
  std::memset(picture, 0, sizeof(*picture));
  TagLib_Complex_Property_Attribute*** propPtr = properties;
  while(!picture->data && *propPtr) {
    TagLib_Complex_Property_Attribute** attrPtr = *propPtr;
    while(*attrPtr) {
      TagLib_Complex_Property_Attribute *attr = *attrPtr;
      switch(attr->value.type) {
      case TagLib_Variant_String:
        if(strcmp("mimeType", attr->key) == 0) {
          picture->mimeType = attr->value.value.stringValue;
        }
        else if(strcmp("description", attr->key) == 0) {
          picture->description = attr->value.value.stringValue;
        }
        else if(strcmp("pictureType", attr->key) == 0) {
          picture->pictureType = attr->value.value.stringValue;
        }
        break;
      case TagLib_Variant_ByteVector:
        if(strcmp("data", attr->key) == 0) {
          picture->data = attr->value.value.byteVectorValue;
          picture->size = attr->value.size;
        }
        break;
      default:
        break;
      }
      ++attrPtr;
    }
    ++propPtr;
  }
}

void taglib_complex_property_free_keys(char **keys)
{
  if(keys == NULL) {
    return;
  }

  char **k = keys;
  while(*k) {
    free(*k++);
  }
  free(keys);
}

void taglib_complex_property_free(
  TagLib_Complex_Property_Attribute ***props)
{
  if(props == NULL) {
    return;
  }
  TagLib_Complex_Property_Attribute*** propPtr = props;
  while(*propPtr) {
    TagLib_Complex_Property_Attribute** attrPtr = *propPtr;
    while(*attrPtr) {
      TagLib_Complex_Property_Attribute *attr = *attrPtr;
      switch(attr->value.type) {
      case TagLib_Variant_String:
        free(attr->value.value.stringValue);
        break;
      case TagLib_Variant_StringList:
        if(attr->value.value.stringListValue) {
          char **s = attr->value.value.stringListValue;
          while(*s) {
            free(*s++);
          }
          free(attr->value.value.stringListValue);
        }
        break;
      case TagLib_Variant_ByteVector:
        free(attr->value.value.byteVectorValue);
        break;
      case TagLib_Variant_Void:
      case TagLib_Variant_Bool:
      case TagLib_Variant_Int:
      case TagLib_Variant_UInt:
      case TagLib_Variant_LongLong:
      case TagLib_Variant_ULongLong:
      case TagLib_Variant_Double:
        break;
      }
      free(attr->key);
      ++attrPtr;
    }
    free(**propPtr);
    free(*propPtr++);
  }
  free(props);
}