summaryrefslogtreecommitdiff
path: root/src/protocol/input.cc
blob: a44ee3ec999188d5b8722938d595eb1c93b11977 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// SPDX-License-Identifier: BSD-3-Clause

#include <stdexcept>
#include <sstream>
#include <vector>
#include <string>

#ifdef INVERTERCTL
#include <getopt.h>
#endif

#include "../util.h"
#include "input.h"

namespace protocol {

#ifdef INVERTERCTL
void get_args(CommandInput* input, std::vector<std::string>& arguments, size_t count) {
    for (size_t i = 0; i < count; i++) {
        if (optind < input->argc && *input->argv[optind] != '-')
            arguments.emplace_back(input->argv[optind++]);
        else {
            std::ostringstream error;
            error << "this command requires " << count << " argument";
            if (count > 1)
                error << "s";
            throw std::invalid_argument(error.str());
        }
    }
}
#endif

#ifdef INVERTERD
void get_args(CommandInput* input, std::vector<std::string>& arguments, size_t count) {
    if (input->argv->size() < count) {
        std::ostringstream error;
        error << "this command requires " << count << " argument";
        if (count > 1)
            error << "s";
        throw std::invalid_argument(error.str());
    }

    for (size_t i = 0; i < count; i++)
        arguments.emplace_back((*input->argv)[i]);
}
#endif

void validate_date_args(const std::string* ys, const std::string* ms, const std::string* ds) {
    static const std::string err_year = "invalid year";
    static const std::string err_month = "invalid month";
    static const std::string err_day = "invalid day";

    int y, m = 0, d = 0;

    // validate year
    if (!is_numeric(*ys) || ys->size() != 4)
        throw std::invalid_argument(err_year);

    y = std::stoi(*ys);
    if (y < 2000 || y > 2099)
        throw std::invalid_argument(err_year);

    // validate month
    if (ms != nullptr) {
        if (!is_numeric(*ms) || ms->size() > 2)
            throw std::invalid_argument(err_month);

        m = std::stoi(*ms);
        if (m < 1 || m > 12)
            throw std::invalid_argument(err_month);
    }

    // validate day
    if (ds != nullptr) {
        if (!is_numeric(*ds) || ds->size() > 2)
            throw std::invalid_argument(err_day);

        d = std::stoi(*ds);
        if (d < 1 || d > 31)
            throw std::invalid_argument(err_day);
    }

    if (y != 0 && m != 0 && d != 0) {
        if (!is_date_valid(y, m, d))
            throw std::invalid_argument("invalid date");
    }
}

void validate_time_args(const std::string* hs, const std::string* ms, const std::string* ss) {
    static const std::string err_hour = "invalid hour";
    static const std::string err_minute = "invalid minute";
    static const std::string err_second = "invalid second";

    unsigned h, m, s;

    if (!is_numeric(*hs) || hs->size() > 2)
        throw std::invalid_argument(err_hour);

    h = static_cast<unsigned>(std::stoul(*hs));
    if (h > 23)
        throw std::invalid_argument(err_hour);

    if (!is_numeric(*ms) || ms->size() > 2)
        throw std::invalid_argument(err_minute);

    m = static_cast<unsigned>(std::stoul(*ms));
    if (m > 59)
        throw std::invalid_argument(err_minute);

    if (!is_numeric(*ss) || ss->size() > 2)
        throw std::invalid_argument(err_second);

    s = static_cast<unsigned>(std::stoul(*ss));
    if (s > 59)
        throw std::invalid_argument(err_second);
}

}