OpenASIP  2.0
BinaryStream.icc
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 BinaryStream.icc
26  *
27  * Implementation of BinaryStream class inline functions.
28  *
29  * @author Ari Metsähalme 2003 (ari.metsahalme-no.spam-tut.fi)
30  * @note reviewed 7 August 2003 by pj, am, jn, ao, rl
31  *
32  * @note rating: yellow
33  */
34 
35 #include "Exception.hh"
36 #include "BinaryStream.hh"
37 
38 namespace TPEF {
39 
40 /**
41  * Reads a single byte from the stream.
42  *
43  * Opens the stream if it's not opened yet.
44  *
45  * @return One byte from the stream.
46  * @exception UnreachableStream If stream is bad.
47  * @exception EndOfFile If end of file was reached unexpectedly.
48  */
49 inline Byte
50 BinaryStream::getByte() {
51  if (!iStream_.is_open()) {
52  openInput(fileName_);
53  }
54 
55  if (iStream_.bad()) {
56  throw UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
57  }
58  if (iStream_.eof()) {
59  throw EndOfFile(__FILE__, __LINE__, __func__, fileName_);
60  }
61 
62  Byte result = iStream_.get();
63 
64  if (iStream_.bad()) {
65  throw UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
66  }
67 
68  return result;
69 }
70 
71 /**
72  * Writes a single byte to the stream.
73  *
74  * Opens the stream if it's not opened yet.
75  *
76  * @note If write position is set past the end of file, putByte will throw
77  * UnreachableStream exception.
78  *
79  * @param byte The byte to write to the stream.
80  * @exception UnreachableStream If stream is bad.
81  * @exception WritePastEOF If write position is set past the end of
82  * file and writing is attempted.
83  */
84 inline void
85 BinaryStream::putByte(Byte byte) {
86  if (extOStream_ != NULL) {
87  extOStream_->put(byte);
88  return;
89  }
90 
91  if (!oStream_.is_open()) {
92  openOutput(fileName_);
93  }
94 
95  if (oStream_.bad()) {
96  throw UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
97  }
98 
99  unsigned int writePos = oStream_.tellp();
100  oStream_.seekp(0, std::ios::end);
101  unsigned int eofPos = oStream_.tellp();
102 
103  if (writePos > eofPos) {
104  oStream_.seekp(writePos);
105  throw WritePastEOF(__FILE__, __LINE__, __func__, fileName_);
106  }
107 
108  oStream_.seekp(writePos);
109  oStream_.put(byte);
110 
111  if (oStream_.bad()) {
112  throw UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
113  }
114 }
115 }