Move MIME-type detection from class CFileUtils to right class CMime
authorKarlson2k <k2k@narod.ru>
Wed, 8 Jan 2014 01:55:48 +0000 (05:55 +0400)
committerKarlson2k <k2k@narod.ru>
Wed, 8 Jan 2014 14:39:22 +0000 (18:39 +0400)
xbmc/utils/FileUtils.cpp
xbmc/utils/FileUtils.h
xbmc/utils/Mime.cpp
xbmc/utils/Mime.h
xbmc/utils/ScraperUrl.cpp

index a79c30d..e6728b3 100644 (file)
@@ -160,114 +160,3 @@ unsigned int CFileUtils::LoadFile(const std::string &filename, void* &outputBuff
 
   return total_read;
 }
-
-CFileUtils::EFileType CFileUtils::GetFileTypeFromMime(const std::string& mimeType)
-{
-  // based on http://mimesniff.spec.whatwg.org/
-
-  std::string type, subtype;
-  if (!parseMimeType(mimeType, type, subtype))
-    return FileTypeUnknown;
-
-  if (type == "application")
-  {
-    if (subtype == "zip")
-      return FileTypeZip;
-    if (subtype == "x-gzip")
-      return FileTypeGZip;
-    if (subtype == "x-rar-compressed")
-      return FileTypeRar;
-    
-    if (subtype == "xml")
-      return FileTypeXml;
-  }
-  else if (type == "text")
-  {
-    if (subtype == "xml")
-      return FileTypeXml;
-    if (subtype == "html")
-      return FileTypeHtml;
-    if (subtype == "plain")
-      return FileTypePlainText;
-  }
-  else if (type == "image")
-  {
-    if (subtype == "bmp")
-      return FileTypeBmp;
-    if (subtype == "gif")
-      return FileTypeGif;
-    if (subtype == "png")
-      return FileTypePng;
-    if (subtype == "jpeg" || subtype == "pjpeg")
-      return FileTypeJpeg;
-  }
-
-  if (StringUtils::EndsWith(subtype, "+zip"))
-    return FileTypeZip;
-  if (StringUtils::EndsWith(subtype, "+xml"))
-    return FileTypeXml;
-
-  return FileTypeUnknown;
-}
-
-bool CFileUtils::parseMimeType(const std::string& mimeType, std::string& type, std::string& subtype)
-{
-  // this is an modified implementation of http://mimesniff.spec.whatwg.org/#parsing-a-mime-type with additional checks for non-empty type and subtype
-  // note: only type and subtype are parsed, parameters are ignored
-
-  static const char* const whitespaceChars = "\x09\x0A\x0C\x0D\x20"; // tab, LF, FF, CR and space
-  static const std::string whitespaceSmclnChars("\x09\x0A\x0C\x0D\x20\x3B"); // tab, LF, FF, CR, space and semicolon
-
-  type.clear();
-  subtype.clear();
-
-  const size_t len = mimeType.length();
-  if (len < 1)
-    return false;
-
-  const char* const mimeTypeC = mimeType.c_str();
-  size_t pos = mimeType.find_first_not_of(whitespaceChars);
-  if (pos == std::string::npos)
-    return false;
-
-  // find "type"
-  size_t t = 0;
-  do
-  {
-    const char chr = mimeTypeC[pos];
-    if (t > 127 || !chr)
-    {
-      type.clear();
-      return false;
-    }
-
-    if (chr >= 'A' && chr <= 'Z')
-      type.push_back(chr + ('a' - 'A')); // convert to lowercase
-    else
-      type.push_back(chr);
-    t++;
-    pos++;
-  } while (mimeTypeC[pos] != '/');
-
-  pos++; // skip '/'
-  t = 0;
-
-  while (mimeTypeC[pos] && whitespaceSmclnChars.find(mimeTypeC[pos]) == std::string::npos && t++ <= 127)
-  {
-    const char chr = mimeTypeC[pos];
-    if (chr >= 'A' && chr <= 'Z')
-      subtype.push_back(chr + ('a' - 'A')); // convert to lowercase
-    else
-      subtype.push_back(chr);
-    pos++;
-  }
-
-  if (subtype.empty() || t > 127)
-  {
-    type.clear();
-    subtype.clear();
-    return false;
-  }
-
-  return true;
-}
index cdd0fb0..cee1371 100644 (file)
@@ -29,21 +29,4 @@ public:
   static bool RenameFile(const CStdString &strFile);
   static bool RemoteAccessAllowed(const CStdString &strPath);
   static unsigned int LoadFile(const std::string &filename, void* &outputBuffer);
