diff --git a/ResourceFiles/CMakeLists.txt b/ResourceFiles/CMakeLists.txt index 94e0693080..fbae84880f 100644 --- a/ResourceFiles/CMakeLists.txt +++ b/ResourceFiles/CMakeLists.txt @@ -31,3 +31,11 @@ target_link_libraries(ResourceFiles ${Boost_LIBRARIES} ${CMAKE_INSTALL_PREFIX}/l target_include_directories(ResourceFiles PUBLIC . PRIVATE ${CMAKE_INSTALL_PREFIX}/include ${Boost_INCLUDE_DIR}) + +find_package(Boost COMPONENTS program_options REQUIRED) + +add_executable(ResInfo ResInfo.cc) +target_link_libraries(ResInfo ResourceFiles ${Boost_LIBRARIES}) +target_include_directories(ResInfo PRIVATE ${Boost_INCLUDE_DIR}) + +install(TARGETS ResInfo RUNTIME DESTINATION bin) diff --git a/ResourceFiles/ResInfo.cc b/ResourceFiles/ResInfo.cc new file mode 100644 index 0000000000..ff8c207ecd --- /dev/null +++ b/ResourceFiles/ResInfo.cc @@ -0,0 +1,140 @@ +#include +#include +#include +#include "ResourceFile.h" +#include "boost/program_options.hpp" +namespace po = boost::program_options; +using std::map; +using std::string; + +static po::options_description desc; + +map formats { +#ifdef __APPLE__ + {"real", ResourceFile::Format::real}, +#endif + {"macbin", ResourceFile::Format::macbin}, + {"basilisk", ResourceFile::Format::basilisk}, + {"applesingle", ResourceFile::Format::applesingle}, + {"underscore_appledouble", ResourceFile::Format::underscore_appledouble} +}; +map reverseFormats; + +static void usage() +{ + std::cerr << "Usage: " << "ResInfo [options] input-file\n"; + std::cerr << desc << std::endl; + std::cerr << "Available Resource Fork formats are:\n"; + for(auto p : formats) + std::cerr << " " << p.first << std::endl; +} + + +int main(int argc, char *argv[]) +{ + desc.add_options() + ("help,h", "show this help message") + ("type,t", "print file type") + ("creator,c", "print creator code") + ("all,a", "print all info") + ("format,f", "print format") + ("count,n", "print number of resources") + ("filename,l", "echo input file name") + ("set-format,F", po::value(), "resource fork format)") + ; + po::options_description hidden, alldesc; + hidden.add_options() + ("input", po::value>(), "input file" ) + ; + alldesc.add(desc).add(hidden); + + po::variables_map options; + try + { + auto parsed = po::command_line_parser(argc, argv) + .options(alldesc) + .positional(po::positional_options_description().add("input", -1)) + .style(po::command_line_style::default_style) + .run(); + + po::store(parsed, options); + } + catch(po::error& e) + { + std::cerr << "ERROR: " << e.what() << std::endl << std::endl; + usage(); + return 1; + } + + po::notify(options); + + if(options.count("help") || !options.count("input")) + { + usage(); + return 0; + } + + for(auto p : formats) + reverseFormats[p.second] = p.first; + + bool showFilename = options.count("filename"); + bool showType = options.count("type") != 0; + bool showCreator = options.count("creator") != 0; + bool showFormat = options.count("format") != 0; + bool showCount = options.count("count") != 0; + + ResourceFile::Format format = ResourceFile::Format::autodetect; + + if(options.count("all")) + showType = showCreator = showFormat = showCount = true; + + if(options.count("set-format")) + { + string s = options["set-format"].as(); + if(formats.find(s) != formats.end()) + format = formats[s]; + else + { + std::cerr << "Unknown format " << s << std::endl << std::endl; + usage(); + exit(1); + } + } + + if(options.count("input")) + for(std::string fn : options["input"].as>()) + { + ResourceFile rsrcFile(fn, format); + + if(!rsrcFile.read()) + { + std::cerr << "Can't read file.\n"; + return 1; + } + + std::ostringstream out; + if(showType) + out << " " << rsrcFile.type; + if(showCreator) + out << " " << rsrcFile.creator; + if(showFormat) + out << " " << reverseFormats[rsrcFile.format]; + if(showCount) + out << " " << rsrcFile.resources.resources.size(); + + string str = out.str(); + if(str.size()) + { + if(showFilename) + std::cout << fn << ": "; + std::cout << out.str().substr(1) << std::endl; + } + else + { + if(showFilename) + std::cout << fn << std::endl; + } + } + + return 0; +}