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 {
16 namespace os {
17 
22 {
23 public:
24  using InputStream::read;
25 
27  {
28  at = 0;
29  data = "";
30  }
31 
32  void reset()
33  {
34  at = 0;
35  data = "";
36  }
37 
38  void reset(const std::string& str)
39  {
40  at = 0;
41  data = str;
42  }
43 
44  void add(const std::string& txt)
45  {
46  data += txt;
47  }
48 
49  void add(const Bytes& b)
50  {
51  for (size_t i = 0; i < b.length(); i++) {
52  data += b.get()[i];
53  }
54  }
55 
57  {
58  char* base = b.get();
59  size_t space = b.length();
60  yarp::conf::ssize_t ct = 0;
61  for (size_t i = 0; i < space; i++) {
62  if (at < data.length()) {
63  base[i] = data[at];
64  at++;
65  ct++;
66  }
67  }
68  return ct;
69  }
70 
71  void close() override
72  {
73  }
74 
75  virtual std::string toString() const
76  {
77  return data;
78  }
79 
80  bool isOk() const override
81  {
82  return true;
83  }
84 
85 private:
87  unsigned int at;
88 };
89 
90 } // namespace os
91 } // namespace yarp
92 
93 #endif // YARP_OS_STRINGINPUTSTREAM_H
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
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:26
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
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(x)
Suppress MSVC C4251 warning for the declaration.
Definition: system.h:336