~ubuntu-branches/ubuntu/utopic/mir/utopic-proposed

« back to all changes in this revision

Viewing changes to src/server/options/program_option.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-03-10 19:28:46 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: package-import@ubuntu.com-20140310192846-rq9qm3ec26yrelo2
Tags: upstream-0.1.6+14.04.20140310
Import upstream version 0.1.6+14.04.20140310

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2012 Canonical Ltd.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify it
5
 
 * under the terms of the GNU General Public License version 3,
6
 
 * as published by the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful,
9
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 * GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License
14
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 *
16
 
 * Authored by: Alan Griffiths <alan@octopull.co.uk>
17
 
 */
18
 
 
19
 
#include "mir/options/program_option.h"
20
 
 
21
 
#include <boost/program_options/parsers.hpp>
22
 
 
23
 
#include <fstream>
24
 
#include <iostream>
25
 
 
26
 
namespace mo = mir::options;
27
 
namespace po = boost::program_options;
28
 
 
29
 
namespace
30
 
{
31
 
    std::string parse_name(std::string name)
32
 
    {
33
 
        return name.substr(0, name.find_first_of(','));
34
 
    }
35
 
}
36
 
 
37
 
mo::ProgramOption::ProgramOption()
38
 
{
39
 
}
40
 
 
41
 
void mo::ProgramOption::parse_arguments(
42
 
    po::options_description const& desc,
43
 
    int argc,
44
 
    char const* argv[])
45
 
{
46
 
    // TODO: Don't allow unregistered options, once we allow better overriding of option parsing
47
 
    po::store(po::command_line_parser(argc, argv).options(desc).allow_unregistered().run(), options);
48
 
    po::notify(options);
49
 
}
50
 
 
51
 
void mo::ProgramOption::parse_environment(
52
 
    po::options_description const& desc,
53
 
    char const* prefix)
54
 
{
55
 
    auto parsed_options = po::parse_environment(
56
 
        desc,
57
 
        [=](std::string const& from) -> std::string
58
 
        {
59
 
             auto const sizeof_prefix = strlen(prefix);
60
 
 
61
 
             if (from.length() < sizeof_prefix || 0 != from.find(prefix)) return std::string();
62
 
 
63
 
             std::string result(from, sizeof_prefix);
64
 
 
65
 
             for(auto& ch : result)
66
 
             {
67
 
                 if (ch == '_') ch = '-';
68
 
                 else ch = tolower(ch);
69
 
             }
70
 
 
71
 
             return result;
72
 
        });
73
 
 
74
 
    po::store(parsed_options, options);
75
 
}
76
 
 
77
 
void mo::ProgramOption::parse_file(
78
 
    po::options_description const& config_file_desc,
79
 
    std::string const& name)
80
 
{
81
 
    std::string config_roots;
82
 
 
83
 
    if (auto config_home = getenv("XDG_CONFIG_HOME"))
84
 
        (config_roots = config_home) += ":";
85
 
    else if (auto home = getenv("HOME"))
86
 
        (config_roots = home) += "/.config:";
87
 
 
88
 
    if (auto config_dirs = getenv("XDG_CONFIG_DIRS"))
89
 
        config_roots += config_dirs;
90
 
    else
91
 
        config_roots += "/etc/xdg";
92
 
 
93
 
    std::istringstream config_stream(config_roots);
94
 
 
95
 
    /* Read options from config files */
96
 
    for (std::string config_root; getline(config_stream, config_root, ':');)
97
 
    {
98
 
        auto const& filename = config_root + "/" + name;
99
 
 
100
 
        try
101
 
        {
102
 
            std::ifstream file(filename);
103
 
            po::store(po::parse_config_file(file, config_file_desc, true), options);
104
 
        }
105
 
        catch (const po::error& error)
106
 
        {
107
 
            std::cerr << "ERROR in " << filename << ": " << error.what() << std::endl;
108
 
        }
109
 
    }
110
 
 
111
 
    po::notify(options);
112
 
}
113
 
 
114
 
bool mo::ProgramOption::is_set(char const* name) const
115
 
{
116
 
    return options.count(parse_name(name));
117
 
}
118
 
 
119
 
 
120
 
bool mo::ProgramOption::get(char const* name, bool default_) const
121
 
{
122
 
    auto const parsed_name = parse_name(name);
123
 
    if (options.count(parsed_name))
124
 
    {
125
 
        return options[parsed_name].as<bool>();
126
 
    }
127
 
 
128
 
    return default_;
129
 
}
130
 
 
131
 
std::string mo::ProgramOption::get(char const* name, char const* default_) const
132
 
{
133
 
    auto const parsed_name = parse_name(name);
134
 
    if (options.count(parsed_name))
135
 
    {
136
 
        return options[parsed_name].as<std::string>();
137
 
    }
138
 
 
139
 
    return default_;
140
 
}
141
 
 
142
 
int mo::ProgramOption::get(char const* name, int default_) const
143
 
{
144
 
    auto const parsed_name = parse_name(name);
145
 
    if (options.count(parsed_name))
146
 
    {
147
 
        return options[parsed_name].as<int>();
148
 
    }
149
 
 
150
 
    return default_;
151
 
}
152
 
 
153
 
boost::any const& mo::ProgramOption::get(char const* name) const
154
 
{
155
 
    auto const parsed_name = parse_name(name);
156
 
    if (options.count(parsed_name))
157
 
    {
158
 
        return options[parsed_name].value();
159
 
    }
160
 
 
161
 
    static boost::any const default_;
162
 
 
163
 
    return default_;
164
 
}