File: StreamHandle.cpp

package info (click to toggle)
pose 3.0a3-3
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 15,500 kB
  • ctags: 20,548
  • sloc: ansic: 72,579; cpp: 50,198; perl: 1,336; python: 1,242; sh: 363; makefile: 290
file content (251 lines) | stat: -rw-r--r-- 6,216 bytes parent folder | download
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* -*- mode: C++; tab-width: 4 -*- */
/* ================================================================================== */
/* Copyright (c) 1998-1999 3Com Corporation or its subsidiaries. All rights reserved. */
/* ================================================================================== */

#include "EmulatorCommon.h"
#include "StreamHandle.h"

#include "ErrorHandling.h"		// this->Throw
#include "Platform_Files.h"		// FileReference



// ===========================================================================
//		 StreamHandle
//
//		A StreamHandle is an abstract handle to streams of data.
//		
// ===========================================================================


/***********************************************************************
 *
 * FUNCTION:	StreamHandle()
 *
 * DESCRIPTION:	StreamHandle constructor.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

StreamHandle::StreamHandle (void)
{
}


/***********************************************************************
 *
 * FUNCTION:	~StreamHandle()
 *
 * DESCRIPTION:	StreamHandle destructor.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

StreamHandle::~StreamHandle (void)
{
}


/***********************************************************************
 *
 * FUNCTION:	Throw()
 *
 * DESCRIPTION:	StreamHandle exception thrower. StreamHandle methods will
 *				call this to throw exceptions.
 *
 * PARAMETERS:	err - error code.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void			
StreamHandle::Throw (ErrCode err)
{
	Errors::Throw (err);
}


/***********************************************************************
 *
 * FUNCTION:	Write
 *
 * DESCRIPTION:	Writing to a StreamHandle DOES NOTHING.
 *
 * PARAMETERS:	length - number of bytes to write
 *				buffer - location of buffer to read out of
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void
StreamHandle::Write (uae_s32 length, const void* buffer)
{
	UNUSED_PARAM (length);
	UNUSED_PARAM (buffer);
}


/***********************************************************************
 *
 * FUNCTION:	GetFileRef()
 *
 * DESCRIPTION:	All StreamHandles support getting the underlying file
 *				reference, but not all StreamHandles use files. So by
 *				by default a StreamHandle will pass back an empty
 *				FileReference and return false.
 *
 * PARAMETERS:	f - FileReference to be passed back.
 *
 * RETURNED:	TRUE if a file reference was returned.
 *				FALSE otherwise.
 *
 ***********************************************************************/

Bool
StreamHandle::GetFileRef (FileReference& f)
{
	// pass back a default (empty) FileReference
	FileReference r;

	f = r;
	return false;
}


// ===========================================================================
//		 BufferHandle
//
//		A BufferHandle is a specialization of a StreamHandle which handles
//		streams in memory buffers. The BufferHandle does not "own" the memory
//		that is passed to it, because this memory can be (and typically is) a
//		read-only resource. Therefore, if the memory you pass to the
//		BufferHandle needs to be freed, it is your responsibility to do that.
//		
// ===========================================================================

/***********************************************************************
 *
 * FUNCTION:	BufferHandle()
 *
 * DESCRIPTION:	BufferHandle constructor.
 *
 * PARAMETERS:	pData - pointer to the start of the buffer
 *				iSize - size of the buffer
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

BufferHandle::BufferHandle (void* data, uae_s32 size)
{
	fData	= (char*) data;
	fSize	= size;
	fPos	= 0;
}


/***********************************************************************
 *
 * FUNCTION:	~BufferHandle
 *
 * DESCRIPTION:	BufferHandle destructor. Does not free the memory that
 *				handle represents.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

BufferHandle::~BufferHandle (void)
{
}


/***********************************************************************
 *
 * FUNCTION:	SetPos
 *
 * DESCRIPTION:	Sets the "cursor" position in the buffer, where the next
 *				read will begin.
 *
 * PARAMETERS:	offset - change in cursor position (signed)
 *				seekMode - 	pass in {kSeekSet, kSeekCur, kSeekEnd}
 *							representing where to begin the offset
 *							(front, current position, end)
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void
BufferHandle::SetPos (uae_s32 offset, SeekMode seekMode)
{
	// used signed value to detect mishaps
	uae_s32	pos = fPos;

	if (seekMode == kSeekSet)
	{
		pos = offset;
	}
	else if (seekMode == kSeekEnd)
	{
		pos = fSize - 1 + offset;
	}
	else
	{
		pos += offset;
	}

	// Note that as with files, it is not an error to set the position
	// past the end of the buffer--only before it. Memory buffers cannot
	// be written to, and our code to read from it will automatically
	// ignore reads past the end of the file, so we are safe.

	if (pos < 0)
	{
//		this->Throw (paramErr);
		assert (false);	// We don't have any cross-platform value to throw
						// here, so let's just assert.
	}

	fPos =  pos;
}


/***********************************************************************
 *
 * FUNCTION:	Read
 *
 * DESCRIPTION:	Reads a specified number of characters from this buffer
 *				object into a specified memory buffer. Will only read up
 *				to the end of this buffer object, or the length specified,
 *				whichever is smaller.
 *
 * PARAMETERS:	length - number of bytes to read
 *				buffer - location of buffer to read into
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void
BufferHandle::Read (uae_s32 length, void* buffer)
{
	uae_s32	copyLength = min (length, (fSize - fPos));

	if (copyLength > 0)
	{
		memcpy (buffer, fData + fPos, copyLength);
		fPos += copyLength;
	}
}