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;
47 /// Error This flag is true if an error of any kind has been detected.
52 // color order matches ANSI escape sequence, don't change
65 explicit raw_ostream(bool unbuffered=false)
66 : Unbuffered(unbuffered), Error(false) {
67 // Start out ready to flush.
68 OutBufStart = OutBufEnd = OutBufCur = 0;
71 virtual ~raw_ostream();
73 /// tell - Return the current offset with the file.
74 uint64_t tell() { return current_pos() + GetNumBytesInBuffer(); }
76 /// has_error - Return the value of the flag in this raw_ostream indicating
77 /// whether an output error has been encountered.
78 bool has_error() const {
82 /// clear_error - Set the flag read by has_error() to false. If the error
83 /// flag is set at the time when this raw_ostream's destructor is called,
84 /// llvm_report_error is called to report the error. Use clear_error()
85 /// after handling the error to avoid this behavior.
90 //===--------------------------------------------------------------------===//
91 // Configuration Interface
92 //===--------------------------------------------------------------------===//
94 /// SetBufferSize - Set the internal buffer size to the specified amount
95 /// instead of the default.
96 void SetBufferSize(size_t Size=4096) {
98 "Buffer size must be somewhat large for invariants to hold");
101 delete [] OutBufStart;
102 OutBufStart = new char[Size];
103 OutBufEnd = OutBufStart+Size;
104 OutBufCur = OutBufStart;
108 /// SetUnbuffered - Set the streams buffering status. When
109 /// unbuffered the stream will flush after every write. This routine
110 /// will also flush the buffer immediately when the stream is being
111 /// set to unbuffered.
112 void SetUnbuffered() {
115 delete [] OutBufStart;
116 OutBufStart = OutBufEnd = OutBufCur = 0;
120 size_t GetNumBytesInBuffer() const {
121 return OutBufCur - OutBufStart;
124 //===--------------------------------------------------------------------===//
125 // Data Output Interface
126 //===--------------------------------------------------------------------===//
129 if (OutBufCur != OutBufStart)
133 raw_ostream &operator<<(char C) {
134 if (OutBufCur >= OutBufEnd)
140 raw_ostream &operator<<(unsigned char C) {
141 if (OutBufCur >= OutBufEnd)
147 raw_ostream &operator<<(signed char C) {
148 if (OutBufCur >= OutBufEnd)
154 raw_ostream &operator<<(const char *Str) {
155 // Inline fast path, particulary for constant strings where a
156 // sufficiently smart compiler will simplify strlen.
158 size_t Size = strlen(Str);
160 // Make sure we can use the fast path.
161 if (OutBufCur+Size > OutBufEnd)
162 return write(Str, Size);
164 memcpy(OutBufCur, Str, Size);
169 raw_ostream &operator<<(const std::string& Str) {
170 write(Str.data(), Str.length());
174 raw_ostream &operator<<(unsigned long N);
175 raw_ostream &operator<<(long N);
176 raw_ostream &operator<<(unsigned long long N);
177 raw_ostream &operator<<(long long N);
178 raw_ostream &operator<<(const void *P);
179 raw_ostream &operator<<(unsigned int N) {
180 this->operator<<(static_cast<unsigned long>(N));
184 raw_ostream &operator<<(int N) {
185 this->operator<<(static_cast<long>(N));
189 raw_ostream &operator<<(double N) {
190 this->operator<<(ftostr(N));
194 raw_ostream &write(unsigned char C);
195 raw_ostream &write(const char *Ptr, size_t Size);
197 // Formatted output, see the format() function in Support/Format.h.
198 raw_ostream &operator<<(const format_object_base &Fmt);
200 /// Changes the foreground color of text that will be output from this point
202 /// @param colors ANSI color to use, the special SAVEDCOLOR can be used to
203 /// change only the bold attribute, and keep colors untouched
204 /// @param bold bold/brighter text, default false
205 /// @param bg if true change the background, default: change foreground
206 /// @returns itself so it can be used within << invocations
207 virtual raw_ostream &changeColor(enum Colors colors, bool bold=false,
208 bool bg=false) { return *this; }
210 /// Resets the colors to terminal defaults. Call this when you are done
211 /// outputting colored text, or before program exit.
212 virtual raw_ostream &resetColor() { return *this; }
214 //===--------------------------------------------------------------------===//
215 // Subclass Interface
216 //===--------------------------------------------------------------------===//
219 /// write_impl - The is the piece of the class that is implemented
220 /// by subclasses. This writes the \args Size bytes starting at
221 /// \arg Ptr to the underlying stream.
223 /// \invariant { Size > 0 }
224 virtual void write_impl(const char *Ptr, size_t Size) = 0;
226 // An out of line virtual method to provide a home for the class vtable.
227 virtual void handle();
229 /// current_pos - Return the current position within the stream, not
230 /// counting the bytes currently in the buffer.
231 virtual uint64_t current_pos() = 0;
234 /// error_detected - Set the flag indicating that an output error has
235 /// been encountered.
236 void error_detected() { Error = true; }
238 //===--------------------------------------------------------------------===//
240 //===--------------------------------------------------------------------===//
242 /// flush_nonempty - Flush the current buffer, which is known to be
243 /// non-empty. This outputs the currently buffered data and resets
244 /// the buffer to empty.
245 void flush_nonempty();
248 //===----------------------------------------------------------------------===//
249 // File Output Streams
250 //===----------------------------------------------------------------------===//
252 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
254 class raw_fd_ostream : public raw_ostream {
259 /// write_impl - See raw_ostream::write_impl.
260 virtual void write_impl(const char *Ptr, size_t Size);
262 /// current_pos - Return the current position within the stream, not
263 /// counting the bytes currently in the buffer.
264 virtual uint64_t current_pos() { return pos; }
267 /// raw_fd_ostream - Open the specified file for writing. If an
268 /// error occurs, information about the error is put into ErrorInfo,
269 /// and the stream should be immediately destroyed; the string will
270 /// be empty if no error occurred.
272 /// \param Filename - The file to open. If this is "-" then the
273 /// stream will use stdout instead.
274 /// \param Binary - The file should be opened in binary mode on
275 /// platforms that support this distinction.
276 /// \param Force - Don't consider the case where the file already
277 /// exists to be an error.
278 raw_fd_ostream(const char *Filename, bool Binary, bool Force,
279 std::string &ErrorInfo);
281 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
282 /// ShouldClose is true, this closes the file when the stream is destroyed.
283 raw_fd_ostream(int fd, bool shouldClose,
284 bool unbuffered=false) : raw_ostream(unbuffered), FD(fd),
285 ShouldClose(shouldClose) {}
289 /// close - Manually flush the stream and close the file.
292 /// tell - Return the current offset with the file.
293 uint64_t tell() { return pos + GetNumBytesInBuffer(); }
295 /// seek - Flushes the stream and repositions the underlying file descriptor
296 /// positition to the offset specified from the beginning of the file.
297 uint64_t seek(uint64_t off);
299 virtual raw_ostream &changeColor(enum Colors colors, bool bold=false,
301 virtual raw_ostream &resetColor();
304 /// raw_stdout_ostream - This is a stream that always prints to stdout.
306 class raw_stdout_ostream : public raw_fd_ostream {
307 // An out of line virtual method to provide a home for the class vtable.
308 virtual void handle();
310 raw_stdout_ostream();
313 /// raw_stderr_ostream - This is a stream that always prints to stderr.
315 class raw_stderr_ostream : public raw_fd_ostream {
316 // An out of line virtual method to provide a home for the class vtable.
317 virtual void handle();
319 raw_stderr_ostream();
322 /// outs() - This returns a reference to a raw_ostream for standard output.
323 /// Use it like: outs() << "foo" << "bar";
326 /// errs() - This returns a reference to a raw_ostream for standard error.
327 /// Use it like: errs() << "foo" << "bar";
331 //===----------------------------------------------------------------------===//
332 // Output Stream Adaptors
333 //===----------------------------------------------------------------------===//
335 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
336 /// simple adaptor class. It does not check for output errors; clients should
337 /// use the underlying stream to detect errors.
338 class raw_os_ostream : public raw_ostream {
341 /// write_impl - See raw_ostream::write_impl.
342 virtual void write_impl(const char *Ptr, size_t Size);
344 /// current_pos - Return the current position within the stream, not
345 /// counting the bytes currently in the buffer.
346 virtual uint64_t current_pos();
349 raw_os_ostream(std::ostream &O) : OS(O) {}
352 /// tell - Return the current offset with the stream.
356 /// raw_string_ostream - A raw_ostream that writes to an std::string. This is a
357 /// simple adaptor class. This class does not encounter output errors.
358 class raw_string_ostream : public raw_ostream {
361 /// write_impl - See raw_ostream::write_impl.
362 virtual void write_impl(const char *Ptr, size_t Size);
364 /// current_pos - Return the current position within the stream, not
365 /// counting the bytes currently in the buffer.
366 virtual uint64_t current_pos() { return OS.size(); }
368 explicit raw_string_ostream(std::string &O) : OS(O) {}
369 ~raw_string_ostream();
371 /// tell - Return the current offset with the stream.
372 uint64_t tell() { return OS.size() + GetNumBytesInBuffer(); }
374 /// str - Flushes the stream contents to the target string and returns
375 /// the string's reference.
382 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
383 /// SmallString. This is a simple adaptor class. This class does not
384 /// encounter output errors.
385 class raw_svector_ostream : public raw_ostream {
386 SmallVectorImpl<char> &OS;
388 /// write_impl - See raw_ostream::write_impl.
389 virtual void write_impl(const char *Ptr, size_t Size);
391 /// current_pos - Return the current position within the stream, not
392 /// counting the bytes currently in the buffer.
393 virtual uint64_t current_pos();
395 explicit raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
396 ~raw_svector_ostream();
398 /// tell - Return the current offset with the stream.
402 } // end llvm namespace