svg_loader LoaderMgr: Delegates file validation check to LoaderMgr.
authorJunsuChoi <jsuya.choi@samsung.com>
Thu, 22 Oct 2020 04:10:48 +0000 (13:10 +0900)
committerHermet Park <chuneon.park@samsung.com>
Tue, 3 Nov 2020 05:50:30 +0000 (14:50 +0900)
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
src/lib/tvgLoaderMgr.h
src/lib/tvgPictureImpl.h

index 6ed2d3e..b241415 100644 (file)
@@ -47,10 +47,47 @@ bool LoaderMgr::term()
     return true;
 }
 
-unique_ptr<Loader> LoaderMgr::loader()
+unique_ptr<Loader> findLoaderByType(FileType type)
 {
+    switch (type) {
+        case FileType::Svg :
 #ifdef THORVG_SVG_LOADER_SUPPORT
-    return unique_ptr<SvgLoader>(new SvgLoader);
+            return unique_ptr<SvgLoader>(new SvgLoader);
 #endif
+            break;
+        default:
+            break;
+    }
     return nullptr;
-}
\ No newline at end of file
+}
+
+unique_ptr<Loader> 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<Loader> LoaderMgr::loader(const string& path)
+{
+    unique_ptr<Loader> loader = nullptr;
+    loader = findLoaderByExt(path);
+    if (loader && loader->open(path.c_str())) {
+        return loader;
+    }
+    return nullptr;
+}
+
+unique_ptr<Loader> LoaderMgr::loader(const char* data, uint32_t size)
+{
+    unique_ptr<Loader> loader = nullptr;
+    for (int i = 0; i < static_cast<int>(FileType::Unknown); i++) {
+        loader = findLoaderByType(static_cast<FileType>(i));
+        if (loader && loader->open(data, size)) {
+            return loader;
+        }
+    }
+    return nullptr;
+}
index 81623f2..193294a 100644 (file)
 
 #include "tvgLoader.h"
 
+enum class FileType { Svg = 0, Unknown = 1};
+
 struct LoaderMgr
 {
     static bool init();
     static bool term();
-    static unique_ptr<Loader> loader();
+    static unique_ptr<Loader> loader(const char* data, uint32_t size);
+    static unique_ptr<Loader> loader(const string& path);
 };
 
-#endif //_TVG_LOADER_MGR_H_
\ No newline at end of file
+#endif //_TVG_LOADER_MGR_H_
index 7ae736c..0c0f8f7 100644 (file)
@@ -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;
     }