OpenASIP  2.0
BinaryReader.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 BinaryReader.cc
26  *
27  * Non-inline definitions of BinaryReader class.
28  *
29  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30  *
31  * @note rating: yellow
32  */
33 
34 #include "BinaryReader.hh"
35 
36 #include <set>
37 
38 #include "Application.hh"
39 #include "TPEFBaseType.hh"
40 #include "BinaryStream.hh"
41 #include "Binary.hh"
42 #include "SafePointer.hh"
43 #include "SectionReader.hh"
44 #include "Exception.hh"
45 
46 namespace TPEF {
47 
48 using std::set;
49 using std::string;
50 using ReferenceManager::SafePointer;
51 
52 // initializes an instance of static member
53 set<BinaryReader*>* BinaryReader::prototypes_ = NULL;
54 
55 /**
56  * Constructor.
57  */
59 }
60 
61 /**
62  * Destructor.
63  */
65 }
66 
67 /**
68  * Constructs a Binary object by reading data from the given binary stream.
69  *
70  * Looks for the concrete binary reader class that can read the given binary
71  * stream. If found, that reader is used to read and construct a Binary
72  * object. This method cleans keytables of reference manager so after running
73  * this method reference manager contain keys of just read binary file.
74  *
75  * @param stream Stream to read binarys data from.
76  * @return Binary that was created from the given stream.
77  * @exception InstanceNotFound If instance for reading wasn't found.
78  * @exception UnreachableStream If given stream can't be read.
79  * @exception KeyAlreadyExists Key was in use when trying to register object.
80  * @exception EndOfFile If end of file were reached while it shouldn't.
81  * @exception OutOfRange Some read value was out of range.
82  * @exception WrongSubclass Some class couldn't do what it was asked for.
83  * @exception UnexpectedValue If there was unexpected value when reading.
84  * @exception UnresolvedReference If there was unresolved references during
85  * reading.
86  */
87 Binary*
89  if (prototypes_ != NULL) {
90 
91  unsigned long startPos = stream.readPosition();
92 
93  set<BinaryReader*>::iterator readers = prototypes_->begin();
94 
95  // checks if any BinaryReader registered can read this stream
96  while (readers != prototypes_->end()) {
97  // isMyStreamType should not move stream position.
98  assert(stream.readPosition() == startPos);
99 
100  if ((*readers)->isMyStreamType(stream)) {
101 
102  // isMyStreamtype must leave stream as it was
103  assert(stream.readPosition() == startPos);
104 
106 
107  Binary *readBinary = (*readers)->readData(stream);
108 
109  try {
111  } catch (const UnresolvedReference &e) {
112  std::stringstream newErrorMsg;
113  newErrorMsg
114  << "Error was probably caused by a broken input file."
115  << std::endl
116  << "Unresolved references during reading: ";
117 
118  UnresolvedReference error(
119  __FILE__, __LINE__, __func__,
120  newErrorMsg.str() + e.errorMessage());
121 
122  error.setCause(e);
123  throw error;
124  }
125 
127 
128  try {
130  } catch (const UnresolvedReference &e) {
131  std::stringstream newErrorMsg;
132  newErrorMsg
133  << "Error was probably caused by a broken input file."
134  << std::endl
135  << "Unresolved references during finalization of "
136  << "reading: ";
137 
138  UnresolvedReference error(
139  __FILE__, __LINE__, __func__,
140  newErrorMsg.str() + e.errorMessage());
141 
142  error.setCause(e);
143  throw error;
144  }
145 
146  // clean up after reading, to avoid abuse of reference manager
148 
149  return readBinary;
150  }
151  readers++;
152  }
153  }
154  throw InstanceNotFound(
155  __FILE__, __LINE__, __func__,
156  "Cannot find suitable reader implementation for file.");
157 }
158 
159 /**
160  * Registers a concrete BinaryReader instance for reading a file type.
161  *
162  * @param reader Concrete reader to be registred.
163  */
164 void
166 
167  if (prototypes_ == NULL) {
168  prototypes_ = new set<BinaryReader*>();
169  }
170 
171  prototypes_->insert(prototypes_->end(),reader);
172 }
173 
174 }
BinaryReader.hh
UnresolvedReference
Definition: Exception.hh:370
TPEF::BinaryStream::readPosition
unsigned int readPosition()
Definition: BinaryStream.cc:561
Exception.hh
TPEF::Binary
Definition: Binary.hh:49
TPEF::BinaryReader::prototypes_
static std::set< BinaryReader * > * prototypes_
Contains instances of concrete BinaryReaders.
Definition: BinaryReader.hh:74
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::SectionReader::finalizeBinary
static void finalizeBinary(Binary *binaryToFinalize, BinaryReader *reader)
Definition: SectionReader.cc:99
Exception::setCause
void setCause(const Exception &cause)
Definition: Exception.cc:75
SafePointer.hh
TPEF::BinaryReader
Definition: BinaryReader.hh:51
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::ReferenceManager::SafePointer::cleanupKeyTables
static void cleanupKeyTables()
Definition: SafePointer.cc:644
Application.hh
SectionReader.hh
__func__
#define __func__
Definition: Application.hh:67
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
TPEF::BinaryReader::BinaryReader
BinaryReader()
Definition: BinaryReader.cc:58
TPEF::BinaryReader::~BinaryReader
virtual ~BinaryReader()
Definition: BinaryReader.cc:64
TPEF::ReferenceManager::SafePointer::resolve
static void resolve()
Definition: SafePointer.cc:480
BinaryStream.hh
TPEFBaseType.hh
TPEF::BinaryReader::readBinary
static Binary * readBinary(BinaryStream &stream)
Definition: BinaryReader.cc:88
InstanceNotFound
Definition: Exception.hh:304
TPEF
Definition: Assembler.hh:43
Binary.hh
TPEF::BinaryReader::registerBinaryReader
static void registerBinaryReader(BinaryReader *reader)
Definition: BinaryReader.cc:165