lottie: Follow Tizen coding guideline.
[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 public:
11     ~LottieFileCache();
12     static LottieFileCache &get()
13     {
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
21 private:
22     LottieFileCache() {}
23
24     std::unordered_map<std::string, std::shared_ptr<LOTModel>> mHash;
25 };
26
27 LottieFileCache::~LottieFileCache() {}
28 std::shared_ptr<LOTModel> LottieFileCache::find(std::string &key)
29 {
30     auto search = mHash.find(key);
31     if (search != mHash.end()) {
32         return search->second;
33     } else {
34         return nullptr;
35     }
36 }
37
38 void LottieFileCache::add(std::string &key, std::shared_ptr<LOTModel> value)
39 {
40     mHash[key] = value;
41 }
42
43 LottieLoader::LottieLoader() {}
44
45 bool LottieLoader::load(std::string &path)
46 {
47     LottieFileCache &fileCache = LottieFileCache::get();
48
49     mModel = fileCache.find(path);
50     if (mModel) return true;
51
52     std::ifstream f;
53     f.open(path);
54
55     if (!f.is_open()) {
56         vCritical << "failed to open file = " << path.c_str();
57         return false;
58     } else {
59         std::stringstream buf;
60         buf << f.rdbuf();
61
62         LottieParser parser(const_cast<char *>(buf.str().data()));
63         mModel = parser.model();
64         fileCache.add(path, mModel);
65
66         f.close();
67     }
68
69     return true;
70 }
71
72 std::shared_ptr<LOTModel> LottieLoader::model()
73 {
74     return mModel;
75 }