loader: replace to logging system.
[platform/core/uifw/lottie-player.git] / src / lottie / lottieloader.cpp
1 #include "lottieloader.h"
2 #include "lottieparser.h"
3
4 #include<fstream>
5 #include<unordered_map>
6
7 using namespace std;
8
9 class LottieFileCache
10 {
11 public:
12    ~LottieFileCache();
13    static LottieFileCache &get() {
14       static LottieFileCache CACHE;
15
16       return CACHE;
17    }
18    std::shared_ptr<LOTModel> find(std::string &key);
19    void add(std::string &key, std::shared_ptr<LOTModel> value);
20 private:
21    LottieFileCache(){}
22
23    std::unordered_map<std::string, std::shared_ptr<LOTModel>> mHash;
24
25 };
26
27 LottieFileCache::~LottieFileCache()
28 {
29
30 }
31 std::shared_ptr<LOTModel>
32 LottieFileCache::find(std::string &key)
33 {
34    auto search = mHash.find(key);
35    if (search != mHash.end()) {
36        return search->second;
37    } else {
38       return nullptr;
39    }
40 }
41
42 void
43 LottieFileCache::add(std::string &key, std::shared_ptr<LOTModel> value)
44 {
45    mHash[key] = value;
46 }
47
48 LottieLoader::LottieLoader()
49 {
50
51 }
52
53 bool LottieLoader::load(std::string &path)
54 {
55    LottieFileCache &fileCache = LottieFileCache::get();
56
57    mModel = fileCache.find(path);
58    if (mModel)
59       return true;
60
61    std::ifstream f;
62    f.open(path);
63
64    if (!f.is_open()) {
65       vCritical << "failed to open file = " << path.c_str();
66       return false;
67    } else {
68       std::stringstream buf;
69       buf << f.rdbuf();
70
71       LottieParser parser(const_cast<char *>(buf.str().data()));
72       mModel = parser.model();
73       fileCache.add(path, mModel);
74
75       f.close();
76    }
77
78    return true;
79 }
80
81 std::shared_ptr<LOTModel> LottieLoader::model()
82 {
83    return mModel;
84 }
85