[go: up one dir, main page]

File: utils.cpp

package info (click to toggle)
drbd-utils 9.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,388 kB
  • sloc: ansic: 43,698; xml: 15,968; cpp: 7,783; sh: 3,699; makefile: 1,353; perl: 353
file content (93 lines) | stat: -rw-r--r-- 2,182 bytes parent folder | download | duplicates (2)
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
#include <utils.h>

// @throws NumberFormatException
uint8_t NumberParser::parse_uint8(std::string& number_str)
{
    uint8_t MAX = 0xFF;
    unsigned long long value = parse_unsigned(number_str);
    if (value > MAX)
    {
        throw NumberFormatException();
    }
    return static_cast<uint8_t> (value);
}

// @throws NumberFormatException
uint16_t NumberParser::parse_uint16(std::string& number_str)
{
    uint16_t MAX = ~(static_cast<uint16_t> (0));
    unsigned long long value = parse_unsigned(number_str);
    if (value > MAX)
    {
        throw NumberFormatException();
    }
    return static_cast<uint16_t> (value);
}

// @throws NumberFormatException
int32_t NumberParser::parse_int32(std::string& number_str)
{
    int32_t MAX = 0x7FFFFFFF;
    int32_t MIN = 0xFFFFFFFF;
    signed long long value = parse_signed(number_str);
    if (value < MIN || value > MAX)
    {
        throw NumberFormatException();
    }
    return static_cast<int32_t> (value);
}

// @throws NumberFormatException
unsigned long long NumberParser::parse_unsigned(std::string& number_str)
{
    const char* number_buf = number_str.c_str();
    char* parse_end {nullptr};
    unsigned long long value = strtoull(number_buf, &parse_end, 10);
    if (*parse_end != '\0')
    {
        throw NumberFormatException();
    }
    return value;
}

// @throws NumberFormatException
signed long long NumberParser::parse_signed(std::string& number_str)
{
    const char* number_buf = number_str.c_str();
    char* parse_end {nullptr};
    signed long long value = strtoll(number_buf, &parse_end, 10);
    if (*parse_end != '\0')
    {
        throw NumberFormatException();
    }
    return value;
}

TermSize::TermSize()
{
    term_size.ws_col    = 0;
    term_size.ws_row    = 0;
    term_size.ws_xpixel = 0;
    term_size.ws_ypixel = 0;
}

bool TermSize::probe_terminal_size()
{
    valid = (ioctl(1, TIOCGWINSZ, &term_size) == 0);
    return valid;
}

bool TermSize::is_valid() const
{
    return valid;
}

uint16_t TermSize::get_size_x() const
{
    return static_cast<uint16_t> (term_size.ws_col);
}

uint16_t TermSize::get_size_y() const
{
    return static_cast<uint16_t> (term_size.ws_row);
}