WPILibC++  2020.3.2-60-g3011ebe
raw_istream.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef WPIUTIL_WPI_RAW_ISTREAM_H_
9 #define WPIUTIL_WPI_RAW_ISTREAM_H_
10 
11 #include <stdint.h>
12 
13 #include <algorithm>
14 #include <cstddef>
15 #include <string>
16 #include <system_error>
17 #include <vector>
18 
19 #include "wpi/ArrayRef.h"
20 #include "wpi/SmallVector.h"
21 #include "wpi/StringRef.h"
22 #include "wpi/Twine.h"
23 
24 namespace wpi {
25 
26 class raw_istream {
27  public:
28  raw_istream() = default;
29  virtual ~raw_istream() = default;
30 
31  raw_istream& read(char& c) {
32  read_impl(&c, 1);
33  return *this;
34  }
35 
36  raw_istream& read(unsigned char& c) {
37  read_impl(&c, 1);
38  return *this;
39  }
40 
41  raw_istream& read(signed char& c) {
42  read_impl(&c, 1);
43  return *this;
44  }
45 
46  raw_istream& read(void* data, size_t len) {
47  read_impl(data, len);
48  return *this;
49  }
50 
51  size_t readsome(void* data, size_t len) {
52  size_t readlen = (std::min)(in_avail(), len);
53  if (readlen == 0) return 0;
54  read_impl(data, readlen);
55  return m_read_count;
56  }
57 
58  raw_istream& readinto(SmallVectorImpl<char>& buf, size_t len) {
59  size_t old_size = buf.size();
60  buf.append(len, 0);
61  read_impl(&buf[old_size], len);
62  buf.resize(old_size + m_read_count);
63  return *this;
64  }
65 
66  raw_istream& readinto(SmallVectorImpl<uint8_t>& buf, size_t len) {
67  size_t old_size = buf.size();
68  buf.append(len, 0);
69  read_impl(&buf[old_size], len);
70  buf.resize(old_size + m_read_count);
71  return *this;
72  }
73 
74  raw_istream& readinto(std::vector<char>& buf, size_t len) {
75  size_t old_size = buf.size();
76  buf.insert(buf.end(), len, 0);
77  read_impl(&buf[old_size], len);
78  buf.resize(old_size + m_read_count);
79  return *this;
80  }
81 
82  raw_istream& readinto(std::vector<uint8_t>& buf, size_t len) {
83  size_t old_size = buf.size();
84  buf.insert(buf.end(), len, 0);
85  read_impl(&buf[old_size], len);
86  buf.resize(old_size + m_read_count);
87  return *this;
88  }
89 
90  raw_istream& readinto(std::string& buf, size_t len) {
91  size_t old_size = buf.size();
92  buf.insert(buf.end(), len, 0);
93  read_impl(&buf[old_size], len);
94  buf.resize(old_size + m_read_count);
95  return *this;
96  }
97 
98  // Read a line from an input stream (up to a maximum length).
99  // The returned buffer will contain the trailing \n (unless the maximum length
100  // was reached). \r's are stripped from the buffer.
101  // @param buf Buffer for output
102  // @param maxLen Maximum length
103  // @return Line
104  StringRef getline(SmallVectorImpl<char>& buf, int maxLen);
105 
106  virtual void close() = 0;
107 
108  // Number of bytes available to read without potentially blocking.
109  // Note this can return zero even if there are bytes actually available to
110  // read.
111  virtual size_t in_avail() const = 0;
112 
113  // Return the number of bytes read by the last read operation.
114  size_t read_count() const { return m_read_count; }
115 
116  bool has_error() const { return m_error; }
117  void clear_error() { m_error = false; }
118 
119  raw_istream(const raw_istream&) = delete;
120  raw_istream& operator=(const raw_istream&) = delete;
121 
122  protected:
123  void error_detected() { m_error = true; }
124  void set_read_count(size_t count) { m_read_count = count; }
125 
126  private:
127  virtual void read_impl(void* data, size_t len) = 0;
128 
129  bool m_error = false;
130  size_t m_read_count = 0;
131 };
132 
133 class raw_mem_istream : public raw_istream {
134  public:
135  // not const as we don't want to allow temporaries
136  explicit raw_mem_istream(std::string& str)
137  : raw_mem_istream(str.data(), str.size()) {}
138  explicit raw_mem_istream(ArrayRef<char> mem)
139  : raw_mem_istream(mem.data(), mem.size()) {}
140  explicit raw_mem_istream(ArrayRef<uint8_t> mem)
141  : raw_mem_istream(reinterpret_cast<const char*>(mem.data()), mem.size()) {
142  }
143  explicit raw_mem_istream(const char* str)
144  : m_cur(str), m_left(std::strlen(str)) {}
145  raw_mem_istream(const char* mem, size_t len) : m_cur(mem), m_left(len) {}
146  void close() override;
147  size_t in_avail() const override;
148 
149  private:
150  void read_impl(void* data, size_t len) override;
151 
152  const char* m_cur;
153  size_t m_left;
154 };
155 
156 class raw_fd_istream : public raw_istream {
157  public:
158  raw_fd_istream(const Twine& filename, std::error_code& ec,
159  size_t bufSize = 4096);
160  raw_fd_istream(int fd, bool shouldClose, size_t bufSize = 4096);
161  ~raw_fd_istream() override;
162  void close() override;
163  size_t in_avail() const override;
164 
165  private:
166  void read_impl(void* data, size_t len) override;
167 
168  char* m_buf;
169  char* m_cur;
170  char* m_end;
171  size_t m_bufSize;
172  int m_fd;
173  bool m_shouldClose;
174 };
175 
176 } // namespace wpi
177 
178 #endif // WPIUTIL_WPI_RAW_ISTREAM_H_
wpi::raw_fd_istream
Definition: raw_istream.h:156
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
wpi::SmallVectorImpl< char >
wpi::raw_istream
Definition: raw_istream.h:26
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: Endian.h:31
wpi::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
wpi::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:145
wpi::raw_mem_istream
Definition: raw_istream.h:133
wpi::SmallVectorImpl::append
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:403
wpi::count
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1092
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85