-
-  enum EFileType
-  {
-    FileTypeUnknown = 0,
-    FileTypeHtml,
-    FileTypeXml,
-    FileTypePlainText,
-    FileTypeZip,
-    FileTypeGZip,
-    FileTypeRar,
-    FileTypeBmp,
-    FileTypeGif,
-    FileTypePng,
-    FileTypeJpeg,
-  };
-  static EFileType GetFileTypeFromMime(const std::string& mimeType);
-  static bool parseMimeType(const std::string& mimeType, std::string& type, std::string& subtype);
 };
index 7a04b20..5d5f6c2 100644 (file)
@@ -576,3 +576,114 @@ string CMime::GetMimeType(const CURL &url, bool lookup)
 
   return strMimeType;
 }
+
+CMime::EFileType CMime::GetFileTypeFromMime(const std::string& mimeType)
+{
+  // based on http://mimesniff.spec.whatwg.org/
+
+  std::string type, subtype;
+  if (!parseMimeType(mimeType, type, subtype))
+    return FileTypeUnknown;
+
+  if (type == "application")
+  {
+    if (subtype == "zip")
+      return FileTypeZip;
+    if (subtype == "x-gzip")
+      return FileTypeGZip;
+    if (subtype == "x-rar-compressed")
+      return FileTypeRar;
+
+    if (subtype == "xml")
+      return FileTypeXml;
+  }
+  else if (type == "text")
+  {
+    if (subtype == "xml")
+      return FileTypeXml;
+    if (subtype == "html")
+      return FileTypeHtml;
+    if (subtype == "plain")
+      return FileTypePlainText;
+  }
+  else if (type == "image")
+  {
+    if (subtype == "bmp")
+      return FileTypeBmp;
+    if (subtype == "gif")
+      return FileTypeGif;
+    if (subtype == "png")
+      return FileTypePng;
+    if (subtype == "jpeg" || subtype == "pjpeg")
+      return FileTypeJpeg;
+  }
+
+  if (StringUtils::EndsWith(subtype, "+zip"))
+    return FileTypeZip;
+  if (StringUtils::EndsWith(subtype, "+xml"))
+    return FileTypeXml;
+
+  return FileTypeUnknown;
+}
+
+bool CMime::parseMimeType(const std::string& mimeType, std::string& type, std::string& subtype)
+{
+  // this is an modified implementation of http://mimesniff.spec.whatwg.org/#parsing-a-mime-type with additional checks for non-empty type and subtype
+  // note: only type and subtype are parsed, parameters are ignored
+
+  static const char* const whitespaceChars = "\x09\x0A\x0C\x0D\x20"; // tab, LF, FF, CR and space
+  static const std::string whitespaceSmclnChars("\x09\x0A\x0C\x0D\x20\x3B"); // tab, LF, FF, CR, space and semicolon
+
+  type.clear();
+  subtype.clear();
+
+  const size_t len = mimeType.length();
+  if (len < 1)
+    return false;
+
+  const char* const mimeTypeC = mimeType.c_str();
+  size_t pos = mimeType.find_first_not_of(whitespaceChars);
+  if (pos == std::string::npos)
+    return false;
+
+  // find "type"
+  size_t t = 0;
+  do
+  {
+    const char chr = mimeTypeC[pos];
+    if (t > 127 || !chr)
+    {
+      type.clear();
+      return false;
+    }
+
+    if (chr >= 'A' && chr <= 'Z')
+      type.push_back(chr + ('a' - 'A')); // convert to lowercase
+    else
+      type.push_back(chr);
+    t++;
+    pos++;
+  } while (mimeTypeC[pos] != '/');
+
+  pos++; // skip '/'
+  t = 0;
+
+  while (mimeTypeC[pos] && whitespaceSmclnChars.find(mimeTypeC[pos]) == std::string::npos && t++ <= 127)
+  {
+    const char chr = mimeTypeC[pos];
+    if (chr >= 'A' && chr <= 'Z')
+      subtype.push_back(chr + ('a' - 'A')); // convert to lowercase
+    else
+      subtype.push_back(chr);
+    pos++;
+  }
+
+  if (subtype.empty() || t > 127)
+  {
+    type.clear();
+    subtype.clear();
+    return false;
+  }
+
+  return true;
+}
index d42407e..565b684 100644 (file)
@@ -33,6 +33,23 @@ public:
   static std::string GetMimeType(const CFileItem &item);
   static std::string GetMimeType(const CURL &url, bool lookup = true);
 
