OpenASIP  2.0
TPEFStringSectionReader.cc
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 TPEFStringSectionReader.cc
26  *
27  * Definition of TPEFStringSectionReader class.
28  *
29  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30  *
31  * @note rating: yellow
32  */
33 
35 #include "SectionReader.hh"
36 #include "TPEFBaseType.hh"
37 #include "SafePointer.hh"
38 #include "BinaryStream.hh"
39 #include "StringSection.hh"
40 
41 namespace TPEF {
42 
43 using ReferenceManager::SafePointer;
44 using ReferenceManager::SectionIndexKey;
45 using ReferenceManager::FileOffsetKey;
46 using std::string;
47 
48 TPEFStringSectionReader TPEFStringSectionReader::proto_;
49 
50 /**
51  * Constructor.
52  *
53  * Registers itself to SectionReader.
54  */
57 }
58 
59 /**
60  * Destructor.
61  */
63 }
64 
65 /**
66  * Returns the type of section it is meant to read.
67  *
68  * @return The type of section it can read.
69  */
72  return Section::ST_STRTAB;
73 }
74 
75 /**
76  * Reads section data from TPEF binary file.
77  *
78  * @param stream Stream to be read from.
79  * @param section Section where the information is to be stored.
80  * @exception UnreachableStream If reading of section fails.
81  * @exception KeyAlreadyExists Key was in use when trying to register object.
82  * @exception EndOfFile If end of file were reached while it shouldn't.
83  * @exception OutOfRange Some of read values were out of range.
84  * @exception WrongSubclass Some class couldn't do what it was asked for.
85  * @exception UnexpectedValue If there was unexpected value when reading.
86  */
87 void
89  BinaryStream& stream, Section* section) const {
90  // base classes implementation must be called
91  TPEFSectionReader::readData(stream, section);
92 
93  StringSection* stringSection = dynamic_cast<StringSection*>(section);
94  assert(stringSection != NULL);
95 
96  // check that link section is defined properly
97  assert(header().linkId == 0);
98 
99  if (!section->noBits()) {
100  while (stream.readPosition() <
101  header().bodyOffset + header().bodyLength) {
102 
103  stringSection->addByte(stream.readByte());
104  }
105 
106  assert(stringSection->byte(stringSection->chunk(0)) == 0);
107  }
108 
109  // if nobits flag was set, add zero string to start of section to make
110  // references valid (many sections needs string section
111  // e.g. symbol section).
112  if (stringSection->empty()) {
113  stringSection->addByte(0);
114  }
115 }
116 }
TPEF::TPEFStringSectionReader::proto_
static TPEFStringSectionReader proto_
Prototype instance of TPEFStringSectionReader to be registered to SectionReader.
Definition: TPEFStringSectionReader.hh:63
TPEF::DataSection::byte
virtual Byte byte(const Chunk *chunk) const
Definition: DataSection.cc:114
TPEF::BinaryStream::readPosition
unsigned int readPosition()
Definition: BinaryStream.cc:561
TPEF::TPEFStringSectionReader::~TPEFStringSectionReader
virtual ~TPEFStringSectionReader()
Definition: TPEFStringSectionReader.cc:62
TPEFStringSectionReader.hh
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::TPEFStringSectionReader::type
virtual Section::SectionType type() const
Definition: TPEFStringSectionReader.cc:71
SafePointer.hh
StringSection.hh
TPEF::DataSection::addByte
virtual void addByte(Byte aByte)
Definition: DataSection.cc:200
TPEF::Section
Definition: Section.hh:64
TPEF::StringSection
Definition: StringSection.hh:48
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::TPEFStringSectionReader::TPEFStringSectionReader
TPEFStringSectionReader()
Definition: TPEFStringSectionReader.cc:55
SectionReader.hh
TPEF::SectionReader::registerSectionReader
static void registerSectionReader(const SectionReader *sReader)
Definition: SectionReader.cc:145
TPEF::TPEFSectionReader
Definition: TPEFSectionReader.hh:48
TPEF::BinaryStream::readByte
Byte readByte()
Definition: BinaryStream.cc:120
TPEF::Section::noBits
bool noBits() const
TPEF::TPEFStringSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: TPEFStringSectionReader.cc:88
TPEF::TPEFSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: TPEFSectionReader.cc:86
TPEF::Section::ST_STRTAB
@ ST_STRTAB
String table.
Definition: Section.hh:71
TPEF::RawSection::empty
bool empty() const
BinaryStream.hh
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEFBaseType.hh
TPEF::RawSection::chunk
virtual Chunk * chunk(SectionOffset offset) const
Definition: Section.cc:212
TPEF::TPEFSectionReader::header
static const Header & header()
Definition: TPEFSectionReader.cc:174
TPEF
Definition: Assembler.hh:43