[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / loader / json-reader.cpp
1 /*
2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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
18 // CLASS HEADER
19 #include <dali-scene3d/internal/loader/json-reader.h>
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23 #include <cstring>
24
25 namespace json
26 {
27 int StrCmp(const json_string_s& js, const char* s)
28 {
29   auto sSize   = strlen(s);
30   auto shorter = std::min(js.string_size, sSize);
31   auto base    = strncmp(js.string, s, shorter);
32   return ((base != 0) || (sSize == js.string_size)) ? base : ((js.string_size < sSize) ? -s[shorter] : js.string[shorter]);
33 }
34
35 int StrCmp(const json_string_s& js, const std::string& s)
36 {
37   auto sSize   = s.size();
38   auto shorter = std::min(js.string_size, sSize);
39   auto base    = strncmp(js.string, s.c_str(), shorter);
40   return ((base != 0) || (sSize == js.string_size)) ? base : ((js.string_size < sSize) ? -s[shorter] : js.string[shorter]);
41 }
42
43 void Validate(const json_value_s& jv, json_type_e type)
44 {
45   if(jv.type != type)
46   {
47     throw std::runtime_error("Invalid type; expected: " + std::to_string(type) + ", got: " + std::to_string(jv.type));
48   }
49 }
50
51 json_value_s* FindObjectChild(const std::string& key, json_object_s& obj)
52 {
53   auto i = obj.start;
54   while(i)
55   {
56     if(0 == StrCmp(*i->name, key))
57     {
58       return i->value;
59     }
60     i = i->next;
61   }
62   return nullptr;
63 }
64
65 } // namespace json