-
Notifications
You must be signed in to change notification settings - Fork 2
/
cuesheet.cpp
150 lines (144 loc) · 4.61 KB
/
cuesheet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "cuesheet.h"
#include <cstdio>
#include <cstring>
char *read_atom(FILE *fl, char *buffer, int buffer_len, bool *eof) {
int whitespace_mode = 0;
int wh = 0;
int ret = 0;
buffer[0] = '\0';
do {
if (wh >= buffer_len) {
return nullptr;
}
ret = fread(&buffer[wh], 1, 1, fl);
if (ret == 0) {
*eof = true;
return buffer;
}
if (buffer[wh] == '\n') {
buffer[wh] = '\0';
return buffer;
}
if (whitespace_mode == 0) {
if ((buffer[wh] != ' ') && (buffer[wh] != '\t') && (buffer[wh] != '\r')) {
if (buffer[wh] == '"') {
whitespace_mode = 2;
} else {
whitespace_mode = 1;
wh++;
}
}
} else if (whitespace_mode == 1) {
if (buffer[wh] == ' ') {
buffer[wh] = '\0';
return buffer;
} else if (buffer[wh] == '"') {
whitespace_mode = 2;
}
wh++;
} else if (whitespace_mode == 2) {
if (buffer[wh] == '"') {
whitespace_mode = 1;
} else {
wh++;
}
}
} while (ret != 0);
return buffer;
}
void read_title(FILE *fl, bool *iseof, cuesh_track *track) {
char atombuf[256];
char *atomtitl = read_atom(fl, atombuf, 256, iseof);
if (!*iseof && atomtitl) {
track->title = std::string(atomtitl);
}
}
void read_performer(FILE *fl, bool *iseof, cuesh_track *track) {
char atombuf[256];
char *atomperf = read_atom(fl, atombuf, 256, iseof);
if (!iseof && atomperf) {
track->performer = std::string(atomperf);
}
}
void read_index(FILE *fl, bool *iseof, cuesh_track *track) {
char atombuf[256];
char *atomindex = read_atom(fl, atombuf, 256, iseof);
if (!*iseof && atomindex) {
track->index = atoi(atomindex);
*iseof = false;
char *duration = read_atom(fl, atombuf, 256, iseof);
if (!*iseof && duration) {
int frames;
track->hours = 0;
sscanf(duration, "%02d:%02d:%02d", &track->minutes, &track->seconds, &frames);
}
}
}
void read_track(FILE *fl, bool *iseof, cuesh_track *track) {
char atombuf[256];
char *atomid = read_atom(fl, atombuf, 256, iseof);
if (!*iseof && atomid) {
track->id = atoi(atomid);
}
char *atomkind = read_atom(fl, atombuf, 256, iseof);
if (!*iseof && atomkind) {
track->type = CSH_TRK_OTHER;
if (strcmp(atomkind, "AUDIO") == 0) {
track->type = CSH_TRK_AUDIO;
}
}
return;
}
cuesh_data *cuesh_init(const char *cuefile) {
FILE *fl = fopen(cuefile, "r");
cuesh_data *cd = new cuesh_data();
cuesh_track *current_track = nullptr;
while (!feof(fl)) {
char atombuf[256];
bool iseof = false;
char *atom = read_atom(fl, atombuf, 32, &iseof);
if (!iseof) {
iseof = false;
if (atom == nullptr) {
return nullptr;
}
if (strcmp(atom, "REM") == 0) {
printf("%s\n", atom);
} else if (strcmp(atom, "PERFORMER") == 0) {
char *atomperf = read_atom(fl, atombuf, 256, &iseof);
if (!iseof && atomperf) {
if (current_track) {
current_track->performer = std::string(atomperf);
} else {
cd->performer = std::string(atomperf);
}
}
} else if (strcmp(atom, "TRACK") == 0) {
if (current_track) {
cd->tracks.push_back(current_track);
current_track = nullptr;
}
current_track = new cuesh_track();
read_track(fl, &iseof, current_track);
} else if (strcmp(atom, "TITLE") == 0) {
if (current_track)
read_title(fl, &iseof, current_track);
} else if (strcmp(atom, "INDEX") == 0) {
if (current_track)
read_index(fl, &iseof, current_track);
} else if (strcmp(atom, "FILE") == 0) {
char *atomfile = read_atom(fl, atombuf, 256, &iseof);
if (!iseof && atomfile) {
cd->filename = std::string(atomfile);
}
}
}
}
printf("%s\n", cd->filename.c_str());
return cd;
}
void cuesh_finit(cuesh_data *data) {
for (auto &d : data->tracks) {
delete d;
}
}