aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/vcpkglib_helpers.cpp
blob: e947dc647502ec9cd5da320b08c490274997a797 (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
#include "vcpkg_Checks.h"
#include "vcpkglib_helpers.h"
#include <unordered_map>

namespace vcpkg {namespace details
{
    void optional_field(const std::unordered_map<std::string, std::string>& fields, std::string& out, const std::string& fieldname)
    {
        auto it = fields.find(fieldname);
        if (it == fields.end())
        {
            out.clear();
        }

        else
        {
            out = it->second;
        }
    };

    void required_field(const std::unordered_map<std::string, std::string>& fields, std::string& out, const std::string& fieldname)
    {
        auto it = fields.find(fieldname);
        vcpkg::Checks::check_throw(it != fields.end(), "Required field not present: %s", fieldname);
        out = it->second;
    };

    void parse_depends(const std::string& depends_string, std::vector<std::string>& out)
    {
        size_t cur = 0;
        do
        {
            auto pos = depends_string.find(',', cur);
            if (pos == std::string::npos)
            {
                out.push_back(depends_string.substr(cur));
                return;
            }
            out.push_back(depends_string.substr(cur, pos - cur));

            // skip comma and space
            ++pos;
            if (depends_string[pos] == ' ')
                ++pos;

            cur = pos;
        }
        while (cur != std::string::npos);
    }
}}