lottie/example: Added image_test.json(resource with image resource )
[platform/core/uifw/lottie-player.git] / src / lottie / lottieloader.cpp
1 /* 
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  * 
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  * 
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  * 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include "lottieloader.h"
20 #include "lottieparser.h"
21
22 #include <fstream>
23 #include <unordered_map>
24 #include <cstring>
25 using namespace std;
26
27 class LottieFileCache {
28 public:
29     static LottieFileCache &get()
30     {
31         static LottieFileCache CACHE;
32
33         return CACHE;
34     }
35     std::shared_ptr<LOTModel> find(const std::string &key);
36     void add(const std::string &key, std::shared_ptr<LOTModel> value);
37
38 private:
39     LottieFileCache() = default;
40
41     std::unordered_map<std::string, std::shared_ptr<LOTModel>> mHash;
42 };
43
44 std::shared_ptr<LOTModel> LottieFileCache::find(const std::string &key)
45 {
46     auto search = mHash.find(key);
47     if (search != mHash.end()) {
48         return search->second;
49     } else {
50         return nullptr;
51     }
52 }
53
54 void LottieFileCache::add(const std::string &key, std::shared_ptr<LOTModel> value)
55 {
56     mHash[key] = std::move(value);
57 }
58
59 static std::string dirname(const std::string &path)
60 {
61     const char *ptr = strrchr(path.c_str(), '/');
62     int len = int(ptr + 1 - path.c_str()); // +1 to include '/'
63     return std::string(path, 0, len);
64 }
65
66 bool LottieLoader::load(const std::string &path)
67 {
68     LottieFileCache &fileCache = LottieFileCache::get();
69
70     mModel = fileCache.find(path);
71     if (mModel) return true;
72
73     std::ifstream f;
74     f.open(path);
75
76     if (!f.is_open()) {
77         vCritical << "failed to open file = " << path.c_str();
78         return false;
79     } else {
80         std::stringstream buf;
81         buf << f.rdbuf();
82
83         LottieParser parser(const_cast<char *>(buf.str().data()), dirname(path).c_str());
84         mModel = parser.model();
85         fileCache.add(path, mModel);
86
87         f.close();
88     }
89
90     return true;
91 }
92
93 bool LottieLoader::loadFromData(std::string &&jsonData, const std::string &key)
94 {
95     LottieFileCache &fileCache = LottieFileCache::get();
96
97     mModel = fileCache.find(key);
98     if (mModel) return true;
99
100     LottieParser parser(const_cast<char *>(jsonData.c_str()));
101     mModel = parser.model();
102     fileCache.add(key, mModel);
103
104     return true;
105 }
106
107 std::shared_ptr<LOTModel> LottieLoader::model()
108 {
109     return mModel;
110 }