CUEParser.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Simple CUE sheet parser suitable for embedded systems.
  3. *
  4. * Copyright (c) 2023 Rabbit Hole Computing
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #pragma once
  20. #include <stdint.h>
  21. #ifndef CUE_MAX_FILENAME
  22. #define CUE_MAX_FILENAME 64
  23. #endif
  24. enum CUEFileMode
  25. {
  26. CUEFile_BINARY = 0,
  27. CUEFile_MOTOROLA,
  28. CUEFile_MP3,
  29. CUEFile_WAVE,
  30. CUEFile_AIFF,
  31. };
  32. enum CUETrackMode
  33. {
  34. CUETrack_AUDIO = 0,
  35. CUETrack_CDG,
  36. CUETrack_MODE1_2048,
  37. CUETrack_MODE1_2352,
  38. CUETrack_MODE2_2048,
  39. CUETrack_MODE2_2324,
  40. CUETrack_MODE2_2336,
  41. CUETrack_MODE2_2352,
  42. CUETrack_CDI_2336,
  43. CUETrack_CDI_2352,
  44. };
  45. struct CUETrackInfo
  46. {
  47. char filename[CUE_MAX_FILENAME+1];
  48. CUEFileMode file_mode;
  49. int track_number;
  50. CUETrackMode track_mode;
  51. uint32_t unstored_pregap_length;
  52. uint32_t pregap_start;
  53. uint32_t data_start;
  54. };
  55. class CUEParser
  56. {
  57. public:
  58. CUEParser();
  59. // Initialize the class to parse data from string.
  60. // The string must remain valid for the lifetime of this object.
  61. CUEParser(const char *cue_sheet);
  62. // Restart parsing from beginning of file
  63. void restart();
  64. // Get information for next track.
  65. // Returns nullptr when there are no more tracks.
  66. // The returned pointer remains valid until next call to next_track()
  67. // or destruction of this object.
  68. const CUETrackInfo *next_track();
  69. protected:
  70. const char *m_cue_sheet;
  71. const char *m_parse_pos;
  72. CUETrackInfo m_track_info;
  73. // Skip any whitespace at beginning of line.
  74. // Returns false if at end of string.
  75. bool start_line();
  76. // Advance parser to next line
  77. void next_line();
  78. // Skip spaces in string, return pointer to first non-space character
  79. const char *skip_space(const char *p) const;
  80. // Read text starting with " and ending with next "
  81. // Returns pointer to character after ending quote.
  82. const char *read_quoted(const char *src, char *dest, int dest_size);
  83. // Parse time from MM:SS:FF format to frame number
  84. uint32_t parse_time(const char *src);
  85. // Parse file mode into enum
  86. CUEFileMode parse_file_mode(const char *src);
  87. // Parse track mode into enum
  88. CUETrackMode parse_track_mode(const char *src);
  89. };