diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b38a2c..6a151fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ set(ACTIONS_PATH "${CMAKE_CURRENT_BINARY_DIR}/src" CACHE STRING "Actions search set(PROJECT_VERSION_BETA "1") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++11 -Wall -Wextra") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11 -Wall -Wextra") +set(DINDEXER_PUB_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include") if ("${DINDEXER_CONFIG_FILE}" STREQUAL "") if (DINDEXER_DEBUG_CFG_FILE) @@ -47,6 +48,7 @@ target_include_directories(${bare_name}-inc add_subdirectory(src/scan) add_subdirectory(src/pq) add_subdirectory(src/main) +add_subdirectory(src/common) target_link_libraries(${PROJECT_NAME} INTERFACE ${PostgreSQL_LIBRARIES} diff --git a/include/dindexer-common/commandline.hpp b/include/dindexer-common/commandline.hpp new file mode 100644 index 0000000..2896cc2 --- /dev/null +++ b/include/dindexer-common/commandline.hpp @@ -0,0 +1,38 @@ +/* Copyright 2015, Michele Santullo + * This file is part of "dindexer". + * + * "dindexer" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "dindexer" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "dindexer". If not, see . + */ + +#ifndef idA71E357A577B4A55AB6A713BCC884CEB +#define idA71E357A577B4A55AB6A713BCC884CEB + +#include +#include +#include +#include +#include +#include + +namespace dinlib { + boost::program_options::options_description get_default_commandline ( void ); + void print_commandline_help ( std::ostream& parStream, const std::string& parSummary, const boost::program_options::options_description& parOpts ); + void print_commandline_version ( std::ostream& parStream ); + + bool manage_common_commandline ( std::ostream& parStream, const char* parAction, const char* parUsage, const boost::program_options::variables_map& parVarMap, const boost::program_options::options_description& parOpts ); + + bool manage_common_commandline ( std::ostream& parStream, const char* parAction, const char* parUsage, const boost::program_options::variables_map& parVarMap, std::initializer_list> parOpts ); +} //namespace dinlib + +#endif diff --git a/include/dindexer-common/mediatypes.hpp b/include/dindexer-common/mediatypes.hpp new file mode 100644 index 0000000..4ccf977 --- /dev/null +++ b/include/dindexer-common/mediatypes.hpp @@ -0,0 +1,34 @@ +/* Copyright 2015, Michele Santullo + * This file is part of "dindexer". + * + * "dindexer" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "dindexer" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "dindexer". If not, see . + */ + +#ifndef id700AFD0F33634ACC88079BB8853A9E13 +#define id700AFD0F33634ACC88079BB8853A9E13 + +namespace dinlib { + enum MediaTypes { + MediaType_CDRom = 'C', + MediaType_Directory = 'D', + MediaType_DVD = 'V', + MediaType_BluRay = 'B', + MediaType_FloppyDisk = 'F', + MediaType_HardDisk = 'H', + MediaType_IomegaZip = 'Z', + MediaType_Other = 'O' + }; +} //namespace dinlib + +#endif diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt new file mode 100644 index 0000000..9a52ea4 --- /dev/null +++ b/src/common/CMakeLists.txt @@ -0,0 +1,13 @@ +project(${bare_name}-common CXX C) + +add_library(${PROJECT_NAME} + commandline.cpp +) + +target_include_directories(${PROJECT_NAME} + PUBLIC ${DINDEXER_PUB_INCLUDE_DIR} +) + +target_link_libraries(${PROJECT_NAME} + PRIVATE ${bare_name}-if +) diff --git a/src/common/commandline.cpp b/src/common/commandline.cpp new file mode 100644 index 0000000..b60c9d4 --- /dev/null +++ b/src/common/commandline.cpp @@ -0,0 +1,96 @@ +/* Copyright 2015, Michele Santullo + * This file is part of "dindexer". + * + * "dindexer" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "dindexer" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "dindexer". If not, see . + */ + +#include "dindexer-common/commandline.hpp" +#include "dindexerConfig.h" +#include "helpers/lengthof.h" +#include +#include +#include + +#define STRINGIZE_IMPL(s) #s +#define STRINGIZE(s) STRINGIZE_IMPL(s) + +namespace po = boost::program_options; + +namespace dinlib { + namespace { + const char* const g_version_string = + PROGRAM_NAME " v" + STRINGIZE(VERSION_MAJOR) "." + STRINGIZE(VERSION_MINOR) "." + STRINGIZE(VERSION_PATCH) +#if VERSION_BETA + "b" +#endif + ; + } //unnamed namespace + + po::options_description get_default_commandline() { + po::options_description desc("General"); + desc.add_options() + ("help,h", "Produces this help message") + ("version", "Prints the program's version and quits") + //("dump-raw,D", po::value(), "Saves the retrieved html to the named file; use - for stdout") + ; + return std::move(desc); + } + + void print_commandline_help (std::ostream& parStream, const std::string& parSummary, const boost::program_options::options_description& parOpts) { +#if !defined(NDEBUG) + parStream << "*******************\n"; + parStream << "*** DEBUG BUILD ***\n"; + parStream << "*******************\n"; + parStream << '\n'; +#endif + parStream << PROGRAM_NAME << " Copyright (C) 2015 Michele Santullo\n"; + parStream << "This program comes with ABSOLUTELY NO WARRANTY.\n"; //for details type `show w'. + parStream << "This is free software, and you are welcome to\n"; + parStream << "redistribute it under certain conditions.\n"; //type `show c' for details. + parStream << '\n'; + parStream << "Usage: " << PROGRAM_NAME << ' ' << parSummary << '\n'; + parStream << parOpts; + } + + void print_commandline_version (std::ostream& parStream) { + parStream << g_version_string << '\n'; + } + + bool manage_common_commandline (std::ostream& parStream, const char* parAction, const char* parUsage, const boost::program_options::variables_map& parVarMap, const boost::program_options::options_description& parOpts) { + if (parVarMap.count("help")) { + std::ostringstream oss_summary; + oss_summary << parAction << ' ' << parUsage; + + print_commandline_help(parStream, oss_summary.str(), parOpts); + return true; + } + else if (parVarMap.count("version")) { + print_commandline_version(parStream); + return true; + } + return false; + } + + bool manage_common_commandline (std::ostream& parStream, const char* parAction, const char* parUsage, const boost::program_options::variables_map& parVarMap, std::initializer_list> parOpts) { + boost::program_options::options_description visible("Available options"); + for (auto opt : parOpts) { + visible.add(opt); + } + return manage_common_commandline(parStream, parAction, parUsage, parVarMap, visible); + } +} //namespace dinlib + diff --git a/src/scan/CMakeLists.txt b/src/scan/CMakeLists.txt index 0ee8669..72d0d4d 100644 --- a/src/scan/CMakeLists.txt +++ b/src/scan/CMakeLists.txt @@ -28,6 +28,12 @@ target_include_directories(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME} PRIVATE ${bare_name}-if + PRIVATE ${bare_name}-common +) + +string(REPLACE "${bare_name}-" "" ACTION_NAME "${PROJECT_NAME}") +target_compile_definitions(${PROJECT_NAME} + PRIVATE ACTION_NAME="${ACTION_NAME}" ) if (DINDEXER_WITH_MEDIA_AUTODETECT) diff --git a/src/scan/commandline.cpp b/src/scan/commandline.cpp index a89f733..ea0e4fd 100644 --- a/src/scan/commandline.cpp +++ b/src/scan/commandline.cpp @@ -16,38 +16,26 @@ */ #include "commandline.hpp" -#include "dindexerConfig.h" #include "helpers/lengthof.h" +#include "dindexer-common/commandline.hpp" #include #include #include -#define STRINGIZE_IMPL(s) #s -#define STRINGIZE(s) STRINGIZE_IMPL(s) - namespace po = boost::program_options; namespace din { namespace { const char g_allowed_types[] = { - static_cast(MediaType_CDRom), - static_cast(MediaType_Directory), - static_cast(MediaType_DVD), - static_cast(MediaType_BluRay), - static_cast(MediaType_FloppyDisk), - static_cast(MediaType_HardDisk), - static_cast(MediaType_IomegaZip), - static_cast(MediaType_Other) + static_cast(dinlib::MediaType_CDRom), + static_cast(dinlib::MediaType_Directory), + static_cast(dinlib::MediaType_DVD), + static_cast(dinlib::MediaType_BluRay), + static_cast(dinlib::MediaType_FloppyDisk), + static_cast(dinlib::MediaType_HardDisk), + static_cast(dinlib::MediaType_IomegaZip), + static_cast(dinlib::MediaType_Other) }; - const char* const g_version_string = - PROGRAM_NAME " v" - STRINGIZE(VERSION_MAJOR) "." - STRINGIZE(VERSION_MINOR) "." - STRINGIZE(VERSION_PATCH) -#if VERSION_BETA - "b" -#endif - ; } //unnamed namespace bool parse_commandline (int parArgc, char* parArgv[], po::variables_map& parVarMap) { @@ -66,30 +54,25 @@ namespace din { type_param_help = oss.str(); } - po::options_description desc("General"); - desc.add_options() - ("help,h", "Produces this help message") - ("version", "Prints the program's version and quits") - //("dump-raw,D", po::value(), "Saves the retrieved html to the named file; use - for stdout") + po::options_description set_options("Set options"); + set_options.add_options() + ("ignore-errors", "Move on even if reading a file fails. Unreadable files are marked as such in the db.") #if defined(WITH_PROGRESS_FEEDBACK) ("quiet,q", "Hide progress messages and print nothing at all") #endif - ; - po::options_description set_options("Set options"); - set_options.add_options() ("setname,n", po::value()->default_value("New set"), "Name to be given to the new set being scanned.") #if defined(WITH_MEDIA_AUTODETECT) ("type,t", po::value(), type_param_help.c_str()) #else ("type,t", po::value()->default_value('V'), type_param_help.c_str()) #endif - ("ignore-errors", "Move on even if reading a file fails. Unreadable files are marked as such in the db.") ; po::options_description positional_options("Positional options"); positional_options.add_options() ("search-path", po::value(), "Search path") ; po::options_description all("Available options"); + const auto desc = dinlib::get_default_commandline(); all.add(desc).add(positional_options).add(set_options); po::positional_options_description pd; pd.add("search-path", 1);//.add("xpath", 1); @@ -102,26 +85,7 @@ namespace din { po::notify(parVarMap); - if (parVarMap.count("help")) { -#if !defined(NDEBUG) - std::cout << "*******************\n"; - std::cout << "*** DEBUG BUILD ***\n"; - std::cout << "*******************\n"; - std::cout << '\n'; -#endif - po::options_description visible("Available options"); - visible.add(desc).add(set_options); - std::cout << PROGRAM_NAME << " Copyright (C) 2015 Michele Santullo\n"; - std::cout << "This program comes with ABSOLUTELY NO WARRANTY.\n"; //for details type `show w'. - std::cout << "This is free software, and you are welcome to\n"; - std::cout << "redistribute it under certain conditions.\n"; //type `show c' for details. - std::cout << '\n'; - std::cout << "Usage: " << PROGRAM_NAME << " [options...] \n"; - std::cout << visible; - return true; - } - else if (parVarMap.count("version")) { - std::cout << g_version_string << '\n'; + if (dinlib::manage_common_commandline(std::cout, ACTION_NAME, "[options...] ", parVarMap, {std::cref(desc), std::cref(set_options)})) { return true; } diff --git a/src/scan/mediatype.cpp b/src/scan/mediatype.cpp index d4a4c53..99a0fd7 100644 --- a/src/scan/mediatype.cpp +++ b/src/scan/mediatype.cpp @@ -40,23 +40,23 @@ namespace din { { } - MediaTypes guess_media_type (std::string&& parPath) { + dinlib::MediaTypes guess_media_type (std::string&& parPath) { DiscInfo info(std::move(parPath)); const DriveTypes drive_type = info.drive_type(); if (DriveType_HardDisk == drive_type) { if (info.mountpoint() == PathName(info.original_path()).path()) - return MediaType_HardDisk; + return dinlib::MediaType_HardDisk; else - return MediaType_Directory; + return dinlib::MediaType_Directory; } else if (DriveType_Optical == drive_type) { switch (info.optical_type()) { case OpticalType_DVD: - return MediaType_DVD; + return dinlib::MediaType_DVD; case OpticalType_CDRom: - return MediaType_CDRom; + return dinlib::MediaType_CDRom; case OpticalType_BluRay: - return MediaType_BluRay; + return dinlib::MediaType_BluRay; default: throw UnknownMediaTypeException("Set autodetect failed because this media type is unknown, please specify the set type manually"); } diff --git a/src/scan/mediatype.hpp b/src/scan/mediatype.hpp index 91e3af1..b7dba5b 100644 --- a/src/scan/mediatype.hpp +++ b/src/scan/mediatype.hpp @@ -20,21 +20,11 @@ #include #include +#include "dindexer-common/mediatypes.hpp" namespace din { - enum MediaTypes { - MediaType_CDRom = 'C', - MediaType_Directory = 'D', - MediaType_DVD = 'V', - MediaType_BluRay = 'B', - MediaType_FloppyDisk = 'F', - MediaType_HardDisk = 'H', - MediaType_IomegaZip = 'Z', - MediaType_Other = 'O' - }; - #if defined(WITH_MEDIA_AUTODETECT) - MediaTypes guess_media_type ( std::string&& parPath ); + dinlib::MediaTypes guess_media_type ( std::string&& parPath ); class UnknownMediaTypeException : std::runtime_error { public: