torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Versions.cpp
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 Source for the Versions class.
18 
20 #include <iostream>
21 
22 namespace torc {
23 namespace architecture {
24 
25  size_t Versions::readVersions(DigestStream& inStream, bool inShowVersion) {
26  // prepare to read from the stream
27  size_t bytesReadOffset = inStream.getBytesRead();
28  char scratch[1 << 10]; // scratch read buffer
29  uint16_t nameLength = 0; // length of tile type name
30 
31  // read the "TORC" magic
32  std::string torc("TORC");
33  inStream.read(scratch, 4);
34  scratch[4] = 0;
35  if(!(torc == scratch)) {
36  mErr() << "This is not a valid Torc database. Magic \"" << scratch
37  << "\" does not match expected \"" << torc << "\"." << std::endl;
38  return 0;
39  }
40 
41  // read the database Versions information
42  inStream.read(mFormat.mMajor);
43  inStream.read(mFormat.mMinor);
44  inStream.read(mFormat.mRevision);
45  inStream.read(mFormat.mReserved);
46  // read the database build number
47  inStream.read(mBuild);
48 
49  // if the database version we just read isn't one that we support, we should throw an
50  // exception and/or return now
51 
52  // read the section header
53  std::string versionHeader;
54  inStream.readSectionHeader(versionHeader);
55  /// \todo Throw a proper exception.
56  if(versionHeader != ">>>>Version >>>>") throw -1;
57 
58  // read the vendor Versions information
59  inStream.read(mVendor.mMajor);
60  inStream.read(mVendor.mMinor);
61  inStream.read(mVendor.mRevision);
62  inStream.read(mVendor.mReserved);
63  // read the vendor Versions string
64  inStream.read(nameLength);
65  /// \todo Throw a proper exception.
66  if(nameLength > sizeof(scratch)) throw -1;
67  inStream.read(scratch, nameLength);
68  scratch[nameLength] = 0;
69  mVendorString = scratch;
70 
71  if(inShowVersion) mOut() << "\tDatabase " << static_cast<uint32_t>(mFormat.mMajor)
72  << "." << static_cast<uint32_t>(mFormat.mMinor) << "."
73  << static_cast<uint32_t>(mFormat.mRevision) << " build " << mBuild << ", Vendor "
74  //<< static_cast<uint32_t>(mVendor.mMajor) << "."
75  //<< static_cast<uint32_t>(mVendor.mMinor) << "."
76  //<< static_cast<uint32_t>(mVendor.mRevision)
77  << '"' << mVendorString << '"' << std::endl;
78 
79  // return the number of bytes read
80  return inStream.getBytesRead() - bytesReadOffset;
81  }
82 
83 } // namespace architecture
84 } // namespace torc
size_t readVersions(DigestStream &inStream, bool inShowVersion=false)
Read the version information.
Definition: Versions.cpp:25
std::istream & read(uint8_t &outValue)
Read and return a uint8_t.
ostream & mErr(void)
Returns the database console error stream.
std::string string
DottedVersion mFormat
The database format version.
Definition: Versions.hpp:54
Header for the Versions class.
boost::uint16_t uint16_t
Imported type name.
Definition: Versions.hpp:47
void readSectionHeader(string &outHeader)
Read and return a section header.
ostream & mOut(void)
Returns the database console output stream.
DottedVersion mVendor
The vendor data version.
Definition: Versions.hpp:58
string mVendorString
The vendor data version string.
Definition: Versions.hpp:60
boost::uint32_t uint32_t
Imported type name.
Definition: Versions.hpp:48
uint32_t mBuild
The database build number.
Definition: Versions.hpp:56
Encapsulation of a device or family digest stream.
size_t getBytesRead(void) const
Returns the number of bytes read.