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;
+}
#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_
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;
}
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;
}