summaryrefslogtreecommitdiff
path: root/src/stream.cc
diff options
context:
space:
mode:
authorBen Smith <binjimin@gmail.com>2017-04-16 16:07:03 -0700
committerGitHub <noreply@github.com>2017-04-16 16:07:03 -0700
commita4751fe78f119305fa8927f1bba10aefecc14f3f (patch)
treee339741de3c5b1ce7382601952d0f80a685cd701 /src/stream.cc
parent15c898a5bd3e257e72e4f5b4fdc831c82bf20e83 (diff)
downloadwabt-a4751fe78f119305fa8927f1bba10aefecc14f3f.tar.gz
wabt-a4751fe78f119305fa8927f1bba10aefecc14f3f.tar.bz2
wabt-a4751fe78f119305fa8927f1bba10aefecc14f3f.zip
Refactor Stream/Writer; write as C++ (#399)
Diffstat (limited to 'src/stream.cc')
-rw-r--r--src/stream.cc164
1 files changed, 67 insertions, 97 deletions
diff --git a/src/stream.cc b/src/stream.cc
index 12510e93..0e1b5c43 100644
--- a/src/stream.cc
+++ b/src/stream.cc
@@ -24,140 +24,110 @@
namespace wabt {
-static FileStream s_stdout_stream;
-static FileStream s_stderr_stream;
-
-void init_stream(Stream* stream, Writer* writer, Stream* log_stream) {
- stream->writer = writer;
- stream->offset = 0;
- stream->result = Result::Ok;
- stream->log_stream = log_stream;
+Stream::Stream(Writer* writer, Stream* log_stream)
+ : writer_(writer),
+ offset_(0),
+ result_(Result::Ok),
+ log_stream_(log_stream) {}
+
+void Stream::AddOffset(ssize_t delta) {
+ offset_ += delta;
}
-void init_file_stream_from_existing(FileStream* stream, FILE* file) {
- init_file_writer_existing(&stream->writer, file);
- init_stream(&stream->base, &stream->writer.base, nullptr);
-}
-
-Stream* init_stdout_stream(void) {
- init_file_stream_from_existing(&s_stdout_stream, stdout);
- return &s_stdout_stream.base;
-}
-
-Stream* init_stderr_stream(void) {
- init_file_stream_from_existing(&s_stderr_stream, stderr);
- return &s_stderr_stream.base;
-}
-
-void write_data_at(Stream* stream,
- size_t offset,
- const void* src,
- size_t size,
- PrintChars print_chars,
- const char* desc) {
- if (WABT_FAILED(stream->result))
+void Stream::WriteDataAt(size_t at,
+ const void* src,
+ size_t size,
+ const char* desc,
+ PrintChars print_chars) {
+ if (WABT_FAILED(result_))
return;
- if (stream->log_stream) {
- write_memory_dump(stream->log_stream, src, size, offset, print_chars,
- nullptr, desc);
- }
- if (stream->writer->write_data) {
- stream->result = stream->writer->write_data(offset, src, size,
- stream->writer->user_data);
+ if (log_stream_) {
+ log_stream_->WriteMemoryDump(src, size, at, nullptr, desc, print_chars);
}
+ result_ = writer_->WriteData(at, src, size);
}
-void write_data(Stream* stream,
- const void* src,
- size_t size,
- const char* desc) {
- write_data_at(stream, stream->offset, src, size, PrintChars::No, desc);
- stream->offset += size;
+void Stream::WriteData(const void* src,
+ size_t size,
+ const char* desc,
+ PrintChars print_chars) {
+ WriteDataAt(offset_, src, size, desc, print_chars);
+ offset_ += size;
}
-void move_data(Stream* stream,
- size_t dst_offset,
- size_t src_offset,
- size_t size) {
- if (WABT_FAILED(stream->result))
+void Stream::MoveData(size_t dst_offset, size_t src_offset, size_t size) {
+ if (WABT_FAILED(result_))
return;
- if (stream->log_stream) {
- writef(stream->log_stream, "; move data: [%" PRIzx ", %" PRIzx
- ") -> [%" PRIzx ", %" PRIzx ")\n",
- src_offset, src_offset + size, dst_offset, dst_offset + size);
- }
- if (stream->writer->write_data) {
- stream->result = stream->writer->move_data(dst_offset, src_offset, size,
- stream->writer->user_data);
+ if (log_stream_) {
+ log_stream_->Writef(
+ "; move data: [%" PRIzx ", %" PRIzx ") -> [%" PRIzx ", %" PRIzx ")\n",
+ src_offset, src_offset + size, dst_offset, dst_offset + size);
}
+ result_ = writer_->MoveData(dst_offset, src_offset, size);
}
-void writef(Stream* stream, const char* format, ...) {
+void Stream::Writef(const char* format, ...) {
WABT_SNPRINTF_ALLOCA(buffer, length, format);
- write_data(stream, buffer, length, nullptr);
-}
-
-void write_u8(Stream* stream, uint32_t value, const char* desc) {
- assert(value <= UINT8_MAX);
- uint8_t value8 = value;
- write_data_at(stream, stream->offset, &value8, sizeof(value8), PrintChars::No,
- desc);
- stream->offset += sizeof(value8);
-}
-
-void write_u32(Stream* stream, uint32_t value, const char* desc) {
- write_data_at(stream, stream->offset, &value, sizeof(value), PrintChars::No,
- desc);
- stream->offset += sizeof(value);
-}
-
-void write_u64(Stream* stream, uint64_t value, const char* desc) {
- write_data_at(stream, stream->offset, &value, sizeof(value), PrintChars::No,
- desc);
- stream->offset += sizeof(value);
+ WriteData(buffer, length);
}
-void write_memory_dump(Stream* stream,
- const void* start,
- size_t size,
- size_t offset,
- PrintChars print_chars,
- const char* prefix,
- const char* desc) {
+void Stream::WriteMemoryDump(const void* start,
+ size_t size,
+ size_t offset,
+ const char* prefix,
+ const char* desc,
+ PrintChars print_chars) {
const uint8_t* p = static_cast<const uint8_t*>(start);
const uint8_t* end = p + size;
while (p < end) {
const uint8_t* line = p;
const uint8_t* line_end = p + DUMP_OCTETS_PER_LINE;
if (prefix)
- writef(stream, "%s", prefix);
- writef(stream, "%07" PRIzx ": ", reinterpret_cast<intptr_t>(p) -
- reinterpret_cast<intptr_t>(start) +
- offset);
+ Writef("%s", prefix);
+ Writef("%07" PRIzx ": ", reinterpret_cast<intptr_t>(p) -
+ reinterpret_cast<intptr_t>(start) + offset);
while (p < line_end) {
for (int i = 0; i < DUMP_OCTETS_PER_GROUP; ++i, ++p) {
if (p < end) {
- writef(stream, "%02x", *p);
+ Writef("%02x", *p);
} else {
- write_char(stream, ' ');
- write_char(stream, ' ');
+ WriteChar(' ');
+ WriteChar(' ');
}
}
- write_char(stream, ' ');
+ WriteChar(' ');
}
if (print_chars == PrintChars::Yes) {
- write_char(stream, ' ');
+ WriteChar(' ');
p = line;
for (int i = 0; i < DUMP_OCTETS_PER_LINE && p < end; ++i, ++p)
- write_char(stream, isprint(*p) ? *p : '.');
+ WriteChar(isprint(*p) ? *p : '.');
}
/* if there are multiple lines, only print the desc on the last one */
if (p >= end && desc)
- writef(stream, " ; %s", desc);
- write_char(stream, '\n');
+ Writef(" ; %s", desc);
+ WriteChar('\n');
}
}
+MemoryStream::MemoryStream() : Stream(&writer_) {}
+
+FileStream::FileStream(const char* filename)
+ : Stream(&writer_), writer_(filename) {}
+
+FileStream::FileStream(FILE* file) : Stream(&writer_), writer_(file) {}
+
+// static
+std::unique_ptr<FileStream> FileStream::CreateStdout() {
+ return std::unique_ptr<FileStream>(new FileStream(stdout));
+}
+
+// static
+std::unique_ptr<FileStream> FileStream::CreateStderr() {
+ return std::unique_ptr<FileStream>(new FileStream(stderr));
+}
+
+
} // namespace wabt