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 OpenFlags : unsigned;
65 char *OutBufStart, *OutBufEnd, *OutBufCur;
88 : BufferMode(unbuffered ? Unbuffered : InternalBuffer) {
90 OutBufStart = OutBufEnd = OutBufCur =
nullptr;
99 uint64_t
tell()
const {
return current_pos() + GetNumBytesInBuffer(); }
112 SetBufferAndMode(
new char[Size], Size, InternalBuffer);
115 size_t GetBufferSize()
const {
118 if (BufferMode != Unbuffered && OutBufStart ==
nullptr)
122 return OutBufEnd - OutBufStart;
130 SetBufferAndMode(
nullptr, 0, Unbuffered);
133 size_t GetNumBytesInBuffer()
const {
134 return OutBufCur - OutBufStart;
142 if (OutBufCur != OutBufStart)
146 raw_ostream &operator<<(
char C) {
147 if (OutBufCur >= OutBufEnd)
153 raw_ostream &operator<<(
unsigned char C) {
154 if (OutBufCur >= OutBufEnd)
160 raw_ostream &operator<<(
signed char C) {
161 if (OutBufCur >= OutBufEnd)
167 raw_ostream &operator<<(ArrayRef<uint8_t> Arr) {
169 size_t Size = Arr.size();
172 if (Size > (
size_t)(OutBufEnd - OutBufCur))
173 return write(Arr.data(), Size);
176 memcpy(OutBufCur, Arr.data(), Size);
182 raw_ostream &operator<<(StringRef Str) {
184 size_t Size = Str.size();
187 if (Size > (
size_t)(OutBufEnd - OutBufCur))
188 return write(Str.data(), Size);
191 memcpy(OutBufCur, Str.data(), Size);
197 raw_ostream &operator<<(
const char *Str) {
201 return this->operator<<(StringRef(Str));
204 raw_ostream &operator<<(
const std::string &Str) {
206 return write(Str.data(), Str.length());
209 raw_ostream &operator<<(const SmallVectorImpl<char> &Str) {
210 return write(Str.data(), Str.size());
213 raw_ostream &operator<<(const std::vector<uint8_t> &Arr) {
215 return write(Arr.data(), Arr.size());
218 raw_ostream &operator<<(const SmallVectorImpl<uint8_t> &Arr) {
219 return write(Arr.data(), Arr.size());
222 raw_ostream &operator<<(
unsigned long N);
223 raw_ostream &operator<<(
long N);
224 raw_ostream &operator<<(
unsigned long long N);
225 raw_ostream &operator<<(
long long N);
226 raw_ostream &operator<<(
const void *P);
228 raw_ostream &operator<<(
unsigned int N) {
229 return this->operator<<(static_cast<unsigned long>(N));
232 raw_ostream &operator<<(
int N) {
233 return this->operator<<(static_cast<long>(N));
236 raw_ostream &operator<<(
double N);
239 raw_ostream &
write_hex(
unsigned long long N);
243 raw_ostream &
write_escaped(StringRef Str,
bool UseHexEscapes =
false);
245 raw_ostream &write(
unsigned char C);
246 raw_ostream &write(
const char *Ptr,
size_t Size);
247 raw_ostream &write(
const uint8_t *Ptr,
size_t Size) {
248 return write(reinterpret_cast<const char *>(Ptr), Size);
252 raw_ostream &operator<<(
const format_object_base &Fmt);
255 raw_ostream &operator<<(
const FormattedString &);
258 raw_ostream &operator<<(
const FormattedNumber &);
261 raw_ostream &operator<<(
const FormattedBytes &);
264 raw_ostream &
indent(
unsigned NumSpaces);
318 virtual void write_impl(
const char *Ptr,
size_t Size) = 0;
321 virtual void handle();
325 virtual uint64_t current_pos()
const = 0;
332 SetBufferAndMode(BufferStart, Size, ExternalBuffer);
347 void SetBufferAndMode(
char *BufferStart,
size_t Size, BufferKind Mode);
351 void flush_nonempty();
355 void copy_to_buffer(
const char *Ptr,
size_t Size);
357 virtual void anchor();
364 virtual void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset) = 0;
365 void anchor()
override;
370 void pwrite(
const char *Ptr,
size_t Size, uint64_t Offset) {
372 uint64_t Pos =
tell();
376 assert(Size + Offset <= Pos &&
"We don't support extending the stream");
378 pwrite_impl(Ptr, Size, Offset);
396 bool SupportsSeeking;
399 void write_impl(
const char *Ptr,
size_t Size)
override;
401 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
405 uint64_t current_pos()
const override {
return pos; }
408 size_t preferred_buffer_size()
const override;
411 void error_detected(std::error_code EC) { this->EC = EC; }
413 void anchor()
override;
427 sys::fs::OpenFlags Flags);
439 bool supportsSeeking() {
return SupportsSeeking; }
443 uint64_t
seek(uint64_t off);
445 std::error_code error()
const {
return EC; }
474 raw_ostream &
nulls();
486 void write_impl(
const char *Ptr,
size_t Size)
override;
490 uint64_t current_pos()
const override {
return OS.size(); }
513 void write_impl(
const char *Ptr,
size_t Size)
override;
515 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
518 uint64_t current_pos()
const override;
531 void flush() =
delete;
543 std::vector<char> &OS;
546 void write_impl(
const char *Ptr,
size_t Size)
override;
548 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
551 uint64_t current_pos()
const override;
564 void flush() =
delete;
579 void write_impl(
const char *Ptr,
size_t Size)
override;
581 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
584 uint64_t current_pos()
const override;
597 void flush() =
delete;
609 std::vector<uint8_t> &OS;
612 void write_impl(
const char *Ptr,
size_t Size)
override;
614 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
617 uint64_t current_pos()
const override;
630 void flush() =
delete;
640 void write_impl(
const char *Ptr,
size_t size)
override;
641 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
645 uint64_t current_pos()
const override;
663 #endif // LLVM_SUPPORT_RAW_OSTREAM_H
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
raw_uvector_ostream(std::vector< uint8_t > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:624
void close()
Manually flush the stream and close the file.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:575
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '', ' ', '"', and anything that doesn't satisfy std::isprint into an escape ...
raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
uint64_t seek(uint64_t off)
Flushes the stream and repositions the underlying file descriptor position to the offset specified fr...
virtual bool is_displayed() const
This function determines if this stream is connected to a "tty" or "console" window.
Definition: raw_ostream.h:295
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:534
raw_usvector_ostream(SmallVectorImpl< uint8_t > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:591
void SetBuffered()
Set the stream to be buffered, with an automatically determined buffer size.
void SetUnbuffered()
Set the stream to be unbuffered.
Definition: raw_ostream.h:128
virtual raw_ostream & reverseColor()
Reverses the foreground and background colors.
Definition: raw_ostream.h:290
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:388
A raw_ostream that discards all output.
Definition: raw_ostream.h:638
ArrayRef< uint8_t > array()
Return an ArrayRef for the vector contents.
Definition: raw_ostream.h:600
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
namespace to hold default to_json function
Definition: SmallString.h:21
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:509
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:99
A raw_ostream that writes to a vector.
Definition: raw_ostream.h:608
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:482
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
ArrayRef< uint8_t > array()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:633
void SetBuffer(char *BufferStart, size_t Size)
Use the provided buffer as the raw_ostream buffer.
Definition: raw_ostream.h:331
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
Definition: raw_ostream.h:287
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:567
raw_fd_ostream(StringRef Filename, std::error_code &EC, sys::fs::OpenFlags Flags)
Open the specified file for writing.
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
Definition: raw_ostream.h:298
Definition: raw_ostream.h:652
const char * getBufferStart() const
Return the beginning of the current stream buffer, or 0 if the stream is unbuffered.
Definition: raw_ostream.h:340
A raw_ostream that writes to a vector.
Definition: raw_ostream.h:542
virtual size_t preferred_buffer_size() const
Return an efficient buffer size for the underlying output mechanism.
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
Definition: raw_ostream.h:110
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:276
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:451
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:999
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
Definition: raw_ostream.h:498
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:363
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:151
raw_svector_ostream(SmallVectorImpl< char > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:525
raw_vector_ostream(std::vector< char > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:558
void clear_error()
Set the flag read by has_error() to false.
Definition: raw_ostream.h:462