YARP
Yet Another Robot Platform
StringInputStream.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_OS_STRINGINPUTSTREAM_H
8 #define YARP_OS_STRINGINPUTSTREAM_H
9 
10 #include <yarp/os/Bytes.h>
11 #include <yarp/os/InputStream.h>
12 
13 #include <string>
14 
15 namespace yarp::os {
16 
21 {
22 public:
23  using InputStream::read;
24 
26  {
27  at = 0;
28  data = "";
29  }
30 
31  void reset()
32  {
33  at = 0;
34  data = "";
35  }
36 
37  void reset(const std::string& str)
38  {
39  at = 0;
40  data = str;
41  }
42 
43  void add(const std::string& txt)
44  {
45  data += txt;
46  }
47 
48  void add(const Bytes& b)
49  {
50  for (size_t i = 0; i < b.length(); i++) {
51  data += b.get()[i];
52  }
53  }
54 
56  {
57  char* base = b.get();
58  size_t space = b.length();
59  yarp::conf::ssize_t ct = 0;
60  for (size_t i = 0; i < space; i++) {
61  if (at < data.length()) {
62  base[i] = data[at];
63  at++;
64  ct++;
65  }
66  }
67  return ct;
68  }
69 
70  void close() override
71  {
72  }
73 
74  virtual std::string toString() const
75  {
76  return data;
77  }
78 
79  bool isOk() const override
80  {
81  return true;
82  }
83 
84 private:
86  unsigned int at;
87 };
88 
89 } // namespace yarp::os
90 
91 #endif // YARP_OS_STRINGINPUTSTREAM_H
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
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:25
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:20
An InputStream that reads from a string.
yarp::conf::ssize_t read(Bytes &b) override
Read a block of data from the stream.
void add(const Bytes &b)
void add(const std::string &txt)
bool isOk() const override
Check if the stream is ok or in an error state.
virtual std::string toString() const
void reset(const std::string &str)
void close() override
Terminate the stream.
::ssize_t ssize_t
Definition: numeric.h:86
An interface to the operating system, including Port based communication.
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(x)
Suppress MSVC C4251 warning for the declaration.
Definition: system.h:338