raw_ostream.h revision 193574
1193323Sed//===--- raw_ostream.h - Raw output stream ----------------------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed//  This file defines the raw_ostream class.
11193323Sed//
12193323Sed//===----------------------------------------------------------------------===//
13193323Sed
14193323Sed#ifndef LLVM_SUPPORT_RAW_OSTREAM_H
15193323Sed#define LLVM_SUPPORT_RAW_OSTREAM_H
16193323Sed
17193323Sed#include "llvm/ADT/StringExtras.h"
18193323Sed#include <cassert>
19193323Sed#include <cstring>
20193323Sed#include <string>
21193323Sed#include <iosfwd>
22193323Sed
23193323Sednamespace llvm {
24193323Sed  class format_object_base;
25193323Sed  template <typename T>
26193323Sed  class SmallVectorImpl;
27193323Sed
28193323Sed/// raw_ostream - This class implements an extremely fast bulk output stream
29193323Sed/// that can *only* output to a stream.  It does not support seeking, reopening,
30193323Sed/// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
31193323Sed/// a chunk at a time.
32193323Sedclass raw_ostream {
33193323Sedprivate:
34193323Sed  /// The buffer is handled in such a way that the buffer is
35193323Sed  /// uninitialized, unbuffered, or out of space when OutBufCur >=
36193323Sed  /// OutBufEnd. Thus a single comparison suffices to determine if we
37193323Sed  /// need to take the slow path to write a single character.
38193323Sed  ///
39193323Sed  /// The buffer is in one of three states:
40193323Sed  ///  1. Unbuffered (Unbuffered == true)
41193323Sed  ///  1. Uninitialized (Unbuffered == false && OutBufStart == 0).
42193323Sed  ///  2. Buffered (Unbuffered == false && OutBufStart != 0 &&
43193323Sed  ///               OutBufEnd - OutBufStart >= 64).
44193323Sed  char *OutBufStart, *OutBufEnd, *OutBufCur;
45193323Sed  bool Unbuffered;
46193323Sed
47193323Sedpublic:
48193574Sed  // color order matches ANSI escape sequence, don't change
49193574Sed  enum Colors {
50193574Sed    BLACK=0,
51193574Sed    RED,
52193574Sed    GREEN,
53193574Sed    YELLOW,
54193574Sed    BLUE,
55193574Sed    MAGENTA,
56193574Sed    CYAN,
57193574Sed    WHITE,
58193574Sed    SAVEDCOLOR
59193574Sed  };
60193574Sed
61193323Sed  explicit raw_ostream(bool unbuffered=false) : Unbuffered(unbuffered) {
62193323Sed    // Start out ready to flush.
63193323Sed    OutBufStart = OutBufEnd = OutBufCur = 0;
64193323Sed  }
65193323Sed
66193323Sed  virtual ~raw_ostream() {
67193323Sed    delete [] OutBufStart;
68193323Sed  }
69193323Sed
70193323Sed  /// tell - Return the current offset with the file.
71193323Sed  uint64_t tell() { return current_pos() + GetNumBytesInBuffer(); }
72193323Sed
73193323Sed  //===--------------------------------------------------------------------===//
74193323Sed  // Configuration Interface
75193323Sed  //===--------------------------------------------------------------------===//
76193323Sed
77193323Sed  /// SetBufferSize - Set the internal buffer size to the specified amount
78193323Sed  /// instead of the default.
79193323Sed  void SetBufferSize(unsigned Size=4096) {
80193323Sed    assert(Size >= 64 &&
81193323Sed           "Buffer size must be somewhat large for invariants to hold");
82193323Sed    flush();
83193323Sed
84193323Sed    delete [] OutBufStart;
85193323Sed    OutBufStart = new char[Size];
86193323Sed    OutBufEnd = OutBufStart+Size;
87193323Sed    OutBufCur = OutBufStart;
88193323Sed    Unbuffered = false;
89193323Sed  }
90193323Sed
91193323Sed  /// SetUnbuffered - Set the streams buffering status. When
92193323Sed  /// unbuffered the stream will flush after every write. This routine
93193323Sed  /// will also flush the buffer immediately when the stream is being
94193323Sed  /// set to unbuffered.
95193323Sed  void SetUnbuffered() {
96193323Sed    flush();
97193323Sed
98193323Sed    delete [] OutBufStart;
99193323Sed    OutBufStart = OutBufEnd = OutBufCur = 0;
100193323Sed    Unbuffered = true;
101193323Sed  }
102193323Sed
103193323Sed  unsigned GetNumBytesInBuffer() const {
104193323Sed    return OutBufCur - OutBufStart;
105193323Sed  }
106193323Sed
107193323Sed  //===--------------------------------------------------------------------===//
108193323Sed  // Data Output Interface
109193323Sed  //===--------------------------------------------------------------------===//
110193323Sed
111193323Sed  void flush() {
112193323Sed    if (OutBufCur != OutBufStart)
113193323Sed      flush_nonempty();
114193323Sed  }
115193323Sed
116193323Sed  raw_ostream &operator<<(char C) {
117193323Sed    if (OutBufCur >= OutBufEnd)
118193323Sed      return write(C);
119193323Sed    *OutBufCur++ = C;
120193323Sed    return *this;
121193323Sed  }
122193323Sed
123193323Sed  raw_ostream &operator<<(unsigned char C) {
124193323Sed    if (OutBufCur >= OutBufEnd)
125193323Sed      return write(C);
126193323Sed    *OutBufCur++ = C;
127193323Sed    return *this;
128193323Sed  }
129193323Sed
130193323Sed  raw_ostream &operator<<(signed char C) {
131193323Sed    if (OutBufCur >= OutBufEnd)
132193323Sed      return write(C);
133193323Sed    *OutBufCur++ = C;
134193323Sed    return *this;
135193323Sed  }
136193323Sed
137193323Sed  raw_ostream &operator<<(const char *Str) {
138193323Sed    // Inline fast path, particulary for constant strings where a
139193323Sed    // sufficiently smart compiler will simplify strlen.
140193323Sed
141193323Sed    unsigned Size = strlen(Str);
142193323Sed
143193323Sed    // Make sure we can use the fast path.
144193323Sed    if (OutBufCur+Size > OutBufEnd)
145193323Sed      return write(Str, Size);
146193323Sed
147193323Sed    memcpy(OutBufCur, Str, Size);
148193323Sed    OutBufCur += Size;
149193323Sed    return *this;
150193323Sed  }
151193323Sed
152193323Sed  raw_ostream &operator<<(const std::string& Str) {
153193323Sed    write(Str.data(), Str.length());
154193323Sed    return *this;
155193323Sed  }
156193323Sed
157193323Sed  raw_ostream &operator<<(unsigned long N);
158193323Sed  raw_ostream &operator<<(long N);
159193323Sed  raw_ostream &operator<<(unsigned long long N);
160193323Sed  raw_ostream &operator<<(long long N);
161193323Sed  raw_ostream &operator<<(const void *P);
162193323Sed  raw_ostream &operator<<(unsigned int N) {
163193323Sed    this->operator<<(static_cast<unsigned long>(N));
164193323Sed    return *this;
165193323Sed  }
166193323Sed
167193323Sed  raw_ostream &operator<<(int N) {
168193323Sed    this->operator<<(static_cast<long>(N));
169193323Sed    return *this;
170193323Sed  }
171193323Sed
172193323Sed  raw_ostream &operator<<(double N) {
173193323Sed    this->operator<<(ftostr(N));
174193323Sed    return *this;
175193323Sed  }
176193323Sed
177193323Sed  raw_ostream &write(unsigned char C);
178193323Sed  raw_ostream &write(const char *Ptr, unsigned Size);
179193323Sed
180193323Sed  // Formatted output, see the format() function in Support/Format.h.
181193323Sed  raw_ostream &operator<<(const format_object_base &Fmt);
182193323Sed
183193574Sed  /// Changes the foreground color of text that will be output from this point
184193574Sed  /// forward.
185193574Sed  /// @param colors ANSI color to use, the special SAVEDCOLOR can be used to
186193574Sed  /// change only the bold attribute, and keep colors untouched
187193574Sed  /// @param bold bold/brighter text, default false
188193574Sed  /// @param bg if true change the background, default: change foreground
189193574Sed  /// @returns itself so it can be used within << invocations
190193574Sed  virtual raw_ostream &changeColor(enum Colors colors, bool bold=false,
191193574Sed                                   bool  bg=false) { return *this; }
192193574Sed
193193574Sed  /// Resets the colors to terminal defaults. Call this when you are done
194193574Sed  /// outputting colored text, or before program exit.
195193574Sed  virtual raw_ostream &resetColor() { return *this; }
196193574Sed
197193323Sed  //===--------------------------------------------------------------------===//
198193323Sed  // Subclass Interface
199193323Sed  //===--------------------------------------------------------------------===//
200193323Sed
201193323Sedprivate:
202193323Sed  /// write_impl - The is the piece of the class that is implemented
203193323Sed  /// by subclasses.  This writes the \args Size bytes starting at
204193323Sed  /// \arg Ptr to the underlying stream.
205193323Sed  ///
206193323Sed  /// \invariant { Size > 0 }
207193323Sed  virtual void write_impl(const char *Ptr, unsigned Size) = 0;
208193323Sed
209193323Sed  // An out of line virtual method to provide a home for the class vtable.
210193323Sed  virtual void handle();
211193323Sed
212193323Sed  /// current_pos - Return the current position within the stream, not
213193323Sed  /// counting the bytes currently in the buffer.
214193323Sed  virtual uint64_t current_pos() = 0;
215193323Sed
216193323Sed  //===--------------------------------------------------------------------===//
217193323Sed  // Private Interface
218193323Sed  //===--------------------------------------------------------------------===//
219193323Sedprivate:
220193323Sed  /// flush_nonempty - Flush the current buffer, which is known to be
221193323Sed  /// non-empty. This outputs the currently buffered data and resets
222193323Sed  /// the buffer to empty.
223193323Sed  void flush_nonempty();
224193323Sed};
225193323Sed
226193323Sed//===----------------------------------------------------------------------===//
227193323Sed// File Output Streams
228193323Sed//===----------------------------------------------------------------------===//
229193323Sed
230193323Sed/// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
231193323Sed///
232193323Sedclass raw_fd_ostream : public raw_ostream {
233193323Sed  int FD;
234193323Sed  bool ShouldClose;
235193323Sed  uint64_t pos;
236193323Sed
237193323Sed  /// write_impl - See raw_ostream::write_impl.
238193323Sed  virtual void write_impl(const char *Ptr, unsigned Size);
239193323Sed
240193323Sed  /// current_pos - Return the current position within the stream, not
241193323Sed  /// counting the bytes currently in the buffer.
242193323Sed  virtual uint64_t current_pos() { return pos; }
243193323Sed
244193323Sedpublic:
245193323Sed  /// raw_fd_ostream - Open the specified file for writing. If an
246193323Sed  /// error occurs, information about the error is put into ErrorInfo,
247193323Sed  /// and the stream should be immediately destroyed; the string will
248193323Sed  /// be empty if no error occurred.
249193323Sed  ///
250193323Sed  /// \param Filename - The file to open. If this is "-" then the
251193323Sed  /// stream will use stdout instead.
252193323Sed  /// \param Binary - The file should be opened in binary mode on
253193323Sed  /// platforms that support this distinction.
254193323Sed  raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
255193323Sed
256193323Sed  /// raw_fd_ostream ctor - FD is the file descriptor that this writes to.  If
257193323Sed  /// ShouldClose is true, this closes the file when the stream is destroyed.
258193323Sed  raw_fd_ostream(int fd, bool shouldClose,
259193323Sed                 bool unbuffered=false) : raw_ostream(unbuffered), FD(fd),
260193323Sed                                          ShouldClose(shouldClose) {}
261193323Sed
262193323Sed  ~raw_fd_ostream();
263193323Sed
264193323Sed  /// close - Manually flush the stream and close the file.
265193323Sed  void close();
266193323Sed
267193323Sed  /// tell - Return the current offset with the file.
268193323Sed  uint64_t tell() { return pos + GetNumBytesInBuffer(); }
269193323Sed
270193323Sed  /// seek - Flushes the stream and repositions the underlying file descriptor
271193323Sed  ///  positition to the offset specified from the beginning of the file.
272193323Sed  uint64_t seek(uint64_t off);
273193574Sed
274193574Sed  virtual raw_ostream &changeColor(enum Colors colors, bool bold=false,
275193574Sed                                   bool bg=false);
276193574Sed  virtual raw_ostream &resetColor();
277193323Sed};
278193323Sed
279193323Sed/// raw_stdout_ostream - This is a stream that always prints to stdout.
280193323Sed///
281193323Sedclass raw_stdout_ostream : public raw_fd_ostream {
282193323Sed  // An out of line virtual method to provide a home for the class vtable.
283193323Sed  virtual void handle();
284193323Sedpublic:
285193323Sed  raw_stdout_ostream();
286193323Sed};
287193323Sed
288193323Sed/// raw_stderr_ostream - This is a stream that always prints to stderr.
289193323Sed///
290193323Sedclass raw_stderr_ostream : public raw_fd_ostream {
291193323Sed  // An out of line virtual method to provide a home for the class vtable.
292193323Sed  virtual void handle();
293193323Sedpublic:
294193323Sed  raw_stderr_ostream();
295193323Sed};
296193323Sed
297193323Sed/// outs() - This returns a reference to a raw_ostream for standard output.
298193323Sed/// Use it like: outs() << "foo" << "bar";
299193323Sedraw_ostream &outs();
300193323Sed
301193323Sed/// errs() - This returns a reference to a raw_ostream for standard error.
302193323Sed/// Use it like: errs() << "foo" << "bar";
303193323Sedraw_ostream &errs();
304193323Sed
305193323Sed
306193323Sed//===----------------------------------------------------------------------===//
307193323Sed// Output Stream Adaptors
308193323Sed//===----------------------------------------------------------------------===//
309193323Sed
310193323Sed/// raw_os_ostream - A raw_ostream that writes to an std::ostream.  This is a
311193323Sed/// simple adaptor class.
312193323Sedclass raw_os_ostream : public raw_ostream {
313193323Sed  std::ostream &OS;
314193323Sed
315193323Sed  /// write_impl - See raw_ostream::write_impl.
316193323Sed  virtual void write_impl(const char *Ptr, unsigned Size);
317193323Sed
318193323Sed  /// current_pos - Return the current position within the stream, not
319193323Sed  /// counting the bytes currently in the buffer.
320193323Sed  virtual uint64_t current_pos();
321193323Sed
322193323Sedpublic:
323193323Sed  raw_os_ostream(std::ostream &O) : OS(O) {}
324193323Sed  ~raw_os_ostream();
325193323Sed
326193323Sed  /// tell - Return the current offset with the stream.
327193323Sed  uint64_t tell();
328193323Sed};
329193323Sed
330193323Sed/// raw_string_ostream - A raw_ostream that writes to an std::string.  This is a
331193323Sed/// simple adaptor class.
332193323Sedclass raw_string_ostream : public raw_ostream {
333193323Sed  std::string &OS;
334193323Sed
335193323Sed  /// write_impl - See raw_ostream::write_impl.
336193323Sed  virtual void write_impl(const char *Ptr, unsigned Size);
337193323Sed
338193323Sed  /// current_pos - Return the current position within the stream, not
339193323Sed  /// counting the bytes currently in the buffer.
340193323Sed  virtual uint64_t current_pos() { return OS.size(); }
341193323Sedpublic:
342193323Sed  raw_string_ostream(std::string &O) : OS(O) {}
343193323Sed  ~raw_string_ostream();
344193323Sed
345193323Sed  /// tell - Return the current offset with the stream.
346193323Sed  uint64_t tell() { return OS.size() + GetNumBytesInBuffer(); }
347193323Sed
348193323Sed  /// str - Flushes the stream contents to the target string and returns
349193323Sed  ///  the string's reference.
350193323Sed  std::string& str() {
351193323Sed    flush();
352193323Sed    return OS;
353193323Sed  }
354193323Sed};
355193323Sed
356193323Sed/// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
357193323Sed/// SmallString.  This is a simple adaptor class.
358193323Sedclass raw_svector_ostream : public raw_ostream {
359193323Sed  SmallVectorImpl<char> &OS;
360193323Sed
361193323Sed  /// write_impl - See raw_ostream::write_impl.
362193323Sed  virtual void write_impl(const char *Ptr, unsigned Size);
363193323Sed
364193323Sed  /// current_pos - Return the current position within the stream, not
365193323Sed  /// counting the bytes currently in the buffer.
366193323Sed  virtual uint64_t current_pos();
367193323Sedpublic:
368193323Sed  raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
369193323Sed  ~raw_svector_ostream();
370193323Sed
371193323Sed  /// tell - Return the current offset with the stream.
372193323Sed  uint64_t tell();
373193323Sed};
374193323Sed
375193323Sed} // end llvm namespace
376193323Sed
377193323Sed#endif
378