apev2.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "apev2.h"
  2. APE::APE()
  3. {
  4. hasData=false;
  5. dirty=false;
  6. }
  7. int APE::Decode(const void *data, size_t len)
  8. {
  9. if (APEv2::Tag::Parse(data, len) == APEv2::APEV2_SUCCESS)
  10. {
  11. hasData=true;
  12. return 1;
  13. }
  14. return 0;
  15. }
  16. // return -1 for empty, 1 for OK, 0 for "don't understand metadata name"
  17. int APE::GetString(const char *metadata, wchar_t *data, int dataLen)
  18. {
  19. if (!hasData)
  20. return 0;
  21. if (!_stricmp(metadata, "replaygain_track_gain")
  22. || !_stricmp(metadata, "replaygain_track_peak")
  23. || !_stricmp(metadata, "replaygain_album_gain")
  24. || !_stricmp(metadata, "replaygain_album_peak"))
  25. {
  26. if (APEv2::Tag::GetString(metadata, data, dataLen) == APEv2::APEV2_SUCCESS)
  27. return 1;
  28. return -1;
  29. }
  30. else
  31. {
  32. const char *ape_key = MapWinampKeyToApeKey(metadata);
  33. if (ape_key)
  34. {
  35. if (APEv2::Tag::GetString(ape_key, data, dataLen) == APEv2::APEV2_SUCCESS)
  36. return 1;
  37. return -1;
  38. }
  39. }
  40. return 0;
  41. }
  42. int APE::SetString(const char *metadata, const wchar_t *data)
  43. {
  44. if (!_stricmp(metadata, "replaygain_track_gain")
  45. || !_stricmp(metadata, "replaygain_track_peak")
  46. || !_stricmp(metadata, "replaygain_album_gain")
  47. || !_stricmp(metadata, "replaygain_album_peak"))
  48. {
  49. APEv2::Tag::SetString(metadata, data);
  50. dirty=true;
  51. hasData=true;
  52. return 1;
  53. }
  54. else
  55. {
  56. const char *ape_key = MapWinampKeyToApeKey(metadata);
  57. if (ape_key)
  58. {
  59. APEv2::Tag::SetString(ape_key, data);
  60. dirty=true;
  61. hasData=true;
  62. return 1;
  63. }
  64. }
  65. return 0;
  66. }
  67. void APE::Clear()
  68. {
  69. APEv2::Tag::Clear();
  70. dirty=true;
  71. hasData=false;
  72. }
  73. void APE::MarkClear()
  74. {
  75. dirty=true;
  76. hasData=false;
  77. }
  78. int APE::SetKeyValueByIndex(size_t index, const char *key, const wchar_t *data)
  79. {
  80. dirty=true;
  81. return APEv2::Tag::SetKeyValueByIndex(index, key, data);
  82. }
  83. int APE::RemoveItem(size_t index)
  84. {
  85. dirty=true;
  86. return APEv2::Tag::RemoveItem(index);
  87. }
  88. int APE::AddItem()
  89. {
  90. dirty=true;
  91. hasData=true;
  92. APEv2::Tag::AddItem();
  93. return APEv2::APEV2_SUCCESS;
  94. }
  95. struct ApeKeyMapping
  96. {
  97. const char *ape_key;
  98. const char *winamp_key;
  99. const wchar_t *winamp_keyW;
  100. };
  101. static ApeKeyMapping apeKeyMapping[] =
  102. {
  103. { "Track", "track", L"track" },
  104. {"Album", "album", L"album" },
  105. {"Artist", "artist", L"artist" },
  106. {"Comment", "comment", L"comment" },
  107. {"Year", "year", L"year" },
  108. {"Genre", "genre", L"genre" },
  109. {"Title", "title", L"title"},
  110. {"Composer", "composer", L"composer"},
  111. {"Performer", "performer", L"performer"},
  112. {"Album artist", "albumartist", L"albumartist"},
  113. };
  114. const wchar_t *APE::MapApeKeyToWinampKeyW(const char *ape_key)
  115. {
  116. size_t num_mappings = sizeof(apeKeyMapping)/sizeof(apeKeyMapping[0]);
  117. for (size_t i=0;i!=num_mappings;i++)
  118. {
  119. if (!_stricmp(ape_key, apeKeyMapping[i].ape_key))
  120. return apeKeyMapping[i].winamp_keyW;
  121. }
  122. return NULL;
  123. }
  124. const char *APE::MapApeKeyToWinampKey(const char *ape_key)
  125. {
  126. size_t num_mappings = sizeof(apeKeyMapping)/sizeof(apeKeyMapping[0]);
  127. for (size_t i=0;i!=num_mappings;i++)
  128. {
  129. if (!_stricmp(ape_key, apeKeyMapping[i].ape_key))
  130. return apeKeyMapping[i].winamp_key;
  131. }
  132. return NULL;
  133. }
  134. const char *APE::MapWinampKeyToApeKey(const char *winamp_key)
  135. {
  136. size_t num_mappings = sizeof(apeKeyMapping)/sizeof(apeKeyMapping[0]);
  137. for (size_t i=0;i!=num_mappings;i++)
  138. {
  139. if (!_stricmp(winamp_key, apeKeyMapping[i].winamp_key))
  140. return apeKeyMapping[i].ape_key;
  141. }
  142. return NULL;
  143. }
  144. int APE::AddKeyValue(const char *key, const wchar_t *data)
  145. {
  146. dirty=true;
  147. hasData=true;
  148. APEv2::Item *newItem = APEv2::Tag::AddItem();
  149. newItem->SetKey(key);
  150. return APEv2::Tag::SetItemData(newItem, data);
  151. }