mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 13:43:43 +00:00
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:
parent
627579522d
commit
af85eb22ad
3 changed files with 49 additions and 15 deletions
|
@ -47,10 +47,47 @@ bool LoaderMgr::term()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
unique_ptr<Loader> LoaderMgr::loader()
|
unique_ptr<Loader> findLoaderByType(FileType type)
|
||||||
{
|
{
|
||||||
|
switch (type) {
|
||||||
|
case FileType::Svg :
|
||||||
#ifdef THORVG_SVG_LOADER_SUPPORT
|
#ifdef THORVG_SVG_LOADER_SUPPORT
|
||||||
return unique_ptr<SvgLoader>(new SvgLoader);
|
return unique_ptr<SvgLoader>(new SvgLoader);
|
||||||
#endif
|
#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;
|
return nullptr;
|
||||||
}
|
}
|
|
@ -24,11 +24,14 @@
|
||||||
|
|
||||||
#include "tvgLoader.h"
|
#include "tvgLoader.h"
|
||||||
|
|
||||||
|
enum class FileType { Svg = 0, Unknown = 1};
|
||||||
|
|
||||||
struct LoaderMgr
|
struct LoaderMgr
|
||||||
{
|
{
|
||||||
static bool init();
|
static bool init();
|
||||||
static bool term();
|
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_
|
#endif //_TVG_LOADER_MGR_H_
|
|
@ -92,11 +92,8 @@ struct Picture::Impl
|
||||||
Result load(const string& path)
|
Result load(const string& path)
|
||||||
{
|
{
|
||||||
if (loader) loader->close();
|
if (loader) loader->close();
|
||||||
loader = LoaderMgr::loader();
|
loader = LoaderMgr::loader(path);
|
||||||
if (!loader || !loader->open(path.c_str())) {
|
if (!loader) return Result::NonSupport;
|
||||||
//LOG: Non supported format
|
|
||||||
return Result::NonSupport;
|
|
||||||
}
|
|
||||||
if (!loader->read()) return Result::Unknown;
|
if (!loader->read()) return Result::Unknown;
|
||||||
return Result::Success;
|
return Result::Success;
|
||||||
}
|
}
|
||||||
|
@ -104,11 +101,8 @@ struct Picture::Impl
|
||||||
Result load(const char* data, uint32_t size)
|
Result load(const char* data, uint32_t size)
|
||||||
{
|
{
|
||||||
if (loader) loader->close();
|
if (loader) loader->close();
|
||||||
loader = LoaderMgr::loader();
|
loader = LoaderMgr::loader(data, size);
|
||||||
if (!loader || !loader->open(data, size)) {
|
if (!loader) return Result::NonSupport;
|
||||||
//LOG: Non supported load data
|
|
||||||
return Result::NonSupport;
|
|
||||||
}
|
|
||||||
if (!loader->read()) return Result::Unknown;
|
if (!loader->read()) return Result::Unknown;
|
||||||
return Result::Success;
|
return Result::Success;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue