From 5448101bdf39af549bf8f944a7b50ba891311455 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Mon, 6 Jul 2015 23:32:58 +1200 Subject: Fixed track end overrun, tidied run-to-EOF (last track) operation, removed pointless typecasts --- Makefile | 2 +- cue-bin-split.c | 42 ++++++++++++++++++++++++------------------ cue-bin-split.h | 5 +++++ misc.h | 2 ++ 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 62611f9..70a9493 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ all: cue-bin-split cue-bin-split: $(OBJECTS) $(CC) -o $(EXEC_NAME) $^ $(LDFLAGS) -%.o: %.c +%.o: %.c %.h $(CC) -c -o $@ $< $(CFLAGS) diff --git a/cue-bin-split.c b/cue-bin-split.c index ded3aaf..45d5035 100644 --- a/cue-bin-split.c +++ b/cue-bin-split.c @@ -25,11 +25,6 @@ * SUCH DAMAGE. */ -#include -#include -#include -#include - #include "cue-bin-split.h" #include "misc.h" @@ -51,7 +46,7 @@ int main(int argc, char **argv) char out_fname[1024]; /* That should do it. Note: overflow IS caught */ int track = 0; int items = 0; - int i = 0; + unsigned long i = 0; char buffer[BUFFER_SIZE]; /* Timestamp building blocks */ @@ -107,7 +102,7 @@ int main(int argc, char **argv) track = 0; items = get_stamp(&mm, &ss, &ff); /* FIXME doesn't check return value */ - start_sec = (double)mm*60 + (double)ss + ((double)ff)/FRAMES_PER_SEC; + start_sec = mm*60 + ss + ((double)ff)/FRAMES_PER_SEC; while ( ( items = get_stamp(&mm, &ss, &ff) ) ) { @@ -136,38 +131,49 @@ int main(int argc, char **argv) fprintf(stderr, "Timestamp #%d malformed\n", track); break; } - finish_sec = (double)mm*60 + (double)ss + ((double)ff)/75; + finish_sec = mm*60 + ss + ((double)ff)/75; printf("%s starts %f s, finishes %f s\n", out_fname, start_sec, finish_sec); } start_sample = start_sec * rate * channels; finish_sample = finish_sec * rate * channels; - if (start_s > finish_s) + if (start_sample > finish_sample) { fprintf(stderr, "ERROR: Finish time can't be before start time, skipping %s", out_fname); continue; } + /* Run to "infinity" if no finish time set. + * Of course, will actually run to EOF assuming file's small enough */ + if (finish_sec == -1) + finish_sample = ULONG_MAX; - /* FIXME this copies whole buffers, potentially going over end of track */ - for (i = (int)start_sample; i != (int)finish_sample; i += items) + for (i = start_sample; i < finish_sample; i += items) { - if ((items = fread(buffer, sample_size, sizeof(buffer)/sample_size, fin))) + /* FIXME perror on items == 0 after read */ + items = fread(buffer, + sample_size, + MIN(sizeof(buffer)/sample_size, (finish_sample - i)), + fin); + if (feof(fin)) + break; + + if ( fwrite(buffer, sample_size, items, fout) != items) { - if (feof(fin)) - break; - fwrite(buffer, items, sample_size, fout); + fprintf(stderr, "Write to %s failed: ", out_fname); + perror("fwrite"); + break; } } fclose(fout); start_sec = finish_sec; - /* FIXME ick */ - if (finish_sec < 0) + /* finish_sample was ULONG_MAX if this was a run to EOF (last track) + * so don't bother looping around */ + if (finish_sample == ULONG_MAX) break; } return 0; } - diff --git a/cue-bin-split.h b/cue-bin-split.h index 1c30ea9..059f9bb 100644 --- a/cue-bin-split.h +++ b/cue-bin-split.h @@ -28,6 +28,11 @@ #ifndef CUE_BIN_SPLIT_H #define CUE_BIN_SPLIT_H +#include +#include +#include +#include + #define FRAMES_PER_SEC 75 #define BUFFER_SIZE 1024*1024 /* Meh, good enough */ diff --git a/misc.h b/misc.h index 813c73b..a21d626 100644 --- a/misc.h +++ b/misc.h @@ -31,6 +31,8 @@ #include #include +#define MIN(a, b) ((a < b)? a : b) + int get_stamp(int *m, int *s, int *f); void construct_out_name(char *buffer, size_t buffer_size, char* format, unsigned int track); void die_help(); -- cgit v1.1