Skip to content

Commit

Permalink
Refactor index parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
lipnitsk committed Oct 26, 2015
1 parent a14693e commit 8855ccd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
33 changes: 14 additions & 19 deletions cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ struct Track {
char *isrc; /* IRSC Code (5.22.4) 12 bytes */
Cdtext *cdtext; /* CD-TEXT */
Rem* rem;
int nindex; /* number of indexes */
long index[MAXINDEX]; /* indexes (in frames) (5.29.2.5)
* relative to start of track
* index[0] should always be zero */
* relative to start of file */
};

struct Cd {
Expand Down Expand Up @@ -132,7 +130,10 @@ Track *track_init(void)
track->isrc = NULL;
track->cdtext = cdtext_init();
track->rem = rem_new();
track->nindex = 0;

int i;
for (i=0; i<MAXTRACK; i++)
track->index[i] = -1;
}

return track;
Expand Down Expand Up @@ -337,25 +338,19 @@ track_get_rem( Track* track)
return NULL;
}

void track_add_index(Track *track, long ind)
void track_set_index(Track *track, int i, long ind)
{
if (MAXTRACK - 1 > track->nindex)
track->nindex++;
else
if (i >= MAXTRACK) {
fprintf(stderr, "too many indexes\n");
return;
}

/* this will overwrite last index if there were too many */
track->index[track->nindex - 1] = ind;
}

int track_get_nindex(Track *track)
{
return track->nindex;
track->index[i] = ind;
}

long track_get_index(Track *track, int i)
{
if ((0 <= i) && (i < track->nindex))
if ((0 <= i) && (i < MAXTRACK))
return track->index[i];

return -1;
Expand All @@ -377,10 +372,10 @@ static void cd_track_dump(Track *track)
printf("sub_mode: %d\n", track->sub_mode);
printf("flags: 0x%x\n", track->flags);
printf("isrc: %s\n", track->isrc);
printf("indexes: %d\n", track->nindex);

for (i = 0; i < track->nindex; ++i)
printf("index %d: %ld\n", i, track->index[i]);
for (i = 0; i < MAXTRACK; ++i)
if (track->index[i] != -1)
printf("index %d: %ld\n", i, track->index[i]);

if (NULL != track->cdtext) {
printf("cdtext:\n");
Expand Down
3 changes: 1 addition & 2 deletions cd.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ Cdtext *track_get_cdtext(Track *track);

Rem* track_get_rem(Track* track);

void track_add_index(Track *track, long index);
int track_get_nindex(Track *track);
void track_set_index(Track *track, int i, long index);
long track_get_index(Track *track, int i);

#endif
27 changes: 15 additions & 12 deletions cue_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -231,24 +231,27 @@ track_statement
| TRACK_ISRC STRING '\n' { track_set_isrc(track, $2); }
| PREGAP time '\n' { track_set_zero_pre(track, $2); }
| INDEX NUMBER time '\n' {
int i = track_get_nindex(track);
long prev_length;

if (0 == i) {
/* first index */
/* Set previous track length if it has not been set */
if (NULL != prev_track && NULL == cur_filename
&& track_get_length (prev_track) == 0) {
/* track shares file with previous track */
prev_length = $3 - track_get_start(prev_track);
track_set_length(prev_track, prev_length);
}

if (1 == $2) {
/* INDEX 01 */
track_set_start(track, $3);

if (NULL != prev_track && NULL == cur_filename) {
/* track shares file with previous track */
prev_length = $3 - track_get_start(prev_track);
track_set_length(prev_track, prev_length);
}
long idx00 = track_get_index (track, 0);

if (idx00 != -1)
track_set_zero_pre (track, $3 - idx00);
}

for (; i <= $2; i++)
track_add_index(track, \
track_get_zero_pre(track) + $3 \
- track_get_start(track));
track_set_index (track, $2, $3);
}
| POSTGAP time '\n' { track_set_zero_post(track, $2); }
| track_data
Expand Down

0 comments on commit 8855ccd

Please sign in to comment.