lottie-player : Initial draft for lottie-player library
[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 class LottieFileCache
8 {
9 public:
10    ~LottieFileCache();
11    static LottieFileCache &get() {
12       static LottieFileCache CACHE;
13
14       return CACHE;
15    }
16    std::shared_ptr<LOTModel> find(std::string &key);
17    void add(std::string &key, std::shared_ptr<LOTModel> value);
18 private:
19    LottieFileCache(){}
20
21    std::unordered_map<std::string, std::shared_ptr<LOTModel>> mHash;
22
23 };
24
25 LottieFileCache::~LottieFileCache()
26 {
27
28 }
29 std::shared_ptr<LOTModel>
30 LottieFileCache::find(std::string &key)
31 {
32    auto search = mHash.find(key);
33    if (search != mHash.end()) {
34        return search->second;
35    } else {
36       return nullptr;
37    }
38 }
39
40 void
41 LottieFileCache::add(std::string &key, std::shared_ptr<LOTModel> value)
42 {
43    mHash[key] = value;
44 }
45
46 LottieLoader::LottieLoader()
47 {
48
49 }
50
51 bool LottieLoader::load(std::string &path)
52 {
53    if (path.empty()) return false;
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       printf("failed to open \n");
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