WPILibC++  unspecified
raw_istream.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2018 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_SUPPORT_RAW_ISTREAM_H_
9 #define WPIUTIL_SUPPORT_RAW_ISTREAM_H_
10 
11 #include <algorithm>
12 #include <cstddef>
13 #include <system_error>
14 
15 #include "llvm/SmallVector.h"
16 #include "llvm/StringRef.h"
17 #include "llvm/Twine.h"
18 
19 namespace wpi {
20 
21 class raw_istream {
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) return 0;
49  read_impl(data, readlen);
50  return readlen;
51  }
52 
53  // Read a line from an input stream (up to a maximum length).
54  // The returned buffer will contain the trailing \n (unless the maximum length
55  // was reached). \r's are stripped from the buffer.
56  // @param buf Buffer for output
57  // @param maxLen Maximum length
58  // @return Line
59  llvm::StringRef getline(llvm::SmallVectorImpl<char>& buf, int maxLen);
60 
61  virtual void close() = 0;
62  virtual size_t in_avail() const = 0;
63 
64  bool has_error() const { return m_error; }
65  void clear_error() { m_error = false; }
66 
67  raw_istream(const raw_istream&) = delete;
68  raw_istream& operator=(const raw_istream&) = delete;
69 
70  protected:
71  void error_detected() { m_error = true; }
72 
73  private:
74  virtual void read_impl(void* data, size_t len) = 0;
75 
76  bool m_error = false;
77 };
78 
79 class raw_mem_istream : public raw_istream {
80  public:
81  explicit raw_mem_istream(llvm::StringRef mem);
82  raw_mem_istream(const char* mem, size_t len) : m_cur(mem), m_left(len) {}
83  void close() override;
84  size_t in_avail() const override;
85 
86  private:
87  void read_impl(void* data, size_t len) override;
88 
89  const char* m_cur;
90  size_t m_left;
91 };
92 
93 class raw_fd_istream : public raw_istream {
94  public:
95  raw_fd_istream(const llvm::Twine& filename, std::error_code& ec,
96  size_t bufSize = 4096);
97  raw_fd_istream(int fd, bool shouldClose, size_t bufSize = 4096);
98  ~raw_fd_istream() override;
99  void close() override;
100  size_t in_avail() const override;
101 
102  private:
103  void read_impl(void* data, size_t len) override;
104 
105  char* m_buf;
106  char* m_cur;
107  char* m_end;
108  size_t m_bufSize;
109  int m_fd;
110  bool m_shouldClose;
111 };
112 
113 } // namespace wpi
114 
115 #endif // WPIUTIL_SUPPORT_RAW_ISTREAM_H_
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
Definition: SocketError.cpp:17
Definition: raw_istream.h:21
Definition: raw_istream.h:93
Definition: raw_istream.h:79
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42