YARP
Yet Another Robot Platform
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 
6 #include <yarp/os/InputStream.h>
7 
8 #include <yarp/os/Bytes.h>
9 #include <yarp/os/ManagedBytes.h>
10 
11 using namespace yarp::os;
12 
13 InputStream::InputStream() = default;
14 InputStream::~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 
38 {
39  return read(b);
40 }
41 
43 {
44 }
45 
46 bool InputStream::setReadTimeout(double timeout)
47 {
48  YARP_UNUSED(timeout);
49  return false;
50 }
51 
52 // slow implementation - only relevant for textmode operation
53 
54 std::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 {
98  yarp::conf::ssize_t off = 0;
99  yarp::conf::ssize_t fullLen = b.length();
100  yarp::conf::ssize_t remLen = fullLen;
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 
123 bool InputStream::setReadEnvelopeCallback(readEnvelopeCallbackType callback, void* data)
124 {
125  YARP_UNUSED(callback);
126  YARP_UNUSED(data);
127  return false;
128 }
A simple abstraction for a block of bytes.
Definition: Bytes.h:25
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.
Definition: InputStream.cpp:20
virtual yarp::conf::ssize_t partialRead(yarp::os::Bytes &b)
Like read, but solicit partial responses.
Definition: InputStream.cpp:37
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.
Definition: InputStream.cpp:96
std::string readLine(const char terminal='\n', bool *success=nullptr)
Read a block of text terminated with a specific marker (or EOF).
Definition: InputStream.cpp:54
InputStream()
Constructor.
virtual bool setReadTimeout(double timeout)
Set activity timeout.
Definition: InputStream.cpp:46
virtual void check()
Perform maintenance actions, if needed.
Definition: InputStream.cpp:16
virtual ~InputStream()
Destructor.
virtual void interrupt()
Interrupt the stream.
Definition: InputStream.cpp:42
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
Definition: ManagedBytes.h:22
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