1 //===--- raw_ostream.h - Raw output stream ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the raw_ostream class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_RAW_OSTREAM_H
15 #define LLVM_SUPPORT_RAW_OSTREAM_H
17 #include "llvm/ADT/StringExtras.h"
24 class format_object_base;
26 class SmallVectorImpl;
28 /// raw_ostream - This class implements an extremely fast bulk output stream
29 /// that can *only* output to a stream. It does not support seeking, reopening,
30 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
31 /// a chunk at a time.
34 /// The buffer is handled in such a way that the buffer is
35 /// uninitialized, unbuffered, or out of space when OutBufCur >=
36 /// OutBufEnd. Thus a single comparison suffices to determine if we
37 /// need to take the slow path to write a single character.
39 /// The buffer is in one of three states:
40 /// 1. Unbuffered (Unbuffered == true)
41 /// 1. Uninitialized (Unbuffered == false && OutBufStart == 0).
42 /// 2. Buffered (Unbuffered == false && OutBufStart != 0 &&
43 /// OutBufEnd - OutBufStart >= 64).
44 char *OutBufStart, *OutBufEnd, *OutBufCur;
48 // color order matches ANSI escape sequence, don't change
61 explicit raw_ostream(bool unbuffered=false) : Unbuffered(unbuffered) {
62 // Start out ready to flush.
63 OutBufStart = OutBufEnd = OutBufCur = 0;
66 virtual ~raw_ostream() {
67 delete [] OutBufStart;
70 /// tell - Return the current offset with the file.
71 uint64_t tell() { return current_pos() + GetNumBytesInBuffer(); }
73 //===--------------------------------------------------------------------===//
74 // Configuration Interface
75 //===--------------------------------------------------------------------===//
77 /// SetBufferSize - Set the internal buffer size to the specified amount
78 /// instead of the default.
79 void SetBufferSize(unsigned Size=4096) {
81 "Buffer size must be somewhat large for invariants to hold");
84 delete [] OutBufStart;
85 OutBufStart = new char[Size];
86 OutBufEnd = OutBufStart+Size;
87 OutBufCur = OutBufStart;
91 /// SetUnbuffered - Set the streams buffering status. When
92 /// unbuffered the stream will flush after every write. This routine
93 /// will also flush the buffer immediately when the stream is being
94 /// set to unbuffered.
95 void SetUnbuffered() {
98 delete [] OutBufStart;
99 OutBufStart = OutBufEnd = OutBufCur = 0;
103 unsigned GetNumBytesInBuffer() const {
104 return OutBufCur - OutBufStart;
107 //===--------------------------------------------------------------------===//
108 // Data Output Interface
109 //===--------------------------------------------------------------------===//
112 if (OutBufCur != OutBufStart)
116 raw_ostream &operator<<(char C) {
117 if (OutBufCur >= OutBufEnd)
123 raw_ostream &operator<<(unsigned char C) {
124 if (OutBufCur >= OutBufEnd)
130 raw_ostream &operator<<(signed char C) {
131 if (OutBufCur >= OutBufEnd)
137 raw_ostream &operator<<(const char *Str) {
138 // Inline fast path, particulary for constant strings where a
139 // sufficiently smart compiler will simplify strlen.
141 unsigned Size = strlen(Str);
143 // Make sure we can use the fast path.
144 if (OutBufCur+Size > OutBufEnd)
145 return write(Str, Size);
147 memcpy(OutBufCur, Str, Size);
152 raw_ostream &operator<<(const std::string& Str) {
153 write(Str.data(), Str.length());
157 raw_ostream &operator<<(unsigned long N);
158 raw_ostream &operator<<(long N);
159 raw_ostream &operator<<(unsigned long long N);
160 raw_ostream &operator<<(long long N);
161 raw_ostream &operator<<(const void *P);
162 raw_ostream &operator<<(unsigned int N) {
163 this->operator<<(static_cast<unsigned long>(N));
167 raw_ostream &operator<<(int N) {
168 this->operator<<(static_cast<long>(N));
172 raw_ostream &operator<<(double N) {
173 this->operator<<(ftostr(N));
177 raw_ostream &write(unsigned char C);
178 raw_ostream &write(const char *Ptr, unsigned Size);
180 // Formatted output, see the format() function in Support/Format.h.
181 raw_ostream &operator<<(const format_object_base &Fmt);
183 /// Changes the foreground color of text that will be output from this point
185 /// @param colors ANSI color to use, the special SAVEDCOLOR can be used to
186 /// change only the bold attribute, and keep colors untouched
187 /// @param bold bold/brighter text, default false
188 /// @param bg if true change the background, default: change foreground
189 /// @returns itself so it can be used within << invocations
190 virtual raw_ostream &changeColor(enum Colors colors, bool bold=false,
191 bool bg=false) { return *this; }
193 /// Resets the colors to terminal defaults. Call this when you are done
194 /// outputting colored text, or before program exit.
195 virtual raw_ostream &resetColor() { return *this; }
197 //===--------------------------------------------------------------------===//
198 // Subclass Interface
199 //===--------------------------------------------------------------------===//
202 /// write_impl - The is the piece of the class that is implemented
203 /// by subclasses. This writes the \args Size bytes starting at
204 /// \arg Ptr to the underlying stream.
206 /// \invariant { Size > 0 }
207 virtual void write_impl(const char *Ptr, unsigned Size) = 0;
209 // An out of line virtual method to provide a home for the class vtable.
210 virtual void handle();
212 /// current_pos - Return the current position within the stream, not
213 /// counting the bytes currently in the buffer.
214 virtual uint64_t current_pos() = 0;
216 //===--------------------------------------------------------------------===//
218 //===--------------------------------------------------------------------===//
220 /// flush_nonempty - Flush the current buffer, which is known to be
221 /// non-empty. This outputs the currently buffered data and resets
222 /// the buffer to empty.
223 void flush_nonempty();
226 //===----------------------------------------------------------------------===//
227 // File Output Streams
228 //===----------------------------------------------------------------------===//
230 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
232 class raw_fd_ostream : public raw_ostream {
237 /// write_impl - See raw_ostream::write_impl.
238 virtual void write_impl(const char *Ptr, unsigned Size);
240 /// current_pos - Return the current position within the stream, not
241 /// counting the bytes currently in the buffer.
242 virtual uint64_t current_pos() { return pos; }
245 /// raw_fd_ostream - Open the specified file for writing. If an
246 /// error occurs, information about the error is put into ErrorInfo,
247 /// and the stream should be immediately destroyed; the string will
248 /// be empty if no error occurred.
250 /// \param Filename - The file to open. If this is "-" then the
251 /// stream will use stdout instead.
252 /// \param Binary - The file should be opened in binary mode on
253 /// platforms that support this distinction.
254 /// \param Force - Don't consider the case where the file already
255 /// exists to be an error.
256 raw_fd_ostream(const char *Filename, bool Binary, bool Force,
257 std::string &ErrorInfo);
259 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
260 /// ShouldClose is true, this closes the file when the stream is destroyed.
261 raw_fd_ostream(int fd, bool shouldClose,
262 bool unbuffered=false) : raw_ostream(unbuffered), FD(fd),
263 ShouldClose(shouldClose) {}
267 /// close - Manually flush the stream and close the file.
270 /// tell - Return the current offset with the file.
271 uint64_t tell() { return pos + GetNumBytesInBuffer(); }
273 /// seek - Flushes the stream and repositions the underlying file descriptor
274 /// positition to the offset specified from the beginning of the file.
275 uint64_t seek(uint64_t off);
277 virtual raw_ostream &changeColor(enum Colors colors, bool bold=false,
279 virtual raw_ostream &resetColor();
282 /// raw_stdout_ostream - This is a stream that always prints to stdout.
284 class raw_stdout_ostream : public raw_fd_ostream {
285 // An out of line virtual method to provide a home for the class vtable.
286 virtual void handle();
288 raw_stdout_ostream();
291 /// raw_stderr_ostream - This is a stream that always prints to stderr.
293 class raw_stderr_ostream : public raw_fd_ostream {
294 // An out of line virtual method to provide a home for the class vtable.
295 virtual void handle();
297 raw_stderr_ostream();
300 /// outs() - This returns a reference to a raw_ostream for standard output.
301 /// Use it like: outs() << "foo" << "bar";
304 /// errs() - This returns a reference to a raw_ostream for standard error.
305 /// Use it like: errs() << "foo" << "bar";
309 //===----------------------------------------------------------------------===//
310 // Output Stream Adaptors
311 //===----------------------------------------------------------------------===//
313 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
314 /// simple adaptor class. It does check for I/O errors; clients should use
315 /// the underlying stream to detect errors.
316 class raw_os_ostream : public raw_ostream {
319 /// write_impl - See raw_ostream::write_impl.
320 virtual void write_impl(const char *Ptr, unsigned Size);
322 /// current_pos - Return the current position within the stream, not
323 /// counting the bytes currently in the buffer.
324 virtual uint64_t current_pos();
327 raw_os_ostream(std::ostream &O) : OS(O) {}
330 /// tell - Return the current offset with the stream.
334 /// raw_string_ostream - A raw_ostream that writes to an std::string. This is a
335 /// simple adaptor class.
336 class raw_string_ostream : public raw_ostream {
339 /// write_impl - See raw_ostream::write_impl.
340 virtual void write_impl(const char *Ptr, unsigned Size);
342 /// current_pos - Return the current position within the stream, not
343 /// counting the bytes currently in the buffer.
344 virtual uint64_t current_pos() { return OS.size(); }
346 raw_string_ostream(std::string &O) : OS(O) {}
347 ~raw_string_ostream();
349 /// tell - Return the current offset with the stream.
350 uint64_t tell() { return OS.size() + GetNumBytesInBuffer(); }
352 /// str - Flushes the stream contents to the target string and returns
353 /// the string's reference.
360 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
361 /// SmallString. This is a simple adaptor class.
362 class raw_svector_ostream : public raw_ostream {
363 SmallVectorImpl<char> &OS;
365 /// write_impl - See raw_ostream::write_impl.
366 virtual void write_impl(const char *Ptr, unsigned Size);
368 /// current_pos - Return the current position within the stream, not
369 /// counting the bytes currently in the buffer.
370 virtual uint64_t current_pos();
372 raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
373 ~raw_svector_ostream();
375 /// tell - Return the current offset with the stream.
379 } // end llvm namespace