aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/vcpkglib_helpers.cpp
blob: 3d14d4b063f5d97ebce39222caa8dfe65428269b (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
78
79
80
81
#include "vcpkg_Checks.h"
#include "vcpkglib_helpers.h"
#include <unordered_map>

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

        return it->second;
    }

    std::string remove_optional_field(std::unordered_map<std::string, std::string>* fields, const std::string& fieldname)
    {
        auto it = fields->find(fieldname);
        if (it == fields->end())
        {
            return std::string();
        }

        const std::string value = std::move(it->second);
        fields->erase(it);
        return value;
    }

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

    std::string remove_required_field(std::unordered_map<std::string, std::string>* fields, const std::string& fieldname)
    {
        auto it = fields->find(fieldname);
        Checks::check_exit(it != fields->end(), "Required field not present: %s", fieldname);

        const std::string value = std::move(it->second);
        fields->erase(it);
        return value;
    }

    std::vector<std::string> parse_depends(const std::string& depends_string)
    {
        if (depends_string.empty())
        {
            return {};
        }

        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));
                break;
            }
            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);

        return out;
    }
}}