frame_userurl.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "nsid3v2.h"
  2. #include "nsid3v2/header.h"
  3. #include "nsid3v2/tag.h"
  4. #include "nsid3v2/frame_utils.h"
  5. #include "nu/ByteReader.h"
  6. #include "nx/nxstring.h"
  7. #if defined(_WIN32) && !defined(strcasecmp)
  8. #define strcasecmp _stricmp
  9. #else
  10. #include <string.h>
  11. #endif
  12. struct ParsedUserURL
  13. {
  14. ParsedString description;
  15. ParsedString value;
  16. };
  17. static int ParseUserURL(const void *data, size_t data_len, ParsedUserURL &parsed)
  18. {
  19. int ret;
  20. if (data_len < 2)
  21. return NErr_Insufficient;
  22. bytereader_value_t byte_reader;
  23. bytereader_init(&byte_reader, data, data_len);
  24. uint8_t encoding = bytereader_read_u8(&byte_reader);
  25. ret = ParseNullTerminatedString(&byte_reader, encoding, parsed.description);
  26. if (ret != NErr_Success)
  27. return ret;
  28. return ParseFrameTerminatedString(&byte_reader, 0, parsed.value);
  29. }
  30. int NSID3v2_Tag_WXXX_Get(const nsid3v2_tag_t t, const char *description, nx_string_t *value, int text_flags)
  31. {
  32. const ID3v2::Tag *tag = (const ID3v2::Tag *)t;
  33. if (!tag)
  34. return NErr_Empty;
  35. const ID3v2::Frame *frame = tag->FindFirstFrame(NSID3V2_FRAME_USER_TEXT);
  36. while (frame)
  37. {
  38. const void *data;
  39. size_t data_len;
  40. ParsedUserURL parsed;
  41. if (frame->GetData(&data, &data_len) == NErr_Success && data_len > 0 && ParseUserURL(data, data_len, parsed) == NErr_Success && DescriptionMatches(parsed.description, description, text_flags))
  42. {
  43. return NXStringCreateFromParsedString(value, parsed.value, text_flags);
  44. }
  45. frame = tag->FindNextFrame(frame);
  46. }
  47. return NErr_Empty;
  48. }
  49. int NSID3v2_Frame_UserURL_Get(const nsid3v2_frame_t f, nx_string_t *description, nx_string_t *value, int text_flags)
  50. {
  51. const ID3v2::Frame *frame = (const ID3v2::Frame *)f;
  52. if (frame)
  53. {
  54. const void *data;
  55. size_t data_len;
  56. ParsedUserURL parsed;
  57. if (frame->GetData(&data, &data_len) == NErr_Success && data_len > 0 && ParseUserURL(data, data_len, parsed) == NErr_Success)
  58. {
  59. int ret = NXStringCreateFromParsedString(value, parsed.value, text_flags);
  60. if (ret != NErr_Success)
  61. return ret;
  62. return NXStringCreateFromParsedString(description, parsed.description, text_flags);
  63. }
  64. }
  65. return NErr_Error;
  66. }