blob: ca0d30db388c0b1941ad5fce0580f544b1343809 [file] [log] [blame]
Chris Lattner5c289f22010-04-09 20:43:54 +00001//===- circular_raw_ostream.cpp - Implement circular_raw_ostream ----------===//
David Greene7ffbb502009-12-23 16:08:15 +00002//
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"
David Greene7ffbb502009-12-23 16:08:15 +000015#include <algorithm>
David Greene7ffbb502009-12-23 16:08:15 +000016using namespace llvm;
17
18void circular_raw_ostream::write_impl(const char *Ptr, size_t Size) {
19 if (BufferSize == 0) {
20 TheStream->write(Ptr, Size);
21 return;
22 }
23
24 // Write into the buffer, wrapping if necessary.
25 while (Size != 0) {
Chris Lattner5c289f22010-04-09 20:43:54 +000026 unsigned Bytes =
27 std::min(unsigned(Size), unsigned(BufferSize - (Cur - BufferArray)));
David Greene7ffbb502009-12-23 16:08:15 +000028 memcpy(Cur, Ptr, Bytes);
29 Size -= Bytes;
30 Cur += Bytes;
31 if (Cur == BufferArray + BufferSize) {
32 // Reset the output pointer to the start of the buffer.
33 Cur = BufferArray;
34 Filled = true;
35 }
36 }
37}
38
Chris Lattner5c289f22010-04-09 20:43:54 +000039void circular_raw_ostream::flushBufferWithBanner() {
David Greene7ffbb502009-12-23 16:08:15 +000040 if (BufferSize != 0) {
41 // Write out the buffer
Chris Lattner5c289f22010-04-09 20:43:54 +000042 TheStream->write(Banner, std::strlen(Banner));
David Greene7ffbb502009-12-23 16:08:15 +000043 flushBuffer();
44 }
45}