YARP
Yet Another Robot Platform
StringInputStream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_OS_STRINGINPUTSTREAM_H
11 #define YARP_OS_STRINGINPUTSTREAM_H
12 
13 #include <yarp/os/Bytes.h>
14 #include <yarp/os/InputStream.h>
15 
16 #include <string>
17 
18 namespace yarp {
19 namespace os {
20 
25 {
26 public:
27  using InputStream::read;
28 
30  {
31  at = 0;
32  data = "";
33  }
34 
35  void reset()
36  {
37  at = 0;
38  data = "";
39  }
40 
41  void reset(const std::string& str)
42  {
43  at = 0;
44  data = str;
45  }
46 
47  void add(const std::string& txt)
48  {
49  data += txt;
50  }
51 
52  void add(const Bytes& b)
53  {
54  for (size_t i = 0; i < b.length(); i++) {
55  data += b.get()[i];
56  }
57  }
58 
60  {
61  char* base = b.get();
62  size_t space = b.length();
63  yarp::conf::ssize_t ct = 0;
64  for (size_t i = 0; i < space; i++) {
65  if (at < data.length()) {
66  base[i] = data[at];
67  at++;
68  ct++;
69  }
70  }
71  return ct;
72  }
73 
74  void close() override
75  {
76  }
77 
78  virtual std::string toString() const
79  {
80  return data;
81  }
82 
83  bool isOk() const override
84  {
85  return true;
86  }
87 
88 private:
90  unsigned int at;
91 };
92 
93 } // namespace os
94 } // namespace yarp
95 
96 #endif // YARP_OS_STRINGINPUTSTREAM_H
yarp::os::StringInputStream::reset
void reset()
Definition: StringInputStream.h:35
YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(x)
Suppress MSVC C4251 warning for the declaration.
Definition: system.h:339
yarp::os::StringInputStream::add
void add(const Bytes &b)
Definition: StringInputStream.h:52
yarp::os::StringInputStream::add
void add(const std::string &txt)
Definition: StringInputStream.h:47
yarp::os::StringInputStream::close
void close() override
Terminate the stream.
Definition: StringInputStream.h:74
yarp::os::StringInputStream::reset
void reset(const std::string &str)
Definition: StringInputStream.h:41
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::StringInputStream
An InputStream that reads from a string.
Definition: StringInputStream.h:25
yarp::os::StringInputStream::read
yarp::conf::ssize_t read(Bytes &b) override
Read a block of data from the stream.
Definition: StringInputStream.h:59
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
yarp::os::InputStream::read
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:23
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
Bytes.h
InputStream.h
yarp::os::StringInputStream::toString
virtual std::string toString() const
Definition: StringInputStream.h:78
yarp::os::InputStream
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:29
yarp::os::StringInputStream::StringInputStream
StringInputStream()
Definition: StringInputStream.h:29
yarp::os::StringInputStream::isOk
bool isOk() const override
Check if the stream is ok or in an error state.
Definition: StringInputStream.h:83