OpenASIP  2.0
BinaryStream.hh
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file BinaryStream.hh
26  *
27  * Declaration of BinaryStream class.
28  *
29  * @author Ari Metsähalme 2003 (ari.metsahalme-no.spam-tut.fi)
30  * @note reviewed 7 August 2003 by pj, am, jn, ao, rl
31  *
32  * @note rating: yellow
33  */
34 
35 #ifndef TTA_BINARYSTREAM_HH
36 #define TTA_BINARYSTREAM_HH
37 
38 #include <fstream>
39 #include <string>
40 #include "TPEFBaseType.hh"
41 #include "Exception.hh"
42 #include "TPEFHeaders.hh"
43 
44 namespace TPEF {
45 
46 /**
47  * Abstracts the input/output binary stream used to read and write TTA
48  * programs.
49  *
50  * It takes care of opening and closing the streams automatically, and
51  * hides possible byte order mismatch.
52  *
53  * The bits read from the stream are converted to the byte order of
54  * the host machine. Conversely, the bits written into the stream will
55  * be converted to the standard byte order of TTA Program Exchange
56  * Format files which adheres to the byte order of the ADF the TPEF
57  * is associated with (by default big endian).
58  */
59 class BinaryStream {
60 public:
61  BinaryStream(std::ostream &stream, bool littleEndian=false);
62  BinaryStream(std::string name, bool littleEndian=false);
63  virtual ~BinaryStream();
64 
65  Byte readByte();
66  HalfWord readHalfWord();
67  Word readWord();
68 
69  void readByteBlock(Byte* buffer, unsigned int howmany);
70  void readHalfWordBlock(HalfWord* buffer, unsigned int howmany);
71  void readWordBlock(Word* buffer, unsigned int howmany);
72 
73  void writeByte(Byte byte);
74  void writeHalfWord(HalfWord halfword);
75  void writeWord(Word word);
76 
77  void writeByteBlock(Byte* bytes, unsigned int howmany);
78  void writeHalfWordBlock(HalfWord* hwords, unsigned int howmany);
79  void writeWordBlock(Word* words, unsigned int howmany);
80 
81  unsigned int readPosition();
82  unsigned int writePosition();
83  void setReadPosition(unsigned int position);
84  void setWritePosition(unsigned int position);
85  bool endOfFile();
86  unsigned int sizeOfFile();
87 
90 
91 private:
92  /// The input stream.
93  std::ifstream iStream_;
94  /// The output stream.
95  std::ofstream oStream_;
96  /// The name of the stream.
97  std::string fileName_;
98 
99  /// Externally given output stream.
100  std::ostream* extOStream_;
101 
102  /// In case we want to store the words in little endian order,
103  /// big endian otherwise.
105 
106  /// Indicates TPEF format version used.
108 
109  /// Assignment not allowed.
111  /// Copying not allowed.
113 
114  void openInput(std::string name);
115  void openOutput(std::string name);
116  void close();
117  Byte getByte();
118  void putByte(Byte byte);
119 
120  bool needsSwap() const;
121 };
122 }
123 
124 #include "BinaryStream.icc"
125 
126 #endif
TPEF::BinaryStream::writeHalfWord
void writeHalfWord(HalfWord halfword)
Definition: BinaryStream.cc:336
TPEF::BinaryStream::readPosition
unsigned int readPosition()
Definition: BinaryStream.cc:561
TPEF::BinaryStream::setReadPosition
void setReadPosition(unsigned int position)
Definition: BinaryStream.cc:629
Exception.hh
TPEF::BinaryStream::operator=
BinaryStream & operator=(BinaryStream &old)
Assignment not allowed.
TPEF::BinaryStream::writeWord
void writeWord(Word word)
Definition: BinaryStream.cc:368
TPEF::BinaryStream::fileName_
std::string fileName_
The name of the stream.
Definition: BinaryStream.hh:97
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::BinaryStream::writeHalfWordBlock
void writeHalfWordBlock(HalfWord *hwords, unsigned int howmany)
Definition: BinaryStream.cc:429
TPEF::BinaryStream::writePosition
unsigned int writePosition()
Definition: BinaryStream.cc:592
TPEFHeaders.hh
TPEF::BinaryStream::readWordBlock
void readWordBlock(Word *buffer, unsigned int howmany)
Definition: BinaryStream.cc:283
Byte
unsigned char Byte
Definition: BaseType.hh:116
TPEF::BinaryStream::putByte
void putByte(Byte byte)
TPEF::BinaryStream::setTPEFVersion
void setTPEFVersion(TPEFHeaders::TPEFVersion version)
Definition: BinaryStream.cc:86
TPEF::BinaryStream::writeByteBlock
void writeByteBlock(Byte *bytes, unsigned int howmany)
Definition: BinaryStream.cc:401
TPEF::BinaryStream::extOStream_
std::ostream * extOStream_
Externally given output stream.
Definition: BinaryStream.hh:100
TPEF::BinaryStream::~BinaryStream
virtual ~BinaryStream()
Definition: BinaryStream.cc:107
TPEF::BinaryStream::littleEndianStorage_
bool littleEndianStorage_
In case we want to store the words in little endian order, big endian otherwise.
Definition: BinaryStream.hh:104
TPEF::BinaryStream::readHalfWordBlock
void readHalfWordBlock(HalfWord *buffer, unsigned int howmany)
Definition: BinaryStream.cc:254
TPEF::TPEFHeaders::TPEFVersion
TPEFVersion
Definition: TPEFHeaders.hh:56
TPEF::BinaryStream::close
void close()
Definition: BinaryStream.cc:544
BinaryStream.icc
TPEF::BinaryStream::setWritePosition
void setWritePosition(unsigned int position)
Definition: BinaryStream.cc:689
TPEF::BinaryStream::iStream_
std::ifstream iStream_
The input stream.
Definition: BinaryStream.hh:93
TPEF::BinaryStream::writeWordBlock
void writeWordBlock(Word *words, unsigned int howmany)
Definition: BinaryStream.cc:457
TPEF::BinaryStream::oStream_
std::ofstream oStream_
The output stream.
Definition: BinaryStream.hh:95
TPEF::BinaryStream::getByte
Byte getByte()
TPEF::BinaryStream::readHalfWord
HalfWord readHalfWord()
Definition: BinaryStream.cc:150
TPEF::BinaryStream::writeByte
void writeByte(Byte byte)
Definition: BinaryStream.cc:310
TPEF::BinaryStream::readByte
Byte readByte()
Definition: BinaryStream.cc:120
TPEF::BinaryStream::endOfFile
bool endOfFile()
Definition: BinaryStream.cc:722
TPEF::BinaryStream::needsSwap
bool needsSwap() const
Definition: BinaryStream.cc:74
TPEF::BinaryStream::BinaryStream
BinaryStream(std::ostream &stream, bool littleEndian=false)
Definition: BinaryStream.cc:50
TPEF::BinaryStream::openInput
void openInput(std::string name)
Definition: BinaryStream.cc:484
TPEF::BinaryStream::TPEFVersion
TPEFHeaders::TPEFVersion TPEFVersion() const
Definition: BinaryStream.cc:95
TPEFBaseType.hh
TPEF::BinaryStream::sizeOfFile
unsigned int sizeOfFile()
Definition: BinaryStream.cc:764
TPEF::BinaryStream::readByteBlock
void readByteBlock(Byte *buffer, unsigned int howmany)
Definition: BinaryStream.cc:225
TPEF::BinaryStream::tpefVersion_
TPEFHeaders::TPEFVersion tpefVersion_
Indicates TPEF format version used.
Definition: BinaryStream.hh:107
TPEF::BinaryStream::readWord
Word readWord()
Definition: BinaryStream.cc:187
TPEF::BinaryStream::openOutput
void openOutput(std::string name)
Definition: BinaryStream.cc:514
TPEF
Definition: Assembler.hh:43