From 97de157724b2121ae1c9a48634fccaee84748350 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Tue, 8 Dec 2015 16:03:30 +0000 Subject: [PATCH] Refactor the new media type autodetection code. --- src/scan/CMakeLists.txt | 1 + src/scan/commandline.cpp | 16 +++++----- src/scan/commandline.hpp | 12 +------ src/scan/discinfo.hpp | 1 + src/scan/main.cpp | 35 ++++----------------- src/scan/mediatype.cpp | 67 ++++++++++++++++++++++++++++++++++++++++ src/scan/mediatype.hpp | 51 ++++++++++++++++++++++++++++++ 7 files changed, 135 insertions(+), 48 deletions(-) create mode 100644 src/scan/mediatype.cpp create mode 100644 src/scan/mediatype.hpp diff --git a/src/scan/CMakeLists.txt b/src/scan/CMakeLists.txt index 904f502..d887d2b 100644 --- a/src/scan/CMakeLists.txt +++ b/src/scan/CMakeLists.txt @@ -13,6 +13,7 @@ add_executable(${PROJECT_NAME} settings.cpp commandline.cpp discinfo.cpp + mediatype.cpp ) target_include_directories(${PROJECT_NAME} diff --git a/src/scan/commandline.cpp b/src/scan/commandline.cpp index dd8d310..1e76b5f 100644 --- a/src/scan/commandline.cpp +++ b/src/scan/commandline.cpp @@ -30,14 +30,14 @@ namespace po = boost::program_options; namespace din { namespace { const char g_allowed_types[] = { - static_cast(SetSourceType_CDRom), - static_cast(SetSourceType_Directory), - static_cast(SetSourceType_DVD), - static_cast(SetSourceType_BluRay), - static_cast(SetSourceType_FloppyDisk), - static_cast(SetSourceType_HardDisk), - static_cast(SetSourceType_IomegaZip), - static_cast(SetSourceType_Other) + 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) }; const char* const g_version_string = PROGRAM_NAME " v" diff --git a/src/scan/commandline.hpp b/src/scan/commandline.hpp index 8ec8262..dc71ae0 100644 --- a/src/scan/commandline.hpp +++ b/src/scan/commandline.hpp @@ -19,19 +19,9 @@ #define id1B7A42F6E46547A6AB0F914E2A91399F #include +#include "mediatype.hpp" namespace din { - enum SetSourceTypes { - SetSourceType_CDRom = 'C', - SetSourceType_Directory = 'D', - SetSourceType_DVD = 'V', - SetSourceType_BluRay = 'B', - SetSourceType_FloppyDisk = 'F', - SetSourceType_HardDisk = 'H', - SetSourceType_IomegaZip = 'Z', - SetSourceType_Other = 'O' - }; - bool parse_commandline ( int parArgc, char* parArgv[], boost::program_options::variables_map& parVarMap ); } //namespace din diff --git a/src/scan/discinfo.hpp b/src/scan/discinfo.hpp index b3de076..3725275 100644 --- a/src/scan/discinfo.hpp +++ b/src/scan/discinfo.hpp @@ -44,6 +44,7 @@ namespace din { const std::string& mountpoint ( void ) const { return m_mountpoint; } const std::string& device ( void ) const { return m_device; } + const std::string& original_path ( void ) const { return m_initial_path; } OpticalTypes optical_type ( void ) const; bool mountpoint_found ( void ) const; DriveTypes drive_type ( void ) const; diff --git a/src/scan/main.cpp b/src/scan/main.cpp index fa84a11..6755631 100644 --- a/src/scan/main.cpp +++ b/src/scan/main.cpp @@ -31,13 +31,11 @@ # include #endif #include -#include "discinfo.hpp" #include "dindexerConfig.h" #include "filesearcher.hpp" #include "indexer.hpp" #include "settings.hpp" #include "commandline.hpp" -#include "pathname.hpp" namespace { void run_hash_calculation ( din::Indexer& parIndexer, bool parShowProgress ); @@ -78,40 +76,19 @@ int main (int parArgc, char* parArgv[]) { char set_type; if (0 == vm.count("type")) { std::cout << "Analyzing disc... "; - din::DiscInfo info((std::string(search_path))); - const din::DriveTypes drive_type = info.drive_type(); - if (din::DriveType_HardDisk == drive_type) { - if (info.mountpoint() == din::PathName(search_path).path()) - set_type = din::SetSourceType_HardDisk; - else - set_type = din::SetSourceType_Directory; + try { + set_type = din::guess_media_type(std::string(search_path)); + std::cout << "Setting type to " << set_type << '\n'; } - else if (din::DriveType_Optical == drive_type) { - switch (info.optical_type()) { - case din::OpticalType_DVD: - set_type = din::SetSourceType_DVD; - break; - case din::OpticalType_CDRom: - set_type = din::SetSourceType_CDRom; - break; - case din::OpticalType_BluRay: - set_type = din::SetSourceType_BluRay; - break; - default: - std::cerr << "Set autodetect failed because this media type is unknown, please specify the set type manually\n"; - return 1; - } - } - else { - std::cerr << "Can't autodetect set type, please specify it manually\n"; + catch (const std::runtime_error& e) { + std::cout << '\n'; + std::cerr << e.what(); return 1; } } else { set_type = vm["type"].as(); } - std::cout << "Setting type to " << set_type << '\n'; - return 0; std::cout << "constructing...\n"; diff --git a/src/scan/mediatype.cpp b/src/scan/mediatype.cpp new file mode 100644 index 0000000..02bb39a --- /dev/null +++ b/src/scan/mediatype.cpp @@ -0,0 +1,67 @@ +/* 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 "mediatype.hpp" +#include "pathname.hpp" +#include "discinfo.hpp" +#include + +namespace din { + UnknownMediaTypeException::UnknownMediaTypeException (const std::string& parWhat) : + std::runtime_error(parWhat) + { + } + UnknownMediaTypeException::UnknownMediaTypeException (const char* parWhat) : + std::runtime_error(parWhat) + { + } + + CantAutodetectException::CantAutodetectException (const std::string& parWhat) : + std::runtime_error(parWhat) + { + } + CantAutodetectException::CantAutodetectException (const char* parWhat) : + std::runtime_error(parWhat) + { + } + + 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; + else + return MediaType_Directory; + } + else if (DriveType_Optical == drive_type) { + switch (info.optical_type()) { + case OpticalType_DVD: + return MediaType_DVD; + case OpticalType_CDRom: + return MediaType_CDRom; + case OpticalType_BluRay: + return MediaType_BluRay; + default: + throw UnknownMediaTypeException("Set autodetect failed because this media type is unknown, please specify the set type manually"); + } + } + else { + throw CantAutodetectException("Can't autodetect set type, please specify it manually"); + } + } +} //namespace din diff --git a/src/scan/mediatype.hpp b/src/scan/mediatype.hpp new file mode 100644 index 0000000..7569913 --- /dev/null +++ b/src/scan/mediatype.hpp @@ -0,0 +1,51 @@ +/* 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 id66D41389BC59433CA58E325395A6197B +#define id66D41389BC59433CA58E325395A6197B + +#include +#include + +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' + }; + + MediaTypes guess_media_type ( std::string&& parPath ); + + class UnknownMediaTypeException : std::runtime_error { + public: + UnknownMediaTypeException ( const std::string& parWhat ); + UnknownMediaTypeException ( const char* parWhat ); + }; + + class CantAutodetectException : std::runtime_error { + public: + CantAutodetectException ( const std::string& parWhat ); + CantAutodetectException ( const char* parWhat ); + }; +} //namespace din + +#endif