File: Arguments.cpp

package info (click to toggle)
pentobi 29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,892 kB
  • sloc: cpp: 25,719; javascript: 875; xml: 40; makefile: 13; sh: 6
file content (76 lines) | stat: -rw-r--r-- 2,030 bytes parent folder | download | duplicates (3)
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
//-----------------------------------------------------------------------------
/** @file libboardgame_gtp/Arguments.cpp
    @author Markus Enzenberger
    @copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------

#include "Arguments.h"

#include <cctype>

namespace libboardgame_gtp {

//-----------------------------------------------------------------------------

void Arguments::check_size(unsigned n) const
{
    if (get_size() == n)
        return;
    if (n == 0)
        throw Failure("no arguments allowed");
    if (n == 1)
        throw Failure("command needs one argument");
    ostringstream msg;
    msg << "command needs " << n << " arguments";
    throw Failure(msg.str());
}

void Arguments::check_size_less_equal(unsigned n) const
{
    if (get_size() <= n)
        return;
    if (n == 1)
        throw Failure("command needs at most one argument");
    ostringstream msg;
    msg << "command needs at most " << n << " arguments";
    throw Failure(msg.str());
}

template<>
string_view Arguments::get(unsigned i) const
{
    if (i < get_size())
        return m_line.get_element(m_line.get_idx_name() + i + 1);
    ostringstream msg;
    msg << "missing argument " << (i + 1);
    throw Failure(msg.str());
}

template<>
string Arguments::get(unsigned i) const
{
    auto s = get(i);
    return {&*s.begin(), s.size()};
}

string Arguments::get_tolower(unsigned i) const
{
    auto value = get<string>(i);
    for (auto& c : value)
        c = static_cast<char>(tolower(c));
    return value;
}

string_view Arguments::get_remaining_line(unsigned i) const
{
    if (i < get_size())
        return m_line.get_trimmed_line_after_elem(m_line.get_idx_name() + i
                                                  + 1);
    ostringstream msg;
    msg << "missing argument " << (i + 1);
    throw Failure(msg.str());
}

//-----------------------------------------------------------------------------

} // namespace libboardgame_gtp