LLVM API Documentation

FormattedStream.cpp

Go to the documentation of this file.
00001 //===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the implementation of formatted_raw_ostream.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Support/FormattedStream.h"
00015 
00016 using namespace llvm;
00017 
00018 /// CountColumns - Examine the given char sequence and figure out which
00019 /// column we end up in after output.
00020 ///
00021 static unsigned CountColumns(unsigned Column, const char *Ptr, size_t Size) {
00022   // Keep track of the current column by scanning the string for
00023   // special characters
00024 
00025   for (const char *End = Ptr + Size; Ptr != End; ++Ptr) {
00026     ++Column;
00027     if (*Ptr == '\n' || *Ptr == '\r')
00028       Column = 0;
00029     else if (*Ptr == '\t')
00030       // Assumes tab stop = 8 characters.
00031       Column += (8 - (Column & 0x7)) & 0x7;
00032   }
00033 
00034   return Column;
00035 }
00036 
00037 /// ComputeColumn - Examine the current output and figure out which
00038 /// column we end up in after output.
00039 void formatted_raw_ostream::ComputeColumn(const char *Ptr, size_t Size) {
00040   // If our previous scan pointer is inside the buffer, assume we already
00041   // scanned those bytes. This depends on raw_ostream to not change our buffer
00042   // in unexpected ways.
00043   if (Ptr <= Scanned && Scanned <= Ptr + Size) {
00044     // Scan all characters added since our last scan to determine the new
00045     // column.
00046     ColumnScanned = CountColumns(ColumnScanned, Scanned, 
00047                                  Size - (Scanned - Ptr));
00048   } else
00049     ColumnScanned = CountColumns(ColumnScanned, Ptr, Size);
00050 
00051   // Update the scanning pointer.
00052   Scanned = Ptr + Size;
00053 }
00054 
00055 /// PadToColumn - Align the output to some column number.
00056 ///
00057 /// \param NewCol - The column to move to.
00058 /// \param MinPad - The minimum space to give after the most recent
00059 /// I/O, even if the current column + minpad > newcol.
00060 ///
00061 void formatted_raw_ostream::PadToColumn(unsigned NewCol) { 
00062   // Figure out what's in the buffer and add it to the column count.
00063   ComputeColumn(getBufferStart(), GetNumBytesInBuffer());
00064 
00065   // Output spaces until we reach the desired column.
00066   unsigned num = NewCol - ColumnScanned;
00067   if (NewCol < ColumnScanned || num < 1)
00068     num = 1;
00069 
00070   // Keep a buffer of spaces handy to speed up processing.
00071   const char *Spaces = "                                                      "
00072     "                                                                         ";
00073 
00074   assert(num < MAX_COLUMN_PAD && "Unexpectedly large column padding");
00075   write(Spaces, num);
00076 }
00077 
00078 void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) {
00079   // Figure out what's in the buffer and add it to the column count.
00080   ComputeColumn(Ptr, Size);
00081 
00082   // Write the data to the underlying stream (which is unbuffered, so
00083   // the data will be immediately written out).
00084   TheStream->write(Ptr, Size);
00085 
00086   // Reset the scanning pointer.
00087   Scanned = 0;
00088 }
00089 
00090 /// fouts() - This returns a reference to a formatted_raw_ostream for
00091 /// standard output.  Use it like: fouts() << "foo" << "bar";
00092 formatted_raw_ostream &llvm::fouts() {
00093   static formatted_raw_ostream S(outs());
00094   return S;
00095 }
00096 
00097 /// ferrs() - This returns a reference to a formatted_raw_ostream for
00098 /// standard error.  Use it like: ferrs() << "foo" << "bar";
00099 formatted_raw_ostream &llvm::ferrs() {
00100   static formatted_raw_ostream S(errs());
00101   return S;
00102 }