summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/BinaryStreamWriter.cpp
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2017-03-02 20:52:51 +0000
committerZachary Turner <zturner@google.com>2017-03-02 20:52:51 +0000
commitd9dc2829ea28a065115203ebc648b3be4846aae4 (patch)
tree1f3aea8728608dd4d850e16760eb899715c4d5ff /llvm/lib/Support/BinaryStreamWriter.cpp
parentea25eca04a93bddf064d3aa370fa0cb29d72af3b (diff)
downloadbcm5719-llvm-d9dc2829ea28a065115203ebc648b3be4846aae4.tar.gz
bcm5719-llvm-d9dc2829ea28a065115203ebc648b3be4846aae4.zip
[Support] Move Stream library from MSF -> Support.
After several smaller patches to get most of the core improvements finished up, this patch is a straight move and header fixup of the source. Differential Revision: https://reviews.llvm.org/D30266 llvm-svn: 296810
Diffstat (limited to 'llvm/lib/Support/BinaryStreamWriter.cpp')
-rw-r--r--llvm/lib/Support/BinaryStreamWriter.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/llvm/lib/Support/BinaryStreamWriter.cpp b/llvm/lib/Support/BinaryStreamWriter.cpp
new file mode 100644
index 00000000000..487bcdc5a4d
--- /dev/null
+++ b/llvm/lib/Support/BinaryStreamWriter.cpp
@@ -0,0 +1,59 @@
+//===- BinaryStreamWriter.cpp - Writes objects to a BinaryStream ----------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/BinaryStreamWriter.h"
+
+#include "llvm/Support/BinaryStreamReader.h"
+#include "llvm/Support/BinaryStreamRef.h"
+
+using namespace llvm;
+
+BinaryStreamWriter::BinaryStreamWriter(WritableBinaryStreamRef S)
+ : Stream(S), Offset(0) {}
+
+Error BinaryStreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) {
+ if (auto EC = Stream.writeBytes(Offset, Buffer))
+ return EC;
+ Offset += Buffer.size();
+ return Error::success();
+}
+
+Error BinaryStreamWriter::writeCString(StringRef Str) {
+ if (auto EC = writeFixedString(Str))
+ return EC;
+ if (auto EC = writeObject('\0'))
+ return EC;
+
+ return Error::success();
+}
+
+Error BinaryStreamWriter::writeFixedString(StringRef Str) {
+ return writeBytes(ArrayRef<uint8_t>(Str.bytes_begin(), Str.bytes_end()));
+}
+
+Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref) {
+ return writeStreamRef(Ref, Ref.getLength());
+}
+
+Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref, uint32_t Length) {
+ BinaryStreamReader SrcReader(Ref.slice(0, Length));
+ // This is a bit tricky. If we just call readBytes, we are requiring that it
+ // return us the entire stream as a contiguous buffer. There is no guarantee
+ // this can be satisfied by returning a reference straight from the buffer, as
+ // an implementation may not store all data in a single contiguous buffer. So
+ // we iterate over each contiguous chunk, writing each one in succession.
+ while (SrcReader.bytesRemaining() > 0) {
+ ArrayRef<uint8_t> Chunk;
+ if (auto EC = SrcReader.readLongestContiguousChunk(Chunk))
+ return EC;
+ if (auto EC = writeBytes(Chunk))
+ return EC;
+ }
+ return Error::success();
+}
OpenPOWER on IntegriCloud