diff options
Diffstat (limited to 'plugingui/rcgen.cc')
-rw-r--r-- | plugingui/rcgen.cc | 143 |
1 files changed, 122 insertions, 21 deletions
diff --git a/plugingui/rcgen.cc b/plugingui/rcgen.cc index f4d7fe6..f7b5590 100644 --- a/plugingui/rcgen.cc +++ b/plugingui/rcgen.cc @@ -27,35 +27,138 @@ #include <stdio.h> #include <string> #include <unistd.h> +#include <sstream> + +#include <getoptpp.hpp> + +std::string usage(const std::string& name, bool brief = false) +{ + std::ostringstream output; + output << + "Usage: " << name << " [options]\n"; + if(!brief) + { + output << + "\n" + "Create resource file from list of input files.\n" + "\n"; + } + return output.str(); +} int main(int argc, char *argv[]) { - printf("/* This file is autogenerated by rcgen. Do not modify! */\n"); - printf("#include \"resource_data.h\"\n"); - printf("\n"); - printf("const rc_data_t rc_data[] =\n"); - printf("{\n"); + bool verbose{false}; + std::vector<std::string> stripPrefixes; + std::string dirRoot; + std::string outfile; + + dg::Options opt; + + opt.add("strip-path", required_argument, 's', + "Strip supplied path prefix from resource names.", + [&]() + { + stripPrefixes.push_back(optarg); + return 0; + }); + + opt.add("dir-root", required_argument, 'd', + "Change to supplied root dir before reading files.", + [&]() + { + dirRoot = optarg; + return 0; + }); + + opt.add("output", required_argument, 'o', + "Write output to specificed file, defaults to stdout.", + [&]() + { + outfile = optarg; + return 0; + }); + + opt.add("verbose", no_argument, 'v', + "Print verbose output during processing.", + [&]() + { + verbose = true; + return 0; + }); + + opt.add("help", no_argument, 'h', + "Print this message and exit.", + [&]() + { + std::cout << usage(argv[0]); + std::cout << "Options:\n"; + opt.help(); + exit(0); + return 0; + }); + + if(opt.process(argc, argv) != 0) + { + return 1; + } + + FILE* out = stdout; + if(!outfile.empty()) + { + out = fopen(outfile.data(), "wb"); + if(!out) + { + fprintf(stderr, "Could not write to file '%s' - quitting\n", + outfile.data()); + return 1; + + } + } - int i = 1; + fprintf(out, "/* This file is autogenerated by rcgen. Do not modify! */\n"); + fprintf(out, "#include \"resource_data.h\"\n"); + fprintf(out, "\n"); + fprintf(out, "const rc_data_t rc_data[] =\n"); + fprintf(out, "{\n"); - if(argc > 2 && std::string(argv[1]) == "-d") + if(!dirRoot.empty()) { - if(chdir(argv[2])) + if(verbose) + { + fprintf(stderr, "Change to dir: %s\n", dirRoot.data()); + } + + if(chdir(dirRoot.data())) { return 1; } - i += 2; } - while(i < argc) + for(const auto& arg : opt.arguments()) { - printf(" {\n \":%s\", ", argv[i]); + std::string resourceName = arg; + for(const auto& stripPrefix : stripPrefixes) + { + if(stripPrefix == resourceName.substr(0, stripPrefix.length())) + { + resourceName = resourceName.substr(stripPrefix.length()); + break; + } + } + + fprintf(out, " {\n \":%s\", ", resourceName.data()); + + if(verbose) + { + fprintf(stderr, "Process: %s\n", arg.data()); + } std::string data; - FILE *fp = fopen(argv[i], "rb"); + FILE *fp = fopen(arg.data(), "rb"); if(!fp) { - fprintf(stderr, "Could not read file '%s' - quitting\n", argv[i]); + fprintf(stderr, "Could not read file '%s' - quitting\n", arg.data()); return 1; } @@ -67,23 +170,21 @@ int main(int argc, char *argv[]) } fclose(fp); - printf("%d,\n \"", (int)data.length()); + fprintf(out, "%d,\n \"", (int)data.length()); for(std::size_t j = 0; j < data.length(); ++j) { if((j != 0) && (j % 16) == 0) { - printf("\"\n \""); + fprintf(out, "\"\n \""); } - printf("\\%o", (unsigned char)data[j]); + fprintf(out, "\\%o", (unsigned char)data[j]); } - printf("\"\n },\n"); - - ++i; + fprintf(out, "\"\n },\n"); } - printf(" { \"\", 0, 0 }\n"); - printf("};\n"); + fprintf(out, " { \"\", 0, 0 }\n"); + fprintf(out, "};\n"); return 0; } |