13#ifndef WPIUTIL_WPI_RAW_OSTREAM_H
14#define WPIUTIL_WPI_RAW_OSTREAM_H
25#include <system_error>
73 char *OutBufStart, *OutBufEnd, *OutBufCur;
75 enum class BufferKind {
125 : Kind(K), BufferMode(unbuffered ? BufferKind::Unbuffered
126 : BufferKind::InternalBuffer) {
128 OutBufStart = OutBufEnd = OutBufCur =
nullptr;
159 SetBufferAndMode(
new char[Size], Size, BufferKind::InternalBuffer);
165 if (BufferMode != BufferKind::Unbuffered && OutBufStart ==
nullptr)
169 return OutBufEnd - OutBufStart;
177 SetBufferAndMode(
nullptr, 0, BufferKind::Unbuffered);
181 return OutBufCur - OutBufStart;
189 if (OutBufCur != OutBufStart)
194 if (OutBufCur >= OutBufEnd)
201 if (OutBufCur >= OutBufEnd)
208 if (OutBufCur >= OutBufEnd)
216 size_t Size = Arr.size();
219 if (Size > (
size_t)(OutBufEnd - OutBufCur))
220 return write(Arr.data(), Size);
223 memcpy(OutBufCur, Arr.data(), Size);
231 size_t Size = Str.size();
234 if (Size > (
size_t)(OutBufEnd - OutBufCur))
235 return write(Str.data(), Size);
238 memcpy(OutBufCur, Str.data(), Size);
244#if defined(__cpp_char8_t)
262 return this->
operator<<(std::string_view(Str));
267 return write(Str.data(), Str.length());
276 return write(Arr.data(), Arr.size());
290 return write(
reinterpret_cast<const char *
>(Ptr), Size);
354 virtual void write_impl(
const char *Ptr,
size_t Size) = 0;
358 virtual uint64_t current_pos()
const = 0;
365 SetBufferAndMode(BufferStart, Size, BufferKind::ExternalBuffer);
370 OutBufCur = OutBufStart + Size;
385 void SetBufferAndMode(
char *BufferStart,
size_t Size, BufferKind Mode);
389 void flush_nonempty();
393 void copy_to_buffer(
const char *Ptr,
size_t Size);
395 virtual void anchor();
400template <
typename OStream,
typename T>
401std::enable_if_t<!std::is_reference_v<OStream> &&
402 std::is_base_of_v<raw_ostream, OStream>,
406 return std::move(OS);
413 virtual void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset) = 0;
414 void anchor()
override;
420 void pwrite(
const char *Ptr,
size_t Size, uint64_t Offset) {
422 uint64_t Pos =
tell();
426 assert(Size + Offset <= Pos &&
"We don't support extending the stream");
428 pwrite_impl(Ptr, Size, Offset);
441 bool SupportsSeeking =
false;
442 bool IsRegularFile =
false;
451 bool IsWindowsConsole =
false;
459 void write_impl(
const char *Ptr,
size_t Size)
override;
461 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
465 uint64_t current_pos()
const override {
return pos; }
468 size_t preferred_buffer_size()
const override;
470 void anchor()
override;
480 void inc_pos(uint64_t Delta) { pos += Delta; }
529 std::error_code
error()
const {
return EC; }
593 void write_impl(
const char *Ptr,
size_t Size)
override;
597 uint64_t current_pos()
const override {
return OS.size(); }
607 std::string &
str() {
return OS; }
610 OS.reserve(
tell() + ExtraSize);
623 void write_impl(
const char *Ptr,
size_t Size)
override;
625 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
628 uint64_t current_pos()
const override;
648 std::string_view
str()
const {
return std::string_view(OS.
data(), OS.
size()); }
664 std::vector<char> &OS;
667 void write_impl(
const char *Ptr,
size_t Size)
override;
669 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
672 uint64_t current_pos()
const override;
688 std::string_view
str() {
return std::string_view(OS.data(), OS.size()); }
700 void write_impl(
const char *Ptr,
size_t Size)
override;
702 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
705 uint64_t current_pos()
const override;
731 std::vector<uint8_t> &OS;
734 void write_impl(
const char *Ptr,
size_t Size)
override;
736 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
739 uint64_t current_pos()
const override;
755 std::span<uint8_t>
array() {
return {OS.data(), OS.size()}; }
756 std::span<const uint8_t>
array()
const {
return {OS.data(), OS.size()}; }
763 void write_impl(
const char *Ptr,
size_t size)
override;
764 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
768 uint64_t current_pos()
const override;
779 void anchor()
override;
787 std::unique_ptr<raw_ostream> OS;
790 void anchor()
override;
797 this->OS->SetUnbuffered();
This file defines the SmallVector class.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1212
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition sha1.h:30
void reserve(size_type N)
Definition SmallVector.h:679
size_t size() const
Definition SmallVector.h:99
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition SmallVector.h:302
Definition raw_ostream.h:775
~buffer_ostream() override
Definition raw_ostream.h:783
buffer_ostream(raw_ostream &OS)
Definition raw_ostream.h:782
Definition raw_ostream.h:786
buffer_unique_ostream(std::unique_ptr< raw_ostream > OS)
Definition raw_ostream.h:793
~buffer_unique_ostream() override
Definition raw_ostream.h:799
A raw_ostream that writes to a file descriptor.
Definition raw_ostream.h:438
std::error_code error() const
Definition raw_ostream.h:529
void tie(raw_ostream *TieTo)
Tie this stream to the specified stream.
Definition raw_ostream.h:527
bool isRegularFile() const
Definition raw_ostream.h:516
raw_fd_ostream(std::string_view Filename, std::error_code &EC)
Open the specified file for writing.
void inc_pos(uint64_t Delta)
Definition raw_ostream.h:480
void close()
Manually flush the stream and close the file.
~raw_fd_ostream() override
raw_fd_ostream(std::string_view Filename, std::error_code &EC, fs::OpenFlags Flags)
int get_fd() const
Return the file descriptor.
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...
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:535
raw_fd_ostream(std::string_view Filename, std::error_code &EC, fs::FileAccess Access)
raw_fd_ostream(std::string_view Filename, std::error_code &EC, fs::CreationDisposition Disp)
raw_fd_ostream(int fd, bool shouldClose, bool unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
FD is the file descriptor that this writes to.
bool supportsSeeking() const
Definition raw_ostream.h:514
raw_fd_ostream(std::string_view Filename, std::error_code &EC, fs::CreationDisposition Disp, fs::FileAccess Access, fs::OpenFlags Flags)
void clear_error()
Set the flag read by has_error() to false.
Definition raw_ostream.h:546
void error_detected(std::error_code EC)
Set the flag indicating that an output error has been encountered.
Definition raw_ostream.h:474
A raw_ostream of a file for reading/writing/seeking.
Definition raw_ostream.h:569
raw_fd_stream(std::string_view Filename, std::error_code &EC)
Open the specified file for reading/writing/seeking.
static bool classof(const raw_ostream *OS)
Check if OS is a pointer of type raw_fd_stream*.
A raw_ostream that discards all output.
Definition raw_ostream.h:761
raw_null_ostream()=default
~raw_null_ostream() override
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:43
static constexpr Colors BLACK
Definition raw_ostream.h:104
OStreamKind get_kind() const
Definition raw_ostream.h:139
static constexpr Colors MAGENTA
Definition raw_ostream.h:109
static constexpr Colors CYAN
Definition raw_ostream.h:110
virtual void enable_colors(bool)
Definition raw_ostream.h:332
raw_ostream & operator<<(const char *Str)
Definition raw_ostream.h:258
void flush()
Definition raw_ostream.h:188
raw_ostream & write(const char *Ptr, size_t Size)
const char * getBufferStart() const
Return the beginning of the current stream buffer, or 0 if the stream is unbuffered.
Definition raw_ostream.h:378
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
Definition raw_ostream.h:157
virtual void reserveExtraSpace(uint64_t ExtraSize)
If possible, pre-allocate ExtraSize bytes for stream data.
Definition raw_ostream.h:150
bool colors_enabled() const
Definition raw_ostream.h:334
raw_ostream & operator<<(unsigned char C)
Definition raw_ostream.h:200
raw_ostream & operator<<(std::span< const uint8_t > Arr)
Definition raw_ostream.h:214
raw_ostream & operator<<(signed char C)
Definition raw_ostream.h:207
raw_ostream(const raw_ostream &)=delete
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
Definition raw_ostream.h:328
size_t GetNumBytesInBuffer() const
Definition raw_ostream.h:180
raw_ostream & write(const uint8_t *Ptr, size_t Size)
Definition raw_ostream.h:289
static constexpr Colors RESET
Definition raw_ostream.h:121
void SetBuffered()
Set the stream to be buffered, with an automatically determined buffer size.
void SetBuffer(char *BufferStart, size_t Size)
Use the provided buffer as the raw_ostream buffer.
Definition raw_ostream.h:364
raw_ostream & write_escaped(std::string_view Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy wpi::isPrint into an escap...
static constexpr Colors BRIGHT_RED
Definition raw_ostream.h:113
static constexpr Colors BRIGHT_CYAN
Definition raw_ostream.h:118
virtual size_t preferred_buffer_size() const
Return an efficient buffer size for the underlying output mechanism.
static constexpr Colors WHITE
Definition raw_ostream.h:111
raw_ostream & operator<<(const SmallVectorImpl< uint8_t > &Arr)
Definition raw_ostream.h:279
static constexpr Colors BRIGHT_GREEN
Definition raw_ostream.h:114
void SetNumBytesInBuffer(size_t Size)
Force-set the number of bytes in the raw_ostream buffer.
Definition raw_ostream.h:369
raw_ostream & operator<<(const std::vector< uint8_t > &Arr)
Definition raw_ostream.h:274
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
static constexpr Colors BRIGHT_WHITE
Definition raw_ostream.h:119
raw_ostream(bool unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
Definition raw_ostream.h:123
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
void SetUnbuffered()
Set the stream to be unbuffered.
Definition raw_ostream.h:175
static constexpr Colors GREEN
Definition raw_ostream.h:106
static constexpr Colors BRIGHT_BLACK
Definition raw_ostream.h:112
static constexpr Colors BRIGHT_BLUE
Definition raw_ostream.h:116
static constexpr Colors BLUE
Definition raw_ostream.h:108
static constexpr Colors YELLOW
Definition raw_ostream.h:107
OStreamKind
Definition raw_ostream.h:46
static constexpr Colors SAVEDCOLOR
Definition raw_ostream.h:120
virtual bool is_displayed() const
This function determines if this stream is connected to a "tty" or "console" window.
Definition raw_ostream.h:324
size_t GetBufferSize() const
Definition raw_ostream.h:162
raw_ostream & operator<<(std::string_view Str)
Definition raw_ostream.h:229
Colors
Definition raw_ostream.h:83
raw_ostream & operator<<(const std::string &Str)
Definition raw_ostream.h:265
raw_ostream & operator<<(char C)
Definition raw_ostream.h:193
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
Definition raw_ostream.h:316
raw_ostream & write(unsigned char C)
uint64_t tell() const
tell - Return the current offset with the file.
Definition raw_ostream.h:137
static constexpr Colors BRIGHT_YELLOW
Definition raw_ostream.h:115
void operator=(const raw_ostream &)=delete
virtual raw_ostream & reverseColor()
Reverses the foreground and background colors.
Definition raw_ostream.h:319
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:306
raw_ostream & operator<<(const SmallVectorImpl< char > &Str)
Definition raw_ostream.h:270
static constexpr Colors BRIGHT_MAGENTA
Definition raw_ostream.h:117
static constexpr Colors RED
Definition raw_ostream.h:105
An abstract base class for streams implementations that also support a pwrite operation.
Definition raw_ostream.h:412
void pwrite(const char *Ptr, size_t Size, uint64_t Offset)
Definition raw_ostream.h:420
raw_pwrite_stream(bool Unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
Definition raw_ostream.h:417
A raw_ostream that writes to an std::string.
Definition raw_ostream.h:589
std::string & str()
Returns the string's reference.
Definition raw_ostream.h:607
void reserveExtraSpace(uint64_t ExtraSize) override
If possible, pre-allocate ExtraSize bytes for stream data.
Definition raw_ostream.h:609
raw_string_ostream(std::string &O)
Definition raw_ostream.h:600
A raw_ostream that writes to an SmallVector or SmallString.
Definition raw_ostream.h:619
SmallVectorImpl< char > & buffer()
Definition raw_ostream.h:649
raw_svector_ostream(SmallVectorImpl< char > &O)
Construct a new raw_svector_ostream.
Definition raw_ostream.h:635
static bool classof(const raw_ostream *OS)
~raw_svector_ostream() override=default
void reserveExtraSpace(uint64_t ExtraSize) override
If possible, pre-allocate ExtraSize bytes for stream data.
Definition raw_ostream.h:651
std::string_view str() const
Return a std::string_view for the vector contents.
Definition raw_ostream.h:648
A raw_ostream that writes to an SmallVector or SmallString.
Definition raw_ostream.h:696
raw_usvector_ostream(SmallVectorImpl< uint8_t > &O)
Construct a new raw_svector_ostream.
Definition raw_ostream.h:712
std::span< uint8_t > array()
Return an std::span for the vector contents.
Definition raw_ostream.h:721
std::span< const uint8_t > array() const
Definition raw_ostream.h:722
~raw_usvector_ostream() override=default
A raw_ostream that writes to a vector.
Definition raw_ostream.h:730
~raw_uvector_ostream() override=default
std::span< const uint8_t > array() const
Definition raw_ostream.h:756
raw_uvector_ostream(std::vector< uint8_t > &O)
Construct a new raw_svector_ostream.
Definition raw_ostream.h:746
std::span< uint8_t > array()
Return a std::span for the vector contents.
Definition raw_ostream.h:755
A raw_ostream that writes to a vector.
Definition raw_ostream.h:663
std::string_view str()
Return a std::string_view for the vector contents.
Definition raw_ostream.h:688
~raw_vector_ostream() override=default
raw_vector_ostream(std::vector< char > &O)
Construct a new raw_svector_ostream.
Definition raw_ostream.h:679
Definition MappedFileRegion.h:12
OpenFlags
Definition fs.h:64
FileAccess
Definition fs.h:59
CreationDisposition
Definition fs.h:37
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
Foonathan namespace.
Definition ntcore_cpp.h:26
raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
raw_ostream & operator<<(raw_ostream &OS, sys::TimePoint<> TP)
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.