Improve file related error reporting, allow for creating CWAV and LZ11 compressed files.

This commit is contained in:
Steven Smith 2015-01-24 10:53:59 -08:00
parent 89619c1ab8
commit 8b9b2f070e
3 changed files with 91 additions and 2 deletions

View file

@ -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) {

View file

@ -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;

View file

@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
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;
}