YARP
Yet Another Robot Platform
InputStream.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include <yarp/os/InputStream.h>
10 
11 #include <yarp/os/Bytes.h>
12 #include <yarp/os/ManagedBytes.h>
13 
14 using namespace yarp::os;
15 
16 InputStream::InputStream() = default;
17 InputStream::~InputStream() = default;
18 
20 {
21 }
22 
24 {
25  unsigned char result;
26  yarp::os::Bytes bytes(reinterpret_cast<char*>(&result), 1);
27  yarp::conf::ssize_t ct = read(bytes);
28  if (ct < 1) {
29  return -1;
30  }
31  return static_cast<int>(result);
32 }
33 
35 {
36  yarp::os::Bytes bytes(b.get() + offset, len);
37  return read(bytes);
38 }
39 
41 {
42  return read(b);
43 }
44 
46 {
47 }
48 
49 bool InputStream::setReadTimeout(double timeout)
50 {
51  YARP_UNUSED(timeout);
52  return false;
53 }
54 
55 // slow implementation - only relevant for textmode operation
56 
57 std::string InputStream::readLine(const char terminal, bool* success)
58 {
59  std::string buf;
60  bool done = false;
61  int esc = 0;
62  if (success != nullptr) {
63  *success = true;
64  }
65  while (!done) {
66  int v = read();
67  if (v < 0) {
68  if (success != nullptr) {
69  *success = false;
70  }
71  return {};
72  }
73  char ch = (char)v;
74  if (v == '\\') {
75  esc++;
76  }
77  if (v != 0 && v != '\r' && v != '\n') {
78  if (v != '\\' || esc >= 2) {
79  while (esc != 0) {
80  buf += '\\';
81  esc--;
82  }
83  }
84  if (v != '\\') {
85  buf += ch;
86  }
87  }
88  if (ch == terminal) {
89  if (esc == 0) {
90  done = true;
91  } else {
92  esc = 0;
93  }
94  }
95  }
96  return buf;
97 }
98 
100 {
101  yarp::conf::ssize_t off = 0;
102  yarp::conf::ssize_t fullLen = b.length();
103  yarp::conf::ssize_t remLen = fullLen;
104  yarp::conf::ssize_t result = 1;
105  while (result > 0 && remLen > 0) {
106  result = read(b, off, remLen);
107  if (result > 0) {
108  remLen -= result;
109  off += result;
110  }
111  }
112  return (result <= 0) ? -1 : fullLen;
113 }
114 
116 {
117  if (len < 100) {
118  char buf[100];
119  Bytes b(buf, len);
120  return readFull(b);
121  }
122  ManagedBytes b(len);
123  return readFull(b.bytes());
124 }
125 
126 bool InputStream::setReadEnvelopeCallback(readEnvelopeCallbackType callback, void* data)
127 {
128  YARP_UNUSED(callback);
129  YARP_UNUSED(data);
130  return false;
131 }
yarp::os::InputStream::~InputStream
virtual ~InputStream()
Destructor.
yarp::os::InputStream::interrupt
virtual void interrupt()
Interrupt the stream.
Definition: InputStream.cpp:45
yarp::os::InputStream::InputStream
InputStream()
Constructor.
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
yarp::os::InputStream::partialRead
virtual yarp::conf::ssize_t partialRead(yarp::os::Bytes &b)
Like read, but solicit partial responses.
Definition: InputStream.cpp:40
yarp::os::InputStream::check
virtual void check()
Perform maintenance actions, if needed.
Definition: InputStream.cpp:19
yarp::os::ManagedBytes::bytes
const Bytes & bytes() const
Definition: ManagedBytes.cpp:177
ManagedBytes.h
yarp::os::ManagedBytes
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
Definition: ManagedBytes.h:25
yarp::os::Bytes::get
const char * get() const
Definition: Bytes.cpp:30
yarp::os::Bytes::length
size_t length() const
Definition: Bytes.cpp:25
yarp::conf::ssize_t
::ssize_t ssize_t
Definition: numeric.h:60
yarp::os::InputStream::readLine
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:57
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
yarp::os::InputStream::setReadTimeout
virtual bool setReadTimeout(double timeout)
Set activity timeout.
Definition: InputStream.cpp:49
yarp::os::InputStream::readFull
yarp::conf::ssize_t readFull(Bytes &b)
Keep reading until buffer is full.
Definition: InputStream.cpp:99
yarp::os::InputStream::read
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:23
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
Bytes.h
InputStream.h
yarp::os::InputStream::readDiscard
yarp::conf::ssize_t readDiscard(size_t len)
Read and discard a fixed number of bytes.
Definition: InputStream.cpp:115
yarp::os::InputStream::setReadEnvelopeCallback
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...
Definition: InputStream.cpp:126