1 | // Copyright (C) 2016 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #include "stdinlistener.h" |
5 | |
6 | #include "tracer.h" |
7 | |
8 | #include <stdio.h> |
9 | |
10 | QT_BEGIN_NAMESPACE |
11 | |
12 | StdInListener::StdInListener(QObject *parent) |
13 | : QSocketNotifier(fileno(stdin), QSocketNotifier::Read, parent) |
14 | { |
15 | TRACE_OBJ |
16 | connect(sender: this, signal: &QSocketNotifier::activated, |
17 | context: this, slot: &StdInListener::receivedData); |
18 | } |
19 | |
20 | StdInListener::~StdInListener() |
21 | { |
22 | TRACE_OBJ |
23 | } |
24 | |
25 | void StdInListener::start() |
26 | { |
27 | setEnabled(true); |
28 | } |
29 | |
30 | void StdInListener::receivedData() |
31 | { |
32 | TRACE_OBJ |
33 | QByteArray ba; |
34 | while (true) { |
35 | const int c = getc(stdin); |
36 | if (c == EOF) { |
37 | setEnabled(false); |
38 | break; |
39 | } |
40 | if (c == '\0') |
41 | break; |
42 | if (c) |
43 | ba.append(c: char(c)); |
44 | if (c == '\n') |
45 | break; |
46 | } |
47 | emit receivedCommand(cmd: QString::fromLocal8Bit(ba)); |
48 | } |
49 | |
50 | QT_END_NAMESPACE |
51 |