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.
This commit is contained in:
JunsuChoi 2020-10-22 13:10:48 +09:00 committed by Hermet Park
parent 627579522d
commit af85eb22ad
3 changed files with 49 additions and 15 deletions

View 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;
}
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;
}

View file

@ -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> 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_

View 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;
}