From 2d0b10ef248a119dc7ad476973ccdedb9c023530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Wed, 29 Jul 2015 09:09:44 +0200 Subject: [PATCH] Remove dependency on gflags library. --- CMakeLists.txt | 5 ++-- README.md | 1 - src/CMakeLists.txt | 9 +++--- src/fsb/extractor.cpp | 68 ++++++++++++++++++++++++++++++------------- 4 files changed, 54 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 29736cf..1cd4872 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ find_package(Boost COMPONENTS system filesystem REQUIRED) find_package(PkgConfig REQUIRED) pkg_check_modules(Ogg REQUIRED ogg) pkg_check_modules(Vorbis REQUIRED vorbis vorbisenc) +pkg_check_modules(GLog REQUIRED libglog) set (GTEST_ROOT "/usr/src/gtest/") if(EXISTS ${GTEST_ROOT}) @@ -21,8 +22,8 @@ if (FVE_BUILD_TESTS) add_subdirectory(${GTEST_ROOT} gtest) endif () -include_directories(./ ${Ogg_INCLUDE_DIRS} ${Vorbis_INCLUDE_DIRS}) -link_directories(./ ${Ogg_LIBRARY_DIRS} ${Vorbis_LIBRARY_DIRS}) +include_directories(./ ${Ogg_INCLUDE_DIRS} ${Vorbis_INCLUDE_DIRS} ${GLog_INCLUDE_DIRS}) +link_directories(./ ${Ogg_LIBRARY_DIRS} ${Vorbis_LIBRARY_DIRS} ${GLog_LIBRARY_DIRS}) add_subdirectory(src) diff --git a/README.md b/README.md index 408e89e..c1f33eb 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,6 @@ sudo aptitude install \ libboost-filesystem-dev \ libboost-iostreams-dev \ libboost-system-dev \ - libgflags-dev \ libgoogle-glog-dev \ libgtest-dev \ libogg-dev \ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a85633e..b302a21 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -17,7 +17,7 @@ add_library(fsb STATIC fsb/container.hpp fsb/fsb.hpp) target_link_libraries(fsb - glog + ${GLog_LIBRARIES} ${Ogg_LIBRARIES} ${Vorbis_LIBRARIES}) @@ -25,8 +25,7 @@ add_executable(extractor fsb/extractor.cpp) target_link_libraries(extractor fsb - glog - gflags + ${GLog_LIBRARIES} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY}) @@ -34,7 +33,7 @@ add_executable(headers_generator_tool fsb/vorbis/headers_generator_tool.cpp) target_link_libraries(headers_generator_tool fsb - glog + ${GLog_LIBRARIES} ${Vorbis_LIBRARIES}) if(FVE_BUILD_TESTS) @@ -47,6 +46,6 @@ if(FVE_BUILD_TESTS) add_test(fsb_test fsb_test) target_link_libraries(fsb_test fsb - glog + ${GLog_LIBRARIES} gtest gtest_main) endif() diff --git a/src/fsb/extractor.cpp b/src/fsb/extractor.cpp index 0639133..37b896b 100644 --- a/src/fsb/extractor.cpp +++ b/src/fsb/extractor.cpp @@ -13,20 +13,13 @@ #include "fsb/container.hpp" #include -#include #include +#include +#include #include #include -DEFINE_string(password, "", - "password used to encode file"); -DEFINE_string(destination, "", - "directory where extracted files will be placed, " - "current working directory is used by default"); -DEFINE_bool(list, false, - "only list content of container without extracting content"); - namespace { struct extractor_options { @@ -36,17 +29,50 @@ struct extractor_options { std::vector paths; }; +void usage(const char *name) { + std::cout << + "Usage: " << name << " [OPTION]... [FSB_FILE]...\n" + "Extracts or lists content of Vorbis files from FSB5 container.\n" + "\n" + "Options:\n" + " -h --help display this help and exit\n" + " -p --password password used to encode FSB files\n" + " -d --destination directory where extracted files will be placed,\n" + " current working directory is used by default\n" + " -l --list only list content of container without extracting\n"; +} + extractor_options parse_options(int argc, char **argv) { extractor_options options; - options.password = FLAGS_password; - options.destination = FLAGS_destination.empty() ? - boost::filesystem::current_path() : FLAGS_destination; - options.extract = !FLAGS_list; - - for (int i=1; i < argc; ++i) { - options.paths.push_back(argv[i]); + options.destination = boost::filesystem::current_path(); + options.extract = true; + + for (int argi=1; argi < argc; ++argi) { + const char *arg = argv[argi]; + if (std::strcmp("--help", arg) == 0 || std::strcmp("-h", arg) == 0) { + usage(argv[0]); + exit(EXIT_SUCCESS); + } else if (std::strcmp("--password", arg) == 0 || std::strcmp("-p", arg) == 0) { + CHECK(argi + 1 < argc) << "An argument is required for " << arg << '.'; + options.password = argv[++argi]; + } else if (std::strcmp("--destination", arg) == 0 || std::strcmp("-d", arg) == 0) { + CHECK(argi + 1 < argc) << "An argument is required for " << arg << '.'; + options.destination = argv[++argi]; + } else if (std::strcmp("--list", arg) == 0 || std::strcmp("-l", arg) == 0) { + options.extract = false; + } else if (std::strcmp("--", arg) == 0) { + while (++argi < argc) + options.paths.push_back(arg); + break; + } else if (arg[0] == '-') { + std::cerr << "Unrecognized flag: " << arg << std::endl; + usage(argv[0]); + exit(EXIT_FAILURE); + } else { + options.paths.push_back(arg); + } } - + return options; } @@ -78,12 +104,10 @@ void print_sample(std::ostream & os, const fsb::sample & sample) { int main(int argc, char **argv) { google::InitGoogleLogging(argv[0]); - google::SetUsageMessage(std::string(argv[0]) + " file.fsb"); - google::ParseCommandLineFlags(&argc, &argv, true); - std::size_t sample_number = 0; const extractor_options options = parse_options(argc, argv); + std::size_t sample_number = 0; for (const auto & path : options.paths) { std::ifstream stream(path.native(), std::ios_base::in | std::ios_base::binary); @@ -110,11 +134,13 @@ int main(int argc, char **argv) { << sample.name << std::endl; continue; } + std::ofstream output(path.native()); + CHECK(output) << "Failed to open output file: " << path; container.extract_sample(sample, output); } } } return 0; -} \ No newline at end of file +}