1// Copyright 2015 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROMIUMOS_WIDE_PROFILING_BUFFER_READER_H_
6#define CHROMIUMOS_WIDE_PROFILING_BUFFER_READER_H_
7
8#include "data_reader.h"
9
10namespace quipper {
11
12// Read from a fixed-size data buffer. Does not take ownership of the buffer.
13class BufferReader : public DataReader {
14 public:
15  // The data source is indicated by |buffer| and is |size| bytes long.
16  BufferReader(const void* buffer, size_t size)
17      : buffer_(reinterpret_cast<const char*>(buffer)), offset_(0) {
18    size_ = size;
19  }
20
21  void SeekSet(size_t offset) override { offset_ = offset; }
22
23  size_t Tell() const override { return offset_; }
24
25  bool ReadData(const size_t size, void* dest) override;
26
27  // Reads |size| bytes of the buffer as a null-terminated string into |str|.
28  // Trailing nulls, if any, are not added to the string, but they are skipped
29  // over. If there is no null terminator within these |size| bytes, then the
30  // string is automatically terminated after |size| bytes.
31  bool ReadString(const size_t size, string* str) override;
32
33 private:
34  // The data buffer from which to read.
35  const char* buffer_;
36
37  // Data read offset from the start of |buffer_|.
38  size_t offset_;
39};
40
41}  // namespace quipper
42
43#endif  // CHROMIUMOS_WIDE_PROFILING_BUFFER_READER_H_
44