+  enum EFileType
+  {
+    FileTypeUnknown = 0,
+    FileTypeHtml,
+    FileTypeXml,
+    FileTypePlainText,
+    FileTypeZip,
+    FileTypeGZip,
+    FileTypeRar,
+    FileTypeBmp,
+    FileTypeGif,
+    FileTypePng,
+    FileTypeJpeg,
+  };
+  static EFileType GetFileTypeFromMime(const std::string& mimeType);
+  static bool parseMimeType(const std::string& mimeType, std::string& type, std::string& subtype);
+
 private:
   static std::map<std::string, std::string> m_mimetypes;
 };
index 6f8e74a..bb11b2b 100644 (file)
@@ -30,7 +30,7 @@
 #include "filesystem/ZipFile.h"
 #include "URIUtils.h"
 #include "utils/XBMCTinyXML.h"
-#include "utils/FileUtils.h"
+#include "utils/Mime.h"
 
 #include <cstring>
 #include <sstream>
@@ -239,9 +239,9 @@ bool CScraperUrl::Get(const SUrlEntry& scrURL, std::string& strHTML, XFILE::CCur
   strHTML = strHTML1;
 
   std::string mimeType(http.GetMimeType());
-  CFileUtils::EFileType ftype = CFileUtils::GetFileTypeFromMime(mimeType);
+  CMime::EFileType ftype = CMime::GetFileTypeFromMime(mimeType);
 
-  if (ftype == CFileUtils::FileTypeZip || ftype == CFileUtils::FileTypeGZip)
+  if (ftype == CMime::FileTypeZip || ftype == CMime::FileTypeGZip)
   {
     XFILE::CZipFile file;
     std::string strBuffer;
@@ -251,7 +251,7 @@ bool CScraperUrl::Get(const SUrlEntry& scrURL, std::string& strHTML, XFILE::CCur
   }
 
   std::string reportedCharset(http.GetServerReportedCharset());
-  if (ftype == CFileUtils::FileTypeHtml)
+  if (ftype == CMime::FileTypeHtml)
   {
     std::string realHtmlCharset, converted;
     if (!CCharsetDetection::ConvertHtmlToUtf8(strHTML, converted, reportedCharset, realHtmlCharset))
@@ -261,7 +261,7 @@ bool CScraperUrl::Get(const SUrlEntry& scrURL, std::string& strHTML, XFILE::CCur
 
     strHTML = converted;
   }
-  else if (ftype == CFileUtils::FileTypeXml)
+  else if (ftype == CMime::FileTypeXml)
   {
     CXBMCTinyXML xmlDoc;
     xmlDoc.Parse(strHTML, reportedCharset);
@@ -275,7 +275,7 @@ bool CScraperUrl::Get(const SUrlEntry& scrURL, std::string& strHTML, XFILE::CCur
       strHTML = converted;
     }
   }
-  else if (ftype == CFileUtils::FileTypePlainText || StringUtils::CompareNoCase(mimeType.substr(0, 5), "text/") == 0)
+  else if (ftype == CMime::FileTypePlainText || StringUtils::CompareNoCase(mimeType.substr(0, 5), "text/") == 0)
   {
     std::string realTextCharset, converted;
     CCharsetDetection::ConvertPlainTextToUtf8(strHTML, converted, reportedCharset, realTextCharset);