File: OutputStream.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 (185 lines) | stat: -rw-r--r-- 3,696 bytes parent folder | download | duplicates (10)
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
180
181
182
183
184
185
/*
 * Modification History
 *
 * 2001-January-9		Jason Rohrer
 * Created.
 *
 * 2001-January-9		Jason Rohrer
 * Changed the "number of bytes written" parameter and return value
 * to longs.
 *
 * 2001-February-3		Jason Rohrer
 * Added a writeDouble function to fix platform-specific double problems. 
 * Also added a writeLong function for completeness.  Implemented
 * these functions, making use of the new TypeIO interface.  
 *
 * 2001-February-12		Jason Rohrer
 * Changed to subclass Stream.
 *
 * 2002-February-25		Jason Rohrer
 * Added a function for writing a string.
 *
 * 2002-March-31    Jason Rohrer
 * Made destructor virtual so it works with subclasses.
 *
 * 2004-May-9   Jason Rohrer
 * Added support for shorts.
 *
 * 2010-May-18    Jason Rohrer
 * String parameters as const to fix warnings.
 */

#include "minorGems/common.h"



#ifndef OUTPUT_STREAM_CLASS_INCLUDED
#define OUTPUT_STREAM_CLASS_INCLUDED

#include "Stream.h"

#include "TypeIO.h"

#include <string.h>



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

	public:
		
		OutputStream();
		virtual ~OutputStream();
		
		/**
		 * Writes bytes to this stream.
		 *
		 * @param inBuffer the buffer of bytes to send.
		 * @param inNumBytes the number of bytes to send.
		 *
		 * @return the number of bytes written successfully,
		 *   or -1 for a stream error.
		 */
		virtual long write( unsigned char *inBuffer, long inNumBytes ) = 0;
		


        /**
         * Writes a string to this stream.
         *
         * @param inString a \0-terminated string.
         *   Must be destroyed by caller.
         * @return the number of bytes written successfully, or -1 for a 
		 *   stream error.
         */
        long writeString( const char *inString );


        
		/**
		 * Writes a double to the stream in a platform-independent way.
		 *
		 * @param inDouble the double to write
		 *
		 * @return the number of bytes written successfully, or -1 for a 
		 *   stream error.
		 */
		long writeDouble( double inDouble );
		
		
		/**
		 * Writes a long to the stream in a platform-independent way.
		 *
		 * @param inLong the long to write
		 *
		 * @return the number of bytes written successfully, or -1 for a 
		 *   stream error.
		 */
		long writeLong( long inLong );



        /**
		 * Writes a short to the stream in a platform-independent way.
		 *
		 * @param inShort the long to write
		 *
		 * @return the number of bytes written successfully, or -1 for a 
		 *   stream error.
		 */
		long writeShort( short inShort );

        
		
	private:
		unsigned char *mDoubleBuffer;
		unsigned char *mLongBuffer;
        unsigned char *mShortBuffer;
		
	};		



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

	}



inline OutputStream::~OutputStream() {

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



inline long OutputStream::writeString( const char *inString ) {
		
	int numBytes = write( (unsigned char *)inString, strlen( inString ) );

	return numBytes;
	}



inline long OutputStream::writeDouble( double inDouble ) {
	TypeIO::doubleToBytes( inDouble, mDoubleBuffer );
	
	int numBytes = write( mDoubleBuffer, 8 );

	return numBytes;
	}
	
	
		
inline long OutputStream::writeLong( long inLong ) {
	TypeIO::longToBytes( inLong, mLongBuffer );
	
	int numBytes = write( mLongBuffer, 4 );

	return numBytes;
	}



inline long OutputStream::writeShort( short inShort ) {
	TypeIO::shortToBytes( inShort, mShortBuffer );
	
	int numBytes = write( mShortBuffer, 2 );

	return numBytes;
	}


	
#endif