OpenASIP
2.0
|
#include <BinaryStream.hh>
Public Member Functions | |
BinaryStream (std::ostream &stream, bool littleEndian=false) | |
BinaryStream (std::string name, bool littleEndian=false) | |
virtual | ~BinaryStream () |
Byte | readByte () |
HalfWord | readHalfWord () |
Word | readWord () |
void | readByteBlock (Byte *buffer, unsigned int howmany) |
void | readHalfWordBlock (HalfWord *buffer, unsigned int howmany) |
void | readWordBlock (Word *buffer, unsigned int howmany) |
void | writeByte (Byte byte) |
void | writeHalfWord (HalfWord halfword) |
void | writeWord (Word word) |
void | writeByteBlock (Byte *bytes, unsigned int howmany) |
void | writeHalfWordBlock (HalfWord *hwords, unsigned int howmany) |
void | writeWordBlock (Word *words, unsigned int howmany) |
unsigned int | readPosition () |
unsigned int | writePosition () |
void | setReadPosition (unsigned int position) |
void | setWritePosition (unsigned int position) |
bool | endOfFile () |
unsigned int | sizeOfFile () |
void | setTPEFVersion (TPEFHeaders::TPEFVersion version) |
TPEFHeaders::TPEFVersion | TPEFVersion () const |
Private Member Functions | |
BinaryStream & | operator= (BinaryStream &old) |
Assignment not allowed. More... | |
BinaryStream (BinaryStream &old) | |
Copying not allowed. More... | |
void | openInput (std::string name) |
void | openOutput (std::string name) |
void | close () |
Byte | getByte () |
void | putByte (Byte byte) |
bool | needsSwap () const |
Private Attributes | |
std::ifstream | iStream_ |
The input stream. More... | |
std::ofstream | oStream_ |
The output stream. More... | |
std::string | fileName_ |
The name of the stream. More... | |
std::ostream * | extOStream_ |
Externally given output stream. More... | |
bool | littleEndianStorage_ |
In case we want to store the words in little endian order, big endian otherwise. More... | |
TPEFHeaders::TPEFVersion | tpefVersion_ |
Indicates TPEF format version used. More... | |
Abstracts the input/output binary stream used to read and write TTA programs.
It takes care of opening and closing the streams automatically, and hides possible byte order mismatch.
The bits read from the stream are converted to the byte order of the host machine. Conversely, the bits written into the stream will be converted to the standard byte order of TTA Program Exchange Format files which adheres to the byte order of the ADF the TPEF is associated with (by default big endian).
Definition at line 59 of file BinaryStream.hh.
TPEF::BinaryStream::BinaryStream | ( | std::ostream & | stream, |
bool | littleEndian = false |
||
) |
Definition at line 50 of file BinaryStream.cc.
TPEF::BinaryStream::BinaryStream | ( | std::string | name, |
bool | littleEndian = false |
||
) |
Prepares the binary stream so that it can be read from / written to.
The input string is used as the name of the file to open (if existing) or to create (if not existing).
name | is the name of the input file. |
Definition at line 64 of file BinaryStream.cc.
|
virtual |
Closes the stream.
No other cleanup or deallocation activity is required.
Definition at line 107 of file BinaryStream.cc.
References close().
|
private |
Copying not allowed.
|
private |
Closes the stream.
Definition at line 544 of file BinaryStream.cc.
References iStream_, and oStream_.
Referenced by ~BinaryStream().
bool TPEF::BinaryStream::endOfFile | ( | ) |
Returns true if read position is at the end of file.
UnreachableStream | If stream is bad or otherwise unreachable. |
Definition at line 722 of file BinaryStream.cc.
References __func__, extOStream_, fileName_, iStream_, openInput(), and Exception::setCause().
|
private |
Referenced by readByte(), and readByteBlock().
|
private |
Returns true in case words should be swapped due to mismatch between the host and current target (TTA) endianness.
Definition at line 74 of file BinaryStream.cc.
References HOST_BIGENDIAN, and littleEndianStorage_.
Referenced by readHalfWord(), readWord(), writeHalfWord(), and writeWord().
|
private |
Opens the binary file for input.
name | Name of the input file. |
UnreachableStream | If file is not found or is unreadable. |
Definition at line 484 of file BinaryStream.cc.
References __func__, extOStream_, iStream_, and oStream_.
Referenced by endOfFile(), readPosition(), setReadPosition(), and sizeOfFile().
|
private |
Opens the binary file for output.
If a file doesn't exist, a new empty file will be created.
name | Name of the output file. |
UnreachableStream | If file cannot be opened. |
Definition at line 514 of file BinaryStream.cc.
References __func__, extOStream_, fileName_, and oStream_.
Referenced by setWritePosition(), and writePosition().
|
private |
Assignment not allowed.
|
private |
Referenced by writeByte(), and writeByteBlock().
Byte TPEF::BinaryStream::readByte | ( | ) |
Reads one Byte from the binary stream.
UnreachableStream | If reading from the stream fails. |
EndOfFile | If end of file were reached. |
Definition at line 120 of file BinaryStream.cc.
References __func__, fileName_, getByte(), and Exception::setCause().
Referenced by TPEF::AOutSymbolSectionReader::initializeSymbol(), TPEF::TPEFReader::isMyStreamType(), TPEF::TPEFCodeSectionReader::readAnnotations(), Assembler::readAssemblerCode(), TPEF::TPEFStringSectionReader::readData(), TPEF::TPEFDataSectionReader::readData(), TPEF::TPEFDebugSectionReader::readData(), TPEF::TPEFASpaceSectionReader::readData(), TPEF::TPEFResourceSectionReader::readData(), TPEF::TPEFSymbolSectionReader::readData(), TPEF::TPEFCodeSectionReader::readData(), TPEF::TPEFRelocSectionReader::readData(), TPEF::AOutStringSectionReader::readData(), TPEF::TPEFSectionReader::readData(), TPEF::AOutDataSectionReader::readData(), TPEF::AOutTextSectionReader::readData(), TPEF::TPEFReader::readData(), TPEF::TPEFCodeSectionReader::readId(), and TPEF::TPEFCodeSectionReader::readInfo().
void TPEF::BinaryStream::readByteBlock | ( | Byte * | buffer, |
unsigned int | howmany | ||
) |
Reads a block of Bytes from the binary stream.
buffer | is where the bytes are returned. |
howmany | is how many bytes are supposed to be read. |
UnreachableStream | If reading from the stream fails. |
EndOfFile | If end of file were reached. |
Definition at line 225 of file BinaryStream.cc.
References __func__, fileName_, getByte(), and Exception::setCause().
Referenced by readHalfWord(), and readWord().
HalfWord TPEF::BinaryStream::readHalfWord | ( | ) |
Reads one HalfWord from the binary stream.
UnreachableStream | If reading from the stream fails. |
EndOfFile | If end of file were reached. |
Definition at line 150 of file BinaryStream.cc.
References __func__, fileName_, needsSwap(), readByteBlock(), Exception::setCause(), and TPEF::Swapper::swap().
Referenced by TPEF::AOutTextSectionReader::initializeImmediateMove(), TPEF::AOutTextSectionReader::initializeMove(), TPEF::AOutSymbolSectionReader::initializeSymbol(), TPEF::AOutReader::isMyStreamType(), TPEF::TPEFDebugSectionReader::readData(), TPEF::TPEFSymbolSectionReader::readData(), TPEF::TPEFLineNumSectionReader::readData(), TPEF::TPEFResourceSectionReader::readData(), TPEF::TPEFCodeSectionReader::readData(), TPEF::TPEFSectionReader::readData(), TPEF::TPEFReader::readData(), readHalfWordBlock(), TPEF::AOutReader::readHeader(), TPEF::TPEFCodeSectionReader::readId(), TPEF::TPEFCodeSectionReader::readInfo(), TPEF::TPEFLineNumSectionReader::readInfo(), and TPEF::TPEFRelocSectionReader::readInfo().
void TPEF::BinaryStream::readHalfWordBlock | ( | HalfWord * | buffer, |
unsigned int | howmany | ||
) |
Reads a block of HalfWords from the binary stream.
buffer | is where the halfwords are returned. |
howmany | is how many halfwords are supposed to be read. |
UnreachableStream | If reading from the stream fails. |
EndOfFile | If end of file were reached. |
Definition at line 254 of file BinaryStream.cc.
References __func__, fileName_, readHalfWord(), and Exception::setCause().
unsigned int TPEF::BinaryStream::readPosition | ( | ) |
Returns the current position of the read cursor.
UnreachableStream | If stream is bad or otherwise unreachable. |
Definition at line 561 of file BinaryStream.cc.
References __func__, extOStream_, fileName_, iStream_, openInput(), and Exception::setCause().
Referenced by TPEF::TPEFReader::isMyStreamType(), TPEF::AOutReader::isMyStreamType(), Assembler::readAssemblerCode(), TPEF::BinaryReader::readBinary(), TPEF::TPEFDataSectionReader::readData(), TPEF::TPEFStringSectionReader::readData(), TPEF::TPEFCodeSectionReader::readData(), TPEF::TPEFSectionReader::readData(), TPEF::AOutTextSectionReader::readData(), TPEF::AOutRelocationSectionReader::readData(), TPEF::AOutSymbolSectionReader::readData(), TPEF::TPEFReader::readData(), TPEF::AOutReader::readData(), TPEF::AOutReader::readHeader(), and sizeOfFile().
Word TPEF::BinaryStream::readWord | ( | ) |
Reads one Word from the binary stream.
UnreachableStream | If reading from the stream fails. |
EndOfFile | If end of file were reached. |
Definition at line 187 of file BinaryStream.cc.
References __func__, fileName_, needsSwap(), readByteBlock(), Exception::setCause(), and TPEF::Swapper::swap().
Referenced by TPEF::AOutTextSectionReader::initializeImmediateMove(), TPEF::AOutTextSectionReader::initializeMove(), TPEF::AOutRelocationSectionReader::initializeRelocElement(), TPEF::AOutSymbolSectionReader::initializeSymbol(), TPEF::TPEFDebugSectionReader::readData(), TPEF::TPEFASpaceSectionReader::readData(), TPEF::TPEFLineNumSectionReader::readData(), TPEF::TPEFSymbolSectionReader::readData(), TPEF::TPEFResourceSectionReader::readData(), TPEF::TPEFRelocSectionReader::readData(), TPEF::TPEFSectionReader::readData(), TPEF::TPEFReader::readData(), TPEF::AOutReader::readHeader(), TPEF::TPEFSectionReader::readInfo(), and readWordBlock().
void TPEF::BinaryStream::readWordBlock | ( | Word * | buffer, |
unsigned int | howmany | ||
) |
Reads a block of Words from the binary stream.
buffer | is where the words are returned. |
howmany | is how many words are supposed to be read. |
UnreachableStream | If reading from the stream fails. |
EndOfFile | If end of file were reached. |
Definition at line 283 of file BinaryStream.cc.
References __func__, fileName_, readWord(), and Exception::setCause().
void TPEF::BinaryStream::setReadPosition | ( | unsigned int | position | ) |
Sets the read cursor position in the stream.
If the stream has reached end-of-file, and read position is then set before eof, eof-status is automatically cleared and reading is again possible. Setting position beyond eof doesn't immediately set eof-status, instead, it is only set after trying to read past eof.
position | New read cursor position. |
UnreachableStream | If stream is bad or otherwise unreachable. |
Definition at line 629 of file BinaryStream.cc.
References __func__, extOStream_, fileName_, iStream_, openInput(), and Exception::setCause().
Referenced by TPEF::TPEFReader::isMyStreamType(), TPEF::AOutReader::isMyStreamType(), Assembler::readAssemblerCode(), TPEF::TPEFASpaceSectionReader::readData(), TPEF::TPEFDebugSectionReader::readData(), TPEF::TPEFResourceSectionReader::readData(), TPEF::TPEFLineNumSectionReader::readData(), TPEF::TPEFSymbolSectionReader::readData(), TPEF::TPEFRelocSectionReader::readData(), TPEF::TPEFSectionReader::readData(), TPEF::TPEFReader::readData(), and sizeOfFile().
void TPEF::BinaryStream::setTPEFVersion | ( | TPEFHeaders::TPEFVersion | version | ) |
Sets TPEF version to be used.
version | TPEF format version. |
Definition at line 86 of file BinaryStream.cc.
References tpefVersion_.
Referenced by TPEF::TPEFWriter::actualWriteBinary(), and TPEF::TPEFReader::readData().
void TPEF::BinaryStream::setWritePosition | ( | unsigned int | position | ) |
Sets the write cursor position in the stream.
If write position is located past the end of the file and writing to the stream is attempted, WritePastEOF exception will be thrown.
position | New write cursor position. |
UnreachableStream | If stream is bad or otherwise unreachable. |
Definition at line 689 of file BinaryStream.cc.
References __func__, extOStream_, fileName_, openOutput(), oStream_, and Exception::setCause().
Referenced by TPEF::TPEFWriter::actualWriteBinary(), TPEF::TPEFSectionWriter::actualWriteHeader(), sizeOfFile(), TPEF::SectionOffsetReplacer::tryToReplace(), TPEF::SectionIdReplacer::tryToReplace(), TPEF::SectionIndexReplacer::tryToReplace(), TPEF::FileOffsetReplacer::tryToReplace(), and TPEF::SectionSizeReplacer::tryToReplace().
unsigned int TPEF::BinaryStream::sizeOfFile | ( | ) |
Checks the size of the file being handled.
First tries to check the size from output stream, if it's not opened, tries to check from input stream.
UnreachableStream | If stream is bad or file does not exist. |
Definition at line 764 of file BinaryStream.cc.
References __func__, extOStream_, fileName_, iStream_, openInput(), oStream_, readPosition(), Exception::setCause(), setReadPosition(), setWritePosition(), and writePosition().
Referenced by TPEF::AOutReader::isMyStreamType(), Assembler::readAssemblerCode(), and TPEF::AOutReader::readHeader().
TPEFHeaders::TPEFVersion TPEF::BinaryStream::TPEFVersion | ( | ) | const |
Returns TPEF version being used.
Definition at line 95 of file BinaryStream.cc.
References tpefVersion_.
Referenced by TPEF::TPEFCodeSectionReader::readId(), and TPEF::TPEFCodeSectionWriter::writeId().
void TPEF::BinaryStream::writeByte | ( | Byte | byte | ) |
Writes one Byte to the binary stream.
byte | The Byte to write to the stream. |
UnreachableStream | If writing to stream fails. |
WritePastEOF | If tried to write past end of file. |
Definition at line 310 of file BinaryStream.cc.
References __func__, fileName_, putByte(), and Exception::setCause().
Referenced by TPEF::TPEFWriter::actualWriteBinary(), TPEF::TPEFStringSectionWriter::actualWriteData(), TPEF::TPEFResourceSectionWriter::actualWriteData(), TPEF::TPEFRelocSectionWriter::actualWriteData(), TPEF::TPEFDataSectionWriter::actualWriteData(), TPEF::TPEFDebugSectionWriter::actualWriteData(), TPEF::TPEFSymbolSectionWriter::actualWriteData(), TPEF::TPEFSectionWriter::actualWriteHeader(), TPEF::TPEFCodeSectionWriter::writeAnnotations(), TPEF::TPEFCodeSectionWriter::writeAttributeField(), TPEF::TPEFCodeSectionWriter::writeDataField(), TPEF::TPEFASpaceSectionWriter::writeElement(), TPEF::TPEFCodeSectionWriter::writeId(), TPEF::TPEFCodeSectionWriter::writeInfo(), and TPEF::SectionIndexReplacer::writeReplacement().
void TPEF::BinaryStream::writeByteBlock | ( | Byte * | bytes, |
unsigned int | howmany | ||
) |
Writes a block of Bytes to the binary stream.
bytes | The block of Bytes to be written. |
howmany | How many Bytes to write. |
UnreachableStream | If writing to stream fails. |
WritePastEOF | If tried to write past end of file. |
Definition at line 401 of file BinaryStream.cc.
References __func__, fileName_, putByte(), and Exception::setCause().
Referenced by writeHalfWord(), and writeWord().
void TPEF::BinaryStream::writeHalfWord | ( | HalfWord | halfword | ) |
Writes one HalfWord to the binary stream.
halfword | The HalfWord to write to the stream. |
UnreachableStream | If writing to stream fails. |
WritePastEOF | If tried to write past end of file. |
Definition at line 336 of file BinaryStream.cc.
References __func__, fileName_, needsSwap(), Exception::setCause(), TPEF::Swapper::swap(), and writeByteBlock().
Referenced by TPEF::TPEFWriter::actualWriteBinary(), TPEF::TPEFLineNumSectionWriter::actualWriteData(), TPEF::TPEFResourceSectionWriter::actualWriteData(), TPEF::TPEFDebugSectionWriter::actualWriteData(), TPEF::TPEFSymbolSectionWriter::actualWriteData(), TPEF::TPEFSectionWriter::actualWriteHeader(), TPEF::SectionIdReplacer::tryToReplace(), TPEF::TPEFCodeSectionWriter::writeDataField(), writeHalfWordBlock(), TPEF::TPEFCodeSectionWriter::writeId(), TPEF::TPEFCodeSectionWriter::writeInfo(), TPEF::TPEFLineNumSectionWriter::writeInfo(), TPEF::TPEFRelocSectionWriter::writeInfo(), and TPEF::SectionIndexReplacer::writeReplacement().
void TPEF::BinaryStream::writeHalfWordBlock | ( | HalfWord * | halfwords, |
unsigned int | howmany | ||
) |
Writes a block of HalfWords to the binary stream.
halfwords | The block of HalfWords to be written. |
howmany | How many HalfWords to write. |
UnreachableStream | If writing to stream fails. |
WritePastEOF | If tried to write past end of file. |
Definition at line 429 of file BinaryStream.cc.
References __func__, fileName_, Exception::setCause(), and writeHalfWord().
unsigned int TPEF::BinaryStream::writePosition | ( | ) |
Returns the current position of the write cursor.
UnreachableStream | If stream is bad or otherwise unreachable. |
Definition at line 592 of file BinaryStream.cc.
References __func__, extOStream_, fileName_, openOutput(), oStream_, and Exception::setCause().
Referenced by TPEF::TPEFWriter::actualWriteBinary(), TPEF::TPEFStringSectionWriter::actualWriteData(), TPEF::TPEFLineNumSectionWriter::actualWriteData(), TPEF::TPEFResourceSectionWriter::actualWriteData(), TPEF::TPEFASpaceSectionWriter::actualWriteData(), TPEF::TPEFRelocSectionWriter::actualWriteData(), TPEF::TPEFDataSectionWriter::actualWriteData(), TPEF::TPEFDebugSectionWriter::actualWriteData(), TPEF::TPEFCodeSectionWriter::actualWriteData(), TPEF::TPEFSymbolSectionWriter::actualWriteData(), TPEF::TPEFSectionWriter::actualWriteHeader(), sizeOfFile(), and TPEF::ValueReplacer::ValueReplacer().
void TPEF::BinaryStream::writeWord | ( | Word | word | ) |
Writes one Word to the binary stream.
word | The Word to write to the stream. |
UnreachableStream | If writing to stream fails. |
WritePastEOF | If tried to write past end of file. |
Definition at line 368 of file BinaryStream.cc.
References __func__, fileName_, needsSwap(), Exception::setCause(), TPEF::Swapper::swap(), and writeByteBlock().
Referenced by TPEF::TPEFWriter::actualWriteBinary(), TPEF::TPEFResourceSectionWriter::actualWriteData(), TPEF::TPEFDebugSectionWriter::actualWriteData(), TPEF::TPEFSectionWriter::actualWriteHeader(), TPEF::SectionOffsetReplacer::tryToReplace(), TPEF::FileOffsetReplacer::tryToReplace(), TPEF::SectionSizeReplacer::tryToReplace(), TPEF::TPEFSectionWriter::writeBodyStartOffset(), TPEF::TPEFSectionWriter::writeInfo(), TPEF::SectionIndexReplacer::writeReplacement(), TPEF::TPEFNullSectionWriter::writeSize(), TPEF::TPEFUDataSectionWriter::writeSize(), TPEF::TPEFSymbolSectionWriter::writeValueAndSize(), and writeWordBlock().
void TPEF::BinaryStream::writeWordBlock | ( | Word * | words, |
unsigned int | howmany | ||
) |
Writes a block of Words to the binary stream.
words | The block of Words to be written. |
howmany | How many Words to write. |
UnreachableStream | If writing to stream fails. |
WritePastEOF | If tried to write past end of file. |
Definition at line 457 of file BinaryStream.cc.
References __func__, fileName_, Exception::setCause(), and writeWord().
|
private |
Externally given output stream.
Definition at line 100 of file BinaryStream.hh.
Referenced by endOfFile(), openInput(), openOutput(), readPosition(), setReadPosition(), setWritePosition(), sizeOfFile(), and writePosition().
|
private |
The name of the stream.
Definition at line 97 of file BinaryStream.hh.
Referenced by endOfFile(), openOutput(), readByte(), readByteBlock(), readHalfWord(), readHalfWordBlock(), readPosition(), readWord(), readWordBlock(), setReadPosition(), setWritePosition(), sizeOfFile(), writeByte(), writeByteBlock(), writeHalfWord(), writeHalfWordBlock(), writePosition(), writeWord(), and writeWordBlock().
|
private |
The input stream.
Definition at line 93 of file BinaryStream.hh.
Referenced by close(), endOfFile(), openInput(), readPosition(), setReadPosition(), and sizeOfFile().
|
private |
In case we want to store the words in little endian order, big endian otherwise.
Definition at line 104 of file BinaryStream.hh.
Referenced by needsSwap().
|
private |
The output stream.
Definition at line 95 of file BinaryStream.hh.
Referenced by close(), openInput(), openOutput(), setWritePosition(), sizeOfFile(), and writePosition().
|
private |
Indicates TPEF format version used.
Definition at line 107 of file BinaryStream.hh.
Referenced by setTPEFVersion(), and TPEFVersion().