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 | // |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame^] | 10 | // This file contains the implementation of formatted_raw_ostream and |
| 11 | // friends. |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Support/FormattedStream.h" |
| 16 | |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame^] | 17 | using namespace llvm; |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 18 | |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame^] | 19 | /// ComputeColumn - Examine the current output and figure out which |
| 20 | /// column we end up in after output. |
| 21 | /// |
| 22 | void formatted_raw_ostream::ComputeColumn(const char *Ptr, unsigned Size) |
| 23 | { |
| 24 | // Keep track of the current column by scanning the string for |
| 25 | // special characters |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 26 | |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame^] | 27 | for (const char *epos = Ptr + Size; Ptr != epos; ++Ptr) { |
| 28 | ++Column; |
| 29 | if (*Ptr == '\n' || *Ptr == '\r') |
| 30 | Column = 0; |
| 31 | else if (*Ptr == '\t') |
| 32 | Column += (8 - (Column & 0x7)) & 0x7; |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 33 | } |
| 34 | } |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame^] | 35 | |
| 36 | /// PadToColumn - Align the output to some column number. |
| 37 | /// |
| 38 | /// \param NewCol - The column to move to. |
| 39 | /// \param MinPad - The minimum space to give after the most recent |
| 40 | /// I/O, even if the current column + minpad > newcol. |
| 41 | /// |
| 42 | void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) |
| 43 | { |
| 44 | flush(); |
| 45 | |
| 46 | // Output spaces until we reach the desired column. |
| 47 | unsigned num = NewCol - Column; |
| 48 | if (NewCol < Column || num < MinPad) { |
| 49 | num = MinPad; |
| 50 | } |
| 51 | |
| 52 | // TODO: Write a whole string at a time. |
| 53 | while (num-- > 0) { |
| 54 | write(' '); |
| 55 | } |
| 56 | } |
| 57 | |