From bf22b809f1ea301852fe81e2d8ff2b0c10696bce Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Thu, 5 Aug 2021 13:20:08 +0900 Subject: [PATCH] tvg loader: code refactoring revise the mimetype load method to keep it clean & neat. --- src/lib/tvgLoader.cpp | 53 +++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/src/lib/tvgLoader.cpp b/src/lib/tvgLoader.cpp index 2ee710e..615d27d 100644 --- a/src/lib/tvgLoader.cpp +++ b/src/lib/tvgLoader.cpp @@ -113,7 +113,7 @@ static LoadModule* _find(FileType type) } -static LoadModule* _find(const string& path) +static LoadModule* _findByPath(const string& path) { auto ext = path.substr(path.find_last_of(".") + 1); if (!ext.compare("tvg")) return _find(FileType::Tvg); @@ -124,6 +124,26 @@ static LoadModule* _find(const string& path) } +static LoadModule* _findByType(const string& mimeType) +{ + if (mimeType.empty()) return nullptr; + + auto type = FileType::Unknown; + + if (mimeType == "tvg") type = FileType::Tvg; + else if (mimeType == "svg" || mimeType == "svg+xml") type = FileType::Svg; + else if (mimeType == "raw") type = FileType::Raw; + else if (mimeType == "png") type = FileType::Png; + else if (mimeType == "jpg" || mimeType == "jpeg") type = FileType::Jpg; + else { + TVGLOG("LOADER", "Given mimetype is unknown = \"%s\".", mimeType.c_str()); + return nullptr; + } + + return _find(type); +} + + /************************************************************************/ /* External Class Implementation */ /************************************************************************/ @@ -149,7 +169,7 @@ shared_ptr LoaderMgr::loader(const string& path, bool* invalid) { *invalid = false; - if (auto loader = _find(path)) { + if (auto loader = _findByPath(path)) { if (loader->open(path)) return shared_ptr(loader); else delete(loader); *invalid = true; @@ -160,31 +180,18 @@ shared_ptr LoaderMgr::loader(const string& path, bool* invalid) shared_ptr LoaderMgr::loader(const char* data, uint32_t size, const string& mimeType, bool copy) { - FileType filetype = FileType::Unknown; - if (!mimeType.empty()) { - if (mimeType == "tvg") filetype = FileType::Tvg; - else if (mimeType == "svg") filetype = FileType::Svg; - else if (mimeType == "svg+xml") filetype = FileType::Svg; - else if (mimeType == "raw") filetype = FileType::Raw; - else if (mimeType == "png") filetype = FileType::Png; - else if (mimeType == "jpg") filetype = FileType::Jpg; - else if (mimeType == "jpeg") filetype = FileType::Jpg; - else TVGLOG("LOADER", "Provided unknown mimetype \"%s\".", mimeType.c_str()); - - if (filetype != FileType::Unknown) { - auto loader = _find(static_cast(filetype)); - if (loader) { - if (loader->open(data, size, copy)) return shared_ptr(loader); - else { - TVGLOG("LOADER", "Provided mimetype \"%s\" (filetype=%d) seems incorrect. Will try other types.", mimeType.c_str(), static_cast(filetype)); - delete(loader); - } - } + //Try first with the given MimeType + if (auto loader = _findByType(mimeType)) { + if (loader->open(data, size, copy)) { + return shared_ptr(loader); + } else { + TVGLOG("LOADER", "Given mimetype \"%s\" seems incorrect or not supported. Will try again with other types.", mimeType.c_str()); + delete(loader); } } + //Abnormal MimeType. Try with the candidates in the order for (int i = 0; i < static_cast(FileType::Unknown); i++) { - if (static_cast(i) == filetype) continue; auto loader = _find(static_cast(i)); if (loader) { if (loader->open(data, size, copy)) return shared_ptr(loader); -- 2.7.4