torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DigestStream.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for the DigestStream class.
18 
19 #ifndef TORC_ARCHITECTURE_DIGESTSTREAM_HPP
20 #define TORC_ARCHITECTURE_DIGESTSTREAM_HPP
21 
22 #include "torc/common/Endian.hpp"
23 #include <boost/filesystem/convenience.hpp>
24 #include <boost/cstdint.hpp>
25 #include "torc/externals/zlib/contrib/iostream3/zfstream.h"
26 
27 namespace torc {
28 namespace architecture {
29 
30  /// \brief Encapsulation of a device or family digest stream.
31  /// \details DigestStreams are used to read family and device database files.
32  class DigestStream : public gzifstream {
33  protected:
34  // types
35  /// \brief Imported type name.
37  /// \brief Imported type name.
38  typedef boost::uint16_t uint16_t;
39  /// \brief Imported type name.
40  typedef boost::uint32_t uint32_t;
41  /// \brief Definition of a digest section header.
42  typedef char DigestSectionHeader[16];
43  // members
44  /// \brief The number of bytes read.
45  size_t mBytesRead;
46  public:
47  /// \brief Public constructor.
49  : gzifstream(inPath.string().c_str(), ios_base::in), mBytesRead(0) {}
50  // accessors
51  /// \brief Returns the number of bytes read.
52  size_t getBytesRead(void) const { return mBytesRead; }
53  // functions
54  /// \brief Read and return a section header.
55  /// \param outHeader The section header that was read from the stream.
56  void readSectionHeader(string& outHeader) {
57  DigestSectionHeader sectionHeader;
58  gzifstream::read(static_cast<char*>(sectionHeader), sizeof(sectionHeader));
59  mBytesRead += sizeof(sectionHeader);
60  outHeader.assign(sectionHeader, sizeof(sectionHeader));
61  }
62  /// \brief Read and return a uint8_t.
63  /// \param outValue The uint8_t that was read from the stream.
64  std::istream& read(uint8_t& outValue) {
65  gzifstream::read(reinterpret_cast<char*>(&outValue), sizeof(uint8_t));
66  //outValue = ntohs(outValue); // no endian conversion needed for single bytes
67  mBytesRead += sizeof(uint8_t);
68  return *this;
69  }
70  /// \brief Read and return a uint16_t.
71  /// \param outValue The uint16_t that was read from the stream.
72  std::istream& read(uint16_t& outValue) {
73  gzifstream::read(reinterpret_cast<char*>(&outValue), sizeof(uint16_t));
74  outValue = ntohs(outValue); // endian conversion
75  mBytesRead += sizeof(uint16_t);
76  return *this;
77  }
78  /// \brief Read and return a uint32_t.
79  /// \param outValue The uint32_t that was read from the stream.
80  std::istream& read(uint32_t& outValue) {
81  gzifstream::read(reinterpret_cast<char*>(&outValue), sizeof(uint32_t));
82  outValue = ntohl(outValue); // endian conversion
83  mBytesRead += sizeof(uint32_t);
84  return *this;
85  }
86  /// \brief Read and return a character string.
87  /// \details This overrides the superclass behavior exclusively for the sake of tracking
88  /// the number of bytes read.
89  /// \param s The character pointer to read into.
90  /// \param n The number of characters to read.
91  std::istream& read(char* s, std::streamsize n) {
92  gzifstream::read(s, n);
93  mBytesRead += n;
94  return *this;
95  }
96  };
97 
98 } // namespace architecture
99 } // namespace torc
100 
101 #endif // TORC_ARCHITECTURE_DIGESTSTREAM_HPP
std::istream & read(uint8_t &outValue)
Read and return a uint8_t.
boost::uint16_t uint16_t
Imported type name.
DigestStream(const boost::filesystem::path &inPath)
Public constructor.
std::istream & read(char *s, std::streamsize n)
Read and return a character string.
std::string string
size_t mBytesRead
The number of bytes read.
std::istream & read(uint32_t &outValue)
Read and return a uint32_t.
boost::filesystem::path path
boost::uint32_t uint32_t
Imported type name.
void readSectionHeader(string &outHeader)
Read and return a section header.
char DigestSectionHeader[16]
Definition of a digest section header.
std::istream & read(uint16_t &outValue)
Read and return a uint16_t.
std::string string
Imported type name.
Encapsulation of a device or family digest stream.
size_t getBytesRead(void) const
Returns the number of bytes read.
Header for endian conversion.