2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
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.
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.
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
19 #include "lottieloader.h"
20 #include "lottieparser.h"
23 #include <unordered_map>
27 class LottieFileCache {
29 static LottieFileCache &get()
31 static LottieFileCache CACHE;
35 std::shared_ptr<LOTModel> find(const std::string &key);
36 void add(const std::string &key, std::shared_ptr<LOTModel> value);
39 LottieFileCache() = default;
41 std::unordered_map<std::string, std::shared_ptr<LOTModel>> mHash;
44 std::shared_ptr<LOTModel> LottieFileCache::find(const std::string &key)
46 auto search = mHash.find(key);
47 if (search != mHash.end()) {
48 return search->second;
54 void LottieFileCache::add(const std::string &key, std::shared_ptr<LOTModel> value)
56 mHash[key] = std::move(value);
59 static std::string dirname(const std::string &path)
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);
66 bool LottieLoader::load(const std::string &path)
68 LottieFileCache &fileCache = LottieFileCache::get();
70 mModel = fileCache.find(path);
71 if (mModel) return true;
77 vCritical << "failed to open file = " << path.c_str();
80 std::stringstream buf;
83 LottieParser parser(const_cast<char *>(buf.str().data()), dirname(path).c_str());
84 mModel = parser.model();
85 fileCache.add(path, mModel);
93 bool LottieLoader::loadFromData(std::string &&jsonData, const std::string &key)
95 LottieFileCache &fileCache = LottieFileCache::get();
97 mModel = fileCache.find(key);
98 if (mModel) return true;
100 LottieParser parser(const_cast<char *>(jsonData.c_str()));
101 mModel = parser.model();
102 fileCache.add(key, mModel);
107 std::shared_ptr<LOTModel> LottieLoader::model()