16945200846e03779ba57893c7d7014852b1761a
[oota-llvm.git] / include / llvm / Support / raw_ostream.h
1 //===--- raw_ostream.h - Raw output stream --------------------------------===//
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 file defines the raw_ostream class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_RAW_OSTREAM_H
15 #define LLVM_SUPPORT_RAW_OSTREAM_H
16
17 #include <cassert>
18 #include <cstring>
19 #include <string>
20 #include <iosfwd>
21
22 namespace llvm {
23
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.
28 class raw_ostream {
29 protected:
30   char *OutBufStart, *OutBufEnd, *OutBufCur;
31 public:
32   raw_ostream() {
33     // Start out ready to flush.
34     OutBufStart = OutBufEnd = OutBufCur = 0;
35   }
36   virtual ~raw_ostream() {}
37   
38   //===--------------------------------------------------------------------===//
39   // Configuration Interface
40   //===--------------------------------------------------------------------===//
41   
42   /// SetBufferSize - Set the internal buffer size to the specified amount
43   /// instead of the default.
44   void SetBufferSize(unsigned Size) {
45     assert(Size >= 64 &&
46            "Buffer size must be somewhat large for invariants to hold");
47     flush();
48     
49     delete [] OutBufStart;
50     OutBufStart = new char[Size];
51     OutBufEnd = OutBufStart+Size;
52     OutBufCur = OutBufStart;
53   }
54   
55   //===--------------------------------------------------------------------===//
56   // Data Output Interface
57   //===--------------------------------------------------------------------===//
58   
59   void flush() {
60     if (OutBufCur != OutBufStart)
61       flush_impl();
62   }
63   
64   raw_ostream &operator<<(char C) {
65     if (OutBufCur >= OutBufEnd)
66       flush_impl();
67     *OutBufCur++ = C;
68     return *this;
69   }
70   
71   raw_ostream &operator<<(const char *Str) {
72     return write(Str, strlen(Str));
73   }
74   
75   raw_ostream &operator<<(unsigned N) {
76     // Zero is a special case.
77     if (N == 0)
78       return *this << '0';
79     
80     char NumberBuffer[20];
81     char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
82     char *CurPtr = EndPtr;
83     
84     while (N) {
85       *--CurPtr = '0' + char(N % 10);
86       N /= 10;
87     }
88     return write(CurPtr, EndPtr-CurPtr);
89   }
90   
91   
92   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;
119         OutBufCur = OutBufStart + NumToEmit;
120         flush_impl();
121       }
122       break;
123     }
124     OutBufCur += Size;
125     return *this;
126   }
127   
128   //===--------------------------------------------------------------------===//
129   // Subclass Interface
130   //===--------------------------------------------------------------------===//
131
132 protected:
133   
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
136   /// buffer to empty.
137   virtual void flush_impl() = 0;
138   
139   /// HandleFlush - A stream's implementation of flush should call this after
140   /// emitting the bytes to the data sink.
141   void HandleFlush() {
142     if (OutBufStart == 0)
143       SetBufferSize(4096);
144     OutBufCur = OutBufStart;
145   }
146 private:
147   // An out of line virtual method to provide a home for the class vtable.
148   virtual void handle();
149 };
150   
151 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
152 ///
153 class raw_fd_ostream : public raw_ostream {
154   int FD;
155   bool ShouldClose;
156 public:
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);
161   
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) {}
165   
166   ~raw_fd_ostream();
167     
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
170   /// buffer to empty.
171   virtual void flush_impl();
172 };
173   
174 /// raw_stdout_ostream - This is a stream that always prints to stdout.
175 ///
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();
179 public:
180   raw_stdout_ostream();
181 };
182
183 /// raw_stderr_ostream - This is a stream that always prints to stderr.
184 ///
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();
188 public:
189   raw_stderr_ostream();
190 };
191   
192 /// outs() - This returns a reference to a raw_ostream for standard output.
193 /// Use it like: outs() << "foo" << "bar";
194 raw_ostream &outs();
195
196 /// errs() - This returns a reference to a raw_ostream for standard error.
197 /// Use it like: errs() << "foo" << "bar";
198 raw_ostream &errs();
199   
200   
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 {
204   std::ostream &OS;
205 public:
206   raw_os_ostream(std::ostream &O) : OS(O) {}
207   
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
210   /// buffer to empty.
211   virtual void flush_impl();
212 };
213   
214 } // end llvm namespace
215
216 #endif