1 //===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===// 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 contains the implementation of formatted_raw_ostream. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Support/FormattedStream.h" 15 16 using namespace llvm; 17 18 /// ComputeColumn - Examine the current output and figure out which 19 /// column we end up in after output. 20 /// 21 void formatted_raw_ostream::ComputeColumn() { 22 // Keep track of the current column by scanning the string for 23 // special characters 24 25 // 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; 38 } 39 } 40 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 /// 47 void 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(); 50 51 // Output spaces until we reach the desired column. 52 unsigned num = NewCol - ColumnScanned; 53 if (NewCol < ColumnScanned || num < MinPad) 54 num = MinPad; 55 56 // Keep a buffer of spaces handy to speed up processing. 57 const char *Spaces = " " 58 " "; 59 60 assert(num < MAX_COLUMN_PAD && "Unexpectedly large column padding"); 61 62 write(Spaces, num); 63 } 64 65 /// fouts() - This returns a reference to a formatted_raw_ostream for 66 /// standard output. Use it like: fouts() << "foo" << "bar"; 67 formatted_raw_ostream &llvm::fouts() { 68 static formatted_raw_ostream S(outs()); 69 return S; 70 } 71 72 /// ferrs() - This returns a reference to a formatted_raw_ostream for 73 /// standard error. Use it like: ferrs() << "foo" << "bar"; 74 formatted_raw_ostream &llvm::ferrs() { 75 static formatted_raw_ostream S(errs()); 76 return S; 77 } 78