From 7223af09ec1010cc568caacf7c0a798781f2f1b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Andr=C3=A9n=20Zachrisson?= Date: Sun, 1 Nov 2015 12:41:48 +0100 Subject: [PATCH] Split out bin size calculation into separate function --- cue2pops.c | 53 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/cue2pops.c b/cue2pops.c index 57b4b89..c2518ac 100644 --- a/cue2pops.c +++ b/cue2pops.c @@ -207,12 +207,37 @@ void NTSCpatcher(unsigned char *inbuf, int tracker) } } +int GetBinSize(char *bin_name) +{ + FILE *bin; + int status; + int size; + + if(!(bin = fopen(bin_name, "rb"))) { // Open the BINARY that is attached to the cue + printf("Error: Cannot open %s\n\n", bin_name); + return -1; + } + + status = fseek(bin, 0, SEEK_END); + if (status != 0) { + printf("Error: Failed to seek %s\n", bin_name); + return -1; + } + + size = ftell(bin); // Get it's size + if (bin_size == -1L) { + printf("Error: Failed to get file %s size\n", bin_name); + return -1; + } + fclose(bin); + + return size; +} + int GetLeadOut(unsigned char *hbuf) { /* MSF is calculated from the dump size so DO NOT APPLY gap++/gap-- ADJUSTMENTS IN THIS FUNCTION ! */ - FILE *bin; - int status; // Formatted Lead-Out MM:SS:FF char LeadOut[7]; @@ -220,23 +245,6 @@ int GetLeadOut(unsigned char *hbuf) int leadoutS; // Calculated Lead-Out __:SS:__ int leadoutF; // Calculated Lead-Out __:__:FF - if(!(bin = fopen(bin_path, "rb"))) { // Open the BINARY that is attached to the cue - printf("Error: Cannot open %s\n\n", bin_path); - return -1; - } - - status = fseek(bin, 0, SEEK_END); - if (status != 0) { - printf("Error: Failed to seek %s\n", bin_path); - return -1; - } - - bin_size = ftell(bin); // Get it's size - if (bin_size == -1L) { - printf("Error: Failed to get file %s size\n", bin_path); - return -1; - } - fclose(bin); sector_count = (bin_size / SECTORSIZE) + (150 * (pregap_count + postgap_count)) + 150; // Convert the bin_size to sector count leadoutM = sector_count / 4500; @@ -979,6 +987,13 @@ int main(int argc, char **argv) } free(cuebuf); + bin_size = GetBinSize(bin_path); + if (bin_size < 0) { + free(bin_path); + free(headerbuf); + return 0; + } + if(GetLeadOut(headerbuf) != 0) { free(bin_path); free(headerbuf);