YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
InputStream.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
8#include <yarp/os/Bytes.h>
10
11using namespace yarp::os;
12
13InputStream::InputStream() = default;
15
17{
18}
19
21{
22 unsigned char result;
23 yarp::os::Bytes bytes(reinterpret_cast<char*>(&result), 1);
24 yarp::conf::ssize_t ct = read(bytes);
25 if (ct < 1) {
26 return -1;
27 }
28 return static_cast<int>(result);
29}
30
32{
33 yarp::os::Bytes bytes(b.get() + offset, len);
34 return read(bytes);
35}
36
41
45
46bool InputStream::setReadTimeout(double timeout)
47{
48 YARP_UNUSED(timeout);
49 return false;
51
52// slow implementation - only relevant for textmode operation
53
54std::string InputStream::readLine(const char terminal, bool* success)
55{
56 std::string buf;
57 bool done = false;
58 int esc = 0;
59 if (success != nullptr) {
60 *success = true;
61 }
62 while (!done) {
63 int v = read();
64 if (v < 0) {
65 if (success != nullptr) {
66 *success = false;
67 }
68 return {};
69 }
70 char ch = (char)v;
71 if (v == '\\') {
72 esc++;
73 }
74 if (v != 0 && v != '\r' && v != '\n') {
75 if (v != '\\' || esc >= 2) {
76 while (esc != 0) {
77 buf += '\\';
78 esc--;
79 }
80 }
81 if (v != '\\') {
82 buf += ch;
83 }
84 }
85 if (ch == terminal) {
86 if (esc == 0) {
87 done = true;
88 } else {
89 esc = 0;
90 }
91 }
92 }
93 return buf;
94}
95
97{
101 yarp::conf::ssize_t result = 1;
102 while (result > 0 && remLen > 0) {
103 result = read(b, off, remLen);
104 if (result > 0) {
105 remLen -= result;
106 off += result;
107 }
108 }
109 return (result <= 0) ? -1 : fullLen;
110}
111
113{
114 if (len < 100) {
115 char buf[100];
116 Bytes b(buf, len);
117 return readFull(b);
118 }
119 ManagedBytes b(len);
120 return readFull(b.bytes());
121}
122
123bool InputStream::setReadEnvelopeCallback(readEnvelopeCallbackType callback, void* data)
124{
125 YARP_UNUSED(callback);
126 YARP_UNUSED(data);
127 return false;
128}
A mini-server for performing network communication in the background.
A simple abstraction for a block of bytes.
Definition Bytes.h:24
size_t length() const
Definition Bytes.cpp:22
const char * get() const
Definition Bytes.cpp:27
yarp::conf::ssize_t readDiscard(size_t len)
Read and discard a fixed number of bytes.
virtual int read()
Read and return a single byte.
virtual yarp::conf::ssize_t partialRead(yarp::os::Bytes &b)
Like read, but solicit partial responses.
virtual bool setReadEnvelopeCallback(readEnvelopeCallbackType callback, void *data)
Install a callback that the InputStream will have to call when the envelope is read from a message in...
yarp::conf::ssize_t readFull(Bytes &b)
Keep reading until buffer is full.
std::string readLine(const char terminal='\n', bool *success=nullptr)
Read a block of text terminated with a specific marker (or EOF).
InputStream()
Constructor.
virtual bool setReadTimeout(double timeout)
Set activity timeout.
virtual void check()
Perform maintenance actions, if needed.
virtual ~InputStream()
Destructor.
virtual void interrupt()
Interrupt the stream.
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
const Bytes & bytes() const
::ssize_t ssize_t
Definition numeric.h:86
An interface to the operating system, including Port based communication.
#define YARP_UNUSED(var)
Definition api.h:162