David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 1 | //===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===// |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 1f318e0 | 2009-07-14 23:14:10 +0000 | [diff] [blame] | 10 | // This file contains the implementation of formatted_raw_ostream. |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/FormattedStream.h" |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 15 | using namespace llvm; |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 16 | |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 17 | /// ComputeColumn - Examine the current output and figure out which |
| 18 | /// column we end up in after output. |
| 19 | /// |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame^] | 20 | void formatted_raw_ostream::ComputeColumn(const char *Ptr, size_t Size) { |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 21 | // Keep track of the current column by scanning the string for |
| 22 | // special characters |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 23 | |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 24 | for (const char *epos = Ptr + Size; Ptr != epos; ++Ptr) { |
| 25 | ++Column; |
| 26 | if (*Ptr == '\n' || *Ptr == '\r') |
| 27 | Column = 0; |
| 28 | else if (*Ptr == '\t') |
| 29 | Column += (8 - (Column & 0x7)) & 0x7; |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 30 | } |
| 31 | } |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 32 | |
| 33 | /// PadToColumn - Align the output to some column number. |
| 34 | /// |
| 35 | /// \param NewCol - The column to move to. |
| 36 | /// \param MinPad - The minimum space to give after the most recent |
| 37 | /// I/O, even if the current column + minpad > newcol. |
| 38 | /// |
Chris Lattner | 1f318e0 | 2009-07-14 23:14:10 +0000 | [diff] [blame] | 39 | void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) { |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 40 | flush(); |
| 41 | |
| 42 | // Output spaces until we reach the desired column. |
| 43 | unsigned num = NewCol - Column; |
Chris Lattner | 1f318e0 | 2009-07-14 23:14:10 +0000 | [diff] [blame] | 44 | if (NewCol < Column || num < MinPad) |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 45 | num = MinPad; |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 46 | |
| 47 | // TODO: Write a whole string at a time. |
Chris Lattner | 1f318e0 | 2009-07-14 23:14:10 +0000 | [diff] [blame] | 48 | while (num-- > 0) |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 49 | write(' '); |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 50 | } |
| 51 | |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 52 | /// fouts() - This returns a reference to a formatted_raw_ostream for |
| 53 | /// standard output. Use it like: fouts() << "foo" << "bar"; |
| 54 | formatted_raw_ostream &llvm::fouts() { |
| 55 | static formatted_raw_ostream S(outs()); |
| 56 | return S; |
| 57 | } |
| 58 | |
| 59 | /// ferrs() - This returns a reference to a formatted_raw_ostream for |
| 60 | /// standard error. Use it like: ferrs() << "foo" << "bar"; |
| 61 | formatted_raw_ostream &llvm::ferrs() { |
| 62 | static formatted_raw_ostream S(errs()); |
| 63 | return S; |
| 64 | } |