WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
raw_istream.hpp
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#pragma once
6
7#include <stdint.h>
8
9#include <algorithm>
10#include <cstddef>
11#include <span>
12#include <string>
13#include <string_view>
14#include <system_error>
15#include <vector>
16
17#include "wpi/util/SmallVector.hpp"
18
19namespace wpi::util {
20
22 public:
23 raw_istream() = default;
24 virtual ~raw_istream() = default;
25
26 raw_istream& read(char& c) {
27 read_impl(&c, 1);
28 return *this;
29 }
30
31 raw_istream& read(unsigned char& c) {
32 read_impl(&c, 1);
33 return *this;
34 }
35
36 raw_istream& read(signed char& c) {
37 read_impl(&c, 1);
38 return *this;
39 }
40
41 raw_istream& read(void* data, size_t len) {
42 read_impl(data, len);
43 return *this;
44 }
45
46 size_t readsome(void* data, size_t len) {
47 size_t readlen = (std::min)(in_avail(), len);
48 if (readlen == 0) {
49 return 0;
50 }
51 read_impl(data, readlen);
52 return m_read_count;
53 }
54
56 size_t old_size = buf.size();
57 buf.append(len, 0);
58 read_impl(&buf[old_size], len);
59 buf.resize(old_size + m_read_count);
60 return *this;
61 }
62
64 size_t old_size = buf.size();
65 buf.append(len, 0);
66 read_impl(&buf[old_size], len);
67 buf.resize(old_size + m_read_count);
68 return *this;
69 }
70
71 raw_istream& readinto(std::vector<char>& buf, size_t len) {
72 size_t old_size = buf.size();
73 buf.insert(buf.end(), len, 0);
74 read_impl(&buf[old_size], len);
75 buf.resize(old_size + m_read_count);
76 return *this;
77 }
78
79 raw_istream& readinto(std::vector<uint8_t>& buf, size_t len) {
80 size_t old_size = buf.size();
81 buf.insert(buf.end(), len, 0);
82 read_impl(&buf[old_size], len);
83 buf.resize(old_size + m_read_count);
84 return *this;
85 }
86
87 raw_istream& readinto(std::string& buf, size_t len) {
88 size_t old_size = buf.size();
89 buf.insert(buf.end(), len, 0);
90 read_impl(&buf[old_size], len);
91 buf.resize(old_size + m_read_count);
92 return *this;
93 }
94
95 // Read a line from an input stream (up to a maximum length).
96 // The returned buffer will contain the trailing \n (unless the maximum length
97 // was reached). \r's are stripped from the buffer.
98 // @param buf Buffer for output
99 // @param maxLen Maximum length
100 // @return Line
101 std::string_view getline(SmallVectorImpl<char>& buf, int maxLen);
102
103 virtual void close() = 0;
104
105 // Number of bytes available to read without potentially blocking.
106 // Note this can return zero even if there are bytes actually available to
107 // read.
108 virtual size_t in_avail() const = 0;
109
110 // Return the number of bytes read by the last read operation.
111 size_t read_count() const { return m_read_count; }
112
113 bool has_error() const { return m_error; }
114 void clear_error() { m_error = false; }
115
116 raw_istream(const raw_istream&) = delete;
118
119 protected:
120 void error_detected() { m_error = true; }
121 void set_read_count(size_t count) { m_read_count = count; }
122
123 private:
124 virtual void read_impl(void* data, size_t len) = 0;
125
126 bool m_error = false;
127 size_t m_read_count = 0;
128};
129
131 public:
132 // not const as we don't want to allow temporaries
133 explicit raw_mem_istream(std::string& str)
134 : raw_mem_istream(str.data(), str.size()) {}
135 explicit raw_mem_istream(std::span<const char> mem)
136 : raw_mem_istream(mem.data(), mem.size()) {}
137 explicit raw_mem_istream(std::span<const uint8_t> mem)
138 : raw_mem_istream(reinterpret_cast<const char*>(mem.data()), mem.size()) {
139 }
140 explicit raw_mem_istream(const char* str)
141 : m_cur(str), m_left(std::strlen(str)) {}
142 raw_mem_istream(const char* mem, size_t len) : m_cur(mem), m_left(len) {}
143 void close() override;
144 size_t in_avail() const override;
145
146 private:
147 void read_impl(void* data, size_t len) override;
148
149 const char* m_cur;
150 size_t m_left;
151};
152
154 public:
155 raw_fd_istream(std::string_view filename, std::error_code& ec,
156 size_t bufSize = 4096);
157 raw_fd_istream(int fd, bool shouldClose, size_t bufSize = 4096);
158 ~raw_fd_istream() override;
159 void close() final;
160 size_t in_avail() const override;
161
162 private:
163 void read_impl(void* data, size_t len) override;
164
165 char* m_buf;
166 char* m_cur;
167 char* m_end;
168 size_t m_bufSize;
169 int m_fd;
170 bool m_shouldClose;
171};
172
173} // namespace wpi::util
Definition BooleanTopic.hpp:24
raw_fd_istream(std::string_view filename, std::error_code &ec, size_t bufSize=4096)
size_t in_avail() const override
raw_fd_istream(int fd, bool shouldClose, size_t bufSize=4096)
bool has_error() const
Definition raw_istream.hpp:113
raw_istream & read(unsigned char &c)
Definition raw_istream.hpp:31
virtual ~raw_istream()=default
virtual void close()=0
raw_istream & readinto(std::vector< uint8_t > &buf, size_t len)
Definition raw_istream.hpp:79
raw_istream(const raw_istream &)=delete
size_t readsome(void *data, size_t len)
Definition raw_istream.hpp:46
raw_istream & operator=(const raw_istream &)=delete
void set_read_count(size_t count)
Definition raw_istream.hpp:121
std::string_view getline(SmallVectorImpl< char > &buf, int maxLen)
raw_istream & read(char &c)
Definition raw_istream.hpp:26
raw_istream & readinto(std::vector< char > &buf, size_t len)
Definition raw_istream.hpp:71
virtual size_t in_avail() const =0
raw_istream & readinto(SmallVectorImpl< uint8_t > &buf, size_t len)
Definition raw_istream.hpp:63
size_t read_count() const
Definition raw_istream.hpp:111
raw_istream & read(signed char &c)
Definition raw_istream.hpp:36
void error_detected()
Definition raw_istream.hpp:120
raw_istream & read(void *data, size_t len)
Definition raw_istream.hpp:41
raw_istream & readinto(std::string &buf, size_t len)
Definition raw_istream.hpp:87
void clear_error()
Definition raw_istream.hpp:114
raw_istream & readinto(SmallVectorImpl< char > &buf, size_t len)
Definition raw_istream.hpp:55
raw_mem_istream(std::string &str)
Definition raw_istream.hpp:133
raw_mem_istream(std::span< const uint8_t > mem)
Definition raw_istream.hpp:137
raw_mem_istream(std::span< const char > mem)
Definition raw_istream.hpp:135
raw_mem_istream(const char *mem, size_t len)
Definition raw_istream.hpp:142
size_t in_avail() const override
raw_mem_istream(const char *str)
Definition raw_istream.hpp:140
Definition StringMap.hpp:773
Definition raw_os_ostream.hpp:19