WPILibC++  unspecified
raw_istream.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2015. 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_SUPPORT_RAW_ISTREAM_H_
9 #define WPIUTIL_SUPPORT_RAW_ISTREAM_H_
10 
11 #include <algorithm>
12 #include <cstddef>
13 #include <system_error>
14 
15 namespace llvm {
16 template <typename T>
17 class SmallVectorImpl;
18 class StringRef;
19 }
20 
21 namespace wpi {
22 
23 class raw_istream {
24  public:
25  raw_istream() = default;
26  virtual ~raw_istream() = default;
27 
28  raw_istream& read(char& c) {
29  read_impl(&c, 1);
30  return *this;
31  }
32 
33  raw_istream& read(unsigned char& c) {
34  read_impl(&c, 1);
35  return *this;
36  }
37 
38  raw_istream& read(signed char& c) {
39  read_impl(&c, 1);
40  return *this;
41  }
42 
43  raw_istream& read(void* data, std::size_t len) {
44  read_impl(data, len);
45  return *this;
46  }
47 
48  std::size_t readsome(void* data, std::size_t len) {
49  std::size_t readlen = std::min(in_avail(), len);
50  if (readlen == 0) return 0;
51  read_impl(data, readlen);
52  return readlen;
53  };
54 
55  // Read a line from an input stream (up to a maximum length).
56  // The returned buffer will contain the trailing \n (unless the maximum length
57  // was reached). \r's are stripped from the buffer.
58  // @param buf Buffer for output
59  // @param maxLen Maximum length
60  // @return Line
61  llvm::StringRef getline(llvm::SmallVectorImpl<char>& buf, int maxLen);
62 
63  virtual void close() = 0;
64  virtual std::size_t in_avail() const = 0;
65 
66  bool has_error() const { return m_error; }
67  void clear_error() { m_error = false; }
68 
69  raw_istream(const raw_istream&) = delete;
70  raw_istream& operator=(const raw_istream&) = delete;
71 
72  protected:
73  void error_detected() { m_error = true; }
74 
75  private:
76  virtual void read_impl(void* data, std::size_t len) = 0;
77 
78  bool m_error = false;
79 };
80 
81 class raw_mem_istream : public raw_istream {
82  public:
84  raw_mem_istream(const char* mem, std::size_t len) : m_cur(mem), m_left(len) {}
85  void close() override;
86  std::size_t in_avail() const override;
87 
88  private:
89  void read_impl(void* data, std::size_t len) override;
90 
91  const char* m_cur;
92  std::size_t m_left;
93 };
94 
95 class raw_fd_istream : public raw_istream {
96  public:
97  raw_fd_istream(llvm::StringRef filename, std::error_code& ec,
98  std::size_t bufSize = 4096);
99  raw_fd_istream(int fd, bool shouldClose, std::size_t bufSize = 4096);
100  ~raw_fd_istream() override;
101  void close() override;
102  std::size_t in_avail() const override;
103 
104  private:
105  void read_impl(void* data, std::size_t len) override;
106 
107  char* m_buf;
108  char* m_cur;
109  char* m_end;
110  std::size_t m_bufSize;
111  int m_fd;
112  bool m_shouldClose;
113 };
114 
115 } // namespace wpi
116 
117 #endif // WPIUTIL_SUPPORT_RAW_ISTREAM_H_
Definition: Path.inc:27
Definition: SocketError.cpp:18
Definition: raw_istream.h:23
Definition: raw_istream.h:95
Definition: raw_istream.h:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42