From dd7090dda0370f35e0e0e2e17f3120d906aa2080 Mon Sep 17 00:00:00 2001 From: "Felix (xq) Queißner" Date: Fri, 19 Jun 2020 00:46:36 +0200 Subject: Adds iconv implementation to convert between input charset and UTF-8 for display. --- src/mimeparser.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/mimeparser.cpp (limited to 'src/mimeparser.cpp') diff --git a/src/mimeparser.cpp b/src/mimeparser.cpp new file mode 100644 index 0000000..dd254a4 --- /dev/null +++ b/src/mimeparser.cpp @@ -0,0 +1,63 @@ +#include "mimeparser.hpp" + + + +bool MimeType::is(const QString &type, const QString &sub_type) const +{ + return (this->type == type) and (this->subtype == sub_type); +} + +QString MimeType::parameter(const QString ¶m_name, QString const & default_value) const +{ + auto val = parameters.value(param_name.toLower()); + if(val.isNull()) + val = default_value; + return val; +} + +MimeType MimeParser::parse(const QString &mime_text) +{ + MimeType type; + + QString arg_list; + QString mime_part; + + if(int idx = mime_text.indexOf(' '); idx >= 0) { + arg_list = mime_text.mid(idx + 1).trimmed().toLower(); + mime_part = mime_text.mid(0, idx).trimmed().toLower(); + } else { + mime_part = mime_text.trimmed().toLower(); + } + + if(int idx = mime_part.indexOf('/'); idx >= 0) { + type.type = mime_part.mid(0, idx); + type.subtype = mime_part.mid(idx + 1); + } else { + type.type = mime_part; + type.subtype = QString { }; + } + + if(not arg_list.isEmpty()) { + for(auto const & _arg : arg_list.split(';')) + { + QString arg = _arg.trimmed(); + if(arg.isEmpty()) // skip over double spaces + continue; + + QString key; + QString value; + + if(int idx = arg.indexOf('='); idx >= 0) { + key = arg.mid(0, idx); + value = arg.mid(idx + 1); + } else { + key = arg; + value = ""; + } + + type.parameters.insert(key, value); + } + } + + return type; +} -- cgit v1.2.3