From c068f93bacba3cd15ba8cc2865e8095b6419ffed Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Tue, 8 Mar 2016 19:54:04 +0100 Subject: [PATCH] New ContentType task. Fix the problems I encountered while trying to use the new task. --- .../scantask/contenttype.hpp | 48 +++++++++++++++++++ .../dindexer-machinery/scantask/mediatype.hpp | 2 +- src/machinery/CMakeLists.txt | 1 + src/machinery/guess_content_type.cpp | 2 +- src/machinery/scantask/contenttype.cpp | 41 ++++++++++++++++ src/machinery/set_listing.cpp | 2 +- src/scan/main.cpp | 8 +++- 7 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 include/dindexer-machinery/scantask/contenttype.hpp create mode 100644 src/machinery/scantask/contenttype.cpp diff --git a/include/dindexer-machinery/scantask/contenttype.hpp b/include/dindexer-machinery/scantask/contenttype.hpp new file mode 100644 index 0000000..011deff --- /dev/null +++ b/include/dindexer-machinery/scantask/contenttype.hpp @@ -0,0 +1,48 @@ +/* Copyright 2015, 2016, 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 id148DBED10A0B45238E810683656BA7D5 +#define id148DBED10A0B45238E810683656BA7D5 + +#include "dindexer-machinery/scantask/base.hpp" +#include "dindexer-machinery/guess_content_type.hpp" +#include "dindexer-machinery/mediatypes.hpp" +#include +#include + +namespace mchlib { + struct FileRecordData; + + namespace scantask { + class ContentType : public Base { + public: + using DirTreeTaskPtr = std::shared_ptr>>; + using MediaTypeTaskPtr = std::shared_ptr>; + + ContentType ( DirTreeTaskPtr parDirTree, MediaTypeTaskPtr parMediaType ); + + private: + virtual void on_data_destroy ( mchlib::ContentTypes& parData ) override; + virtual void on_data_create ( mchlib::ContentTypes& parData ) override; + + DirTreeTaskPtr m_dir_tree; + MediaTypeTaskPtr m_media_type; + }; + } //namespace scantask +} //namespace mchlib + +#endif diff --git a/include/dindexer-machinery/scantask/mediatype.hpp b/include/dindexer-machinery/scantask/mediatype.hpp index ec13239..ee0c3cc 100644 --- a/include/dindexer-machinery/scantask/mediatype.hpp +++ b/include/dindexer-machinery/scantask/mediatype.hpp @@ -24,7 +24,7 @@ namespace mchlib { namespace scantask { - class MediaType : Base { + class MediaType : public Base { public: MediaType ( char parDefault, bool parForce, std::string parSearchPath ); virtual ~MediaType ( void ) noexcept = default; diff --git a/src/machinery/CMakeLists.txt b/src/machinery/CMakeLists.txt index 052d42a..cd6eb36 100644 --- a/src/machinery/CMakeLists.txt +++ b/src/machinery/CMakeLists.txt @@ -19,6 +19,7 @@ add_library(${PROJECT_NAME} SHARED scantask/dirtree.cpp scantask/mediatype.cpp scantask/hashing.cpp + scantask/contenttype.cpp ) #target_include_directories(${PROJECT_NAME} diff --git a/src/machinery/guess_content_type.cpp b/src/machinery/guess_content_type.cpp index 285a547..ae7453b 100644 --- a/src/machinery/guess_content_type.cpp +++ b/src/machinery/guess_content_type.cpp @@ -155,7 +155,7 @@ namespace mchlib { assert(std::equal(parContent.begin(), parContent.end(), SetListing(std::vector(parContent)).sorted_list().begin())); //TODO: assert that the first item in the list is the shortest string - std::shared_ptr pathname(new PathName(parContent.front().abs_path)); + std::shared_ptr pathname(new PathName("")); ConstSetListingView view(parContent.begin(), parContent.end(), pathname->atom_count(), pathname); assert(parContent.size() >= 1); return guess_content_type(parMediaType, view, parContent.size() - 1); diff --git a/src/machinery/scantask/contenttype.cpp b/src/machinery/scantask/contenttype.cpp new file mode 100644 index 0000000..bca91ef --- /dev/null +++ b/src/machinery/scantask/contenttype.cpp @@ -0,0 +1,41 @@ +/* Copyright 2015, 2016, 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-machinery/scantask/contenttype.hpp" +#include + +namespace mchlib { + namespace scantask { + ContentType::ContentType (DirTreeTaskPtr parDirTree, MediaTypeTaskPtr parMediaType) : + m_dir_tree(parDirTree), + m_media_type(parMediaType) + { + assert(m_dir_tree); + assert(m_media_type); + } + + void ContentType::on_data_destroy (mchlib::ContentTypes& parData) { + parData = ContentType_Unknown; + } + + void ContentType::on_data_create (mchlib::ContentTypes& parData) { + auto media_type = m_media_type->get_or_create(); + const auto& tree = m_dir_tree->get_or_create(); + parData = mchlib::guess_content_type(media_type, tree); + } + } //namespace scantask +} //namespace mchlib diff --git a/src/machinery/set_listing.cpp b/src/machinery/set_listing.cpp index 69460ce..cda981b 100644 --- a/src/machinery/set_listing.cpp +++ b/src/machinery/set_listing.cpp @@ -99,7 +99,7 @@ namespace mchlib { { assert(parBasePath); assert(m_base_path or m_current == m_end); - assert(m_current == m_end or m_base_path->atom_count() == PathName(m_current->path).atom_count()); + assert(m_current == m_end or m_base_path->atom_count() == PathName(m_current->path).atom_count() + parLevelOffset); assert(m_current == m_end or m_base_path->atom_count() == m_current->level + m_level_offset); //Look for the point where the children of this entry start diff --git a/src/scan/main.cpp b/src/scan/main.cpp index 924efd7..92e5f2a 100644 --- a/src/scan/main.cpp +++ b/src/scan/main.cpp @@ -29,6 +29,7 @@ #include "dindexer-machinery/scantask/dirtree.hpp" #include "dindexer-machinery/scantask/mediatype.hpp" #include "dindexer-machinery/scantask/hashing.hpp" +#include "dindexer-machinery/scantask/contenttype.hpp" #include #include #include @@ -78,12 +79,15 @@ int main (int parArgc, char* parArgv[]) { const std::string search_path(vm["search-path"].as()); std::shared_ptr scan_dirtree(new mchlib::scantask::DirTree(search_path)); - std::shared_ptr media_type(new mchlib::scantask::MediaType((vm.count("type") ? vm["type"].as() : 'O'), not vm.count("type"), search_path)); + std::shared_ptr media_type(new mchlib::scantask::MediaType((vm.count("type") ? vm["type"].as() : 'O'), vm.count("type"), search_path)); std::shared_ptr hashing(new mchlib::scantask::Hashing(scan_dirtree, true)); + std::shared_ptr content_type(new mchlib::scantask::ContentType(scan_dirtree, media_type)); + + std::cout << "Content type: " << mchlib::content_type_to_char(content_type->get_or_create()) << std::endl; const auto& hashes = hashing->get_or_create(); for (const auto& hash : hashes) { - std::cout << '"' << hash.path << "\" -> " << mchlib::tiger_to_string(hash.hash) << std::endl; + std::cout << '"' << hash.path << "\" -> " << mchlib::tiger_to_string(hash.hash) << "\n"; } return 0;