#ifndef LLVM_SUPPORT_RAW_OSTREAM_H
#define LLVM_SUPPORT_RAW_OSTREAM_H
-#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
-#include <cassert>
-#include <cstring>
-#include <string>
-#include <iosfwd>
+#include "llvm/Support/DataTypes.h"
namespace llvm {
class format_object_base;
/// 1. Unbuffered (BufferMode == Unbuffered)
/// 1. Uninitialized (BufferMode != Unbuffered && OutBufStart == 0).
/// 2. Buffered (BufferMode != Unbuffered && OutBufStart != 0 &&
- /// OutBufEnd - OutBufStart >= 64).
+ /// OutBufEnd - OutBufStart >= 1).
///
/// If buffered, then the raw_ostream owns the buffer if (BufferMode ==
/// InternalBuffer); otherwise the buffer has been set via SetBuffer and is
size_t GetBufferSize() {
// If we're supposed to be buffered but haven't actually gotten around
// to allocating the buffer yet, return the value that would be used.
- if (!Unbuffered && !OutBufStart)
+ if (BufferMode != Unbuffered && OutBufStart == 0)
return preferred_buffer_size();
// Otherwise just return the size of the allocated buffer.
raw_ostream &operator<<(const std::string &Str) {
// Avoid the fast path, it would only increase code size for a marginal win.
-
write(Str.data(), Str.length());
return *this;
}
return *this;
}
- raw_ostream &operator<<(double N) {
- this->operator<<(ftostr(N));
- return *this;
- }
+ raw_ostream &operator<<(double N);
/// write_hex - Output \arg N in hexadecimal, without any prefix or padding.
raw_ostream &write_hex(unsigned long long N);
// Formatted output, see the format() function in Support/Format.h.
raw_ostream &operator<<(const format_object_base &Fmt);
+ /// indent - Insert 'NumSpaces' spaces.
+ raw_ostream &indent(unsigned NumSpaces);
+
+
/// Changes the foreground color of text that will be output from this point
/// forward.
/// @param colors ANSI color to use, the special SAVEDCOLOR can be used to
/// outputting colored text, or before program exit.
virtual raw_ostream &resetColor() { return *this; }
+ /// This function determines if this stream is connected to a "tty" or
+ /// "console" window. That is, the output would be displayed to the user
+ /// rather than being put on a pipe or stored in a file.
+ virtual bool is_displayed() const { return false; }
+
//===--------------------------------------------------------------------===//
// Subclass Interface
//===--------------------------------------------------------------------===//
virtual size_t preferred_buffer_size();
public:
- /// raw_fd_ostream - Open the specified file for writing. If an
- /// error occurs, information about the error is put into ErrorInfo,
- /// and the stream should be immediately destroyed; the string will
- /// be empty if no error occurred.
+
+ enum {
+ /// F_Excl - When opening a file, this flag makes raw_fd_ostream
+ /// report an error if the file already exists.
+ F_Excl = 1,
+
+ /// F_Append - When opening a file, if it already exists append to the
+ /// existing file instead of returning an error. This may not be specified
+ /// with F_Excl.
+ F_Append = 2,
+
+ /// F_Binary - The file should be opened in binary mode on platforms that
+ /// make this distinction.
+ F_Binary = 4
+ };
+
+ /// raw_fd_ostream - Open the specified file for writing. If an error occurs,
+ /// information about the error is put into ErrorInfo, and the stream should
+ /// be immediately destroyed; the string will be empty if no error occurred.
+ /// This allows optional flags to control how the file will be opened.
///
/// \param Filename - The file to open. If this is "-" then the
/// stream will use stdout instead.
- /// \param Binary - The file should be opened in binary mode on
- /// platforms that support this distinction.
- /// \param Force - Don't consider the case where the file already
- /// exists to be an error.
- raw_fd_ostream(const char *Filename, bool Binary, bool Force,
- std::string &ErrorInfo);
+ raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
+ unsigned Flags = 0);
/// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
/// ShouldClose is true, this closes the file when the stream is destroyed.
/// close - Manually flush the stream and close the file.
void close();
- /// tell - Return the current offset with the file.
- uint64_t tell() { return pos + GetNumBytesInBuffer(); }
-
/// seek - Flushes the stream and repositions the underlying file descriptor
/// positition to the offset specified from the beginning of the file.
uint64_t seek(uint64_t off);
virtual raw_ostream &changeColor(enum Colors colors, bool bold=false,
bool bg=false);
virtual raw_ostream &resetColor();
+
+ virtual bool is_displayed() const;
};
/// raw_stdout_ostream - This is a stream that always prints to stdout.
// Output Stream Adaptors
//===----------------------------------------------------------------------===//
-/// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
-/// simple adaptor class. It does not check for output errors; clients should
-/// use the underlying stream to detect errors.
-class raw_os_ostream : public raw_ostream {
- std::ostream &OS;
-
- /// write_impl - See raw_ostream::write_impl.
- virtual void write_impl(const char *Ptr, size_t Size);
-
- /// current_pos - Return the current position within the stream, not
- /// counting the bytes currently in the buffer.
- virtual uint64_t current_pos();
-
-public:
- raw_os_ostream(std::ostream &O) : OS(O) {}
- ~raw_os_ostream();
-
- /// tell - Return the current offset with the stream.
- uint64_t tell();
-};
-
/// raw_string_ostream - A raw_ostream that writes to an std::string. This is a
/// simple adaptor class. This class does not encounter output errors.
class raw_string_ostream : public raw_ostream {
explicit raw_string_ostream(std::string &O) : OS(O) {}
~raw_string_ostream();
- /// tell - Return the current offset with the stream.
- uint64_t tell() { return OS.size() + GetNumBytesInBuffer(); }
-
/// str - Flushes the stream contents to the target string and returns
/// the string's reference.
std::string& str() {
/// counting the bytes currently in the buffer.
virtual uint64_t current_pos();
public:
+ /// Construct a new raw_svector_ostream.
+ ///
+ /// \arg O - The vector to write to; this should generally have at least 128
+ /// bytes free to avoid any extraneous memory overhead.
explicit raw_svector_ostream(SmallVectorImpl<char> &O);
~raw_svector_ostream();
- /// tell - Return the current offset with the stream.
- uint64_t tell();
+ /// str - Flushes the stream contents to the target vector and return a
+ /// StringRef for the vector contents.
+ StringRef str();
};
/// raw_null_ostream - A raw_ostream that discards all output.