diff --git a/src/scan/mimetype.cpp b/src/scan/mimetype.cpp index 7bfa415..8631c41 100644 --- a/src/scan/mimetype.cpp +++ b/src/scan/mimetype.cpp @@ -4,6 +4,17 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include namespace din { using MagicCookie = std::unique_ptr; @@ -61,4 +72,41 @@ namespace din { return nullptr; } } + + SplitMime split_mime (const std::string& parFull) { + using boost::spirit::ascii::space; + using boost::spirit::qi::char_; + using boost::spirit::qi::lit; + using boost::spirit::qi::raw; + using boost::spirit::qi::_val; + using boost::string_ref; + using boost::phoenix::construct; + using boost::spirit::_1; + using boost::phoenix::begin; + using boost::phoenix::size; + namespace px = boost::phoenix; + using RuleType = boost::spirit::qi::rule; + + SplitMime retval; + + auto full_it = parFull.begin(); + const auto beg = parFull.begin(); + RuleType mime_token = raw[+(char_ - ';')][_val = px::bind(&string_ref::substr, construct(px::ref(parFull)), begin(_1) - px::ref(beg), size(_1))]; + RuleType charset_token = raw[+(char_)][_val = px::bind(&string_ref::substr, construct(px::ref(parFull)), begin(_1) - px::ref(beg), size(_1))]; + + const bool parse_ret = boost::spirit::qi::phrase_parse( + full_it, + parFull.end(), + mime_token >> ';' >> lit("charset=") >> charset_token, + space, + retval + ); + + if (not parse_ret or parFull.end() != full_it) { + return SplitMime(); + } + else { + return retval; + } + } } //namespace din diff --git a/src/scan/mimetype.hpp b/src/scan/mimetype.hpp index c09b4f3..abe7939 100644 --- a/src/scan/mimetype.hpp +++ b/src/scan/mimetype.hpp @@ -20,8 +20,12 @@ #include #include +#include +#include namespace din { + using SplitMime = std::pair; + class MimeType { public: MimeType ( void ); @@ -35,6 +39,8 @@ namespace din { std::unique_ptr m_local_data; }; + + SplitMime split_mime ( const std::string& parFull ); } //namespace din #endif