| 1 | /*************************************************************************** |
| 2 | copyright : (C) 2003 by Scott Wheeler |
| 3 | email : [email protected] |
| 4 | ***************************************************************************/ |
| 5 | |
| 6 | /*************************************************************************** |
| 7 | * This library is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU Lesser General Public License version * |
| 9 | * 2.1 as published by the Free Software Foundation. * |
| 10 | * * |
| 11 | * This library is distributed in the hope that it will be useful, but * |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 | * Lesser General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU Lesser General Public * |
| 17 | * License along with this library; if not, write to the Free Software * |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * |
| 19 | * USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | #include "tag_c.h" |
| 23 | |
| 24 | #include <cstdlib> |
| 25 | #include <cstring> |
| 26 | #include <sstream> |
| 27 | #include <utility> |
| 28 | |
| 29 | #ifdef HAVE_CONFIG_H |
| 30 | # include "config.h" |
| 31 | #endif |
| 32 | |
| 33 | #include "taglib_config.h" |
| 34 | #include "tstringlist.h" |
| 35 | #include "tbytevectorstream.h" |
| 36 | #include "tiostream.h" |
| 37 | #include "tfile.h" |
| 38 | #include "tpropertymap.h" |
| 39 | #include "fileref.h" |
| 40 | #include "mpegfile.h" |
| 41 | #include "tag.h" |
| 42 | #include "id3v2framefactory.h" |
| 43 | #ifdef TAGLIB_WITH_ASF |
| 44 | #include "asffile.h" |
| 45 | #endif |
| 46 | #ifdef TAGLIB_WITH_VORBIS |
| 47 | #include "vorbisfile.h" |
| 48 | #include "flacfile.h" |
| 49 | #include "oggflacfile.h" |
| 50 | #include "speexfile.h" |
| 51 | #include "opusfile.h" |
| 52 | #endif |
| 53 | #ifdef TAGLIB_WITH_APE |
| 54 | #include "mpcfile.h" |
| 55 | #include "wavpackfile.h" |
| 56 | #endif |
| 57 | #ifdef TAGLIB_WITH_TRUEAUDIO |
| 58 | #include "trueaudiofile.h" |
| 59 | #endif |
| 60 | #ifdef TAGLIB_WITH_MP4 |
| 61 | #include "mp4file.h" |
| 62 | #endif |
| 63 | #ifdef TAGLIB_WITH_RIFF |
| 64 | #include "aifffile.h" |
| 65 | #include "wavfile.h" |
| 66 | #endif |
| 67 | #ifdef TAGLIB_WITH_APE |
| 68 | #include "apefile.h" |
| 69 | #endif |
| 70 | #ifdef TAGLIB_WITH_MOD |
| 71 | #include "itfile.h" |
| 72 | #include "modfile.h" |
| 73 | #include "s3mfile.h" |
| 74 | #include "xmfile.h" |
| 75 | #endif |
| 76 | #ifdef TAGLIB_WITH_DSF |
| 77 | #include "dsffile.h" |
| 78 | #include "dsdifffile.h" |
| 79 | #endif |
| 80 | #ifdef TAGLIB_WITH_SHORTEN |
| 81 | #include "shortenfile.h" |
| 82 | #endif |
| 83 | #ifdef TAGLIB_WITH_MATROSKA |
| 84 | #include "matroskafile.h" |
| 85 | #endif |
| 86 | |
| 87 | using namespace TagLib; |
| 88 | |
| 89 | namespace |
| 90 | { |
| 91 | List<char *> strings; |
| 92 | bool unicodeStrings = true; |
| 93 | bool stringManagementEnabled = true; |
| 94 | |
| 95 | char *stringToCharArray(const String &s) |
| 96 | { |
| 97 | const std::string str = s.to8Bit(unicodeStrings); |
| 98 | |
| 99 | #ifdef HAVE_ISO_STRDUP |
| 100 | |
| 101 | return ::_strdup(str.c_str()); |
| 102 | |
| 103 | #else |
| 104 | |
| 105 | return ::strdup(str.c_str()); |
| 106 | |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | String charArrayToString(const char *s) |
| 111 | { |
| 112 | return String(s, unicodeStrings ? String::UTF8 : String::Latin1); |
| 113 | } |
| 114 | } // namespace |
| 115 | |
| 116 | void taglib_set_strings_unicode(BOOL unicode) |
| 117 | { |
| 118 | unicodeStrings = (unicode != 0); |
| 119 | } |
| 120 | |
| 121 | void taglib_set_string_management_enabled(BOOL management) |
| 122 | { |
| 123 | stringManagementEnabled = (management != 0); |
| 124 | } |
| 125 | |
| 126 | void taglib_free(void* pointer) |
| 127 | { |
| 128 | free(pointer); |
| 129 | } |
| 130 | |
| 131 | //////////////////////////////////////////////////////////////////////////////// |
| 132 | // TagLib::IOStream wrapper |
| 133 | //////////////////////////////////////////////////////////////////////////////// |
| 134 | |
| 135 | TagLib_IOStream *taglib_memory_iostream_new(const char *data, unsigned int size) |
| 136 | { |
| 137 | return reinterpret_cast<TagLib_IOStream *>( |
| 138 | new ByteVectorStream(ByteVector(data, size))); |
| 139 | } |
| 140 | |
| 141 | void taglib_iostream_free(TagLib_IOStream *stream) |
| 142 | { |
| 143 | delete reinterpret_cast<IOStream *>(stream); |
| 144 | } |
| 145 | |
| 146 | //////////////////////////////////////////////////////////////////////////////// |
| 147 | // TagLib::FileRef wrapper |
| 148 | //////////////////////////////////////////////////////////////////////////////// |
| 149 | |
| 150 | TagLib_File *taglib_file_new(const char *filename) |
| 151 | { |
| 152 | return reinterpret_cast<TagLib_File *>(new FileRef(filename)); |
| 153 | } |
| 154 | |
| 155 | #ifdef _WIN32 |
| 156 | TagLib_File *taglib_file_new_wchar(const wchar_t *filename) |
| 157 | { |
| 158 | return reinterpret_cast<TagLib_File *>(new FileRef(filename)); |
| 159 | } |
| 160 | #endif |
| 161 | |
| 162 | template<typename T> |
| 163 | TagLib_File *taglib_file_new_type_any_char(const T *filename, TagLib_File_Type type) |
| 164 | { |
| 165 | File *file = NULL; |
| 166 | switch(type) { |
| 167 | case TagLib_File_MPEG: |
| 168 | file = new MPEG::File(filename); |
| 169 | break; |
| 170 | #ifdef TAGLIB_WITH_VORBIS |
| 171 | case TagLib_File_OggVorbis: |
| 172 | file = new Ogg::Vorbis::File(filename); |
| 173 | break; |
| 174 | case TagLib_File_FLAC: |
| 175 | file = new FLAC::File(filename); |
| 176 | break; |
| 177 | case TagLib_File_OggFlac: |
| 178 | file = new Ogg::FLAC::File(filename); |
| 179 | break; |
| 180 | case TagLib_File_Speex: |
| 181 | file = new Ogg::Speex::File(filename); |
| 182 | break; |
| 183 | case TagLib_File_Opus: |
| 184 | file = new Ogg::Opus::File(filename); |
| 185 | break; |
| 186 | #endif |
| 187 | #ifdef TAGLIB_WITH_APE |
| 188 | case TagLib_File_MPC: |
| 189 | file = new MPC::File(filename); |
| 190 | break; |
| 191 | case TagLib_File_WavPack: |
| 192 | file = new WavPack::File(filename); |
| 193 | break; |
| 194 | case TagLib_File_APE: |
| 195 | file = new APE::File(filename); |
| 196 | break; |
| 197 | #endif |
| 198 | #ifdef TAGLIB_WITH_TRUEAUDIO |
| 199 | case TagLib_File_TrueAudio: |
| 200 | file = new TrueAudio::File(filename); |
| 201 | break; |
| 202 | #endif |
| 203 | #ifdef TAGLIB_WITH_MP4 |
| 204 | case TagLib_File_MP4: |
| 205 | file = new MP4::File(filename); |
| 206 | break; |
| 207 | #endif |
| 208 | #ifdef TAGLIB_WITH_ASF |
| 209 | case TagLib_File_ASF: |
| 210 | file = new ASF::File(filename); |
| 211 | break; |
| 212 | #endif |
| 213 | #ifdef TAGLIB_WITH_RIFF |
| 214 | case TagLib_File_AIFF: |
| 215 | file = new RIFF::AIFF::File(filename); |
| 216 | break; |
| 217 | case TagLib_File_WAV: |
| 218 | file = new RIFF::WAV::File(filename); |
| 219 | break; |
| 220 | #endif |
| 221 | #ifdef TAGLIB_WITH_MOD |
| 222 | case TagLib_File_IT: |
| 223 | file = new IT::File(filename); |
| 224 | break; |
| 225 | case TagLib_File_Mod: |
| 226 | file = new Mod::File(filename); |
| 227 | break; |
| 228 | case TagLib_File_S3M: |
| 229 | file = new S3M::File(filename); |
| 230 | break; |
| 231 | case TagLib_File_XM: |
| 232 | file = new XM::File(filename); |
| 233 | break; |
| 234 | #endif |
| 235 | #ifdef TAGLIB_WITH_DSF |
| 236 | case TagLib_File_DSF: |
| 237 | file = new DSF::File(filename); |
| 238 | break; |
| 239 | case TagLib_File_DSDIFF: |
| 240 | file = new DSDIFF::File(filename); |
| 241 | break; |
| 242 | #endif |
| 243 | #ifdef TAGLIB_WITH_SHORTEN |
| 244 | case TagLib_File_SHORTEN: |
| 245 | file = new Shorten::File(filename); |
| 246 | break; |
| 247 | #endif |
| 248 | #ifdef TAGLIB_WITH_MATROSKA |
| 249 | case TagLib_File_MATROSKA: |
| 250 | file = new Matroska::File(filename); |
| 251 | break; |
| 252 | #endif |
| 253 | default: |
| 254 | break; |
| 255 | } |
| 256 | return file ? reinterpret_cast<TagLib_File *>(new FileRef(file)) : NULL; |
| 257 | } |
| 258 | |
| 259 | TagLib_File *taglib_file_new_type(const char *filename, TagLib_File_Type type) |
| 260 | { |
| 261 | return taglib_file_new_type_any_char(filename, type); |
| 262 | } |
| 263 | |
| 264 | #ifdef _WIN32 |
| 265 | TagLib_File *taglib_file_new_type_wchar(const wchar_t *filename, TagLib_File_Type type) |
| 266 | { |
| 267 | return taglib_file_new_type_any_char(filename, type); |
| 268 | } |
| 269 | #endif |
| 270 | |
| 271 | TagLib_File *taglib_file_new_iostream(TagLib_IOStream *stream) |
| 272 | { |
| 273 | return reinterpret_cast<TagLib_File *>( |
| 274 | new FileRef(reinterpret_cast<IOStream *>(stream))); |
| 275 | } |
| 276 | |
| 277 | void taglib_file_free(TagLib_File *file) |
| 278 | { |
| 279 | delete reinterpret_cast<FileRef *>(file); |
| 280 | } |
| 281 | |
| 282 | BOOL taglib_file_is_valid(const TagLib_File *file) |
| 283 | { |
| 284 | return !reinterpret_cast<const FileRef *>(file)->isNull(); |
| 285 | } |
| 286 | |
| 287 | TagLib_Tag *taglib_file_tag(const TagLib_File *file) |
| 288 | { |
| 289 | auto f = reinterpret_cast<const FileRef *>(file); |
| 290 | return reinterpret_cast<TagLib_Tag *>(f->tag()); |
| 291 | } |
| 292 | |
| 293 | const TagLib_AudioProperties *taglib_file_audioproperties(const TagLib_File *file) |
| 294 | { |
| 295 | auto f = reinterpret_cast<const FileRef *>(file); |
| 296 | return reinterpret_cast<const TagLib_AudioProperties *>(f->audioProperties()); |
| 297 | } |
| 298 | |
| 299 | BOOL taglib_file_save(TagLib_File *file) |
| 300 | { |
| 301 | return reinterpret_cast<FileRef *>(file)->save(); |
| 302 | } |
| 303 | |
| 304 | //////////////////////////////////////////////////////////////////////////////// |
| 305 | // TagLib::Tag wrapper |
| 306 | //////////////////////////////////////////////////////////////////////////////// |
| 307 | |
| 308 | char *taglib_tag_title(const TagLib_Tag *tag) |
| 309 | { |
| 310 | auto t = reinterpret_cast<const Tag *>(tag); |
| 311 | char *s = stringToCharArray(t->title()); |
| 312 | if(stringManagementEnabled) |
| 313 | strings.append(s); |
| 314 | return s; |
| 315 | } |
| 316 | |
| 317 | char *taglib_tag_artist(const TagLib_Tag *tag) |
| 318 | { |
| 319 | auto t = reinterpret_cast<const Tag *>(tag); |
| 320 | char *s = stringToCharArray(t->artist()); |
| 321 | if(stringManagementEnabled) |
| 322 | strings.append(s); |
| 323 | return s; |
| 324 | } |
| 325 | |
| 326 | char *taglib_tag_album(const TagLib_Tag *tag) |
| 327 | { |
| 328 | auto t = reinterpret_cast<const Tag *>(tag); |
| 329 | char *s = stringToCharArray(t->album()); |
| 330 | if(stringManagementEnabled) |
| 331 | strings.append(s); |
| 332 | return s; |
| 333 | } |
| 334 | |
| 335 | char *taglib_tag_comment(const TagLib_Tag *tag) |
| 336 | { |
| 337 | auto t = reinterpret_cast<const Tag *>(tag); |
| 338 | char *s = stringToCharArray(t->comment()); |
| 339 | if(stringManagementEnabled) |
| 340 | strings.append(s); |
| 341 | return s; |
| 342 | } |
| 343 | |
| 344 | char *taglib_tag_genre(const TagLib_Tag *tag) |
| 345 | { |
| 346 | auto t = reinterpret_cast<const Tag *>(tag); |
| 347 | char *s = stringToCharArray(t->genre()); |
| 348 | if(stringManagementEnabled) |
| 349 | strings.append(s); |
| 350 | return s; |
| 351 | } |
| 352 | |
| 353 | unsigned int taglib_tag_year(const TagLib_Tag *tag) |
| 354 | { |
| 355 | auto t = reinterpret_cast<const Tag *>(tag); |
| 356 | return t->year(); |
| 357 | } |
| 358 | |
| 359 | unsigned int taglib_tag_track(const TagLib_Tag *tag) |
| 360 | { |
| 361 | auto t = reinterpret_cast<const Tag *>(tag); |
| 362 | return t->track(); |
| 363 | } |
| 364 | |
| 365 | void taglib_tag_set_title(TagLib_Tag *tag, const char *title) |
| 366 | { |
| 367 | auto t = reinterpret_cast<Tag *>(tag); |
| 368 | t->setTitle(charArrayToString(title)); |
| 369 | } |
| 370 | |
| 371 | void taglib_tag_set_artist(TagLib_Tag *tag, const char *artist) |
| 372 | { |
| 373 | auto t = reinterpret_cast<Tag *>(tag); |
| 374 | t->setArtist(charArrayToString(artist)); |
| 375 | } |
| 376 | |
| 377 | void taglib_tag_set_album(TagLib_Tag *tag, const char *album) |
| 378 | { |
| 379 | auto t = reinterpret_cast<Tag *>(tag); |
| 380 | t->setAlbum(charArrayToString(album)); |
| 381 | } |
| 382 | |
| 383 | void taglib_tag_set_comment(TagLib_Tag *tag, const char *comment) |
| 384 | { |
| 385 | auto t = reinterpret_cast<Tag *>(tag); |
| 386 | t->setComment(charArrayToString(comment)); |
| 387 | } |
| 388 | |
| 389 | void taglib_tag_set_genre(TagLib_Tag *tag, const char *genre) |
| 390 | { |
| 391 | auto t = reinterpret_cast<Tag *>(tag); |
| 392 | t->setGenre(charArrayToString(genre)); |
| 393 | } |
| 394 | |
| 395 | void taglib_tag_set_year(TagLib_Tag *tag, unsigned int year) |
| 396 | { |
| 397 | auto t = reinterpret_cast<Tag *>(tag); |
| 398 | t->setYear(year); |
| 399 | } |
| 400 | |
| 401 | void taglib_tag_set_track(TagLib_Tag *tag, unsigned int track) |
| 402 | { |
| 403 | auto t = reinterpret_cast<Tag *>(tag); |
| 404 | t->setTrack(track); |
| 405 | } |
| 406 | |
| 407 | void taglib_tag_free_strings() |
| 408 | { |
| 409 | if(!stringManagementEnabled) |
| 410 | return; |
| 411 | |
| 412 | for(auto &string : std::as_const(strings)) |
| 413 | free(string); |
| 414 | strings.clear(); |
| 415 | } |
| 416 | |
| 417 | //////////////////////////////////////////////////////////////////////////////// |
| 418 | // TagLib::AudioProperties wrapper |
| 419 | //////////////////////////////////////////////////////////////////////////////// |
| 420 | |
| 421 | int taglib_audioproperties_length(const TagLib_AudioProperties *audioProperties) |
| 422 | { |
| 423 | auto p = reinterpret_cast<const AudioProperties *>(audioProperties); |
| 424 | return p->lengthInSeconds(); |
| 425 | } |
| 426 | |
| 427 | int taglib_audioproperties_bitrate(const TagLib_AudioProperties *audioProperties) |
| 428 | { |
| 429 | auto p = reinterpret_cast<const AudioProperties *>(audioProperties); |
| 430 | return p->bitrate(); |
| 431 | } |
| 432 | |
| 433 | int taglib_audioproperties_samplerate(const TagLib_AudioProperties *audioProperties) |
| 434 | { |
| 435 | auto p = reinterpret_cast<const AudioProperties *>(audioProperties); |
| 436 | return p->sampleRate(); |
| 437 | } |
| 438 | |
| 439 | int taglib_audioproperties_channels(const TagLib_AudioProperties *audioProperties) |
| 440 | { |
| 441 | auto p = reinterpret_cast<const AudioProperties *>(audioProperties); |
| 442 | return p->channels(); |
| 443 | } |
| 444 | |
| 445 | void taglib_id3v2_set_default_text_encoding(TagLib_ID3v2_Encoding encoding) |
| 446 | { |
| 447 | String::Type type = String::Latin1; |
| 448 | |
| 449 | switch(encoding) |
| 450 | { |
| 451 | case TagLib_ID3v2_Latin1: |
| 452 | type = String::Latin1; |
| 453 | break; |
| 454 | case TagLib_ID3v2_UTF16: |
| 455 | type = String::UTF16; |
| 456 | break; |
| 457 | case TagLib_ID3v2_UTF16BE: |
| 458 | type = String::UTF16BE; |
| 459 | break; |
| 460 | case TagLib_ID3v2_UTF8: |
| 461 | type = String::UTF8; |
| 462 | break; |
| 463 | } |
| 464 | |
| 465 | ID3v2::FrameFactory::instance()->setDefaultTextEncoding(type); |
| 466 | } |
| 467 | |
| 468 | |
| 469 | /****************************************************************************** |
| 470 | * Properties API |
| 471 | ******************************************************************************/ |
| 472 | namespace { |
| 473 | |
| 474 | void _taglib_property_set(TagLib_File *file, const char* prop, const char* value, bool append) |
| 475 | { |
| 476 | if(file == NULL || prop == NULL) |
| 477 | return; |
| 478 | |
| 479 | auto tfile = reinterpret_cast<FileRef *>(file); |
| 480 | auto propStr = charArrayToString(prop); |
| 481 | PropertyMap map = tfile->tag()->properties(); |
| 482 | |
| 483 | if(value) { |
| 484 | auto property = map.find(propStr); |
| 485 | if(property == map.end()) { |
| 486 | map.insert(propStr, StringList(charArrayToString(value))); |
| 487 | } |
| 488 | else { |
| 489 | if(append) { |
| 490 | property->second.append(charArrayToString(value)); |
| 491 | } |
| 492 | else { |
| 493 | property->second = StringList(charArrayToString(value)); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | else { |
| 498 | map.erase(propStr); |
| 499 | } |
| 500 | |
| 501 | tfile->setProperties(map); |
| 502 | } |
| 503 | |
| 504 | } // namespace |
| 505 | |
| 506 | void taglib_property_set(TagLib_File *file, const char *prop, const char *value) |
| 507 | { |
| 508 | _taglib_property_set(file, prop, value, false); |
| 509 | } |
| 510 | |
| 511 | void taglib_property_set_append(TagLib_File *file, const char *prop, const char *value) |
| 512 | { |
| 513 | _taglib_property_set(file, prop, value, true); |
| 514 | } |
| 515 | |
| 516 | char** taglib_property_keys(const TagLib_File *file) |
| 517 | { |
| 518 | if(file == NULL) |
| 519 | return NULL; |
| 520 | |
| 521 | const PropertyMap map = reinterpret_cast<const FileRef *>(file)->properties(); |
| 522 | if(map.isEmpty()) |
| 523 | return NULL; |
| 524 | |
| 525 | auto props = static_cast<char **>(malloc(sizeof(char *) * (map.size() + 1))); |
| 526 | char **pp = props; |
| 527 | |
| 528 | for(const auto &i : map) { |
| 529 | *pp++ = stringToCharArray(i.first); |
| 530 | } |
| 531 | *pp = NULL; |
| 532 | |
| 533 | return props; |
| 534 | } |
| 535 | |
| 536 | char **taglib_property_get(const TagLib_File *file, const char *prop) |
| 537 | { |
| 538 | if(file == NULL || prop == NULL) |
| 539 | return NULL; |
| 540 | |
| 541 | const PropertyMap map = reinterpret_cast<const FileRef *>(file)->properties(); |
| 542 | |
| 543 | auto property = map.find(charArrayToString(prop)); |
| 544 | if(property == map.end()) |
| 545 | return NULL; |
| 546 | |
| 547 | auto props = static_cast<char **>(malloc(sizeof(char *) * (property->second.size() + 1))); |
| 548 | char **pp = props; |
| 549 | |
| 550 | for(const auto &i : property->second) { |
| 551 | *pp++ = stringToCharArray(i); |
| 552 | } |
| 553 | *pp = NULL; |
| 554 | |
| 555 | return props; |
| 556 | } |
| 557 | |
| 558 | void taglib_property_free(char **props) |
| 559 | { |
| 560 | if(props == NULL) |
| 561 | return; |
| 562 | |
| 563 | char **p = props; |
| 564 | while(*p) { |
| 565 | free(*p++); |
| 566 | } |
| 567 | free(props); |
| 568 | } |
| 569 | |
| 570 | |
| 571 | /****************************************************************************** |
| 572 | * Complex Properties API |
| 573 | ******************************************************************************/ |
| 574 | |
| 575 | namespace { |
| 576 | |
| 577 | bool _taglib_complex_property_set( |
| 578 | TagLib_File *file, const char *key, |
| 579 | const TagLib_Complex_Property_Attribute **value, bool append) |
| 580 | { |
| 581 | if(file == NULL || key == NULL) |
| 582 | return false; |
| 583 | |
| 584 | auto tfile = reinterpret_cast<FileRef *>(file); |
| 585 | auto keyStr = charArrayToString(key); |
| 586 | |
| 587 | if(value == NULL) { |
| 588 | return tfile->setComplexProperties(keyStr, {}); |
| 589 | } |
| 590 | |
| 591 | VariantMap map; |
| 592 | const TagLib_Complex_Property_Attribute** attrPtr = value; |
| 593 | while(*attrPtr) { |
| 594 | const TagLib_Complex_Property_Attribute *attr = *attrPtr; |
| 595 | auto attrKey = charArrayToString(attr->key); |
| 596 | switch(attr->value.type) { |
| 597 | case TagLib_Variant_Void: |
| 598 | map.insert(attrKey, Variant()); |
| 599 | break; |
| 600 | case TagLib_Variant_Bool: |
| 601 | map.insert(attrKey, attr->value.value.boolValue != 0); |
| 602 | break; |
| 603 | case TagLib_Variant_Int: |
| 604 | map.insert(attrKey, attr->value.value.intValue); |
| 605 | break; |
| 606 | case TagLib_Variant_UInt: |
| 607 | map.insert(attrKey, attr->value.value.uIntValue); |
| 608 | break; |
| 609 | case TagLib_Variant_LongLong: |
| 610 | map.insert(attrKey, attr->value.value.longLongValue); |
| 611 | break; |
| 612 | case TagLib_Variant_ULongLong: |
| 613 | map.insert(attrKey, attr->value.value.uLongLongValue); |
| 614 | break; |
| 615 | case TagLib_Variant_Double: |
| 616 | map.insert(attrKey, attr->value.value.doubleValue); |
| 617 | break; |
| 618 | case TagLib_Variant_String: |
| 619 | map.insert(attrKey, charArrayToString(attr->value.value.stringValue)); |
| 620 | break; |
| 621 | case TagLib_Variant_StringList: { |
| 622 | StringList strs; |
| 623 | if(attr->value.value.stringListValue) { |
| 624 | char **s = attr->value.value.stringListValue;; |
| 625 | while(*s) { |
| 626 | strs.append(charArrayToString(*s++)); |
| 627 | } |
| 628 | } |
| 629 | map.insert(attrKey, strs); |
| 630 | break; |
| 631 | } |
| 632 | case TagLib_Variant_ByteVector: |
| 633 | map.insert(attrKey, ByteVector(attr->value.value.byteVectorValue, |
| 634 | attr->value.size)); |
| 635 | break; |
| 636 | } |
| 637 | ++attrPtr; |
| 638 | } |
| 639 | |
| 640 | return append ? tfile->setComplexProperties(keyStr, tfile->complexProperties(keyStr).append(map)) |
| 641 | : tfile->setComplexProperties(keyStr, {map}); |
| 642 | } |
| 643 | |
| 644 | } // namespace |
| 645 | |
| 646 | BOOL taglib_complex_property_set( |
| 647 | TagLib_File *file, const char *key, |
| 648 | const TagLib_Complex_Property_Attribute **value) |
| 649 | { |
| 650 | return _taglib_complex_property_set(file, key, value, false); |
| 651 | } |
| 652 | |
| 653 | BOOL taglib_complex_property_set_append( |
| 654 | TagLib_File *file, const char *key, |
| 655 | const TagLib_Complex_Property_Attribute **value) |
| 656 | { |
| 657 | return _taglib_complex_property_set(file, key, value, true); |
| 658 | } |
| 659 | |
| 660 | char** taglib_complex_property_keys(const TagLib_File *file) |
| 661 | { |
| 662 | if(file == NULL) { |
| 663 | return NULL; |
| 664 | } |
| 665 | |
| 666 | const StringList strs = reinterpret_cast<const FileRef *>(file)->complexPropertyKeys(); |
| 667 | if(strs.isEmpty()) { |
| 668 | return NULL; |
| 669 | } |
| 670 | |
| 671 | auto keys = static_cast<char **>(malloc(sizeof(char *) * (strs.size() + 1))); |
| 672 | char **keyPtr = keys; |
| 673 | |
| 674 | for(const auto &str : strs) { |
| 675 | *keyPtr++ = stringToCharArray(str); |
| 676 | } |
| 677 | *keyPtr = NULL; |
| 678 | |
| 679 | return keys; |
| 680 | } |
| 681 | |
| 682 | TagLib_Complex_Property_Attribute*** taglib_complex_property_get( |
| 683 | const TagLib_File *file, const char *key) |
| 684 | { |
| 685 | if(file == NULL || key == NULL) { |
| 686 | return NULL; |
| 687 | } |
| 688 | |
| 689 | const auto variantMaps = reinterpret_cast<const FileRef *>(file)->complexProperties(charArrayToString(key)); |
| 690 | if(variantMaps.isEmpty()) { |
| 691 | return NULL; |
| 692 | } |
| 693 | |
| 694 | auto props = static_cast<TagLib_Complex_Property_Attribute ***>( |
| 695 | malloc(sizeof(TagLib_Complex_Property_Attribute **) * (variantMaps.size() + 1))); |
| 696 | TagLib_Complex_Property_Attribute ***propPtr = props; |
| 697 | |
| 698 | for(const auto &variantMap : variantMaps) { |
| 699 | if(!variantMap.isEmpty()) { |
| 700 | auto attrs = static_cast<TagLib_Complex_Property_Attribute **>( |
| 701 | malloc(sizeof(TagLib_Complex_Property_Attribute *) * (variantMap.size() + 1))); |
| 702 | auto attr = static_cast<TagLib_Complex_Property_Attribute *>( |
| 703 | malloc(sizeof(TagLib_Complex_Property_Attribute) * variantMap.size())); |
| 704 | TagLib_Complex_Property_Attribute **attrPtr = attrs; |
| 705 | // The next assignment is redundant to silence the clang analyzer, |
| 706 | // it is done at the end of the loop, which must be entered because |
| 707 | // variantMap is not empty. |
| 708 | *attrPtr = attr; |
| 709 | for(const auto &[k, v] : variantMap) { |
| 710 | attr->key = stringToCharArray(k); |
| 711 | attr->value.size = 0; |
| 712 | switch(v.type()) { |
| 713 | case Variant::Void: |
| 714 | attr->value.type = TagLib_Variant_Void; |
| 715 | attr->value.value.stringValue = NULL; |
| 716 | break; |
| 717 | case Variant::Bool: |
| 718 | attr->value.type = TagLib_Variant_Bool; |
| 719 | attr->value.value.boolValue = v.value<bool>(); |
| 720 | break; |
| 721 | case Variant::Int: |
| 722 | attr->value.type = TagLib_Variant_Int; |
| 723 | attr->value.value.intValue = v.value<int>(); |
| 724 | break; |
| 725 | case Variant::UInt: |
| 726 | attr->value.type = TagLib_Variant_UInt; |
| 727 | attr->value.value.uIntValue = v.value<unsigned int>(); |
| 728 | break; |
| 729 | case Variant::LongLong: |
| 730 | attr->value.type = TagLib_Variant_LongLong; |
| 731 | attr->value.value.longLongValue = v.value<long long>(); |
| 732 | break; |
| 733 | case Variant::ULongLong: |
| 734 | attr->value.type = TagLib_Variant_ULongLong; |
| 735 | attr->value.value.uLongLongValue = v.value<unsigned long long>(); |
| 736 | break; |
| 737 | case Variant::Double: |
| 738 | attr->value.type = TagLib_Variant_Double; |
| 739 | attr->value.value.doubleValue = v.value<double>(); |
| 740 | break; |
| 741 | case Variant::String: { |
| 742 | attr->value.type = TagLib_Variant_String; |
| 743 | auto str = v.value<String>(); |
| 744 | attr->value.value.stringValue = stringToCharArray(str); |
| 745 | attr->value.size = str.size(); |
| 746 | break; |
| 747 | } |
| 748 | case Variant::StringList: { |
| 749 | attr->value.type = TagLib_Variant_StringList; |
| 750 | auto strs = v.value<StringList>(); |
| 751 | auto strPtr = static_cast<char **>(malloc(sizeof(char *) * (strs.size() + 1))); |
| 752 | attr->value.value.stringListValue = strPtr; |
| 753 | attr->value.size = strs.size(); |
| 754 | for(const auto &str : strs) { |
| 755 | *strPtr++ = stringToCharArray(str); |
| 756 | } |
| 757 | *strPtr = NULL; |
| 758 | break; |
| 759 | } |
| 760 | case Variant::ByteVector: { |
| 761 | attr->value.type = TagLib_Variant_ByteVector; |
| 762 | const ByteVector data = v.value<ByteVector>(); |
| 763 | auto bytePtr = static_cast<char *>(malloc(data.size())); |
| 764 | attr->value.value.byteVectorValue = bytePtr; |
| 765 | attr->value.size = data.size(); |
| 766 | ::memcpy(bytePtr, data.data(), data.size()); |
| 767 | break; |
| 768 | } |
| 769 | case Variant::ByteVectorList: |
| 770 | case Variant::VariantList: |
| 771 | case Variant::VariantMap: { |
| 772 | attr->value.type = TagLib_Variant_String; |
| 773 | std::stringstream ss; |
| 774 | ss << v; |
| 775 | attr->value.value.stringValue = stringToCharArray(ss.str()); |
| 776 | break; |
| 777 | } |
| 778 | } |
| 779 | *attrPtr++ = attr++; |
| 780 | } |
| 781 | *attrPtr = NULL; |
| 782 | *propPtr++ = attrs; |
| 783 | } |
| 784 | } |
| 785 | *propPtr = NULL; |
| 786 | return props; |
| 787 | } |
| 788 | |
| 789 | void taglib_picture_from_complex_property( |
| 790 | TagLib_Complex_Property_Attribute*** properties, |
| 791 | TagLib_Complex_Property_Picture_Data *picture) |
| 792 | { |
| 793 | if(!properties || !picture) { |
| 794 | return; |
| 795 | } |
| 796 | std::memset(picture, 0, sizeof(*picture)); |
| 797 | TagLib_Complex_Property_Attribute*** propPtr = properties; |
| 798 | while(!picture->data && *propPtr) { |
| 799 | TagLib_Complex_Property_Attribute** attrPtr = *propPtr; |
| 800 | while(*attrPtr) { |
| 801 | TagLib_Complex_Property_Attribute *attr = *attrPtr; |
| 802 | switch(attr->value.type) { |
| 803 | case TagLib_Variant_String: |
| 804 | if(strcmp("mimeType", attr->key) == 0) { |
| 805 | picture->mimeType = attr->value.value.stringValue; |
| 806 | } |
| 807 | else if(strcmp("description", attr->key) == 0) { |
| 808 | picture->description = attr->value.value.stringValue; |
| 809 | } |
| 810 | else if(strcmp("pictureType", attr->key) == 0) { |
| 811 | picture->pictureType = attr->value.value.stringValue; |
| 812 | } |
| 813 | break; |
| 814 | case TagLib_Variant_ByteVector: |
| 815 | if(strcmp("data", attr->key) == 0) { |
| 816 | picture->data = attr->value.value.byteVectorValue; |
| 817 | picture->size = attr->value.size; |
| 818 | } |
| 819 | break; |
| 820 | default: |
| 821 | break; |
| 822 | } |
| 823 | ++attrPtr; |
| 824 | } |
| 825 | ++propPtr; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | void taglib_complex_property_free_keys(char **keys) |
| 830 | { |
| 831 | if(keys == NULL) { |
| 832 | return; |
| 833 | } |
| 834 | |
| 835 | char **k = keys; |
| 836 | while(*k) { |
| 837 | free(*k++); |
| 838 | } |
| 839 | free(keys); |
| 840 | } |
| 841 | |
| 842 | void taglib_complex_property_free( |
| 843 | TagLib_Complex_Property_Attribute ***props) |
| 844 | { |
| 845 | if(props == NULL) { |
| 846 | return; |
| 847 | } |
| 848 | TagLib_Complex_Property_Attribute*** propPtr = props; |
| 849 | while(*propPtr) { |
| 850 | TagLib_Complex_Property_Attribute** attrPtr = *propPtr; |
| 851 | while(*attrPtr) { |
| 852 | TagLib_Complex_Property_Attribute *attr = *attrPtr; |
| 853 | switch(attr->value.type) { |
| 854 | case TagLib_Variant_String: |
| 855 | free(attr->value.value.stringValue); |
| 856 | break; |
| 857 | case TagLib_Variant_StringList: |
| 858 | if(attr->value.value.stringListValue) { |
| 859 | char **s = attr->value.value.stringListValue; |
| 860 | while(*s) { |
| 861 | free(*s++); |
| 862 | } |
| 863 | free(attr->value.value.stringListValue); |
| 864 | } |
| 865 | break; |
| 866 | case TagLib_Variant_ByteVector: |
| 867 | free(attr->value.value.byteVectorValue); |
| 868 | break; |
| 869 | case TagLib_Variant_Void: |
| 870 | case TagLib_Variant_Bool: |
| 871 | case TagLib_Variant_Int: |
| 872 | case TagLib_Variant_UInt: |
| 873 | case TagLib_Variant_LongLong: |
| 874 | case TagLib_Variant_ULongLong: |
| 875 | case TagLib_Variant_Double: |
| 876 | break; |
| 877 | } |
| 878 | free(attr->key); |
| 879 | ++attrPtr; |
| 880 | } |
| 881 | free(**propPtr); |
| 882 | free(*propPtr++); |
| 883 | } |
| 884 | free(props); |
| 885 | } |