SNode.C
Loading...
Searching...
No Matches
KeyboardReader.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "KeyboardReader.h"
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include "utils/Timeval.h"
25
26#include <iostream>
27#include <stdexcept>
28#include <string>
29#include <sys/types.h>
30#include <unistd.h>
31
32#endif // DOXYGEN_SHOULD_SKIP_THIS
33
34namespace apps::towercalculator {
35
36 KeyboardReader::KeyboardReader(const std::function<void(long)>& cb)
37 : core::eventreceiver::ReadEventReceiver("KeyboardReader", 0)
38 , callBack(cb) {
39 if (!enable(STDIN_FILENO)) {
40 std::cout << "KeyboardReader not activated";
41 }
42 }
43
45 std::cout << "ReadEvent" << std::endl;
46 /*
47 long value;
48
49 std::cin >> long;
50 */
51
52 char buffer[256];
53 const ssize_t ret = read(STDIN_FILENO, buffer, 256);
54
55 if (ret > 0) {
56 buffer[ret] = 0;
57 try {
58 long value = 0;
59 value = std::stol(buffer);
60
61 std::cout << "Value = " << value << std::endl;
62
63 callBack(value);
64 } catch (std::invalid_argument& e) {
65 std::cout << e.what() << ": "
66 << "no conversion possible: input = " << buffer << std::endl;
67 }
68 }
69 }
70
72 // delete this; // do not delete
73 }
74
75} // namespace apps::towercalculator
KeyboardReader(const std::function< void(long)> &cb)