blob: a5cf629b0718a553f2b387bcc1c2f107067b603c [file] [log] [blame]
Chris Lattner60d39622008-08-17 01:35:29 +00001//===--- raw_ostream.cpp - Implement the raw_ostream classes --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements support for bulk buffered stream output.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/raw_ostream.h"
Chris Lattner07f51f72008-08-17 04:13:37 +000015#include <ostream>
Chris Lattner60d39622008-08-17 01:35:29 +000016using namespace llvm;
17
Chris Lattner60d39622008-08-17 01:35:29 +000018#include <fcntl.h>
Argyrios Kyrtzidisb68dc362008-08-17 09:25:21 +000019
20#if defined(_MSC_VER)
Chris Lattner60d39622008-08-17 01:35:29 +000021#include <io.h>
Owen Andersoncb371882008-08-21 00:14:44 +000022#ifndef STDIN_FILENO
23# define STDIN_FILENO 0
24#endif
25#ifndef STDOUT_FILENO
26# define STDOUT_FILENO 1
27#endif
28#ifndef STDERR_FILENO
29# define STDERR_FILENO 2
30#endif
Chris Lattner60d39622008-08-17 01:35:29 +000031#endif
32
33// An out of line virtual method to provide a home for the class vtable.
34void raw_ostream::handle() {}
35
Owen Anderson66b17ba2008-08-21 20:58:52 +000036raw_ostream &raw_ostream::operator<<(unsigned long N) {
37 // Zero is a special case.
38 if (N == 0)
39 return *this << '0';
40
41 char NumberBuffer[20];
42 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
43 char *CurPtr = EndPtr;
44
45 while (N) {
46 *--CurPtr = '0' + char(N % 10);
47 N /= 10;
48 }
49 return write(CurPtr, EndPtr-CurPtr);
50}
51
52raw_ostream &raw_ostream::operator<<(long N) {
53 if (N < 0) {
54 if (OutBufCur >= OutBufEnd)
55 flush_impl();
56 *OutBufCur++ = '-';
57
58 N = -N;
59 }
60
61 return this->operator<<(static_cast<unsigned long>(N));
62}
63
64raw_ostream &raw_ostream::operator<<(unsigned long long N) {
65 // Zero is a special case.
66 if (N == 0)
67 return *this << '0';
68
69 char NumberBuffer[20];
70 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
71 char *CurPtr = EndPtr;
72
73 while (N) {
74 *--CurPtr = '0' + char(N % 10);
75 N /= 10;
76 }
77 return write(CurPtr, EndPtr-CurPtr);
78}
79
80raw_ostream &raw_ostream::operator<<(long long N) {
81 if (N < 0) {
82 if (OutBufCur >= OutBufEnd)
83 flush_impl();
84 *OutBufCur++ = '-';
85
86 N = -N;
87 }
88
89 return this->operator<<(static_cast<unsigned long long>(N));
90}
91
92raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) {
93 if (OutBufCur+Size > OutBufEnd)
94 flush_impl();
95
96 // Handle short strings specially, memcpy isn't very good at very short
97 // strings.
98 switch (Size) {
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
103 case 0: break;
104 default:
105 // Normally the string to emit is shorter than the buffer.
106 if (Size <= unsigned(OutBufEnd-OutBufStart)) {
107 memcpy(OutBufCur, Ptr, Size);
108 break;
109 }
110
111 // If emitting a string larger than our buffer, emit in chunks. In this
112 // case we know that we just flushed the buffer.
113 while (Size) {
114 unsigned NumToEmit = OutBufEnd-OutBufStart;
115 if (Size < NumToEmit) NumToEmit = Size;
116 assert(OutBufCur == OutBufStart);
117 memcpy(OutBufStart, Ptr, NumToEmit);
118 Ptr += NumToEmit;
Owen Andersonfd2a0532008-08-21 22:39:33 +0000119 Size -= NumToEmit;
Owen Anderson66b17ba2008-08-21 20:58:52 +0000120 OutBufCur = OutBufStart + NumToEmit;
121 flush_impl();
122 }
123 break;
124 }
125 OutBufCur += Size;
126 return *this;
127}
128
Chris Lattner60d39622008-08-17 01:35:29 +0000129//===----------------------------------------------------------------------===//
130// raw_fd_ostream
131//===----------------------------------------------------------------------===//
132
133/// raw_fd_ostream - Open the specified file for writing. If an error occurs,
134/// information about the error is put into ErrorInfo, and the stream should
135/// be immediately destroyed.
136raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo) {
Chris Lattnerc52b1282008-08-17 03:53:23 +0000137 // Handle "-" as stdout.
138 if (Filename[0] == '-' && Filename[1] == 0) {
139 FD = STDOUT_FILENO;
140 ShouldClose = false;
141 return;
142 }
143
Chris Lattner60d39622008-08-17 01:35:29 +0000144 FD = open(Filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
145 if (FD < 0) {
146 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
147 ShouldClose = false;
148 } else {
149 ShouldClose = true;
150 }
151}
152
153raw_fd_ostream::~raw_fd_ostream() {
154 flush();
155 if (ShouldClose)
156 close(FD);
157}
158
159void raw_fd_ostream::flush_impl() {
160 if (OutBufCur-OutBufStart)
Chris Lattnerd497df52008-08-17 01:46:05 +0000161 ::write(FD, OutBufStart, OutBufCur-OutBufStart);
Chris Lattner60d39622008-08-17 01:35:29 +0000162 HandleFlush();
163}
164
Chris Lattner07f51f72008-08-17 04:13:37 +0000165//===----------------------------------------------------------------------===//
166// raw_stdout/err_ostream
167//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000168
169raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
170raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false) {}
171
172// An out of line virtual method to provide a home for the class vtable.
173void raw_stdout_ostream::handle() {}
174void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000175
176/// outs() - This returns a reference to a raw_ostream for standard output.
177/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000178raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000179 static raw_stdout_ostream S;
180 return S;
181}
182
183/// errs() - This returns a reference to a raw_ostream for standard error.
184/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000185raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000186 static raw_stderr_ostream S;
187 return S;
188}
189
190//===----------------------------------------------------------------------===//
191// raw_os_ostream
192//===----------------------------------------------------------------------===//
193
194/// flush_impl - The is the piece of the class that is implemented by
195/// subclasses. This outputs the currently buffered data and resets the
196/// buffer to empty.
197void raw_os_ostream::flush_impl() {
198 if (OutBufCur-OutBufStart)
199 OS.write(OutBufStart, OutBufCur-OutBufStart);
200 HandleFlush();
201}