aboutsummaryrefslogtreecommitdiff
path: root/src/environment.cpp
blob: 20515739b765eb89ada02b7a49d656b14d33afb3 (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
#include "environment.hpp"

#include <iostream>
#include <string>

namespace wanda
{

environment::environment(char const *const *env)
{
    if (!env)
    {
        return;
    }

    std::string buffer{};
    for (; *env != nullptr; ++env)
    {
        buffer = *env;
        int split_point = buffer.find('=');
        if (split_point != std::string::npos)
        {
            m_cache[buffer.substr(0, split_point)] = buffer.substr(split_point + 1);
        }
    }
}

std::string & environment::operator[](std::string const & variable)
{
    return m_cache[variable];
}

std::string const & environment::operator[](std::string const & variable) const
{
    static std::string const empty{};
    if(auto needle = m_cache.find(variable); needle != cend())
    {
        return needle->second;
    }
    return empty;
}

environment::iterator environment::begin()
{
    return m_cache.begin();
}

environment::const_iterator environment::begin() const
{
    return m_cache.begin();
}

environment::const_iterator environment::cbegin() const
{
    return m_cache.cbegin();
}

environment::iterator environment::end()
{
    return m_cache.end();
}

environment::const_iterator environment::end() const
{
    return m_cache.end();
}

environment::const_iterator environment::cend() const
{
    return m_cache.cend();
}

} // namespace wanda