WPILibC++ 2023.4.3-108-ge5452e3
raw_istream.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef WPIUTIL_WPI_RAW_ISTREAM_H_
6#define WPIUTIL_WPI_RAW_ISTREAM_H_
7
8#include <stdint.h>
9
10#include <algorithm>
11#include <cstddef>
12#include <span>
13#include <string>
14#include <string_view>
15#include <system_error>
16#include <vector>
17
18#include "wpi/SmallVector.h"
19
20namespace wpi {
21
23 public:
24 raw_istream() = default;
25 virtual ~raw_istream() = default;
26
27 raw_istream& read(char& c) {
28 read_impl(&c, 1);
29 return *this;
30 }
31
32 raw_istream& read(unsigned char& c) {
33 read_impl(&c, 1);
34 return *this;
35 }
36
37 raw_istream& read(signed char& c) {
38 read_impl(&c, 1);
39 return *this;
40 }
41
42 raw_istream& read(void* data, size_t len) {
43 read_impl(data, len);
44 return *this;
45 }
46
47 size_t readsome(void* data, size_t len) {
48 size_t readlen = (std::min)(in_avail(), len);
49 if (readlen == 0) {
50 return 0;
51 }
52 read_impl(data, readlen);
53 return m_read_count;
54 }
55
57 size_t old_size = buf.size();
58 buf.append(len, 0);
59 read_impl(&buf[old_size], len);
60 buf.resize(old_size + m_read_count);
61 return *this;
62 }
63
65 size_t old_size = buf.size();
66 buf.append(len, 0);
67 read_impl(&buf[old_size], len);
68 buf.resize(old_size + m_read_count);
69 return *this;
70 }
71
72 raw_istream& readinto(std::vector<char>& buf, size_t len) {
73 size_t old_size = buf.size();
74 buf.insert(buf.end(), len, 0);
75 read_impl(&buf[old_size], len);
76 buf.resize(old_size + m_read_count);
77 return *this;
78 }
79
80 raw_istream& readinto(std::vector<uint8_t>& buf, size_t len) {
81 size_t old_size = buf.size();
82 buf.insert(buf.end(), len, 0);
83 read_impl(&buf[old_size], len);
84 buf.resize(old_size + m_read_count);
85 return *this;
86 }
87
88 raw_istream& readinto(std::string& buf, size_t len) {
89 size_t old_size = buf.size();
90 buf.insert(buf.end(), len, 0);
91 read_impl(&buf[old_size], len);
92 buf.resize(old_size + m_read_count);
93 return *this;
94 }
95
96 // Read a line from an input stream (up to a maximum length).
97 // The returned buffer will contain the trailing \n (unless the maximum length
98 // was reached). \r's are stripped from the buffer.
99 // @param buf Buffer for output
100 // @param maxLen Maximum length
101 // @return Line
103
104 virtual void close() = 0;
105
106 // Number of bytes available to read without potentially blocking.
107 // Note this can return zero even if there are bytes actually available to
108 // read.
109 virtual size_t in_avail() const = 0;
110
111 // Return the number of bytes read by the last read operation.
112 size_t read_count() const { return m_read_count; }
113
114 bool has_error() const { return m_error; }
115 void clear_error() { m_error = false; }
116
117 raw_istream(const raw_istream&) = delete;
119
120 protected:
121 void error_detected() { m_error = true; }
122 void set_read_count(size_t count) { m_read_count = count; }
123
124 private:
125 virtual void read_impl(void* data, size_t len) = 0;
126
127 bool m_error = false;
128 size_t m_read_count = 0;
129};
130
132 public:
133 // not const as we don't want to allow temporaries
134 explicit raw_mem_istream(std::string& str)
135 : raw_mem_istream(str.data(), str.size()) {}
136 explicit raw_mem_istream(std::span<const char> mem)
137 : raw_mem_istream(mem.data(), mem.size()) {}
138 explicit raw_mem_istream(std::span<const uint8_t> mem)
139 : raw_mem_istream(reinterpret_cast<const char*>(mem.data()), mem.size()) {
140 }
141 explicit raw_mem_istream(const char* str)
142 : m_cur(str), m_left(std::strlen(str)) {}
143 raw_mem_istream(const char* mem, size_t len) : m_cur(mem), m_left(len) {}
144 void close() override;
145 size_t in_avail() const override;
146
147 private:
148 void read_impl(void* data, size_t len) override;
149
150 const char* m_cur;
151 size_t m_left;
152};
153
155 public:
156 raw_fd_istream(std::string_view filename, std::error_code& ec,
157 size_t bufSize = 4096);
158 raw_fd_istream(int fd, bool shouldClose, size_t bufSize = 4096);
159 ~raw_fd_istream() override;
160 void close() final;
161 size_t in_avail() const override;
162
163 private:
164 void read_impl(void* data, size_t len) override;
165
166 char* m_buf;
167 char* m_cur;
168 char* m_end;
169 size_t m_bufSize;
170 int m_fd;
171 bool m_shouldClose;
172};
173
174} // namespace wpi
175
176#endif // WPIUTIL_WPI_RAW_ISTREAM_H_
This file defines the SmallVector class.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:670
void resize(size_type N)
Definition: SmallVector.h:622
size_t size() const
Definition: SmallVector.h:78
Definition: raw_istream.h:154
raw_fd_istream(std::string_view filename, std::error_code &ec, size_t bufSize=4096)
void close() final
~raw_fd_istream() override
raw_fd_istream(int fd, bool shouldClose, size_t bufSize=4096)
size_t in_avail() const override
Definition: raw_istream.h:22
bool has_error() const
Definition: raw_istream.h:114
raw_istream & read(signed char &c)
Definition: raw_istream.h:37
virtual void close()=0
raw_istream & readinto(std::string &buf, size_t len)
Definition: raw_istream.h:88
size_t readsome(void *data, size_t len)
Definition: raw_istream.h:47
virtual size_t in_avail() const =0
raw_istream()=default
raw_istream & readinto(SmallVectorImpl< uint8_t > &buf, size_t len)
Definition: raw_istream.h:64
raw_istream & readinto(std::vector< uint8_t > &buf, size_t len)
Definition: raw_istream.h:80
virtual ~raw_istream()=default
std::string_view getline(SmallVectorImpl< char > &buf, int maxLen)
raw_istream & operator=(const raw_istream &)=delete
raw_istream & readinto(std::vector< char > &buf, size_t len)
Definition: raw_istream.h:72
raw_istream & read(void *data, size_t len)
Definition: raw_istream.h:42
raw_istream & readinto(SmallVectorImpl< char > &buf, size_t len)
Definition: raw_istream.h:56
void error_detected()
Definition: raw_istream.h:121
raw_istream(const raw_istream &)=delete
raw_istream & read(unsigned char &c)
Definition: raw_istream.h:32
raw_istream & read(char &c)
Definition: raw_istream.h:27
void set_read_count(size_t count)
Definition: raw_istream.h:122
void clear_error()
Definition: raw_istream.h:115
size_t read_count() const
Definition: raw_istream.h:112
Definition: raw_istream.h:131
raw_mem_istream(const char *str)
Definition: raw_istream.h:141
void close() override
raw_mem_istream(std::string &str)
Definition: raw_istream.h:134
raw_mem_istream(std::span< const uint8_t > mem)
Definition: raw_istream.h:138
raw_mem_istream(const char *mem, size_t len)
Definition: raw_istream.h:143
size_t in_avail() const override
raw_mem_istream(std::span< const char > mem)
Definition: raw_istream.h:136
basic_string_view< char > string_view
Definition: core.h:520
constexpr auto count() -> size_t
Definition: core.h:1204
constexpr common_t< T1, T2 > min(const T1 x, const T2 y) noexcept
Compile-time pairwise minimum function.
Definition: min.hpp:35
EIGEN_CONSTEXPR Index size(const T &x)
Definition: Meta.h:479
Definition: BFloat16.h:88
static constexpr const velocity::meters_per_second_t c(299792458.0)
Speed of light in vacuum.
Definition: AprilTagFieldLayout.h:18
Definition: format.h:1552