remove sneaked inserting unicodes, zero-width no-break space.
[platform/core/uifw/lottie-player.git] / src / lottie / lottieloader.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Flora License, Version 1.1 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "lottieloader.h"
18 #include "lottieparser.h"
19
20 #include <fstream>
21 #include <unordered_map>
22
23 using namespace std;
24
25 class LottieFileCache {
26 public:
27     static LottieFileCache &get()
28     {
29         static LottieFileCache CACHE;
30
31         return CACHE;
32     }
33     std::shared_ptr<LOTModel> find(const std::string &key);
34     void add(const std::string &key, std::shared_ptr<LOTModel> value);
35
36 private:
37     LottieFileCache() = default;
38
39     std::unordered_map<std::string, std::shared_ptr<LOTModel>> mHash;
40 };
41
42 std::shared_ptr<LOTModel> LottieFileCache::find(const std::string &key)
43 {
44     auto search = mHash.find(key);
45     if (search != mHash.end()) {
46         return search->second;
47     } else {
48         return nullptr;
49     }
50 }
51
52 void LottieFileCache::add(const std::string &key, std::shared_ptr<LOTModel> value)
53 {
54     mHash[key] = std::move(value);
55 }
56
57 bool LottieLoader::load(const std::string &path)
58 {
59     LottieFileCache &fileCache = LottieFileCache::get();
60
61     mModel = fileCache.find(path);
62     if (mModel) return true;
63
64     std::ifstream f;
65     f.open(path);
66
67     if (!f.is_open()) {
68         vCritical << "failed to open file = " << path.c_str();
69         return false;
70     } else {
71         std::stringstream buf;
72         buf << f.rdbuf();
73
74         LottieParser parser(const_cast<char *>(buf.str().data()));
75         mModel = parser.model();
76         fileCache.add(path, mModel);
77
78         f.close();
79     }
80
81     return true;
82 }
83
84 bool LottieLoader::loadFromData(std::string &&jsonData, const std::string &key)
85 {
86     LottieFileCache &fileCache = LottieFileCache::get();
87
88     mModel = fileCache.find(key);
89     if (mModel) return true;
90
91     LottieParser parser(const_cast<char *>(jsonData.c_str()));
92     mModel = parser.model();
93     fileCache.add(key, mModel);
94
95     return true;
96 }
97
98 std::shared_ptr<LOTModel> LottieLoader::model()
99 {
100     return mModel;
101 }