1
0
Fork 0
mirror of https://github.com/ErikAndren/cue2pops-mac.git synced 2025-04-19 15:38:04 +02:00

Remove lots of code duplication

This commit is contained in:
Erik Andrén Zachrisson 2015-11-01 22:17:09 +01:00
parent d4ffa2429e
commit ce0d78b3bd

View file

@ -501,7 +501,6 @@ int main(int argc, char **argv)
}
cue_ptr++; // Jump to the BINARY name/path starting with "
bin_path = malloc((strlen(cue_ptr) + strlen(cue_name)) * 2);
if (bin_path == NULL) {
printf("Error: Failed to allocate memory for the bin_path string\n");
@ -1040,344 +1039,6 @@ int main(int argc, char **argv)
return 0;
}
/* 2 user arguments : no command, output file is user argument 2 */
if(gap_more == 0 && gap_less == 0 && vmode == 0 && trainer == 0 && argc == 3) {
printf("Saving the virtual CD-ROM image. Please wait...\n");
if(!(vcd_file = fopen(vcd_name, "wb"))) {
printf("Error : Cannot write to %s\n\n", vcd_name);
free(bin_path);
free(headerbuf);
free(outbuf);
return 0;
}
fwrite(headerbuf, 1, HEADERSIZE, vcd_file);
fclose(vcd_file);
free(headerbuf);
if(!(vcd_file = fopen(vcd_name, "ab+"))) {
printf("Error : Cannot write to %s\n\n", vcd_name);
free(bin_path);
free(outbuf);
return 0;
}
if(!(bin_file = fopen(bin_path, "rb"))) {
printf("Error: Cannot open %s\n\n", bin_path);
free(bin_path);
free(outbuf);
return 0;
}
free(bin_path);
for(i = 0; i < bin_size; i += HEADERSIZE) {
if(fix_CDRWIN == 1 && (i + HEADERSIZE >= daTrack_ptr)) {
if(debug != 0) printf("Padding the CDRWIN dump inside of the virtual CD-ROM image...");
fread(outbuf, HEADERSIZE - (i + HEADERSIZE - daTrack_ptr), 1, bin_file);
fwrite(outbuf, HEADERSIZE - (i + HEADERSIZE - daTrack_ptr), 1, vcd_file);
char padding[(150 * SECTORSIZE) * 2];
fwrite(padding, 150 * SECTORSIZE, 1, vcd_file);
fread(outbuf, HEADERSIZE - (HEADERSIZE - (i + HEADERSIZE - daTrack_ptr)), 1, bin_file);
fwrite(outbuf, HEADERSIZE - (HEADERSIZE - (i + HEADERSIZE - daTrack_ptr)), 1, vcd_file);
fix_CDRWIN = 0;
if(debug != 0) {
printf(" Done.\n");
printf("Continuing...\n");
}
//if(vmode == 1) printf("----------------------------------------------------------------------------------\n");
} else {
if(vmode == 1 && i == 0) {
printf("----------------------------------------------------------------------------------\n");
printf("NTSC Patcher is ON\n");
printf("----------------------------------------------------------------------------------\n");
}
fread(outbuf, HEADERSIZE, 1, bin_file);
if(i == 0) GameIdentifier(outbuf);
if(GameTitle >= 0 && GameHasCheats == 1 && trainer == 1 && i == 0) {
printf("GameTrainer is ON\n");
printf("----------------------------------------------------------------------------------\n");
}
if(GameTitle >= 0 && GameTrained == 0 && GameHasCheats == 1 && trainer == 1 && i <= daTrack_ptr) GameTrainer(outbuf);
if(GameTitle >= 0 && GameFixed == 0 && fix_game == 1 && i <= daTrack_ptr) GameFixer(outbuf);
if(vmode == 1 && i <= daTrack_ptr) NTSCpatcher(outbuf, i);
if(i + HEADERSIZE >= bin_size) {
fwrite(outbuf, HEADERSIZE - (i + HEADERSIZE - bin_size), 1, vcd_file);
} else {
fwrite(outbuf, HEADERSIZE, 1, vcd_file);
}
}
}
if(GameTitle >= 0 && fix_game == 1 && GameFixed == 0) {
printf("COULD NOT APPLY THE GAME FIXE(S) : No data to patch found\n");
printf("----------------------------------------------------------------------------------\n");
}
if(GameTitle >= 0 && GameHasCheats == 1 && GameTrained == 0 && trainer == 1) {
printf("COULD NOT APPLY THE GAME CHEAT(S) : No data to patch found\n");
printf("----------------------------------------------------------------------------------\n");
}
free(outbuf);
fclose(bin_file);
fclose(vcd_file);
printf("A POPS virtual CD-ROM image was saved to :\n");
printf("%s\n\n", vcd_name);
return 1;
}
/* 3 user arguments : 1 command, the command is user argument 2, output file is user argument 3 */
if(((gap_more == 0 && vmode == 0 && trainer == 1) || (gap_less == 0 && vmode == 0 && trainer == 1) || (gap_more == 1 && vmode == 0 && trainer == 0) || (gap_less == 1 && vmode == 0 && trainer == 0) || (gap_more == 0 && gap_less == 0 && vmode == 1 && trainer == 0)) && argc == 4) {
printf("Saving the virtual CD-ROM image. Please wait...\n");
if(!(vcd_file = fopen(vcd_name, "wb"))) {
printf("Error : Cannot write to %s\n\n", vcd_name);
free(bin_path);
free(headerbuf);
free(outbuf);
return 0;
}
fwrite(headerbuf, 1, HEADERSIZE, vcd_file);
fclose(vcd_file);
free(headerbuf);
if(!(vcd_file = fopen(vcd_name, "ab+"))) {
printf("Error : Cannot write to %s\n\n", vcd_name);
free(bin_path);
free(outbuf);
return 0;
}
if(!(bin_file = fopen(bin_path, "rb"))) {
printf("Error: Cannot open %s\n\n", bin_path);
free(bin_path);
free(outbuf);
return 0;
}
free(bin_path);
for(i = 0; i < bin_size; i += HEADERSIZE) {
if(fix_CDRWIN == 1 && (i + HEADERSIZE >= daTrack_ptr)) {
if(debug != 0) printf("Padding the CDRWIN dump inside of the virtual CD-ROM image...");
fread(outbuf, HEADERSIZE - (i + HEADERSIZE - daTrack_ptr), 1, bin_file);
fwrite(outbuf, HEADERSIZE - (i + HEADERSIZE - daTrack_ptr), 1, vcd_file);
char padding[(150 * SECTORSIZE) * 2];
fwrite(padding, 150 * SECTORSIZE, 1, vcd_file);
fread(outbuf, HEADERSIZE - (HEADERSIZE - (i + HEADERSIZE - daTrack_ptr)), 1, bin_file);
fwrite(outbuf, HEADERSIZE - (HEADERSIZE - (i + HEADERSIZE - daTrack_ptr)), 1, vcd_file);
fix_CDRWIN = 0;
if(debug != 0) {
printf(" Done.\n");
printf("Continuing...\n");
}
//if(vmode == 1) printf("----------------------------------------------------------------------------------\n");
} else {
if(vmode == 1 && i == 0) {
printf("----------------------------------------------------------------------------------\n");
printf("NTSC Patcher is ON\n");
printf("----------------------------------------------------------------------------------\n");
}
fread(outbuf, HEADERSIZE, 1, bin_file);
if(i == 0) GameIdentifier(outbuf);
if(GameTitle >= 0 && GameHasCheats == 1 && trainer == 1 && i == 0) {
printf("GameTrainer is ON\n");
printf("----------------------------------------------------------------------------------\n");
}
if(GameTitle >= 0 && GameTrained == 0 && GameHasCheats == 1 && trainer == 1 && i <= daTrack_ptr) GameTrainer(outbuf);
if(GameTitle >= 0 && GameFixed == 0 && fix_game == 1 && i <= daTrack_ptr) GameFixer(outbuf);
if(vmode == 1 && i <= daTrack_ptr) NTSCpatcher(outbuf, i);
if(i + HEADERSIZE >= bin_size) {
fwrite(outbuf, HEADERSIZE - (i + HEADERSIZE - bin_size), 1, vcd_file);
} else {
fwrite(outbuf, HEADERSIZE, 1, vcd_file);
}
}
}
if(GameTitle >= 0 && fix_game == 1 && GameFixed == 0) {
printf("COULD NOT APPLY THE GAME FIXE(S) : No data to patch found\n");
printf("----------------------------------------------------------------------------------\n");
}
if(GameTitle >= 0 && GameHasCheats == 1 && GameTrained == 0 && trainer == 1) {
printf("COULD NOT APPLY THE GAME CHEAT(S) : No data to patch found\n");
printf("----------------------------------------------------------------------------------\n");
}
free(outbuf);
fclose(bin_file);
fclose(vcd_file);
printf("A POPS virtual CD-ROM image was saved to :\n");
printf("%s\n\n", vcd_name);
return 1;
}
/* 4 user arguments : 2 commands, commands are user arguments 2 and 3, output file is user argument 4 */
if(((gap_more == 1 && vmode == 0 && trainer == 1) || (gap_less == 1 && vmode == 0 && trainer == 1) || (gap_more == 1 && vmode == 1 && trainer == 0) || (gap_less == 1 && vmode == 1 && trainer == 0) || (gap_more == 0 && gap_less == 0 && vmode == 1 && trainer == 1)) && argc == 5) {
printf("Saving the virtual CD-ROM image. Please wait...\n");
if(!(vcd_file = fopen(vcd_name, "wb"))) {
printf("Error : Cannot write to %s\n\n", vcd_name);
free(bin_path);
free(headerbuf);
free(outbuf);
return 0;
}
fwrite(headerbuf, 1, HEADERSIZE, vcd_file);
fclose(vcd_file);
free(headerbuf);
if(!(vcd_file = fopen(vcd_name, "ab+"))) {
printf("Error : Cannot write to %s\n\n", vcd_name);
free(bin_path);
free(outbuf);
return 0;
}
if(!(bin_file = fopen(bin_path, "rb"))) {
printf("Error: Cannot open %s\n\n", bin_path);
free(bin_path);
free(outbuf);
return 0;
}
free(bin_path);
for(i = 0; i < bin_size; i += HEADERSIZE) {
if(fix_CDRWIN == 1 && (i + HEADERSIZE >= daTrack_ptr)) {
if(debug != 0) printf("Padding the CDRWIN dump inside of the virtual CD-ROM image...");
fread(outbuf, HEADERSIZE - (i + HEADERSIZE - daTrack_ptr), 1, bin_file);
fwrite(outbuf, HEADERSIZE - (i + HEADERSIZE - daTrack_ptr), 1, vcd_file);
char padding[(150 * SECTORSIZE) * 2];
fwrite(padding, 150 * SECTORSIZE, 1, vcd_file);
fread(outbuf, HEADERSIZE - (HEADERSIZE - (i + HEADERSIZE - daTrack_ptr)), 1, bin_file);
fwrite(outbuf, HEADERSIZE - (HEADERSIZE - (i + HEADERSIZE - daTrack_ptr)), 1, vcd_file);
fix_CDRWIN = 0;
if(debug != 0) {
printf(" Done.\n");
printf("Continuing...\n");
}
//if(vmode == 1) printf("----------------------------------------------------------------------------------\n");
} else {
if(vmode == 1 && i == 0) {
printf("----------------------------------------------------------------------------------\n");
printf("NTSC Patcher is ON\n");
printf("----------------------------------------------------------------------------------\n");
}
fread(outbuf, HEADERSIZE, 1, bin_file);
if(i == 0) GameIdentifier(outbuf);
if(GameTitle >= 0 && GameHasCheats == 1 && trainer == 1 && i == 0) {
printf("GameTrainer is ON\n");
printf("----------------------------------------------------------------------------------\n");
}
if(GameTitle >= 0 && GameTrained == 0 && GameHasCheats == 1 && trainer == 1 && i <= daTrack_ptr) GameTrainer(outbuf);
if(GameTitle >= 0 && GameFixed == 0 && fix_game == 1 && i <= daTrack_ptr) GameFixer(outbuf);
if(vmode == 1 && i <= daTrack_ptr) NTSCpatcher(outbuf, i);
if(i + HEADERSIZE >= bin_size) {
fwrite(outbuf, HEADERSIZE - (i + HEADERSIZE - bin_size), 1, vcd_file);
} else {
fwrite(outbuf, HEADERSIZE, 1, vcd_file);
}
}
}
if(GameTitle >= 0 && fix_game == 1 && GameFixed == 0) {
printf("COULD NOT APPLY THE GAME FIXE(S) : No data to patch found\n");
printf("----------------------------------------------------------------------------------\n");
}
if(GameTitle >= 0 && GameHasCheats == 1 && GameTrained == 0 && trainer == 1) {
printf("COULD NOT APPLY THE GAME CHEAT(S) : No data to patch found\n");
printf("----------------------------------------------------------------------------------\n");
}
free(outbuf);
fclose(bin_file);
fclose(vcd_file);
printf("A POPS virtual CD-ROM image was saved to :\n");
printf("%s\n\n", vcd_name);
return 1;
}
/* 5 user arguments : 3 commands; Commands are user arguments 2, 3 and 4; Output file is user argument 5 */
if(argc == 6) {
printf("Saving the virtual CD-ROM image. Please wait...\n");
if(!(vcd_file = fopen(vcd_name, "wb"))) {
printf("Error : Cannot write to %s\n\n", vcd_name);
free(bin_path);
free(headerbuf);
free(outbuf);
return 0;
}
fwrite(headerbuf, 1, HEADERSIZE, vcd_file);
fclose(vcd_file);
free(headerbuf);
if(!(vcd_file = fopen(vcd_name, "ab+"))) {
printf("Error : Cannot write to %s\n\n", vcd_name);
free(bin_path);
free(outbuf);
return 0;
}
if(!(bin_file = fopen(bin_path, "rb"))) {
printf("Error: Cannot open %s\n\n", bin_path);
free(bin_path);
free(outbuf);
return 0;
}
free(bin_path);
for(i = 0; i < bin_size; i += HEADERSIZE) {
if(fix_CDRWIN == 1 && (i + HEADERSIZE >= daTrack_ptr)) {
if(debug != 0) printf("Padding the CDRWIN dump inside of the virtual CD-ROM image...");
fread(outbuf, HEADERSIZE - (i + HEADERSIZE - daTrack_ptr), 1, bin_file);
fwrite(outbuf, HEADERSIZE - (i + HEADERSIZE - daTrack_ptr), 1, vcd_file);
char padding[(150 * SECTORSIZE) * 2];
fwrite(padding, 150 * SECTORSIZE, 1, vcd_file);
fread(outbuf, HEADERSIZE - (HEADERSIZE - (i + HEADERSIZE - daTrack_ptr)), 1, bin_file);
fwrite(outbuf, HEADERSIZE - (HEADERSIZE - (i + HEADERSIZE - daTrack_ptr)), 1, vcd_file);
fix_CDRWIN = 0;
if(debug != 0) {
printf(" Done.\n");
printf("Continuing...\n");
}
//if(vmode == 1) printf("----------------------------------------------------------------------------------\n");
} else {
if(vmode == 1 && i == 0) {
printf("----------------------------------------------------------------------------------\n");
printf("NTSC Patcher is ON\n");
printf("----------------------------------------------------------------------------------\n");
}
fread(outbuf, HEADERSIZE, 1, bin_file);
if(i == 0) GameIdentifier(outbuf);
if(GameTitle >= 0 && GameHasCheats == 1 && trainer == 1 && i == 0) {
printf("GameTrainer is ON\n");
printf("----------------------------------------------------------------------------------\n");
}
if(GameTitle >= 0 && GameTrained == 0 && GameHasCheats == 1 && trainer == 1 && i <= daTrack_ptr) GameTrainer(outbuf);
if(GameTitle >= 0 && GameFixed == 0 && fix_game == 1 && i <= daTrack_ptr) GameFixer(outbuf);
if(vmode == 1 && i <= daTrack_ptr) NTSCpatcher(outbuf, i);
if(i + HEADERSIZE >= bin_size) {
fwrite(outbuf, HEADERSIZE - (i + HEADERSIZE - bin_size), 1, vcd_file);
} else {
fwrite(outbuf, HEADERSIZE, 1, vcd_file);
}
}
}
if(GameTitle >= 0 && fix_game == 1 && GameFixed == 0) {
printf("COULD NOT APPLY THE GAME FIXE(S) : No data to patch found\n");
printf("----------------------------------------------------------------------------------\n");
}
if(GameTitle >= 0 && GameHasCheats == 1 && GameTrained == 0 && trainer == 1) {
printf("COULD NOT APPLY THE GAME CHEAT(S) : No data to patch found\n");
printf("----------------------------------------------------------------------------------\n");
}
free(outbuf);
fclose(bin_file);
fclose(vcd_file);
printf("A POPS virtual CD-ROM image was saved to :\n");
printf("%s\n\n", vcd_name);
return 1;
}
printf("Saving the virtual CD-ROM image. Please wait...\n");
if(!(vcd_file = fopen(vcd_name, "wb"))) {
printf("Error : Cannot write to %s\n\n", vcd_name);
@ -1386,7 +1047,6 @@ int main(int argc, char **argv)
free(outbuf);
return 0;
}
fwrite(headerbuf, 1, HEADERSIZE, vcd_file);
fclose(vcd_file);
free(headerbuf);
@ -1420,7 +1080,6 @@ int main(int argc, char **argv)
printf(" Done.\n");
printf("Continuing...\n");
}
//if(vmode == 1) printf("----------------------------------------------------------------------------------\n");
} else {
if(vmode == 1 && i == 0) {
printf("----------------------------------------------------------------------------------\n");
@ -1458,7 +1117,7 @@ int main(int argc, char **argv)
printf("A POPS virtual CD-ROM image was saved to :\n");
printf("%s\n\n", vcd_name);
return 1;
}
/* EOSRC, oh mah dayum */