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