1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===- circular_raw_ostream.cpp - Implement circular_raw_ostream ----------===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This implements support for circular buffered streams.
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/circular_raw_ostream.h"
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <algorithm>
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid circular_raw_ostream::write_impl(const char *Ptr, size_t Size) {
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (BufferSize == 0) {
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TheStream->write(Ptr, Size);
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Write into the buffer, wrapping if necessary.
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (Size != 0) {
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Bytes =
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      std::min(unsigned(Size), unsigned(BufferSize - (Cur - BufferArray)));
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    memcpy(Cur, Ptr, Bytes);
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Size -= Bytes;
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Cur += Bytes;
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Cur == BufferArray + BufferSize) {
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Reset the output pointer to the start of the buffer.
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cur = BufferArray;
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Filled = true;
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid circular_raw_ostream::flushBufferWithBanner() {
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (BufferSize != 0) {
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Write out the buffer
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TheStream->write(Banner, std::strlen(Banner));
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    flushBuffer();
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
46