1 //===--- raw_ostream.cpp - Implement the raw_ostream classes --------------===//
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 implements support for bulk buffered stream output.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/Support/Format.h"
16 #include "llvm/System/Program.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Config/config.h"
21 #if defined(HAVE_UNISTD_H)
24 #if defined(HAVE_FCNTL_H)
32 # define STDIN_FILENO 0
35 # define STDOUT_FILENO 1
38 # define STDERR_FILENO 2
45 // An out of line virtual method to provide a home for the class vtable.
46 void raw_ostream::handle() {}
48 raw_ostream &raw_ostream::operator<<(unsigned long N) {
49 // Zero is a special case.
53 char NumberBuffer[20];
54 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
55 char *CurPtr = EndPtr;
58 *--CurPtr = '0' + char(N % 10);
61 return write(CurPtr, EndPtr-CurPtr);
64 raw_ostream &raw_ostream::operator<<(long N) {
66 if (OutBufCur >= OutBufEnd)
73 return this->operator<<(static_cast<unsigned long>(N));
76 raw_ostream &raw_ostream::operator<<(unsigned long long N) {
77 // Zero is a special case.
81 char NumberBuffer[20];
82 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
83 char *CurPtr = EndPtr;
86 *--CurPtr = '0' + char(N % 10);
89 return write(CurPtr, EndPtr-CurPtr);
92 raw_ostream &raw_ostream::operator<<(long long N) {
94 if (OutBufCur >= OutBufEnd)
101 return this->operator<<(static_cast<unsigned long long>(N));
104 raw_ostream &raw_ostream::operator<<(const void *P) {
105 // FIXME: This could be much faster if it matters.
106 return *this << format("%p", P);
110 raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) {
111 if (OutBufCur+Size > OutBufEnd)
114 // Handle short strings specially, memcpy isn't very good at very short
117 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
118 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
119 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
120 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
123 // Normally the string to emit is shorter than the buffer.
124 if (Size <= unsigned(OutBufEnd-OutBufStart)) {
125 memcpy(OutBufCur, Ptr, Size);
129 // If emitting a string larger than our buffer, emit in chunks. In this
130 // case we know that we just flushed the buffer.
132 unsigned NumToEmit = OutBufEnd-OutBufStart;
133 if (Size < NumToEmit) NumToEmit = Size;
134 assert(OutBufCur == OutBufStart);
135 memcpy(OutBufStart, Ptr, NumToEmit);
138 OutBufCur = OutBufStart + NumToEmit;
148 raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
149 // If we have more than a few bytes left in our output buffer, try formatting
150 // directly onto its end.
151 unsigned NextBufferSize = 127;
152 if (OutBufEnd-OutBufCur > 3) {
153 unsigned BufferBytesLeft = OutBufEnd-OutBufCur;
154 unsigned BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
156 // Common case is that we have plenty of space.
157 if (BytesUsed < BufferBytesLeft) {
158 OutBufCur += BytesUsed;
162 // Otherwise, we overflowed and the return value tells us the size to try
164 NextBufferSize = BytesUsed;
167 // If we got here, we didn't have enough space in the output buffer for the
168 // string. Try printing into a SmallVector that is resized to have enough
169 // space. Iterate until we win.
170 SmallVector<char, 128> V;
173 V.resize(NextBufferSize);
175 // Try formatting into the SmallVector.
176 unsigned BytesUsed = Fmt.print(&V[0], NextBufferSize);
178 // If BytesUsed fit into the vector, we win.
179 if (BytesUsed <= NextBufferSize)
180 return write(&V[0], BytesUsed);
182 // Otherwise, try again with a new size.
183 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
184 NextBufferSize = BytesUsed;
188 //===----------------------------------------------------------------------===//
190 //===----------------------------------------------------------------------===//
192 // Out of line virtual method.
193 void format_object_base::home() {
196 //===----------------------------------------------------------------------===//
198 //===----------------------------------------------------------------------===//
200 /// raw_fd_ostream - Open the specified file for writing. If an error
201 /// occurs, information about the error is put into ErrorInfo, and the
202 /// stream should be immediately destroyed; the string will be empty
203 /// if no error occurred.
204 raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary,
205 std::string &ErrorInfo) : pos(0) {
208 // Handle "-" as stdout.
209 if (Filename[0] == '-' && Filename[1] == 0) {
211 // If user requested binary then put stdout into binary mode if
214 sys::Program::ChangeStdoutToBinary();
219 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
224 FD = open(Filename, Flags, 0644);
226 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
233 raw_fd_ostream::~raw_fd_ostream() {
241 void raw_fd_ostream::flush_impl() {
242 assert (FD >= 0 && "File already closed.");
243 if (OutBufCur-OutBufStart) {
244 pos += (OutBufCur - OutBufStart);
245 ::write(FD, OutBufStart, OutBufCur-OutBufStart);
250 void raw_fd_ostream::close() {
251 assert (ShouldClose);
258 uint64_t raw_fd_ostream::seek(uint64_t off) {
260 pos = lseek(FD, off, SEEK_SET);
264 //===----------------------------------------------------------------------===//
265 // raw_stdout/err_ostream
266 //===----------------------------------------------------------------------===//
268 raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
269 raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false) {}
271 // An out of line virtual method to provide a home for the class vtable.
272 void raw_stdout_ostream::handle() {}
273 void raw_stderr_ostream::handle() {}
275 /// outs() - This returns a reference to a raw_ostream for standard output.
276 /// Use it like: outs() << "foo" << "bar";
277 raw_ostream &llvm::outs() {
278 static raw_stdout_ostream S;
282 /// errs() - This returns a reference to a raw_ostream for standard error.
283 /// Use it like: errs() << "foo" << "bar";
284 raw_ostream &llvm::errs() {
285 static raw_stderr_ostream S;
289 //===----------------------------------------------------------------------===//
291 //===----------------------------------------------------------------------===//
293 raw_os_ostream::~raw_os_ostream() {
297 /// flush_impl - The is the piece of the class that is implemented by
298 /// subclasses. This outputs the currently buffered data and resets the
300 void raw_os_ostream::flush_impl() {
301 if (OutBufCur-OutBufStart)
302 OS.write(OutBufStart, OutBufCur-OutBufStart);
306 //===----------------------------------------------------------------------===//
307 // raw_string_ostream
308 //===----------------------------------------------------------------------===//
310 raw_string_ostream::~raw_string_ostream() {
314 /// flush_impl - The is the piece of the class that is implemented by
315 /// subclasses. This outputs the currently buffered data and resets the
317 void raw_string_ostream::flush_impl() {
318 if (OutBufCur-OutBufStart)
319 OS.append(OutBufStart, OutBufCur-OutBufStart);
323 //===----------------------------------------------------------------------===//
324 // raw_svector_ostream
325 //===----------------------------------------------------------------------===//
327 raw_svector_ostream::~raw_svector_ostream() {
331 /// flush_impl - The is the piece of the class that is implemented by
332 /// subclasses. This outputs the currently buffered data and resets the
334 void raw_svector_ostream::flush_impl() {
335 if (OutBufCur-OutBufStart)
336 OS.append(OutBufStart, OutBufCur);