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 explicit raw_ostream(bool unbuffered=false) : Unbuffered(unbuffered) {
49 // Start out ready to flush.
50 OutBufStart = OutBufEnd = OutBufCur = 0;
53 virtual ~raw_ostream() {
54 delete [] OutBufStart;
57 /// tell - Return the current offset with the file.
58 uint64_t tell() { return current_pos() + GetNumBytesInBuffer(); }
60 //===--------------------------------------------------------------------===//
61 // Configuration Interface
62 //===--------------------------------------------------------------------===//
64 /// SetBufferSize - Set the internal buffer size to the specified amount
65 /// instead of the default.
66 void SetBufferSize(unsigned Size=4096) {
68 "Buffer size must be somewhat large for invariants to hold");
71 delete [] OutBufStart;
72 OutBufStart = new char[Size];
73 OutBufEnd = OutBufStart+Size;
74 OutBufCur = OutBufStart;
78 /// SetUnbuffered - Set the streams buffering status. When
79 /// unbuffered the stream will flush after every write. This routine
80 /// will also flush the buffer immediately when the stream is being
81 /// set to unbuffered.
82 void SetUnbuffered() {
85 delete [] OutBufStart;
86 OutBufStart = OutBufEnd = OutBufCur = 0;
90 unsigned GetNumBytesInBuffer() const {
91 return OutBufCur - OutBufStart;
94 //===--------------------------------------------------------------------===//
95 // Data Output Interface
96 //===--------------------------------------------------------------------===//
99 if (OutBufCur != OutBufStart)
103 raw_ostream &operator<<(char C) {
104 if (OutBufCur >= OutBufEnd)
110 raw_ostream &operator<<(unsigned char C) {
111 if (OutBufCur >= OutBufEnd)
117 raw_ostream &operator<<(signed char C) {
118 if (OutBufCur >= OutBufEnd)
124 raw_ostream &operator<<(const char *Str) {
125 // Inline fast path, particulary for constant strings where a
126 // sufficiently smart compiler will simplify strlen.
128 unsigned Size = strlen(Str);
130 // Make sure we can use the fast path.
131 if (OutBufCur+Size > OutBufEnd)
132 return write(Str, Size);
134 memcpy(OutBufCur, Str, Size);
139 raw_ostream &operator<<(const std::string& Str) {
140 write(Str.data(), Str.length());
144 raw_ostream &operator<<(unsigned long N);
145 raw_ostream &operator<<(long N);
146 raw_ostream &operator<<(unsigned long long N);
147 raw_ostream &operator<<(long long N);
148 raw_ostream &operator<<(const void *P);
149 raw_ostream &operator<<(unsigned int N) {
150 this->operator<<(static_cast<unsigned long>(N));
154 raw_ostream &operator<<(int N) {
155 this->operator<<(static_cast<long>(N));
159 raw_ostream &operator<<(double N) {
160 this->operator<<(ftostr(N));
164 raw_ostream &write(unsigned char C);
165 raw_ostream &write(const char *Ptr, unsigned Size);
167 // Formatted output, see the format() function in Support/Format.h.
168 raw_ostream &operator<<(const format_object_base &Fmt);
170 //===--------------------------------------------------------------------===//
171 // Subclass Interface
172 //===--------------------------------------------------------------------===//
175 /// write_impl - The is the piece of the class that is implemented
176 /// by subclasses. This writes the \args Size bytes starting at
177 /// \arg Ptr to the underlying stream.
179 /// \invariant { Size > 0 }
180 virtual void write_impl(const char *Ptr, unsigned Size) = 0;
182 // An out of line virtual method to provide a home for the class vtable.
183 virtual void handle();
185 /// current_pos - Return the current position within the stream, not
186 /// counting the bytes currently in the buffer.
187 virtual uint64_t current_pos() = 0;
189 //===--------------------------------------------------------------------===//
191 //===--------------------------------------------------------------------===//
193 /// flush_nonempty - Flush the current buffer, which is known to be
194 /// non-empty. This outputs the currently buffered data and resets
195 /// the buffer to empty.
196 void flush_nonempty();
199 //===----------------------------------------------------------------------===//
200 // File Output Streams
201 //===----------------------------------------------------------------------===//
203 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
205 class raw_fd_ostream : public raw_ostream {
210 /// write_impl - See raw_ostream::write_impl.
211 virtual void write_impl(const char *Ptr, unsigned Size);
213 /// current_pos - Return the current position within the stream, not
214 /// counting the bytes currently in the buffer.
215 virtual uint64_t current_pos() { return pos; }
218 /// raw_fd_ostream - Open the specified file for writing. If an
219 /// error occurs, information about the error is put into ErrorInfo,
220 /// and the stream should be immediately destroyed; the string will
221 /// be empty if no error occurred.
223 /// \param Filename - The file to open. If this is "-" then the
224 /// stream will use stdout instead.
225 /// \param Binary - The file should be opened in binary mode on
226 /// platforms that support this distinction.
227 raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
229 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
230 /// ShouldClose is true, this closes the file when the stream is destroyed.
231 raw_fd_ostream(int fd, bool shouldClose,
232 bool unbuffered=false) : raw_ostream(unbuffered), FD(fd),
233 ShouldClose(shouldClose) {}
237 /// close - Manually flush the stream and close the file.
240 /// tell - Return the current offset with the file.
241 uint64_t tell() { return pos + GetNumBytesInBuffer(); }
243 /// seek - Flushes the stream and repositions the underlying file descriptor
244 /// positition to the offset specified from the beginning of the file.
245 uint64_t seek(uint64_t off);
248 /// raw_stdout_ostream - This is a stream that always prints to stdout.
250 class raw_stdout_ostream : public raw_fd_ostream {
251 // An out of line virtual method to provide a home for the class vtable.
252 virtual void handle();
254 raw_stdout_ostream();
257 /// raw_stderr_ostream - This is a stream that always prints to stderr.
259 class raw_stderr_ostream : public raw_fd_ostream {
260 // An out of line virtual method to provide a home for the class vtable.
261 virtual void handle();
263 raw_stderr_ostream();
266 /// outs() - This returns a reference to a raw_ostream for standard output.
267 /// Use it like: outs() << "foo" << "bar";
270 /// errs() - This returns a reference to a raw_ostream for standard error.
271 /// Use it like: errs() << "foo" << "bar";
275 //===----------------------------------------------------------------------===//
276 // Output Stream Adaptors
277 //===----------------------------------------------------------------------===//
279 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
280 /// simple adaptor class.
281 class raw_os_ostream : public raw_ostream {
284 /// write_impl - See raw_ostream::write_impl.
285 virtual void write_impl(const char *Ptr, unsigned Size);
287 /// current_pos - Return the current position within the stream, not
288 /// counting the bytes currently in the buffer.
289 virtual uint64_t current_pos();
292 raw_os_ostream(std::ostream &O) : OS(O) {}
295 /// tell - Return the current offset with the stream.
299 /// raw_string_ostream - A raw_ostream that writes to an std::string. This is a
300 /// simple adaptor class.
301 class raw_string_ostream : public raw_ostream {
304 /// write_impl - See raw_ostream::write_impl.
305 virtual void write_impl(const char *Ptr, unsigned Size);
307 /// current_pos - Return the current position within the stream, not
308 /// counting the bytes currently in the buffer.
309 virtual uint64_t current_pos() { return OS.size(); }
311 raw_string_ostream(std::string &O) : OS(O) {}
312 ~raw_string_ostream();
314 /// tell - Return the current offset with the stream.
315 uint64_t tell() { return OS.size() + GetNumBytesInBuffer(); }
317 /// str - Flushes the stream contents to the target string and returns
318 /// the string's reference.
325 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
326 /// SmallString. This is a simple adaptor class.
327 class raw_svector_ostream : public raw_ostream {
328 SmallVectorImpl<char> &OS;
330 /// write_impl - See raw_ostream::write_impl.
331 virtual void write_impl(const char *Ptr, unsigned Size);
333 /// current_pos - Return the current position within the stream, not
334 /// counting the bytes currently in the buffer.
335 virtual uint64_t current_pos();
337 raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
338 ~raw_svector_ostream();
340 /// tell - Return the current offset with the stream.
344 } // end llvm namespace