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

STEllAR-GROUP / hpx / #856

28 Dec 2022 02:00AM UTC coverage: 86.602% (+0.05%) from 86.55%
#856

push

StellarBot
Merge #6119

6119: Update CMakeLists.txt r=hkaiser a=khuck

updating the default APEX version


Co-authored-by: Kevin Huck <khuck@cs.uoregon.edu>

174566 of 201573 relevant lines covered (86.6%)

1876093.78 hits per line

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

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

7
#include <hpx/program_options/config.hpp>
8
#include <hpx/assert.hpp>
9
#include <hpx/program_options/detail/config_file.hpp>
10
#include <hpx/program_options/detail/convert.hpp>
11
#include <hpx/program_options/errors.hpp>
12

13
#include <fstream>
14
#include <set>
15
#include <string>
16

17
namespace hpx::program_options::detail {
18

19
    common_config_file_iterator::common_config_file_iterator(
7✔
20
        std::set<std::string> const& allowed_options, bool allow_unregistered)
21
      : allowed_options(allowed_options)
7✔
22
      , m_allow_unregistered(allow_unregistered)
7✔
23
    {
14✔
24
        for (auto const& allowed_option : allowed_options)
57✔
25
        {
26
            add_option(allowed_option.c_str());
50✔
27
        }
28
    }
7✔
29

30
    void common_config_file_iterator::add_option(char const* name)
50✔
31
    {
32
        std::string s(name);
50✔
33
        HPX_ASSERT(!s.empty());
50✔
34
        if (*s.rbegin() == '*')
50✔
35
        {
36
            s.resize(s.size() - 1);
2✔
37
            bool bad_prefixes(false);
2✔
38
            // If 's' is a prefix of one of allowed suffix, then
39
            // lower_bound will return that element.
40
            // If some element is prefix of 's', then lower_bound will
41
            // return the next element.
42
            std::set<std::string>::iterator i = allowed_prefixes.lower_bound(s);
2✔
43
            if (i != allowed_prefixes.end())
2✔
44
            {
45
                if (i->find(s) == 0)
×
46
                    bad_prefixes = true;
×
47
            }
×
48
            if (i != allowed_prefixes.begin())
2✔
49
            {
50
                --i;
×
51
                if (s.find(*i) == 0)
×
52
                    bad_prefixes = true;
×
53
            }
×
54
            if (bad_prefixes)
2✔
55
                throw error("options '" + std::string(name) + "' and '" + *i +
×
56
                    "*' will both match the same arguments from the "
57
                    "configuration file");
58
            allowed_prefixes.insert(s);
2✔
59
        }
2✔
60
    }
50✔
61

62
    namespace {
63

64
        inline std::string trim_ws(std::string const& s)
163✔
65
        {
66
            std::string::size_type n, n2;
67
            n = s.find_first_not_of(" \t\r\n");
163✔
68
            if (n == std::string::npos)
163✔
69
                return std::string();
12✔
70
            else
71
            {
72
                n2 = s.find_last_not_of(" \t\r\n");
151✔
73
                return s.substr(n, n2 - n + 1);
151✔
74
            }
75
        }
163✔
76
    }    // namespace
77

78
    void common_config_file_iterator::get()
56✔
79
    {
80
        std::string s;
56✔
81
        std::string::size_type n;
82
        bool found = false;
56✔
83

84
        while (this->getline(s))
72✔
85
        {
86
            // strip '#' comments and whitespace
87
            if ((n = s.find('#')) != std::string::npos)
65✔
88
                s = s.substr(0, n);
3✔
89
            s = trim_ws(s);
65✔
90

91
            if (!s.empty())
65✔
92
            {
93
                // Handle section name
94
                if (*s.begin() == '[' && *s.rbegin() == ']')
56✔
95
                {
96
                    m_prefix = s.substr(1, s.size() - 2);
7✔
97
                    if (*m_prefix.rbegin() != '.')
7✔
98
                        m_prefix += '.';
7✔
99
                }
7✔
100
                else if ((n = s.find('=')) != std::string::npos)
49✔
101
                {
102
                    std::string name = m_prefix + trim_ws(s.substr(0, n));
49✔
103
                    std::string value = trim_ws(s.substr(n + 1));
49✔
104

105
                    bool registered = allowed_option(name);
49✔
106
                    if (!registered && !m_allow_unregistered)
49✔
107
                        throw unknown_option(name);
×
108

109
                    found = true;
49✔
110
                    this->value().string_key = name;
49✔
111
                    this->value().value.clear();
49✔
112
                    this->value().value.push_back(value);
49✔
113
                    this->value().unregistered = !registered;
49✔
114
                    this->value().original_tokens.clear();
49✔
115
                    this->value().original_tokens.push_back(name);
49✔
116
                    this->value().original_tokens.push_back(value);
49✔
117
                    break;
118
                }
49✔
119
                else
120
                {
121
                    throw invalid_config_file_syntax(
×
122
                        s, invalid_syntax::unrecognized_line);
123
                }
124
            }
7✔
125
        }
126
        if (!found)
56✔
127
            found_eof();
7✔
128
    }
56✔
129

130
    bool common_config_file_iterator::allowed_option(std::string const& s) const
49✔
131
    {
132
        std::set<std::string>::const_iterator i = allowed_options.find(s);
49✔
133
        if (i != allowed_options.end())
49✔
134
            return true;
41✔
135
        // If s is "pa" where "p" is allowed prefix then
136
        // lower_bound should find the element after "p".
137
        // This depends on 'allowed_prefixes' invariant.
138
        i = allowed_prefixes.lower_bound(s);
8✔
139
        if (i != allowed_prefixes.begin() && s.find(*--i) == 0)
8✔
140
            return true;
2✔
141
        return false;
6✔
142
    }
49✔
143

144
#if defined(__COMO_VERSION__) && __COMO_VERSION__ >= 4303 ||                   \
145
    (defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION >= 741)
146
    template <>
147
    bool basic_config_file_iterator<wchar_t>::getline(std::string& s)
148
    {
149
        std::wstring ws;
150
        // On Comeau, using two-argument version causes
151
        // call to some internal function with std::wstring, and '\n'
152
        // (not L'\n') and compile can't resolve that call.
153

154
        if (std::getline(*is, ws, L'\n'))
155
        {
156
            s = to_utf8(ws);
157
            return true;
158
        }
159
        else
160
        {
161
            return false;
162
        }
163
    }
164
#endif
165
}    // namespace hpx::program_options::detail
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