13#ifndef WPIUTIL_WPI_RAW_OSTREAM_H
14#define WPIUTIL_WPI_RAW_OSTREAM_H
25#include <system_error>
72 char *OutBufStart, *OutBufEnd, *OutBufCur;
78 enum class BufferKind {
112 : Kind(K), BufferMode(unbuffered ? BufferKind::Unbuffered
113 : BufferKind::InternalBuffer) {
115 OutBufStart = OutBufEnd = OutBufCur =
nullptr;
146 SetBufferAndMode(
new char[Size], Size, BufferKind::InternalBuffer);
152 if (BufferMode != BufferKind::Unbuffered && OutBufStart ==
nullptr)
156 return OutBufEnd - OutBufStart;
164 SetBufferAndMode(
nullptr, 0, BufferKind::Unbuffered);
168 return OutBufCur - OutBufStart;
176 if (OutBufCur != OutBufStart)
181 if (OutBufCur >= OutBufEnd)
188 if (OutBufCur >= OutBufEnd)
195 if (OutBufCur >= OutBufEnd)
203 size_t Size = Arr.size();
206 if (Size > (
size_t)(OutBufEnd - OutBufCur))
207 return write(Arr.data(), Size);
210 memcpy(OutBufCur, Arr.data(), Size);
218 size_t Size = Str.size();
221 if (Size > (
size_t)(OutBufEnd - OutBufCur))
222 return write(Str.data(), Size);
225 memcpy(OutBufCur, Str.data(), Size);
231#if defined(__cpp_char8_t)
254 return write(Str.data(), Str.length());
263 return write(Arr.data(), Arr.size());
277 return write(
reinterpret_cast<const char *
>(Ptr), Size);
345 virtual void write_impl(
const char *Ptr,
size_t Size) = 0;
349 virtual uint64_t current_pos()
const = 0;
356 SetBufferAndMode(BufferStart, Size, BufferKind::ExternalBuffer);
361 OutBufCur = OutBufStart + Size;
376 void SetBufferAndMode(
char *BufferStart,
size_t Size, BufferKind Mode);
380 void flush_nonempty();
384 void copy_to_buffer(
const char *Ptr,
size_t Size);
387 void flush_tied_then_write(
const char *Ptr,
size_t Size);
389 virtual void anchor();
394template <
typename OStream,
typename T>
395std::enable_if_t<!std::is_reference_v<OStream> &&
396 std::is_base_of_v<raw_ostream, OStream>,
400 return std::move(OS);
407 virtual void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset) = 0;
408 void anchor()
override;
414 void pwrite(
const char *Ptr,
size_t Size, uint64_t Offset) {
416 uint64_t Pos =
tell();
420 assert(Size + Offset <= Pos &&
"We don't support extending the stream");
422 pwrite_impl(Ptr, Size, Offset);
435 bool SupportsSeeking =
false;
436 bool IsRegularFile =
false;
441 bool IsWindowsConsole =
false;
449 void write_impl(
const char *Ptr,
size_t Size)
override;
451 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
455 uint64_t current_pos()
const override {
return pos; }
458 size_t preferred_buffer_size()
const override;
460 void anchor()
override;
470 void inc_pos(uint64_t Delta) { pos += Delta; }
512 std::error_code
error()
const {
return EC; }
576 void write_impl(
const char *Ptr,
size_t Size)
override;
580 uint64_t current_pos()
const override {
return OS.size(); }
590 std::string &
str() {
return OS; }
593 OS.reserve(
tell() + ExtraSize);
606 void write_impl(
const char *Ptr,
size_t Size)
override;
608 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
611 uint64_t current_pos()
const override;
640 std::vector<char> &OS;
643 void write_impl(
const char *Ptr,
size_t Size)
override;
645 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
648 uint64_t current_pos()
const override;
676 void write_impl(
const char *Ptr,
size_t Size)
override;
678 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
681 uint64_t current_pos()
const override;
707 std::vector<uint8_t> &OS;
710 void write_impl(
const char *Ptr,
size_t Size)
override;
712 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
715 uint64_t current_pos()
const override;
731 std::span<uint8_t>
array() {
return {OS.data(), OS.size()}; }
732 std::span<const uint8_t>
array()
const {
return {OS.data(), OS.size()}; }
739 void write_impl(
const char *Ptr,
size_t size)
override;
740 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
744 uint64_t current_pos()
const override;
755 void anchor()
override;
763 std::unique_ptr<raw_ostream> OS;
766 void anchor()
override;
This file defines the SmallVector class.
void reserve(size_type N)
Definition: SmallVector.h:669
size_t size() const
Definition: SmallVector.h:98
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:291
Definition: raw_ostream.h:751
~buffer_ostream() override
Definition: raw_ostream.h:759
buffer_ostream(raw_ostream &OS)
Definition: raw_ostream.h:758
Definition: raw_ostream.h:762
buffer_unique_ostream(std::unique_ptr< raw_ostream > OS)
Definition: raw_ostream.h:769
~buffer_unique_ostream() override
Definition: raw_ostream.h:775
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:432
std::error_code error() const
Definition: raw_ostream.h:512
bool isRegularFile() const
Definition: raw_ostream.h:506
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:470
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:467
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:518
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:504
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:529
void error_detected(std::error_code EC)
Set the flag indicating that an output error has been encountered.
Definition: raw_ostream.h:464
A raw_ostream of a file for reading/writing/seeking.
Definition: raw_ostream.h:552
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:737
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:99
OStreamKind get_kind() const
Definition: raw_ostream.h:126
static constexpr Colors MAGENTA
Definition: raw_ostream.h:104
static constexpr Colors CYAN
Definition: raw_ostream.h:105
virtual void enable_colors(bool)
Definition: raw_ostream.h:319
raw_ostream & operator<<(const char *Str)
Definition: raw_ostream.h:245
void flush()
Definition: raw_ostream.h:175
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:369
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
Definition: raw_ostream.h:144
virtual void reserveExtraSpace(uint64_t ExtraSize)
If possible, pre-allocate ExtraSize bytes for stream data.
Definition: raw_ostream.h:137
bool colors_enabled() const
Definition: raw_ostream.h:321
raw_ostream & operator<<(unsigned char C)
Definition: raw_ostream.h:187
raw_ostream & operator<<(std::span< const uint8_t > Arr)
Definition: raw_ostream.h:201
raw_ostream & operator<<(signed char C)
Definition: raw_ostream.h:194
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:315
size_t GetNumBytesInBuffer() const
Definition: raw_ostream.h:167
raw_ostream & write(const uint8_t *Ptr, size_t Size)
Definition: raw_ostream.h:276
static constexpr Colors RESET
Definition: raw_ostream.h:108
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:355
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...
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:106
raw_ostream & operator<<(const SmallVectorImpl< uint8_t > &Arr)
Definition: raw_ostream.h:266
void tie(raw_ostream *TieTo)
Tie this stream to the specified stream.
Definition: raw_ostream.h:325
void SetNumBytesInBuffer(size_t Size)
Force-set the number of bytes in the raw_ostream buffer.
Definition: raw_ostream.h:360
raw_ostream & operator<<(const std::vector< uint8_t > &Arr)
Definition: raw_ostream.h:261
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
raw_ostream(bool unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
Definition: raw_ostream.h:110
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
void SetUnbuffered()
Set the stream to be unbuffered.
Definition: raw_ostream.h:162
static constexpr Colors GREEN
Definition: raw_ostream.h:101
static constexpr Colors BLUE
Definition: raw_ostream.h:103
static constexpr Colors YELLOW
Definition: raw_ostream.h:102
OStreamKind
Definition: raw_ostream.h:46
static constexpr Colors SAVEDCOLOR
Definition: raw_ostream.h:107
virtual bool is_displayed() const
This function determines if this stream is connected to a "tty" or "console" window.
Definition: raw_ostream.h:311
size_t GetBufferSize() const
Definition: raw_ostream.h:149
raw_ostream & operator<<(std::string_view Str)
Definition: raw_ostream.h:216
Colors
Definition: raw_ostream.h:86
raw_ostream & operator<<(const std::string &Str)
Definition: raw_ostream.h:252
raw_ostream & operator<<(char C)
Definition: raw_ostream.h:180
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
Definition: raw_ostream.h:303
raw_ostream & write(unsigned char C)
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:124
void operator=(const raw_ostream &)=delete
virtual raw_ostream & reverseColor()
Reverses the foreground and background colors.
Definition: raw_ostream.h:306
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:293
raw_ostream & operator<<(const SmallVectorImpl< char > &Str)
Definition: raw_ostream.h:257
static constexpr Colors RED
Definition: raw_ostream.h:100
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:406
void pwrite(const char *Ptr, size_t Size, uint64_t Offset)
Definition: raw_ostream.h:414
raw_pwrite_stream(bool Unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
Definition: raw_ostream.h:411
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:572
std::string & str()
Returns the string's reference.
Definition: raw_ostream.h:590
void reserveExtraSpace(uint64_t ExtraSize) override
If possible, pre-allocate ExtraSize bytes for stream data.
Definition: raw_ostream.h:592
raw_string_ostream(std::string &O)
Definition: raw_ostream.h:583
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:602
raw_svector_ostream(SmallVectorImpl< char > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:618
~raw_svector_ostream() override=default
void reserveExtraSpace(uint64_t ExtraSize) override
If possible, pre-allocate ExtraSize bytes for stream data.
Definition: raw_ostream.h:629
std::string_view str() const
Return a std::string_view for the vector contents.
Definition: raw_ostream.h:627
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:672
raw_usvector_ostream(SmallVectorImpl< uint8_t > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:688
std::span< uint8_t > array()
Return an std::span for the vector contents.
Definition: raw_ostream.h:697
std::span< const uint8_t > array() const
Definition: raw_ostream.h:698
~raw_usvector_ostream() override=default
A raw_ostream that writes to a vector.
Definition: raw_ostream.h:706
~raw_uvector_ostream() override=default
std::span< const uint8_t > array() const
Definition: raw_ostream.h:732
raw_uvector_ostream(std::vector< uint8_t > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:722
std::span< uint8_t > array()
Return a std::span for the vector contents.
Definition: raw_ostream.h:731
A raw_ostream that writes to a vector.
Definition: raw_ostream.h:639
std::string_view str()
Return a std::string_view for the vector contents.
Definition: raw_ostream.h:664
~raw_vector_ostream() override=default
raw_vector_ostream(std::vector< char > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:655
basic_string_view< char > string_view
Definition: core.h:501
OpenFlags
Definition: fs.h:64
FileAccess
Definition: fs.h:59
CreationDisposition
Definition: fs.h:37
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_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
std::enable_if_t<!std::is_reference_v< OStream > &&std::is_base_of_v< raw_ostream, OStream >, OStream && > operator<<(OStream &&OS, const T &Value)
Call the appropriate insertion operator, given an rvalue reference to a raw_ostream object and return...
Definition: raw_ostream.h:398