OpenASIP  2.0
TPEFStringSectionWriter.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 TPEFStringSectionWriter.cc
26  *
27  * Definition of TPEFStringSectionWriter class.
28  *
29  * @author Jussi Nykänen 2003 (nykanen-no.spam-cs.tut.fi)
30  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
31  *
32  * @note rating: yellow
33  */
34 
36 #include "StringSection.hh"
37 #include "SafePointer.hh"
38 #include "SectionSizeReplacer.hh"
39 #include "ReferenceKey.hh"
40 #include "BinaryStream.hh"
41 
42 namespace TPEF {
43 
44 using ReferenceManager::SafePointer;
45 using ReferenceManager::SectionKey;
46 using ReferenceManager::SectionOffsetKey;
47 using ReferenceManager::FileOffsetKey;
48 
49 
50 const TPEFStringSectionWriter TPEFStringSectionWriter::instance_;
51 
52 /**
53  * Constructor.
54  *
55  * Registers itself to SectionWriter.
56  */
59 }
60 
61 /**
62  * Destructor.
63  */
65 }
66 
67 /**
68  * Returns the type of the section this writer can write.
69  *
70  * @return The type of the section writer can write.
71  */
74  return Section::ST_STRTAB;
75 }
76 
77 /**
78  * Writes the data of the section to the stream.
79  *
80  * @param stream The stream to be written.
81  * @param sect The section which data is written to stream.
82  */
83 void
85  BinaryStream& stream,
86  const Section* sect) const {
87 
88  const StringSection* stringSection =
89  dynamic_cast<const StringSection*>(sect);
90 
91  assert(stringSection != NULL);
92 
93  FileOffset startOffset = stream.writePosition();
94 
95  // file offset to data of section
97  sect->chunk(0));
98 
99  SectionOffset sectOffset = 0;
100  SectionKey sKey = SafePointer::sectionKeyFor(stringSection);
101  SectionId id = sKey.sectionId();
102 
103  // first byte
104  assert(stringSection->byte(stringSection->chunk(0)) == 0);
105 
106  // last byte must be also zero
107  assert(stringSection->byte(
108  stringSection->chunk(
109  stringSection->length() - 1)) == 0);
110 
111  for (unsigned int i = 0; i < stringSection->length(); i++) {
112  sectOffset = stream.writePosition() - startOffset;
113 
114  Chunk* chunk = NULL;
115 
116  try {
117  chunk = stringSection->chunk(sectOffset);
118  } catch (const NotChunkable& n) {
119  bool sectionToBeWrittenIsNotChunkable = false;
120  assert(sectionToBeWrittenIsNotChunkable);
121  }
122 
123  if (SafePointer::isReferenced(chunk)) {
124 
126  SectionOffsetKey(id, sectOffset), chunk);
127  }
128 
129  stream.writeByte(stringSection->byte(chunk));
130  }
131 
133  stringSection, stream.writePosition() - startOffset);
134 }
135 
136 /**
137  * Returns size of char.
138  *
139  * @return Size of char.
140  */
141 Word
143  return sizeof(char);
144 }
145 
146 }
TPEF::SectionId
HalfWord SectionId
Type for storing binary file section ids.
Definition: TPEFBaseType.hh:43
SectionSizeReplacer.hh
TPEF::Section::chunk
virtual Chunk * chunk(SectionOffset offset) const
Definition: Section.cc:169
TPEF::DataSection::byte
virtual Byte byte(const Chunk *chunk) const
Definition: DataSection.cc:114
TPEF::ReferenceManager::SafePointer::isReferenced
static bool isReferenced(const SafePointable *object)
Definition: SafePointer.cc:282
TPEFStringSectionWriter.hh
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::BinaryStream::writePosition
unsigned int writePosition()
Definition: BinaryStream.cc:592
TPEF::TPEFStringSectionWriter::instance_
static const TPEFStringSectionWriter instance_
Unique instance of class.
Definition: TPEFStringSectionWriter.hh:66
SafePointer.hh
TPEF::ReferenceManager::SectionKey::sectionId
SectionId sectionId() const
StringSection.hh
TPEF::ReferenceManager::SafePointer::addObjectReference
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
Definition: SafePointer.cc:306
TPEF::TPEFStringSectionWriter::actualWriteData
virtual void actualWriteData(BinaryStream &stream, const Section *sect) const
Definition: TPEFStringSectionWriter.cc:84
TPEF::Section
Definition: Section.hh:64
TPEF::StringSection
Definition: StringSection.hh:48
TPEF::TPEFStringSectionWriter::type
virtual Section::SectionType type() const
Definition: TPEFStringSectionWriter.cc:73
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::SectionWriter::registerSectionWriter
static void registerSectionWriter(const SectionWriter *sWriter)
Definition: SectionWriter.cc:148
TPEF::ReferenceManager::SafePointer::sectionKeyFor
static SectionKey sectionKeyFor(const SafePointable *obj)
Definition: SafePointer.cc:408
TPEF::TPEFStringSectionWriter::~TPEFStringSectionWriter
virtual ~TPEFStringSectionWriter()
Definition: TPEFStringSectionWriter.cc:64
TPEF::FileOffset
Word FileOffset
Type for storing absolute file offsets.
Definition: TPEFBaseType.hh:52
TPEF::TPEFStringSectionWriter::elementSize
virtual Word elementSize(const Section *section) const
Definition: TPEFStringSectionWriter.cc:142
TPEF::BinaryStream::writeByte
void writeByte(Byte byte)
Definition: BinaryStream.cc:310
TPEF::SectionOffset
Word SectionOffset
Type for storing offsets relative to a given base offset value.
Definition: TPEFBaseType.hh:49
TPEF::ReferenceManager::SectionKey
Definition: ReferenceKey.hh:145
TPEF::ReferenceManager::FileOffsetKey
Definition: ReferenceKey.hh:121
TPEF::SectionSizeReplacer::setSize
static void setSize(const SafePointable *obj, Word size)
Definition: SectionSizeReplacer.cc:100
TPEF::Section::ST_STRTAB
@ ST_STRTAB
String table.
Definition: Section.hh:71
TPEF::TPEFStringSectionWriter::TPEFStringSectionWriter
TPEFStringSectionWriter()
Definition: TPEFStringSectionWriter.cc:57
TPEF::ReferenceManager::SectionOffsetKey
Definition: ReferenceKey.hh:93
BinaryStream.hh
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::DataSection::length
virtual Word length() const
Definition: DataSection.cc:210
NotChunkable
Definition: Exception.hh:353
TPEF::RawSection::chunk
virtual Chunk * chunk(SectionOffset offset) const
Definition: Section.cc:212
ReferenceKey.hh
TPEF::TPEFSectionWriter
Definition: TPEFSectionWriter.hh:49
TPEF::Chunk
Definition: Chunk.hh:45
TPEF
Definition: Assembler.hh:43