From: notaz Date: Fri, 11 Jul 2008 15:20:04 +0000 (+0000) Subject: bin_to_cso_mp3 improved X-Git-Tag: v1.85~424 X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f8a6410104d1294b75fbcb0f63c1dc39e3a5c40c;p=picodrive.git bin_to_cso_mp3 improved git-svn-id: file:///home/notaz/opt/svn/PicoDrive@539 be3aeb3a-fb24-0410-a615-afba39da0efa --- diff --git a/tools/bin_to_cso_mp3/bin_to_cso_mp3.c b/tools/bin_to_cso_mp3/bin_to_cso_mp3.c index 87a55991..39ca7bf2 100644 --- a/tools/bin_to_cso_mp3/bin_to_cso_mp3.c +++ b/tools/bin_to_cso_mp3/bin_to_cso_mp3.c @@ -2,12 +2,14 @@ * bin_to_cso_mp3 * originally written by Exophase as "bin_to_iso_ogg" * updated for cso/mp3 by notaz + * v2 */ #include #include #include #include #include +#include #ifndef MAX_PATH #define MAX_PATH 1024 @@ -100,12 +102,28 @@ static void myexit(int code) char *skip_whitespace(char *str) { - while(*str == ' ') + while (isspace(*str)) str++; return str; } +char *skip_whitespace_rev(char *str) +{ + while (isspace(*str)) + str--; + + return str; +} + +char *skip_nonspace_rev(char *str) +{ + while (!isspace(*str)) + str--; + + return str; +} + s32 load_bin_cue(char *cue_file_name) { FILE *cue_file = fopen(cue_file_name, "rb"); @@ -116,6 +134,7 @@ s32 load_bin_cue(char *cue_file_name) { char line_buffer[256]; char *line_buffer_ptr; + char *tmp; char bin_file_name[MAX_PATH]; char *separator_pos; @@ -129,11 +148,31 @@ s32 load_bin_cue(char *cue_file_name) u32 i; // First, get filename. Only support binary right now. - fgets(line_buffer, 255, cue_file); - - strcpy(bin_file_name, strchr(line_buffer, '"') + 1); - - *(strrchr(bin_file_name, '"')) = 0; + tmp = fgets(line_buffer, 255, cue_file); + if (tmp == NULL) goto invalid; + separator_pos = line_buffer + strlen(line_buffer) - 1; + separator_pos = skip_whitespace_rev(separator_pos); + if (separator_pos <= line_buffer) goto invalid; + separator_pos = skip_nonspace_rev(separator_pos); + if (separator_pos <= line_buffer) goto invalid; + separator_pos = skip_whitespace_rev(separator_pos); + if (separator_pos <= line_buffer) goto invalid; + // see if what's there is a quote. + if(*separator_pos == '"') + { + separator_pos[0] = 0; + separator_pos = strrchr(line_buffer, '"'); + if (separator_pos == NULL) goto invalid; + strcpy(bin_file_name, separator_pos + 1); + } + else + { + // Otherwise go to the next space. + separator_pos[1] = 0; + separator_pos = strrchr(line_buffer, ' '); + if (separator_pos == NULL) goto invalid; + strcpy(bin_file_name, separator_pos + 1); + } // Might have to change directory first. separator_pos = strrchr(cue_file_name, DIR_SEPARATOR_CHAR); @@ -153,8 +192,6 @@ s32 load_bin_cue(char *cue_file_name) cd_bin.bin_file = fopen(bin_file_name, "rb"); #endif - printf("loaded bin file %s (%p)\n", bin_file_name, cd_bin.bin_file); - *separator_pos = DIR_SEPARATOR_CHAR; chdir(current_dir); } @@ -167,6 +204,16 @@ s32 load_bin_cue(char *cue_file_name) #endif } + if (cd_bin.bin_file == NULL) + { + printf("can't open bin file: \"%s\"\n", bin_file_name); + return -1; + } + else + { + printf("found bin file: %s\n", bin_file_name); + } + for(i = 0; i < 100; i++) { cd_bin.logical_tracks[i] = NULL; @@ -338,6 +385,9 @@ s32 load_bin_cue(char *cue_file_name) return 0; } + return -1; +invalid: + printf("error: invalid/unsupported .cue file\n"); return -1; } @@ -600,9 +650,9 @@ s32 convert_bin_cue(char *output_name_base) #ifdef _WIN32 static void update_path(void) { - char buff1[MAX_PATH], buff2[MAX_PATH]; + char buff1[MAX_PATH*4], *buff2; char *path; - int i; + int size, i; path = getenv("PATH"); GetModuleFileNameA(NULL, buff1, sizeof(buff1)); @@ -610,8 +660,13 @@ static void update_path(void) if (buff1[i] == '\\') break; buff1[i] = 0; - snprintf(buff2, sizeof(buff2), "%s;%s", path, buff1); + size = strlen(path) + strlen(buff1) + 3; + buff2 = malloc(size); + if (buff2 == NULL) return; + + snprintf(buff2, size, "%s;%s", path, buff1); SetEnvironmentVariableA("PATH", buff2); + free(buff2); } #endif diff --git a/tools/bin_to_cso_mp3/bin_to_cso_mp3.exe b/tools/bin_to_cso_mp3/bin_to_cso_mp3.exe index 733795cb..5571dc9a 100755 Binary files a/tools/bin_to_cso_mp3/bin_to_cso_mp3.exe and b/tools/bin_to_cso_mp3/bin_to_cso_mp3.exe differ