00001 // Torc - Copyright 2011 University of Southern California. All Rights Reserved. 00002 // $HeadURL: https://torc-isi.svn.sourceforge.net/svnroot/torc-isi/branches/staging/0.9/src/torc/architecture/DigestStream.hpp $ 00003 // $Id: DigestStream.hpp 10 2011-10-12 18:40:16Z nsteiner $ 00004 00005 // This program is free software: you can redistribute it and/or modify it under the terms of the 00006 // GNU General Public License as published by the Free Software Foundation, either version 3 of the 00007 // License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 00010 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 00011 // the GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License along with this program. If 00014 // not, see <http://www.gnu.org/licenses/>. 00015 00016 /// \file 00017 /// \brief Header for the DigestStream class. 00018 00019 #ifndef TORC_ARCHITECTURE_DIGESTSTREAM_HPP 00020 #define TORC_ARCHITECTURE_DIGESTSTREAM_HPP 00021 00022 #include "torc/common/Endian.hpp" 00023 #include <boost/filesystem/convenience.hpp> 00024 #include <boost/cstdint.hpp> 00025 #include "torc/externals/zlib/contrib/iostream3/zfstream.h" 00026 00027 namespace torc { 00028 namespace architecture { 00029 00030 /// \brief Encapsulation of a device or family digest stream. 00031 /// \details DigestStreams are used to read family and device database files. 00032 class DigestStream : public gzifstream { 00033 protected: 00034 // types 00035 /// \brief Imported type name. 00036 typedef std::string string; 00037 /// \brief Imported type name. 00038 typedef boost::uint16_t uint16_t; 00039 /// \brief Imported type name. 00040 typedef boost::uint32_t uint32_t; 00041 /// \brief Definition of a digest section header. 00042 typedef char DigestSectionHeader[16]; 00043 // members 00044 /// \brief The number of bytes read. 00045 size_t mBytesRead; 00046 public: 00047 /// \brief Public constructor. 00048 DigestStream(const boost::filesystem::path& inPath) 00049 : gzifstream(inPath.string().c_str(), ios_base::in), mBytesRead(0) {} 00050 // accessors 00051 /// \brief Returns the number of bytes read. 00052 size_t getBytesRead(void) const { return mBytesRead; } 00053 // functions 00054 /// \brief Read and return a section header. 00055 /// \param outHeader The section header that was read from the stream. 00056 void readSectionHeader(string& outHeader) { 00057 DigestSectionHeader sectionHeader; 00058 gzifstream::read(static_cast<char*>(sectionHeader), sizeof(sectionHeader)); 00059 mBytesRead += sizeof(sectionHeader); 00060 outHeader.assign(sectionHeader, sizeof(sectionHeader)); 00061 } 00062 /// \brief Read and return a uint8_t. 00063 /// \param outValue The uint8_t that was read from the stream. 00064 std::istream& read(uint8_t& outValue) { 00065 gzifstream::read(reinterpret_cast<char*>(&outValue), sizeof(uint8_t)); 00066 //outValue = ntohs(outValue); // no endian conversion needed for single bytes 00067 mBytesRead += sizeof(uint8_t); 00068 return *this; 00069 } 00070 /// \brief Read and return a uint16_t. 00071 /// \param outValue The uint16_t that was read from the stream. 00072 std::istream& read(uint16_t& outValue) { 00073 gzifstream::read(reinterpret_cast<char*>(&outValue), sizeof(uint16_t)); 00074 outValue = ntohs(outValue); // endian conversion 00075 mBytesRead += sizeof(uint16_t); 00076 return *this; 00077 } 00078 /// \brief Read and return a uint32_t. 00079 /// \param outValue The uint32_t that was read from the stream. 00080 std::istream& read(uint32_t& outValue) { 00081 gzifstream::read(reinterpret_cast<char*>(&outValue), sizeof(uint32_t)); 00082 outValue = ntohl(outValue); // endian conversion 00083 mBytesRead += sizeof(uint32_t); 00084 return *this; 00085 } 00086 /// \brief Read and return a character string. 00087 /// \details This overrides the superclass behavior exclusively for the sake of tracking 00088 /// the number of bytes read. 00089 /// \param s The character pointer to read into. 00090 /// \param n The number of characters to read. 00091 std::istream& read(char* s, std::streamsize n) { 00092 gzifstream::read(s, n); 00093 mBytesRead += n; 00094 return *this; 00095 } 00096 }; 00097 00098 } // namespace architecture 00099 } // namespace torc 00100 00101 #endif // TORC_ARCHITECTURE_DIGESTSTREAM_HPP