David Greene | 7ffbb50 | 2009-12-23 16:08:15 +0000 | [diff] [blame^] | 1 | //===- circulat_raw_ostream.cpp - Implement the circular_raw_ostream class -===// |
| 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 implements support for circular buffered streams. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/circular_raw_ostream.h" |
| 15 | |
| 16 | #include <algorithm> |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | void circular_raw_ostream::write_impl(const char *Ptr, size_t Size) { |
| 21 | if (BufferSize == 0) { |
| 22 | TheStream->write(Ptr, Size); |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | // Write into the buffer, wrapping if necessary. |
| 27 | while (Size != 0) { |
| 28 | unsigned Bytes = std::min(Size, BufferSize - (Cur - BufferArray)); |
| 29 | memcpy(Cur, Ptr, Bytes); |
| 30 | Size -= Bytes; |
| 31 | Cur += Bytes; |
| 32 | if (Cur == BufferArray + BufferSize) { |
| 33 | // Reset the output pointer to the start of the buffer. |
| 34 | Cur = BufferArray; |
| 35 | Filled = true; |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void circular_raw_ostream::flushBufferWithBanner(void) { |
| 41 | if (BufferSize != 0) { |
| 42 | // Write out the buffer |
| 43 | int num = std::strlen(Banner); |
| 44 | TheStream->write(Banner, num); |
| 45 | flushBuffer(); |
| 46 | } |
| 47 | } |