1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-07-02 14:04:22 +00:00

Add guess_content_type() function.

WiP.
This commit is contained in:
King_DuckZ 2016-02-19 09:46:55 +01:00
parent 8a97afd6bf
commit 64b5affa4f
6 changed files with 279 additions and 5 deletions

View file

@ -0,0 +1,39 @@
/* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef id17F1582F16C8478E8D9795BECBF275A3
#define id17F1582F16C8478E8D9795BECBF275A3
#include "dindexer-common/mediatypes.hpp"
#include "dindexer-machinery/recorddata.hpp"
#include <vector>
namespace mchlib {
enum ContentTypes {
ContentType_Generic,
ContentType_Backup,
ContentType_VideoDVD,
ContentType_VideoBD,
ContentType_Unknown
};
template <bool> class SetListingView;
ContentTypes guess_content_type ( dinlib::MediaTypes parMediaType, const SetListingView<true>& parContent );
} //namespace mchlib
#endif

View file

@ -45,9 +45,7 @@ namespace mchlib {
friend class mchlib::SetListingView<Const>; friend class mchlib::SetListingView<Const>;
friend class boost::iterator_core_access; friend class boost::iterator_core_access;
template <bool> friend class DirIterator; template <bool> friend class DirIterator;
typedef boost::iterator_facade<DirIterator<Const>, FileRecordData, boost::random_access_traversal_tag> base_class; typedef boost::iterator_facade<DirIterator<Const>, FileRecordData, boost::forward_traversal_tag> base_class;
typedef typename base_class::difference_type difference_type;
typedef typename base_class::reference reference;
struct enabler {}; struct enabler {};
public: public:
typedef typename std::conditional< typedef typename std::conditional<
@ -55,8 +53,14 @@ namespace mchlib {
std::vector<mchlib::FileRecordData>::const_iterator, std::vector<mchlib::FileRecordData>::const_iterator,
std::vector<mchlib::FileRecordData>::iterator std::vector<mchlib::FileRecordData>::iterator
>::type VecIterator; >::type VecIterator;
typedef typename base_class::difference_type difference_type;
typedef typename base_class::value_type value_type;
typedef typename base_class::pointer pointer;
typedef typename base_class::reference reference;
typedef typename base_class::iterator_category iterator_category;
DirIterator ( DirIterator<Const>&& parOther ); DirIterator ( DirIterator&& parOther );
DirIterator ( const DirIterator& parOther );
template <bool OtherConst> template <bool OtherConst>
DirIterator ( DirIterator<OtherConst>&& parOther, typename std::enable_if<std::is_convertible<typename DirIterator<OtherConst>::VecIterator, VecIterator>::value, enabler>::type = enabler() ); DirIterator ( DirIterator<OtherConst>&& parOther, typename std::enable_if<std::is_convertible<typename DirIterator<OtherConst>::VecIterator, VecIterator>::value, enabler>::type = enabler() );
DirIterator ( VecIterator parBegin, VecIterator parEnd, const PathName* parBasePath, std::size_t parLevelOffset ); DirIterator ( VecIterator parBegin, VecIterator parEnd, const PathName* parBasePath, std::size_t parLevelOffset );
@ -131,6 +135,11 @@ namespace mchlib {
SetListingView<true> make_view ( void ) const; SetListingView<true> make_view ( void ) const;
SetListingView<true> make_cview ( void ) const; SetListingView<true> make_cview ( void ) const;
bool empty ( void ) const;
std::size_t size ( void ) const;
std::size_t files_count ( void ) const;
std::size_t dir_count ( void ) const;
private: private:
ListType m_list; ListType m_list;
std::shared_ptr<PathName> m_base_path; std::shared_ptr<PathName> m_base_path;

View file

@ -0,0 +1,77 @@
/* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef id036D985665EC437096DF7EBC5607DB0A
#define id036D985665EC437096DF7EBC5607DB0A
#include "set_listing.hpp"
#include <cstddef>
#include <algorithm>
namespace mchlib {
template <bool Const>
std::size_t count_listing_dirs ( const SetListingView<Const>& parList );
template <bool Const>
std::size_t count_listing_files ( const SetListingView<Const>& parList );
template <bool Const>
std::size_t count_listing_items ( const SetListingView<Const>& parList );
template <bool Const>
std::size_t count_listing_items_recursive ( const SetListingView<Const>& parList );
template <bool Const>
inline
std::size_t count_listing_dirs (const SetListingView<Const>& parList) {
return std::count_if(
parList.cbegin(),
parList.cend(),
[] (const FileRecordData& parItm) {
return parItm.is_directory;
}
);
}
template <bool Const>
inline
std::size_t count_listing_files (const SetListingView<Const>& parList) {
return std::count_if(
parList.cbegin(),
parList.cend(),
[] (const FileRecordData& parItm) {
return not parItm.is_directory;
}
);
}
template <bool Const>
inline
std::size_t count_listing_items (const SetListingView<Const>& /*parList*/) {
assert(false);
return 0;
//return std::count_if(
// parList.cbegin(),
// parList.cend(),
// [] (const FileRecordData&) {
// return true;
// }
//);
}
} //namespace mchlib
#endif

View file

@ -13,6 +13,7 @@ add_library(${PROJECT_NAME} SHARED
discinfo.cpp discinfo.cpp
mediatype.cpp mediatype.cpp
machinery_info.cpp machinery_info.cpp
guess_content_type.cpp
set_listing.cpp set_listing.cpp
) )

View file

