summaryrefslogtreecommitdiff
path: root/src/style/style_source.cpp
blob: f46a6fb09b07fc3407643d8a017fe8e9083b23ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <mbgl/style/style_source.hpp>
#include <mbgl/platform/platform.hpp>
#include <mbgl/platform/log.hpp>

#include <limits>

namespace mbgl {

void parse(const rapidjson::Value& value, std::vector<std::string>& target, const char *name) {
    if (!value.HasMember(name))
        return;

    const rapidjson::Value& property = value[name];
    if (!property.IsArray())
        return;

    for (rapidjson::SizeType i = 0; i < property.Size(); i++)
        if (!property[i].IsString())
            return;

    for (rapidjson::SizeType i = 0; i < property.Size(); i++)
        target.emplace_back(std::string(property[i].GetString(), property[i].GetStringLength()));
}

void parse(const rapidjson::Value& value, std::string& target, const char* name) {
    if (!value.HasMember(name))
        return;

    const rapidjson::Value& property = value[name];
    if (!property.IsString())
        return;

    target = { property.GetString(), property.GetStringLength() };
}

void parse(const rapidjson::Value& value, uint16_t& target, const char* name) {
    if (!value.HasMember(name))
        return;

    const rapidjson::Value& property = value[name];
    if (!property.IsUint())
        return;

    unsigned int uint = property.GetUint();
    if (uint > std::numeric_limits<uint16_t>::max())
        return;

    target = uint;
}

template <size_t N>
void parse(const rapidjson::Value& value, std::array<float, N>& target, const char* name) {
    if (!value.HasMember(name))
        return;

    const rapidjson::Value& property = value[name];
    if (!property.IsArray() || property.Size() != N)
        return;

    for (rapidjson::SizeType i = 0; i < property.Size(); i++)
        if (!property[i].IsNumber())
            return;

    for (rapidjson::SizeType i = 0; i < property.Size(); i++)
        target[i] = property[i].GetDouble();
}

void SourceInfo::parseTileJSONProperties(const rapidjson::Value& value) {
    parse(value, tiles, "tiles");
    parse(value, min_zoom, "minzoom");
    parse(value, max_zoom, "maxzoom");
    parse(value, attribution, "attribution");
    parse(value, center, "center");
    parse(value, bounds, "bounds");
}

}