File: InputStream.h

package info (click to toggle)
between 6%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,532 kB
  • sloc: cpp: 28,110; php: 718; ansic: 638; objc: 245; sh: 236; makefile: 97; perl: 67
file content (179 lines) | stat: -rw-r--r-- 3,816 bytes parent folder | download | duplicates (30)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Modification History
 *
 * 2001-January-9		Jason Rohrer
 * Created.
 *
 * 2001-January-9		Jason Rohrer
 * Changed the "number of bytes read" parameter and return value
 * to longs.  
 *
 * 2001-February-3		Jason Rohrer
 * Added a readDouble function to fix platform-specific double problems. 
 * Also added a readLong function for completeness.  Implemented
 * these functions, making use of the new TypeIO interface.
 *
 * 2001-February-12		Jason Rohrer
 * Changed to subclass Stream.  
 *
 * 2002-March-9    Jason Rohrer
 * Added a readByte() function.  
 *
 * 2002-March-31    Jason Rohrer
 * Made destructor virtual so it works with subclasses.
 *
 * 2004-May-9   Jason Rohrer
 * Added support for shorts.
 */

#include "minorGems/common.h"


#ifndef INPUT_STREAM_CLASS_INCLUDED
#define INPUT_STREAM_CLASS_INCLUDED

#include "Stream.h"

#include "TypeIO.h"



/**
 * Interface for a byte input stream.
 *
 * @author Jason Rohrer
 */ 
class InputStream : public Stream {

	public:
		
		InputStream();
		virtual ~InputStream();
		
		/**
		 * Reads bytes from this stream.
		 *
		 * @param inBuffer the buffer where read bytes will be put.
		 *   Must be pre-allocated memory space.
		 * @param inNumBytes the number of bytes to read from the stream.
		 *
		 * @return the number of bytes read successfully,
		 *   or -1 for a stream error.
		 */
		virtual long read( unsigned char *inBuffer, long inNumBytes ) = 0;



        /**
         * Reads a byte from this stream.
         *
         * @param outByte pointer to where the byte should be stored.
         *
         * @return the number of bytes read successfully, or -1 for a 
         *   stream error.
         */
        long readByte( unsigned char *outByte );


        
		/**
		 * Reads a double from the stream in a platform-independent way.
		 *
		 * @param outDouble pointer to where the double should be stored.
		 *
		 * @return the number of bytes read successfully, or -1 for a 
		 *   stream error.
		 */
		long readDouble( double *outDouble );
		
		
		/**
		 * Reads a long from the stream in a platform-independent way.
		 *
		 * @param outLong pointer to where the long should be stored.
		 *
		 * @return the number of bytes read successfully, or -1 for a 
		 *   stream error.
		 */
		long readLong( long *outLong );
		
		
		/**
		 * Reads a short from the stream in a platform-independent way.
		 *
		 * @param outShort pointer to where the short should be stored.
		 *
		 * @return the number of bytes read successfully, or -1 for a 
		 *   stream error.
		 */
		long readShort( short *outShort );
	
	
	private:
		unsigned char *mDoubleBuffer;
		unsigned char *mLongBuffer;
        unsigned char *mShortBuffer;
        unsigned char *mByteBuffer;
        
	};		



inline InputStream::InputStream() 
	: mDoubleBuffer( new unsigned char[8] ),
      mLongBuffer( new unsigned char[4] ),
      mShortBuffer( new unsigned char[2] ),
      mByteBuffer( new unsigned char[1] ) {

	}



inline InputStream::~InputStream() {

	delete [] mDoubleBuffer;
	delete [] mShortBuffer;
	delete [] mLongBuffer;
    delete [] mByteBuffer;
	}



inline long InputStream::readByte( unsigned char *outByte ) {
    int numBytes = read( mByteBuffer, 1 );

    *outByte = mByteBuffer[0];

    return numBytes;
    }



inline long InputStream::readDouble( double *outDouble ) {
	int numBytes = read( mDoubleBuffer, 8 );
	*outDouble = TypeIO::bytesToDouble( mDoubleBuffer );
	
	return numBytes;
	}
	
	
		
inline long InputStream::readLong( long *outLong ) {
	int numBytes = read( mLongBuffer, 4 );
	*outLong = TypeIO::bytesToLong( mLongBuffer );
	
	return numBytes;
	}



inline long InputStream::readShort( short *outShort ) {
	int numBytes = read( mShortBuffer, 4 );
	*outShort = TypeIO::bytesToShort( mShortBuffer );
	
	return numBytes;
	}

	
	
#endif