@ -0,0 +1,111 @@
/* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef idF5514EBEE7DA404CA2271A4AE74A28D4
#define idF5514EBEE7DA404CA2271A4AE74A28D4
#include "guess_content_type.hpp"
#include "dindexer-machinery/set_listing.hpp"
#include "dindexer-machinery/set_listing_helpers.hpp"
#include <boost/iterator/filter_iterator.hpp>
#include <cstdint>
#include <algorithm>
#include <functional>
#include <ciso646>
#include <regex>
namespace mchlib {
namespace {
template <typename O, uint16_t L>
struct IsLevelLikeO {
bool operator() ( const FileRecordData& parEntry );
};
struct EntryChecking {
typedef bool(*CheckerFunction)(dinlib::MediaTypes, const ConstSetListingView&);
std::size_t max_total_entries;
CheckerFunction checker_func;
ContentTypes content_type;
};
template <typename O, uint16_t L>
bool IsLevelLikeO<O, L>::operator() (const FileRecordData& parEntry) {
return O()(parEntry.level, L);
}
template <typename O, uint16_t L>
std::vector<const FileRecordData*> fetch_entries (const std::vector<FileRecordData>& parContent) {
using IsLevOk = IsLevelLikeO<O, L>;
auto it_begin = boost::make_filter_iterator<IsLevOk>(parContent.begin(), parContent.end());
auto it_end = boost::make_filter_iterator<IsLevOk>(parContent.end(), parContent.end());
std::vector<const FileRecordData*> retval;
for (; it_begin != it_end; ++it_begin) {
retval.push_back(&*it_begin);
}
return std::move(retval);
}
bool is_path_eq (const char* parPath, const FileRecordData& parEntry) {
return (parEntry.path == parPath);
}
bool identify_video_dvd (dinlib::MediaTypes parMediaType, const ConstSetListingView& parContent) {
if (parMediaType != dinlib::MediaType_DVD) {
return false;
}
const auto items_count = count_listing_items(parContent);
if (items_count < 2) {
return false;
}
auto it_video_ts = std::find_if(parContent.begin(), parContent.end(), std::bind(&is_path_eq, "VIDEO_TS", std::placeholders::_1));
if (parContent.end() == it_video_ts) {
return false;
}
auto it_audio_ts = std::find_if(parContent.begin(), parContent.end(), std::bind(&is_path_eq, "AUDIO_TS", std::placeholders::_1));
if (parContent.end() == it_audio_ts) {
return false;
}
return true;
}
} //unnamed namespace
ContentTypes guess_content_type (dinlib::MediaTypes parMediaType, const ConstSetListingView& parContent) {
std::vector<EntryChecking> checker_chain {
{ 100, &identify_video_dvd, ContentType_VideoDVD }
};
assert(false);
const auto total_entries = count_listing_items_recursive(parContent);
for (const auto& chk : checker_chain) {
if (chk.max_total_entries and chk.max_total_entries >= total_entries) {
if (chk.checker_func(parMediaType, parContent)) {
return chk.content_type;
}
}
}
return ContentType_Generic;
}
} //namespace mchlib
#endif

View file

@ -45,7 +45,7 @@ namespace mchlib {
namespace implem { namespace implem {
template <bool Const> template <bool Const>
DirIterator<Const>::DirIterator (DirIterator<Const>&& parOther) : DirIterator<Const>::DirIterator (DirIterator&& parOther) :
m_current(std::move(parOther.m_current)), m_current(std::move(parOther.m_current)),
m_end(std::move(parOther.m_end)), m_end(std::move(parOther.m_end)),
m_base_path(std::move(parOther.m_base_path)), m_base_path(std::move(parOther.m_base_path)),
@ -53,6 +53,15 @@ namespace mchlib {
{ {
} }
template <bool Const>
DirIterator<Const>::DirIterator (const DirIterator& parOther) :
m_current(parOther.m_current),
m_end(parOther.m_end),
m_base_path(parOther.m_base_path),
m_level_offset(parOther.m_level_offset)
{
}
template <bool Const> template <bool Const>
template <bool OtherConst> template <bool OtherConst>
DirIterator<Const>::DirIterator (DirIterator<OtherConst>&& parOther, typename std::enable_if<std::is_convertible<typename DirIterator<OtherConst>::VecIterator, VecIterator>::value, enabler>::type) : DirIterator<Const>::DirIterator (DirIterator<OtherConst>&& parOther, typename std::enable_if<std::is_convertible<typename DirIterator<OtherConst>::VecIterator, VecIterator>::value, enabler>::type) :
@ -177,6 +186,34 @@ namespace mchlib {
return const_iterator(m_list.end(), m_list.end(), m_base_path.get(), 0); return const_iterator(m_list.end(), m_list.end(), m_base_path.get(), 0);
} }
bool SetListing::empty() const {
return m_list.empty();
}
std::size_t SetListing::size() const {
return m_list.size();
}
std::size_t SetListing::files_count() const {
return std::count_if(
m_list.begin(),
m_list.end(),
[] (const FileRecordData& parItm) {
return not parItm.is_directory;
}
);
}
std::size_t SetListing::dir_count() const {
return std::count_if(
m_list.begin(),
m_list.end(),
[] (const FileRecordData& parItm) {
return parItm.is_directory;
}
);
}
SetListingView<false> SetListing::make_view() { SetListingView<false> SetListing::make_view() {
const auto offs = (m_list.empty() ? 0 : PathName(m_list.front().abs_path).atom_count()); const auto offs = (m_list.empty() ? 0 : PathName(m_list.front().abs_path).atom_count());
return SetListingView<false>(m_list.begin(), m_list.end(), offs, m_base_path); return SetListingView<false>(m_list.begin(), m_list.end(), offs, m_base_path);