diff --git a/CMakeLists.txt b/CMakeLists.txt index 116fee9..0ea802b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,5 +3,5 @@ project(bannertool) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -set(SOURCE_FILES source/main.cpp source/3ds/cbmd.cpp source/3ds/lz11.cpp source/3ds/util.cpp source/lodepng/lodepng.cpp) +set(SOURCE_FILES source/main.cpp source/commandline.cpp source/3ds/cbmd.cpp source/3ds/lz11.cpp source/3ds/util.cpp source/lodepng/lodepng.cpp) add_executable(bannertool ${SOURCE_FILES}) \ No newline at end of file diff --git a/source/commandline.cpp b/source/commandline.cpp new file mode 100644 index 0000000..b0d58b2 --- /dev/null +++ b/source/commandline.cpp @@ -0,0 +1,61 @@ +#include "commandline.h" + +std::map cmd_get_args(int argc, char* argv[]) { + std::map args; + for(int i = 0; i < argc; i++) { + if((strncmp(argv[i], "-", 1) == 0 || strncmp(argv[i], "--", 2) == 0) && argc != i + 1) { + args.insert(std::pair(argv[i], argv[i + 1])); + i++; + } + } + + return args; +} + +char* cmd_find_arg(std::map args, const char* shortOpt, const char* longOpt) { + char sopt[strlen(shortOpt) + 2]; + sprintf(sopt, "-%s", shortOpt); + char lopt[strlen(longOpt) + 3]; + sprintf(lopt, "--%s", longOpt); + + std::map::iterator match = args.find(sopt); + if(match != args.end()) { + return (*match).second; + } + + match = args.find(lopt); + if(match != args.end()) { + return (*match).second; + } + + return NULL; +} + +void cmd_print_usage(const char* executedFrom) { + printf("Usage: %s \n", executedFrom); + cmd_print_commands(); +} + +void cmd_print_info(const char* command) { + if(strcmp(command, "makebanner") == 0) { + printf("makebanner - Creates a .bnr file.\n"); + printf(" -i/--image: PNG file to use as the banner image.\n"); + printf(" -a/--audio: Audio file to use as the banner's tune.\n"); + printf(" -o/--output: File to output the created banner to.\n"); + } +} + +void cmd_print_commands() { + printf("Available commands:\n"); + cmd_print_info("makebanner"); +} + +void cmd_missing_args(const char* command) { + printf("Missing arguments for command \"%s\".\n", command); + cmd_print_info(command); +} + +void cmd_invalid_command(const char* command) { + printf("Invalid command \"%s\".\n", command); + cmd_print_commands(); +} \ No newline at end of file diff --git a/source/commandline.h b/source/commandline.h new file mode 100644 index 0000000..64f6d8e --- /dev/null +++ b/source/commandline.h @@ -0,0 +1,22 @@ +#ifndef __COMMANDLINE_H__ +#define __COMMANDLINE_H__ + +#include + +#include + +struct compare_strings { + bool operator()(char const *a, char const *b) { + return strcmp(a, b) < 0; + } +}; + +std::map cmd_get_args(int argc, char* argv[]); +char* cmd_find_arg(std::map args, const char* shortOpt, const char* longOpt); +void cmd_print_usage(const char* executedFrom); +void cmd_print_info(const char* command); +void cmd_print_commands(); +void cmd_missing_args(const char* command); +void cmd_invalid_command(const char* command); + +#endif \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index 8bdf0fd..f8aad98 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,10 +1,13 @@ -#include "data.h" #include "3ds/3ds.h" +#include "data.h" +#include "commandline.h" #include #include #include +#include + u8* convert_to_cgfx(const char* image, u32 width, u32 height, u32* size) { u32 convertedSize = 0; u8* converted = image_to_tiles(image, width, height, &convertedSize); @@ -73,13 +76,30 @@ int make_banner(const char* image, const char* audio, const char* output) { fwrite(bnr, 1, bnrSize, fd); fclose(fd); free(bnr); + printf("Created banner \"%s\".", output); return 0; } int main(int argc, char* argv[]) { - if(argc != 4) { - printf("Usage: %s