• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/libs/core/program_options/src/parsers.cpp
1
//  Copyright Vladimir Prus 2002-2004.
2
//
3
//  SPDX-License-Identifier: BSL-1.0
4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
#include <hpx/program_options/config.hpp>
8
#include <hpx/modules/debugging.hpp>
9
#include <hpx/program_options/detail/cmdline.hpp>
10
#include <hpx/program_options/detail/config_file.hpp>
11
#include <hpx/program_options/detail/convert.hpp>
12
#include <hpx/program_options/environment_iterator.hpp>
13
#include <hpx/program_options/options_description.hpp>
14
#include <hpx/program_options/parsers.hpp>
15
#include <hpx/program_options/positional_options.hpp>
16

17
#include <algorithm>
18
#include <cctype>
19
#include <fstream>
20
#include <functional>
21
#include <memory>
22
#include <set>
23
#include <string>
24
#include <utility>
25
#include <vector>
26

27
#ifdef _WIN32
28
#include <stdlib.h>
29
#else
30
#include <unistd.h>
31
#endif
32

33
namespace hpx::program_options {
34

35
    namespace {
36

37
        inline woption woption_from_option(option const& opt)
×
38
        {
39
            woption result;
40
            result.string_key = opt.string_key;
×
41
            result.position_key = opt.position_key;
×
42
            result.unregistered = opt.unregistered;
×
43

44
            std::transform(opt.value.begin(), opt.value.end(),
×
45
                back_inserter(result.value), [](auto&& arg) {
×
46
                    return detail::from_utf8(std::forward<decltype(arg)>(arg));
×
47
                });
48

49
            std::transform(opt.original_tokens.begin(),
×
50
                opt.original_tokens.end(),
51
                back_inserter(result.original_tokens), [](auto&& arg) {
×
52
                    return detail::from_utf8(std::forward<decltype(arg)>(arg));
×
53
                });
54
            return result;
×
55
        }
×
56
    }    // namespace
57

58
    basic_parsed_options<wchar_t>::basic_parsed_options(
×
59
        parsed_options const& po)
×
60
      : description(po.description)
×
61
      , utf8_encoded_options(po)
62
      , m_options_prefix(po.m_options_prefix)
×
63
    {
64
        for (auto const& option : po.options)
×
65
            options.emplace_back(woption_from_option(option));
×
66
    }
×
67

68
    template <typename Char>
69
    basic_parsed_options<Char> parse_config_file(std::basic_istream<Char>& is,
×
70
        options_description const& desc, bool allow_unregistered)
71
    {
72
        std::set<std::string> allowed_options;
73

74
        std::vector<std::shared_ptr<option_description>> const& options =
75
            desc.options();
×
76
        for (auto const& option : options)
×
77
        {
78
            option_description const& d = *option;
79

80
            if (d.long_name().empty())
×
81
                throw error("abbreviated option names are not permitted in "
×
82
                            "options configuration files");
83

84
            allowed_options.insert(d.long_name());
×
85
        }
86

87
        // Parser return char strings
88
        parsed_options result(&desc);
89
        std::copy(detail::basic_config_file_iterator<Char>(
×
90
                      is, allowed_options, allow_unregistered),
91
            detail::basic_config_file_iterator<Char>(),
×
92
            back_inserter(result.options));
93

94
        // Convert char strings into desired type.
95
        return basic_parsed_options<Char>(result);
×
96
    }
97

98
    template HPX_CORE_EXPORT basic_parsed_options<char> parse_config_file(
99
        std::basic_istream<char>& is, options_description const& desc,
100
        bool allow_unregistered);
101

102
    template HPX_CORE_EXPORT basic_parsed_options<wchar_t> parse_config_file(
103
        std::basic_istream<wchar_t>& is, options_description const& desc,
104
        bool allow_unregistered);
105

106
    template <typename Char>
107
    basic_parsed_options<Char> parse_config_file(char const* filename,
×
108
        options_description const& desc, bool allow_unregistered)
109
    {
110
        // Parser return char strings
111
        std::basic_ifstream<Char> strm(filename);
×
112
        if (!strm)
×
113
        {
114
            throw reading_file(filename);
×
115
        }
116

117
        basic_parsed_options<Char> result =
×
118
            parse_config_file(strm, desc, allow_unregistered);
119

120
        if (strm.bad())
×
121
        {
122
            throw reading_file(filename);
×
123
        }
124

125
        return result;
×
126
    }
×
127

128
    template HPX_CORE_EXPORT basic_parsed_options<char> parse_config_file(
129
        char const* filename, options_description const& desc,
130
        bool allow_unregistered);
131

132
    template HPX_CORE_EXPORT basic_parsed_options<wchar_t> parse_config_file(
133
        char const* filename, options_description const& desc,
134
        bool allow_unregistered);
135

136
    parsed_options parse_environment(options_description const& desc,
×
137
        std::function<std::string(std::string)> const& name_mapper)
138
    {
139
        parsed_options result(&desc);
140

141
#if defined(__FreeBSD__)
142
        char** env = freebsd_environ;
143
#else
144
        char** env = environ;
×
145
#endif
146
        for (environment_iterator i(env), e; i != e; ++i)
×
147
        {
148
            std::string option_name = name_mapper(i->first);
×
149

150
            if (!option_name.empty())
×
151
            {
152
                option n;
153
                n.string_key = HPX_MOVE(option_name);
×
154
                n.value.push_back(i->second);
×
155
                result.options.push_back(n);
×
156
            }
×
157
        }
158

159
        return result;
×
160
    }
161

162
    namespace detail {
163

164
        class prefix_name_mapper
×
165
        {
166
        public:
167
            explicit prefix_name_mapper(std::string const& prefix)
168
              : prefix(prefix)
169
            {
170
            }
171

172
            std::string operator()(std::string const& s) const
×
173
            {
174
                std::string result;
175
                if (s.find(prefix) == 0)
×
176
                {
177
                    for (std::string::size_type n = prefix.size(); n < s.size();
×
178
                        ++n)
179
                    {
180
                        // Intel-Win-7.1 does not understand push_back on
181
                        // string.
182
                        result += static_cast<char>(std::tolower(s[n]));
×
183
                    }
184
                }
185
                return result;
×
186
            }
187

188
        private:
189
            std::string prefix;
190
        };
191
    }    // namespace detail
192

193
    parsed_options parse_environment(
×
194
        options_description const& desc, std::string const& prefix)
195
    {
196
        return parse_environment(desc, detail::prefix_name_mapper(prefix));
×
197
    }
198

199
    parsed_options parse_environment(
×
200
        options_description const& desc, char const* prefix)
201
    {
202
        return parse_environment(desc, std::string(prefix));
×
203
    }
204
}    // namespace hpx::program_options
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc