From 8b9b2f070e04b447cfb288fa29aee541a93fc526 Mon Sep 17 00:00:00 2001 From: Steven Smith Date: Sat, 24 Jan 2015 10:53:59 -0800 Subject: [PATCH] Improve file related error reporting, allow for creating CWAV and LZ11 compressed files. --- source/commandline.cpp | 10 ++++++ source/main.cpp | 80 +++++++++++++++++++++++++++++++++++++++++- source/wav.cpp | 3 +- 3 files changed, 91 insertions(+), 2 deletions(-) diff --git a/source/commandline.cpp b/source/commandline.cpp index 7680dc0..6d2c470 100644 --- a/source/commandline.cpp +++ b/source/commandline.cpp @@ -42,12 +42,22 @@ void cmd_print_info(const char* command) { printf(" -i/--image: PNG file to use as the banner image.\n"); printf(" -a/--audio: WAV file to use as the banner's tune.\n"); printf(" -o/--output: File to output the created banner to.\n"); + } else if(strcmp(command, "makecwav") == 0) { + printf("makecwav - Creates a CWAV file from a WAV.\n"); + printf(" -i/--input: WAV file to convert.\n"); + printf(" -o/--output: File to output the created CWAV to.\n"); + } else if(strcmp(command, "lz11") == 0) { + printf("lz11 - Compresses a file with LZ11.\n"); + printf(" -i/--input: File to compress.\n"); + printf(" -o/--output: File to output the compressed data to.\n"); } } void cmd_print_commands() { printf("Available commands:\n"); cmd_print_info("makebanner"); + cmd_print_info("makecwav"); + cmd_print_info("lz11"); } void cmd_missing_args(const char* command) { diff --git a/source/main.cpp b/source/main.cpp index 3bce946..a55a5fc 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -63,17 +63,77 @@ int make_banner(const char* image, const char* audio, const char* output) { FILE* fd = fopen(output, "wb"); if(!fd) { - printf("ERROR: Could not open output file.\n"); + printf("ERROR: Could not open output file: %s\n", strerror(errno)); return 3; } fwrite(bnr, 1, bnrSize, fd); fclose(fd); + free(bnr); + printf("Created banner \"%s\".\n", output); return 0; } +int make_cwav(char* input, char* output) { + u32 cwavSize = 0; + u8* cwav = convert_to_cwav(input, &cwavSize); + if(!cwav) { + return 1; + } + + FILE* fd = fopen(output, "wb"); + if(!fd) { + printf("ERROR: Could not open output file: %s\n", strerror(errno)); + return 2; + } + + fwrite(cwav, 1, cwavSize, fd); + fclose(fd); + + free(cwav); + + printf("Created CWAV \"%s\".\n", output); + return 0; +} + +int lz11(char* input, char* output) { + FILE* in = fopen(input, "r"); + if(!in) { + printf("ERROR: Could not open input file: %s\n", strerror(errno)); + return 1; + } + + fseek(in, 0, SEEK_END); + u32 size = (u32) ftell(in); + fseek(in, 0, SEEK_SET); + + u8 data[size]; + fread(data, 1, size, in); + fclose(in); + + u32 compressedSize; + u8* compressed = lz11_compress(data, size, &compressedSize); + if(!compressed) { + return 2; + } + + FILE* fd = fopen(output, "wb"); + if(!fd) { + printf("ERROR: Could not open output file: %s\n", strerror(errno)); + return 3; + } + + fwrite(compressed, 1, compressedSize, fd); + fclose(fd); + + free(compressed); + + printf("Compressed to file \"%s\".\n", output); + return 0; +} + int main(int argc, char* argv[]) { if(argc < 2) { cmd_print_usage(argv[0]); @@ -92,6 +152,24 @@ int main(int argc, char* argv[]) { } return make_banner(banner, audio, output); + } else if(strcmp(command, "makecwav") == 0) { + char* input = cmd_find_arg(args, "i", "input"); + char* output = cmd_find_arg(args, "o", "output"); + if(!input || !output) { + cmd_missing_args(command); + return -1; + } + + return make_cwav(input, output); + } else if(strcmp(command, "lz11") == 0) { + char* input = cmd_find_arg(args, "i", "input"); + char* output = cmd_find_arg(args, "o", "output"); + if(!input || !output) { + cmd_missing_args(command); + return -1; + } + + return lz11(input, output); } else { cmd_invalid_command(command); return -1; diff --git a/source/wav.cpp b/source/wav.cpp index 631518d..d3852fc 100644 --- a/source/wav.cpp +++ b/source/wav.cpp @@ -3,6 +3,7 @@ #include #include #include +#include bool find_chunk(FILE* fd, const char* magic) { char curr[5] = {0}; @@ -20,7 +21,7 @@ bool find_chunk(FILE* fd, const char* magic) { WAV* read_wav(const char* file) { FILE* fd = fopen(file, "r"); if(!fd) { - printf("ERROR: Could not open WAV file.\n"); + printf("ERROR: Could not open WAV file: %s\n", strerror(errno)); return NULL; }