blob: 867e5de4d1295608cd2162da188fb8f5eddb3ee1 [file] [log] [blame]
David Greene191cf282009-07-13 16:49:27 +00001//===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===//
David Greene62fe47a2009-07-10 21:14:44 +00002//
3// The LLVM Compiler Infrastructure
4//
David Greene191cf282009-07-13 16:49:27 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
David Greene62fe47a2009-07-10 21:14:44 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner1f318e02009-07-14 23:14:10 +000010// This file contains the implementation of formatted_raw_ostream.
David Greene62fe47a2009-07-10 21:14:44 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/FormattedStream.h"
David Greeneed0e2ad2009-07-23 23:21:10 +000015
David Greene191cf282009-07-13 16:49:27 +000016using namespace llvm;
David Greene62fe47a2009-07-10 21:14:44 +000017
David Greene191cf282009-07-13 16:49:27 +000018/// ComputeColumn - Examine the current output and figure out which
19/// column we end up in after output.
20///
David Greeneeb857282009-07-29 16:08:27 +000021void formatted_raw_ostream::ComputeColumn() {
David Greene191cf282009-07-13 16:49:27 +000022 // Keep track of the current column by scanning the string for
23 // special characters
David Greene62fe47a2009-07-10 21:14:44 +000024
David Greeneeb857282009-07-29 16:08:27 +000025 // The buffer may have been allocated underneath us.
26 if (Scanned == 0 && GetNumBytesInBuffer() != 0) {
27 Scanned = begin();
28 }
29
30 while (Scanned != end()) {
31 ++ColumnScanned;
32 if (*Scanned == '\n' || *Scanned == '\r')
33 ColumnScanned = 0;
34 else if (*Scanned == '\t')
35 // Assumes tab stop = 8 characters.
36 ColumnScanned += (8 - (ColumnScanned & 0x7)) & 0x7;
37 ++Scanned;
David Greene62fe47a2009-07-10 21:14:44 +000038 }
39}
David Greene191cf282009-07-13 16:49:27 +000040
41/// PadToColumn - Align the output to some column number.
42///
43/// \param NewCol - The column to move to.
44/// \param MinPad - The minimum space to give after the most recent
45/// I/O, even if the current column + minpad > newcol.
46///
David Greeneeb857282009-07-29 16:08:27 +000047void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) {
48 // Figure out what's in the buffer and add it to the column count.
49 ComputeColumn();
David Greene191cf282009-07-13 16:49:27 +000050
51 // Output spaces until we reach the desired column.
David Greeneeb857282009-07-29 16:08:27 +000052 unsigned num = NewCol - ColumnScanned;
53 if (NewCol < ColumnScanned || num < MinPad)
David Greene191cf282009-07-13 16:49:27 +000054 num = MinPad;
David Greene191cf282009-07-13 16:49:27 +000055
David Greeneed0e2ad2009-07-23 23:21:10 +000056 // Keep a buffer of spaces handy to speed up processing.
David Greeneeb857282009-07-29 16:08:27 +000057 const char *Spaces = " "
58 " ";
David Greeneed0e2ad2009-07-23 23:21:10 +000059
60 assert(num < MAX_COLUMN_PAD && "Unexpectedly large column padding");
61
62 write(Spaces, num);
David Greene191cf282009-07-13 16:49:27 +000063}
64
Dan Gohmana4a68c12009-08-15 02:01:04 +000065void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) {
66 ComputeColumn();
67 TheStream->write(Ptr, Size);
68 Scanned = begin();
69}
70
David Greene71847812009-07-14 20:18:05 +000071/// fouts() - This returns a reference to a formatted_raw_ostream for
72/// standard output. Use it like: fouts() << "foo" << "bar";
73formatted_raw_ostream &llvm::fouts() {
74 static formatted_raw_ostream S(outs());
75 return S;
76}
77
78/// ferrs() - This returns a reference to a formatted_raw_ostream for
79/// standard error. Use it like: ferrs() << "foo" << "bar";
80formatted_raw_ostream &llvm::ferrs() {
81 static formatted_raw_ostream S(errs());
82 return S;
83}