OpenASIP 2.2
Loading...
Searching...
No Matches
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"
39#include "ReferenceKey.hh"
40#include "BinaryStream.hh"
41
42namespace TPEF {
43
44using ReferenceManager::SafePointer;
45using ReferenceManager::SectionKey;
46using ReferenceManager::SectionOffsetKey;
47using ReferenceManager::FileOffsetKey;
48
49
50const TPEFStringSectionWriter TPEFStringSectionWriter::instance_;
51
52/**
53 * Constructor.
54 *
55 * Registers itself to SectionWriter.
56 */
60
61/**
62 * Destructor.
63 */
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 */
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 */
83void
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 */
141Word
143 return sizeof(char);
144}
145
146}
#define assert(condition)
void writeByte(Byte byte)
unsigned int writePosition()
virtual Word length() const
virtual Byte byte(const Chunk *chunk) const
virtual Chunk * chunk(SectionOffset offset) const
Definition Section.cc:212
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
static SectionKey sectionKeyFor(const SafePointable *obj)
static bool isReferenced(const SafePointable *object)
static void setSize(const SafePointable *obj, Word size)
static void registerSectionWriter(const SectionWriter *sWriter)
virtual Chunk * chunk(SectionOffset offset) const
Definition Section.cc:169
@ ST_STRTAB
String table.
Definition Section.hh:71
static const TPEFStringSectionWriter instance_
Unique instance of class.
virtual Section::SectionType type() const
virtual Word elementSize(const Section *section) const
virtual void actualWriteData(BinaryStream &stream, const Section *sect) const
HalfWord SectionId
Type for storing binary file section ids.
Word SectionOffset
Type for storing offsets relative to a given base offset value.
Word FileOffset
Type for storing absolute file offsets.