1 //****************************************************************************//
2 // streamsource.h //
3 // Copyright (C) 2001-2003 Bruno 'Beosil' Heidelberger //
4 //****************************************************************************//
5 // This library is free software; you can redistribute it and/or modify it //
6 // under the terms of the GNU Lesser General Public License as published by //
7 // the Free Software Foundation; either version 2.1 of the License, or (at //
8 // your option) any later version. //
9 //****************************************************************************//
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 //****************************************************************************//
16 // Includes //
17 //****************************************************************************//
18
19 #include "cal3d/streamsource.h"
20 #include "cal3d/error.h"
21 #include "cal3d/platform.h"
22
23 /*****************************************************************************/
24 /** Constructs a stream source instance from an existing istream.
25 *
26 * This function is the only constructor of the stream source.
27 *
28 * @param inputStream The input stream to use, which should be set up and
29 * ready to be read from before making the stream source.
30 *****************************************************************************/
31
CalStreamSource(std::istream & inputStream)32 CalStreamSource::CalStreamSource(std::istream& inputStream)
33 : mInputStream(&inputStream)
34 {
35 }
36
37
38 /**
39 * Destruct the CalStreamSource. Note that input stream is not closed here;
40 * this should be handled externally.
41 */
42
~CalStreamSource()43 CalStreamSource::~CalStreamSource()
44 {
45 }
46
47 /*****************************************************************************/
48 /** Checks whether the data source is in a good state.
49 *
50 * This function checks if the istream can be used.
51 *
52 * @return One of the following values:
53 * \li \b true if data source is in a good state
54 * \li \b false if not
55 *****************************************************************************/
56
ok() const57 bool CalStreamSource::ok() const
58 {
59 if (!mInputStream)
60 return false;
61
62 return true;
63 }
64
65 /*****************************************************************************/
66 /** Sets the error code and message related to a streaming source.
67 *
68 *****************************************************************************/
69
setError() const70 void CalStreamSource::setError() const
71 {
72 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
73 }
74
75
76 /*****************************************************************************/
77 /** Reads a number of bytes.
78 *
79 * This function reads a given number of bytes from this data source.
80 *
81 * @param pBuffer A pointer to the buffer where the bytes are stored into.
82 * @param length The number of bytes that should be read.
83 *
84 * @return One of the following values:
85 * \li \b true if successful
86 * \li \b false if an error happend
87 *****************************************************************************/
88
readBytes(void * pBuffer,int length)89 bool CalStreamSource::readBytes(void* pBuffer, int length)
90 {
91 //Check that the stream is usable
92 if (!ok()) return false;
93
94 return CalPlatform::readBytes( *mInputStream, pBuffer, length );
95
96 }
97
98 /*****************************************************************************/
99 /** Reads a float.
100 *
101 * This function reads a float from this data source.
102 *
103 * @param value A reference to the float into which the data is read.
104 *
105 * @return One of the following values:
106 * \li \b true if successful
107 * \li \b false if an error happend
108 *****************************************************************************/
109
readFloat(float & value)110 bool CalStreamSource::readFloat(float& value)
111 {
112 //Check that the stream is usable
113 if (!ok()) return false;
114
115 return CalPlatform::readFloat( *mInputStream, value );
116 }
117
118 /*****************************************************************************/
119 /** Reads a short.
120 *
121 * This function reads a short from this data source.
122 *
123 * @param value A reference to the short into which the data is read.
124 *
125 * @return One of the following values:
126 * \li \b true if successful
127 * \li \b false if an error happend
128 *****************************************************************************/
129
readShort(short & value)130 bool CalStreamSource::readShort(short& value)
131 {
132 //Check that the stream is usable
133 if (!ok()) return false;
134
135 return CalPlatform::readShort( *mInputStream, value );
136 }
137
138 /*****************************************************************************/
139 /** Reads an integer.
140 *
141 * This function reads an integer from this data source.
142 *
143 * @param value A reference to the integer into which the data is read.
144 *
145 * @return One of the following values:
146 * \li \b true if successful
147 * \li \b false if an error happend
148 *****************************************************************************/
149
readInteger(int & value)150 bool CalStreamSource::readInteger(int& value)
151 {
152 //Check that the stream is usable
153 if (!ok()) return false;
154
155 return CalPlatform::readInteger( *mInputStream, value );
156 }
157
158 /*****************************************************************************/
159 /** Reads a string.
160 *
161 * This function reads a string from this data source.
162 *
163 * @param value A reference to the string into which the data is read.
164 *
165 * @return One of the following values:
166 * \li \b true if successful
167 * \li \b false if an error happend
168 *****************************************************************************/
169
readString(std::string & strValue)170 bool CalStreamSource::readString(std::string& strValue)
171 {
172 //Check that the stream is usable
173 if (!ok()) return false;
174
175 return CalPlatform::readString( *mInputStream, strValue );
176 }
177