From d523d190e4c0b596d1ce45c4c297e85bb49441b7 Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Thu, 22 Oct 2020 13:10:48 +0900 Subject: [PATCH] svg_loader LoaderMgr: Delegates file validation check to LoaderMgr. Delegates extension string check and header(open) check for file to LoaderMgr. This makes it easy to add a loader afterwards. Change-Id: I93b355cdc78fe371b77ada56fe08e4dc47e0d5d5 --- src/lib/tvgLoaderMgr.cpp | 43 ++++++++++++++++++++++++++++++++++++++++--- src/lib/tvgLoaderMgr.h | 7 +++++-- src/lib/tvgPictureImpl.h | 14 ++++---------- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/src/lib/tvgLoaderMgr.cpp b/src/lib/tvgLoaderMgr.cpp index 6ed2d3e..b241415 100644 --- a/src/lib/tvgLoaderMgr.cpp +++ b/src/lib/tvgLoaderMgr.cpp @@ -47,10 +47,47 @@ bool LoaderMgr::term() return true; } -unique_ptr LoaderMgr::loader() +unique_ptr findLoaderByType(FileType type) { + switch (type) { + case FileType::Svg : #ifdef THORVG_SVG_LOADER_SUPPORT - return unique_ptr(new SvgLoader); + return unique_ptr(new SvgLoader); #endif + break; + default: + break; + } return nullptr; -} \ No newline at end of file +} + +unique_ptr findLoaderByExt(const string& path) +{ + string ext = path.substr(path.find_last_of(".") + 1); + if (!ext.compare("svg")) { + return findLoaderByType(FileType::Svg); + } + return nullptr; +} + +unique_ptr LoaderMgr::loader(const string& path) +{ + unique_ptr loader = nullptr; + loader = findLoaderByExt(path); + if (loader && loader->open(path.c_str())) { + return loader; + } + return nullptr; +} + +unique_ptr LoaderMgr::loader(const char* data, uint32_t size) +{ + unique_ptr loader = nullptr; + for (int i = 0; i < static_cast(FileType::Unknown); i++) { + loader = findLoaderByType(static_cast(i)); + if (loader && loader->open(data, size)) { + return loader; + } + } + return nullptr; +} diff --git a/src/lib/tvgLoaderMgr.h b/src/lib/tvgLoaderMgr.h index 81623f2..193294a 100644 --- a/src/lib/tvgLoaderMgr.h +++ b/src/lib/tvgLoaderMgr.h @@ -24,11 +24,14 @@ #include "tvgLoader.h" +enum class FileType { Svg = 0, Unknown = 1}; + struct LoaderMgr { static bool init(); static bool term(); - static unique_ptr loader(); + static unique_ptr loader(const char* data, uint32_t size); + static unique_ptr loader(const string& path); }; -#endif //_TVG_LOADER_MGR_H_ \ No newline at end of file +#endif //_TVG_LOADER_MGR_H_ diff --git a/src/lib/tvgPictureImpl.h b/src/lib/tvgPictureImpl.h index 7ae736c..0c0f8f7 100644 --- a/src/lib/tvgPictureImpl.h +++ b/src/lib/tvgPictureImpl.h @@ -92,11 +92,8 @@ struct Picture::Impl Result load(const string& path) { if (loader) loader->close(); - loader = LoaderMgr::loader(); - if (!loader || !loader->open(path.c_str())) { - //LOG: Non supported format - return Result::NonSupport; - } + loader = LoaderMgr::loader(path); + if (!loader) return Result::NonSupport; if (!loader->read()) return Result::Unknown; return Result::Success; } @@ -104,11 +101,8 @@ struct Picture::Impl Result load(const char* data, uint32_t size) { if (loader) loader->close(); - loader = LoaderMgr::loader(); - if (!loader || !loader->open(data, size)) { - //LOG: Non supported load data - return Result::NonSupport; - } + loader = LoaderMgr::loader(data, size); + if (!loader) return Result::NonSupport; if (!loader->read()) return Result::Unknown; return Result::Success; } -- 2.7.4