1 //===--- raw_ostream.h - Raw output stream --------------------------------===//
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
24 /// raw_ostream - This class implements an extremely fast bulk output stream
25 /// that can *only* output to a stream. It does not support seeking, reopening,
26 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
27 /// a chunk at a time.
30 char *OutBufStart, *OutBufEnd, *OutBufCur;
33 // Start out ready to flush.
34 OutBufStart = OutBufEnd = OutBufCur = 0;
36 virtual ~raw_ostream() {}
38 //===--------------------------------------------------------------------===//
39 // Configuration Interface
40 //===--------------------------------------------------------------------===//
42 /// SetBufferSize - Set the internal buffer size to the specified amount
43 /// instead of the default.
44 void SetBufferSize(unsigned Size) {
46 "Buffer size must be somewhat large for invariants to hold");
49 delete [] OutBufStart;
50 OutBufStart = new char[Size];
51 OutBufEnd = OutBufStart+Size;
52 OutBufCur = OutBufStart;
55 //===--------------------------------------------------------------------===//
56 // Data Output Interface
57 //===--------------------------------------------------------------------===//
60 if (OutBufCur != OutBufStart)
64 raw_ostream &operator<<(char C) {
65 if (OutBufCur >= OutBufEnd)
71 raw_ostream &operator<<(const char *Str) {
72 return write(Str, strlen(Str));
75 raw_ostream &operator<<(unsigned N) {
76 // Zero is a special case.
80 char NumberBuffer[20];
81 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
82 char *CurPtr = EndPtr;
85 *--CurPtr = '0' + char(N % 10);
88 return write(CurPtr, EndPtr-CurPtr);
92 raw_ostream &write(const char *Ptr, unsigned Size) {
93 if (OutBufCur+Size > OutBufEnd)
96 // Handle short strings specially, memcpy isn't very good at very short
99 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
100 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
101 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
102 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
105 // Normally the string to emit is shorter than the buffer.
106 if (Size <= unsigned(OutBufEnd-OutBufStart)) {
107 memcpy(OutBufCur, Ptr, Size);
111 // If emitting a string larger than our buffer, emit in chunks. In this
112 // case we know that we just flushed the buffer.
114 unsigned NumToEmit = OutBufEnd-OutBufStart;
115 if (Size < NumToEmit) NumToEmit = Size;
116 assert(OutBufCur == OutBufStart);
117 memcpy(OutBufStart, Ptr, NumToEmit);
119 OutBufCur = OutBufStart + NumToEmit;
128 //===--------------------------------------------------------------------===//
129 // Subclass Interface
130 //===--------------------------------------------------------------------===//
134 /// flush_impl - The is the piece of the class that is implemented by
135 /// subclasses. This outputs the currently buffered data and resets the
137 virtual void flush_impl() = 0;
139 /// HandleFlush - A stream's implementation of flush should call this after
140 /// emitting the bytes to the data sink.
142 if (OutBufStart == 0)
144 OutBufCur = OutBufStart;
147 // An out of line virtual method to provide a home for the class vtable.
148 virtual void handle();
151 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
153 class raw_fd_ostream : public raw_ostream {
157 /// raw_fd_ostream - Open the specified file for writing. If an error occurs,
158 /// information about the error is put into ErrorInfo, and the stream should
159 /// be immediately destroyed.
160 raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
162 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
163 /// ShouldClose is true, this closes the file when
164 raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
168 /// flush_impl - The is the piece of the class that is implemented by
169 /// subclasses. This outputs the currently buffered data and resets the
171 virtual void flush_impl();
174 /// raw_stdout_ostream - This is a stream that always prints to stdout.
176 class raw_stdout_ostream : public raw_fd_ostream {
177 // An out of line virtual method to provide a home for the class vtable.
178 virtual void handle();
180 raw_stdout_ostream();
183 /// raw_stderr_ostream - This is a stream that always prints to stderr.
185 class raw_stderr_ostream : public raw_fd_ostream {
186 // An out of line virtual method to provide a home for the class vtable.
187 virtual void handle();
189 raw_stderr_ostream();
192 /// outs() - This returns a reference to a raw_ostream for standard output.
193 /// Use it like: outs() << "foo" << "bar";
196 /// errs() - This returns a reference to a raw_ostream for standard error.
197 /// Use it like: errs() << "foo" << "bar";
201 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
202 /// simple adaptor class.
203 class raw_os_ostream : public raw_ostream {
206 raw_os_ostream(std::ostream &O) : OS(O) {}
208 /// flush_impl - The is the piece of the class that is implemented by
209 /// subclasses. This outputs the currently buffered data and resets the
211 virtual void flush_impl();
214 } // end llvm namespace