cue_parser.h (2245B)
1 // SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com> 2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) 3 4 #pragma once 5 #include "cd_image.h" 6 #include "common/types.h" 7 #include <optional> 8 #include <string_view> 9 #include <utility> 10 #include <vector> 11 12 namespace Common { 13 class Error; 14 } 15 16 namespace CueParser { 17 18 using TrackMode = CDImage::TrackMode; 19 using MSF = CDImage::Position; 20 21 enum : s32 22 { 23 MIN_TRACK_NUMBER = 1, 24 MAX_TRACK_NUMBER = 99, 25 MIN_INDEX_NUMBER = 0, 26 MAX_INDEX_NUMBER = 99 27 }; 28 29 enum class TrackFlag : u32 30 { 31 PreEmphasis = (1 << 0), 32 CopyPermitted = (1 << 1), 33 FourChannelAudio = (1 << 2), 34 SerialCopyManagement = (1 << 3), 35 }; 36 37 struct Track 38 { 39 u32 number; 40 u32 flags; 41 std::string file; 42 std::vector<std::pair<u32, MSF>> indices; 43 TrackMode mode; 44 MSF start; 45 std::optional<MSF> length; 46 std::optional<MSF> zero_pregap; 47 48 const MSF* GetIndex(u32 n) const; 49 50 ALWAYS_INLINE bool HasFlag(TrackFlag flag) const { return (flags & static_cast<u32>(flag)) != 0; } 51 ALWAYS_INLINE void SetFlag(TrackFlag flag) { flags |= static_cast<u32>(flag); } 52 ALWAYS_INLINE void RemoveFlag(TrackFlag flag) { flags &= ~static_cast<u32>(flag); } 53 }; 54 55 class File 56 { 57 public: 58 File(); 59 ~File(); 60 61 const Track* GetTrack(u32 n) const; 62 63 bool Parse(std::FILE* fp, Error* error); 64 65 private: 66 Track* GetMutableTrack(u32 n); 67 68 void SetError(u32 line_number, Error* error, const char* format, ...); 69 70 static std::string_view GetToken(const char*& line); 71 static std::optional<MSF> GetMSF(std::string_view token); 72 73 bool ParseLine(const char* line, u32 line_number, Error* error); 74 75 bool HandleFileCommand(const char* line, u32 line_number, Error* error); 76 bool HandleTrackCommand(const char* line, u32 line_number, Error* error); 77 bool HandleIndexCommand(const char* line, u32 line_number, Error* error); 78 bool HandlePregapCommand(const char* line, u32 line_number, Error* error); 79 bool HandleFlagCommand(const char* line, u32 line_number, Error* error); 80 81 bool CompleteLastTrack(u32 line_number, Error* error); 82 bool SetTrackLengths(u32 line_number, Error* error); 83 84 std::vector<Track> m_tracks; 85 std::optional<std::string> m_current_file; 86 std::optional<Track> m_current_track; 87 }; 88 89 } // namespace CueParser