14 #ifndef WPIUTIL_WPI_RAW_OSTREAM_H
15 #define WPIUTIL_WPI_RAW_OSTREAM_H
17 #include "wpi/ArrayRef.h"
18 #include "wpi/SmallVector.h"
19 #include "wpi/StringRef.h"
26 #include <system_error>
30 class format_object_base;
31 class FormattedString;
32 class FormattedNumber;
37 enum FileAccess : unsigned;
38 enum OpenFlags : unsigned;
39 enum CreationDisposition : unsigned;
67 char *OutBufStart, *OutBufEnd, *OutBufCur;
90 : BufferMode(unbuffered ? Unbuffered : InternalBuffer) {
92 OutBufStart = OutBufEnd = OutBufCur =
nullptr;
101 uint64_t
tell()
const {
return current_pos() + GetNumBytesInBuffer(); }
114 SetBufferAndMode(
new char[Size], Size, InternalBuffer);
117 size_t GetBufferSize()
const {
120 if (BufferMode != Unbuffered && OutBufStart ==
nullptr)
124 return OutBufEnd - OutBufStart;
132 SetBufferAndMode(
nullptr, 0, Unbuffered);
135 size_t GetNumBytesInBuffer()
const {
136 return OutBufCur - OutBufStart;
144 if (OutBufCur != OutBufStart)
148 raw_ostream &operator<<(
char C) {
149 if (OutBufCur >= OutBufEnd)
155 raw_ostream &operator<<(
unsigned char C) {
156 if (OutBufCur >= OutBufEnd)
162 raw_ostream &operator<<(
signed char C) {
163 if (OutBufCur >= OutBufEnd)
169 raw_ostream &operator<<(ArrayRef<uint8_t> Arr) {
171 size_t Size = Arr.size();
174 if (Size > (
size_t)(OutBufEnd - OutBufCur))
175 return write(Arr.data(), Size);
178 memcpy(OutBufCur, Arr.data(), Size);
184 raw_ostream &operator<<(StringRef Str) {
186 size_t Size = Str.size();
189 if (Size > (
size_t)(OutBufEnd - OutBufCur))
190 return write(Str.data(), Size);
193 memcpy(OutBufCur, Str.data(), Size);
199 raw_ostream &operator<<(
const char *Str) {
203 return this->operator<<(StringRef(Str));
206 raw_ostream &operator<<(
const std::string &Str) {
208 return write(Str.data(), Str.length());
211 raw_ostream &operator<<(
const SmallVectorImpl<char> &Str) {
212 return write(Str.data(), Str.size());
215 raw_ostream &operator<<(
const std::vector<uint8_t> &Arr) {
217 return write(Arr.data(), Arr.size());
220 raw_ostream &operator<<(
const SmallVectorImpl<uint8_t> &Arr) {
221 return write(Arr.data(), Arr.size());
224 raw_ostream &operator<<(
unsigned long N);
225 raw_ostream &operator<<(
long N);
226 raw_ostream &operator<<(
unsigned long long N);
227 raw_ostream &operator<<(
long long N);
228 raw_ostream &operator<<(
const void *P);
230 raw_ostream &operator<<(
unsigned int N) {
231 return this->operator<<(static_cast<unsigned long>(N));
234 raw_ostream &operator<<(
int N) {
235 return this->operator<<(static_cast<long>(N));
238 raw_ostream &operator<<(
double N);
241 raw_ostream &
write_hex(
unsigned long long N);
245 raw_ostream &
write_escaped(StringRef Str,
bool UseHexEscapes =
false);
247 raw_ostream &write(
unsigned char C);
248 raw_ostream &write(
const char *Ptr,
size_t Size);
249 raw_ostream &write(
const uint8_t *Ptr,
size_t Size) {
250 return write(reinterpret_cast<const char *>(Ptr), Size);
254 raw_ostream &operator<<(
const format_object_base &Fmt);
257 raw_ostream &operator<<(
const FormattedString &);
260 raw_ostream &operator<<(
const FormattedNumber &);
263 raw_ostream &operator<<(
const FormattedBytes &);
266 raw_ostream &
indent(
unsigned NumSpaces);
320 virtual void write_impl(
const char *Ptr,
size_t Size) = 0;
323 virtual void handle();
327 virtual uint64_t current_pos()
const = 0;
334 SetBufferAndMode(BufferStart, Size, ExternalBuffer);
349 void SetBufferAndMode(
char *BufferStart,
size_t Size, BufferKind Mode);
353 void flush_nonempty();
357 void copy_to_buffer(
const char *Ptr,
size_t Size);
359 virtual void anchor();
366 virtual void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset) = 0;
367 void anchor()
override;
372 void pwrite(
const char *Ptr,
size_t Size, uint64_t Offset) {
374 uint64_t Pos =
tell();
378 assert(Size + Offset <= Pos &&
"We don't support extending the stream");
380 pwrite_impl(Ptr, Size, Offset);
394 bool SupportsSeeking;
397 bool IsWindowsConsole =
false;
407 void write_impl(
const char *Ptr,
size_t Size)
override;
409 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
413 uint64_t current_pos()
const override {
return pos; }
416 size_t preferred_buffer_size()
const override;
419 void error_detected(std::error_code EC) { this->EC = EC; }
421 void anchor()
override;
434 sys::fs::CreationDisposition Disp);
436 sys::fs::FileAccess Access);
438 sys::fs::OpenFlags Flags);
440 sys::fs::CreationDisposition Disp, sys::fs::FileAccess Access,
441 sys::fs::OpenFlags Flags);
454 bool supportsSeeking() {
return SupportsSeeking; }
458 uint64_t
seek(uint64_t off);
460 std::error_code error()
const {
return EC; }
489 raw_ostream &
nulls();
501 void write_impl(
const char *Ptr,
size_t Size)
override;
505 uint64_t current_pos()
const override {
return OS.size(); }
528 void write_impl(
const char *Ptr,
size_t Size)
override;
530 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
533 uint64_t current_pos()
const override;
546 void flush() =
delete;
558 std::vector<char> &OS;
561 void write_impl(
const char *Ptr,
size_t Size)
override;
563 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
566 uint64_t current_pos()
const override;
579 void flush() =
delete;
594 void write_impl(
const char *Ptr,
size_t Size)
override;
596 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
599 uint64_t current_pos()
const override;
612 void flush() =
delete;
624 std::vector<uint8_t> &OS;
627 void write_impl(
const char *Ptr,
size_t Size)
override;
629 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
632 uint64_t current_pos()
const override;
645 void flush() =
delete;
655 void write_impl(
const char *Ptr,
size_t size)
override;
656 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
660 uint64_t current_pos()
const override;
671 virtual void anchor()
override;
680 #endif // LLVM_SUPPORT_RAW_OSTREAM_H
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:582
virtual raw_ostream & reverseColor()
Reverses the foreground and background colors.
Definition: raw_ostream.h:292
void clear_error()
Set the flag read by has_error() to false.
Definition: raw_ostream.h:477
uint64_t seek(uint64_t off)
Flushes the stream and repositions the underlying file descriptor position to the offset specified fr...
const char * getBufferStart() const
Return the beginning of the current stream buffer, or 0 if the stream is unbuffered.
Definition: raw_ostream.h:342
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:549
ArrayRef< uint8_t > array()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:648
raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
Definition: raw_ostream.h:513
ArrayRef< uint8_t > array()
Return an ArrayRef for the vector contents.
Definition: raw_ostream.h:615
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
Definition: raw_ostream.h:112
virtual raw_ostream & changeColor(enum Colors Color, bool Bold=false, bool BG=false)
Changes the foreground color of text that will be output from this point forward.
Definition: raw_ostream.h:278
void SetBuffer(char *BufferStart, size_t Size)
Use the provided buffer as the raw_ostream buffer.
Definition: raw_ostream.h:333
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:365
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy wpi::isPrint into an escap...
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
virtual size_t preferred_buffer_size() const
Return an efficient buffer size for the underlying output mechanism.
WPILib C++ utilities (wpiutil) namespace.
Definition: EventLoopRunner.h:17
A raw_ostream that writes to a vector.
Definition: raw_ostream.h:623
void SetUnbuffered()
Set the stream to be unbuffered.
Definition: raw_ostream.h:130
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:158
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:497
A raw_ostream that discards all output.
Definition: raw_ostream.h:653
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:590
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
bool has_error() const
Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encou...
Definition: raw_ostream.h:466
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:524
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
raw_uvector_ostream(std::vector< uint8_t > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:639
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
Definition: raw_ostream.h:300
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:390
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1007
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
Definition: raw_ostream.h:289
void SetBuffered()
Set the stream to be buffered, with an automatically determined buffer size.
A raw_ostream that writes to a vector.
Definition: raw_ostream.h:557
raw_fd_ostream(StringRef Filename, std::error_code &EC)
Open the specified file for writing.
raw_svector_ostream(SmallVectorImpl< char > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:540
void close()
Manually flush the stream and close the file.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:47
raw_vector_ostream(std::vector< char > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:573
virtual bool is_displayed() const
This function determines if this stream is connected to a "tty" or "console" window.
Definition: raw_ostream.h:297
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:101
Definition: raw_ostream.h:667
raw_usvector_ostream(SmallVectorImpl< uint8_t > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:606