mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2025-02-19 12:04:54 +00:00
Put some common stuff into a separate library.
This commit is contained in:
parent
a0b87e6a2d
commit
6c30621400
9 changed files with 211 additions and 68 deletions
|
@ -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}
|
||||
|
|
38
include/dindexer-common/commandline.hpp
Normal file
38
include/dindexer-common/commandline.hpp
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef idA71E357A577B4A55AB6A713BCC884CEB
|
||||
#define idA71E357A577B4A55AB6A713BCC884CEB
|
||||
|
||||
#include <boost/program_options/options_description.hpp>
|
||||
#include <boost/program_options/variables_map.hpp>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <initializer_list>
|
||||
#include <functional>
|
||||
|
||||
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<std::reference_wrapper<const boost::program_options::options_description>> parOpts );
|
||||
} //namespace dinlib
|
||||
|
||||
#endif
|
34
include/dindexer-common/mediatypes.hpp
Normal file
34
include/dindexer-common/mediatypes.hpp
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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
|
13
src/common/CMakeLists.txt
Normal file
13
src/common/CMakeLists.txt
Normal file
|
@ -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
|
||||
)
|
96
src/common/commandline.cpp
Normal file
96
src/common/commandline.cpp
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "dindexer-common/commandline.hpp"
|
||||
#include "dindexerConfig.h"
|
||||
#include "helpers/lengthof.h"
|
||||
#include <boost/program_options.hpp>
|
||||
#include <ostream>
|
||||
#include <utility>
|
||||
|
||||
#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<std::string>(), "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<std::reference_wrapper<const boost::program_options::options_description>> 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
|
||||
|
|
@ -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)
|
||||
|
|
|
@ -16,38 +16,26 @@
|
|||
*/
|
||||
|
||||
#include "commandline.hpp"
|
||||
#include "dindexerConfig.h"
|
||||
#include "helpers/lengthof.h"
|
||||
#include "dindexer-common/commandline.hpp"
|
||||
#include <boost/program_options.hpp>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
#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<char>(MediaType_CDRom),
|
||||
static_cast<char>(MediaType_Directory),
|
||||
static_cast<char>(MediaType_DVD),
|
||||
static_cast<char>(MediaType_BluRay),
|
||||
static_cast<char>(MediaType_FloppyDisk),
|
||||
static_cast<char>(MediaType_HardDisk),
|
||||
static_cast<char>(MediaType_IomegaZip),
|
||||
static_cast<char>(MediaType_Other)
|
||||
static_cast<char>(dinlib::MediaType_CDRom),
|
||||
static_cast<char>(dinlib::MediaType_Directory),
|
||||
static_cast<char>(dinlib::MediaType_DVD),
|
||||
static_cast<char>(dinlib::MediaType_BluRay),
|
||||
static_cast<char>(dinlib::MediaType_FloppyDisk),
|
||||
static_cast<char>(dinlib::MediaType_HardDisk),
|
||||
static_cast<char>(dinlib::MediaType_IomegaZip),
|
||||
static_cast<char>(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<std::string>(), "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<std::string>()->default_value("New set"), "Name to be given to the new set being scanned.")
|
||||
#if defined(WITH_MEDIA_AUTODETECT)
|
||||
("type,t", po::value<char>(), type_param_help.c_str())
|
||||
#else
|
||||
("type,t", po::value<char>()->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<std::string>(), "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...] <search-path>\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...] <search-path>", parVarMap, {std::cref(desc), std::cref(set_options)})) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
|
|
@ -20,21 +20,11 @@
|
|||
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#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:
|
||||
|
|
Loading…
Add table
Reference in a new issue