Home | History | Annotate | Download | only in quipper
      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_DATA_READER_H_
      6 #define CHROMIUMOS_WIDE_PROFILING_DATA_READER_H_
      7 
      8 #include <stddef.h>
      9 #include <stdint.h>
     10 
     11 #include "binary_data_utils.h"
     12 #include "compat/string.h"
     13 
     14 namespace quipper {
     15 
     16 class DataReader {
     17  public:
     18   DataReader() : is_cross_endian_(false) {}
     19   virtual ~DataReader() {}
     20 
     21   // Moves the data read pointer to |offset| bytes from the beginning of the
     22   // data.
     23   virtual void SeekSet(size_t offset) = 0;
     24 
     25   // Returns the position of the data read pointer, in bytes from the beginning
     26   // of the data.
     27   virtual size_t Tell() const = 0;
     28 
     29   virtual size_t size() const { return size_; }
     30 
     31   // Reads raw data into |dest|. Returns true if it managed to read |size|
     32   // bytes.
     33   virtual bool ReadData(const size_t size, void* dest) = 0;
     34 
     35   // Reads raw data into a string.
     36   virtual bool ReadDataString(const size_t size, string* dest);
     37 
     38   // Like ReadData(), but prints an error if it doesn't read all |size| bytes.
     39   virtual bool ReadDataValue(const size_t size, const string& value_name,
     40                              void* dest);
     41 
     42   // Read integers with endian swapping.
     43   bool ReadUint16(uint16_t* value) { return ReadIntValue(value); }
     44   bool ReadUint32(uint32_t* value) { return ReadIntValue(value); }
     45   bool ReadUint64(uint64_t* value) { return ReadIntValue(value); }
     46 
     47   // Read a string. Returns true if it managed to read |size| bytes (excluding
     48   // null terminator). The actual string may be shorter than the number of bytes
     49   // requested.
     50   virtual bool ReadString(const size_t size, string* str) = 0;
     51 
     52   // Reads a string from data into |dest| at the current offset. The string in
     53   // data is prefixed with a 32-bit size field. The size() of |*dest| after the
     54   // read will be the null-terminated string length of the underlying string
     55   // data, and not necessarily the same as the size field in the data.
     56   bool ReadStringWithSizeFromData(string* dest);
     57 
     58   bool is_cross_endian() const { return is_cross_endian_; }
     59 
     60   void set_is_cross_endian(bool value) { is_cross_endian_ = value; }
     61 
     62  protected:
     63   // Size of the data source.
     64   size_t size_;
     65 
     66  private:
     67   // Like ReadData(), but used specifically to read integers. Will swap byte
     68   // order if necessary.
     69   // For type-safety this one private and let public member functions call it.
     70   template <typename T>
     71   bool ReadIntValue(T* dest) {
     72     if (!ReadData(sizeof(T), dest)) return false;
     73     *dest = MaybeSwap(*dest, is_cross_endian_);
     74     return true;
     75   }
     76 
     77   // For cross-endian data reading.
     78   bool is_cross_endian_;
     79 };
     80 
     81 }  // namespace quipper
     82 
     83 #endif  // CHROMIUMOS_WIDE_PROFILING_DATA_READER_H_
     84