1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
17 #ifndef SQUIRRELJME_STREAM_H
18 #define SQUIRRELJME_STREAM_H
24 #ifndef SJME_CXX_IS_EXTERNED
25 #define SJME_CXX_IS_EXTERNED
26 #define SJME_CXX_SQUIRRELJME_STREAM_H
28 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
29 #endif /* #ifdef __cplusplus */
31 /*--------------------------------------------------------------------------*/
34 * Represents a data stream that can be read from.
38 typedef struct sjme_stream_inputCore sjme_stream_inputCore
;
41 * Represents a data stream that can be read from.
45 typedef struct sjme_stream_inputCore
* sjme_stream_input
;
48 * The core output stream which is written to with data.
52 typedef struct sjme_stream_outputCore sjme_stream_outputCore
;
55 * Represents a data stream that can be written to.
59 typedef struct sjme_stream_outputCore
* sjme_stream_output
;
62 * Determines the number of bytes which are quickly available before blocking
65 * @param stream The stream to read from.
66 * @param outAvail The number of bytes which are available.
67 * @return On any resultant error.
70 typedef sjme_errorCode (*sjme_stream_inputAvailableFunc
)(
71 sjme_attrInNotNull sjme_stream_input stream
,
72 sjme_attrOutNotNull sjme_attrOutNegativeOnePositive sjme_jint
* outAvail
);
75 * Closes the given input stream and frees up any resources.
77 * @param stream The stream to close.
78 * @return On any resultant error.
81 typedef sjme_errorCode (*sjme_stream_inputCloseFunc
)(
82 sjme_attrInNotNull sjme_stream_input stream
);
85 * Reads from the given input stream and writes to the destination buffer.
87 * @param stream The stream to read from.
88 * @param readCount The number of bytes which were read, if end of stream is
89 * reached this will be @c -1 .
90 * @param dest The destination buffer.
91 * @param length The number of bytes to read.
92 * @return Any resultant error, if any.
95 typedef sjme_errorCode (*sjme_stream_inputReadFunc
)(
96 sjme_attrInNotNull sjme_stream_input stream
,
97 sjme_attrOutNotNull sjme_attrOutNegativeOnePositive sjme_jint
* readCount
,
98 sjme_attrOutNotNullBuf(length
) sjme_pointer dest
,
99 sjme_attrInPositive sjme_jint length
);
102 * Functions for input streams.
106 typedef struct sjme_stream_inputFunctions
108 /** Number of bytes available. */
109 sjme_stream_inputAvailableFunc available
;
112 sjme_stream_inputCloseFunc close
;
114 /** Read from stream. */
115 sjme_stream_inputReadFunc read
;
116 } sjme_stream_inputFunctions
;
118 struct sjme_stream_inputCore
120 /** Functions for input. */
121 const sjme_stream_inputFunctions
* functions
;
123 /** Front end holders. */
124 sjme_frontEnd frontEnd
;
126 /** The current number of read bytes. */
129 /** Uncommon stream specific data. */
130 sjme_jlong uncommon
[sjme_flexibleArrayCount
];
134 * Gets the state information from the given input stream.
136 * @param uncommonType The uncommon type.
137 * @param base The base pointer.
140 #define SJME_INPUT_UNCOMMON(uncommonType, base) \
141 SJME_UNCOMMON_MEMBER(sjme_stream_inputCore, uncommon, \
142 uncommonType, (base))
145 * Determines the size of the input stream structure.
147 * @param uncommonSize The uncommon size.
148 * @return The input stream structure size.
151 #define SJME_SIZEOF_INPUT_STREAM_N(uncommonSize) \
152 SJME_SIZEOF_UNCOMMON_N(sjme_stream_inputCore, uncommon, uncommonSize)
155 * Determines the size of the input stream structure.
157 * @param uncommonType The uncommon type.
158 * @return The input stream structure size.
161 #define SJME_SIZEOF_INPUT_STREAM(uncommonType) \
162 SJME_SIZEOF_INPUT_STREAM_N(sizeof(uncommonType))
165 * Closes the specified output stream.
167 * @param stream The output stream to close.
168 * @param optResult Optional output result.
169 * @return On any resultant error, if any.
172 typedef sjme_errorCode (*sjme_stream_outputCloseFunc
)(
173 sjme_attrInNotNull sjme_stream_output stream
,
174 sjme_attrOutNullable sjme_pointer
* optResult
);
177 * Writes to the given output stream.
179 * @param stream The stream to write to.
180 * @param buf The bytes to write.
181 * @param length The number of bytes to write.
182 * @return On any resultant error, if any.
185 typedef sjme_errorCode (*sjme_stream_outputWriteFunc
)(
186 sjme_attrInNotNull sjme_stream_output stream
,
187 sjme_attrInNotNull sjme_cpointer buf
,
188 sjme_attrInPositiveNonZero sjme_jint length
);
191 * Functions for writing to the output.
195 typedef struct sjme_stream_outputFunctions
197 /** Closes the specified stream. */
198 sjme_stream_outputCloseFunc close
;
200 /** Writes to the given output stream. */
201 sjme_stream_outputWriteFunc write
;
202 } sjme_stream_outputFunctions
;
204 struct sjme_stream_outputCore
206 /** Functions for output. */
207 const sjme_stream_outputFunctions
* functions
;
209 /** Front end holders. */
210 sjme_frontEnd frontEnd
;
212 /** The current number of written bytes. */
213 sjme_jint totalWritten
;
215 /** Uncommon stream specific data. */
216 sjme_jlong uncommon
[sjme_flexibleArrayCount
];
220 * Gets the state information from the given output stream.
222 * @param uncommonType The uncommon type.
223 * @param base The base pointer.
226 #define SJME_OUTPUT_UNCOMMON(uncommonType, base) \
227 SJME_UNCOMMON_MEMBER(sjme_stream_outputCore, uncommon, \
228 uncommonType, (base))
231 * Determines the size of the output stream structure.
233 * @param uncommonSize The uncommon size.
234 * @return The output stream structure size.
237 #define SJME_SIZEOF_OUTPUT_STREAM_N(uncommonSize) \
238 SJME_SIZEOF_UNCOMMON_N(sjme_stream_outputCore, uncommon, uncommonSize)
241 * Determines the size of the output stream structure.
243 * @param uncommonType The uncommon type.
244 * @return The output stream structure size.
247 #define SJME_SIZEOF_OUTPUT_STREAM(uncommonType) \
248 SJME_SIZEOF_OUTPUT_STREAM_N(sizeof(uncommonType))
251 * Determines the number of bytes which are quickly available before blocking
254 * @param stream The stream to read from.
255 * @param outAvail The number of bytes which are available.
256 * @return On any resultant error or @c NULL .
259 sjme_errorCode
sjme_stream_inputAvailable(
260 sjme_attrInNotNull sjme_stream_input stream
,
261 sjme_attrOutNotNull sjme_attrOutNegativeOnePositive sjme_jint
* outAvail
);
264 * Closes an input stream.
266 * @param stream The stream to close.
267 * @return Any resultant error, if any.
270 sjme_errorCode
sjme_stream_inputClose(
271 sjme_attrInNotNull sjme_stream_input stream
);
274 * Creates a stream which reads from the given block of memory.
276 * Memory based streams are never blocking.
278 * @param inPool The pool to allocate within.
279 * @param outStream The resultant stream.
280 * @param base The buffer to access.
281 * @param length The length of the buffer.
282 * @return On any resultant error, if any.
285 sjme_errorCode
sjme_stream_inputOpenMemory(
286 sjme_attrInNotNull sjme_alloc_pool
* inPool
,
287 sjme_attrOutNotNull sjme_stream_input
* outStream
,
288 sjme_attrInNotNull sjme_cpointer base
,
289 sjme_attrInPositive sjme_jint length
);
292 * Reads from the given input stream and writes to the destination buffer.
294 * @param stream The stream to read from.
295 * @param readCount The number of bytes which were read, if end of stream is
296 * reached this will be @c -1 .
297 * @param dest The destination buffer.
298 * @param length The number of bytes to read.
299 * @return Any resultant error, if any.
302 sjme_errorCode
sjme_stream_inputRead(
303 sjme_attrInNotNull sjme_stream_input stream
,
304 sjme_attrOutNotNull sjme_attrOutNegativeOnePositive sjme_jint
* readCount
,
305 sjme_attrOutNotNullBuf(length
) sjme_pointer dest
,
306 sjme_attrInPositive sjme_jint length
);
309 * Reads from the given input stream and writes to the destination buffer.
311 * @param stream The stream to read from.
312 * @param readCount The number of bytes which were read, if end of stream is
313 * reached this will be @c -1 .
314 * @param dest The destination buffer.
315 * @param offset The offset into the destination buffer.
316 * @param length The number of bytes to read.
317 * @return Any resultant error, if any.
320 sjme_errorCode
sjme_stream_inputReadIter(
321 sjme_attrInNotNull sjme_stream_input stream
,
322 sjme_attrOutNotNull sjme_attrOutNegativeOnePositive sjme_jint
* readCount
,
323 sjme_attrOutNotNullBuf(length
) sjme_pointer dest
,
324 sjme_attrInPositive sjme_jint offset
,
325 sjme_attrInPositive sjme_jint length
);
328 * Reads a single byte from the input stream.
330 * @param stream The stream to read from.
331 * @param result The resultant byte, @c -1 indicates end of stream while
332 * every other value is always positive.
333 * @return Any resultant error, if any.
336 sjme_errorCode
sjme_stream_inputReadSingle(
337 sjme_attrInNotNull sjme_stream_input stream
,
338 sjme_attrOutNotNull sjme_attrOutNegativeOnePositive sjme_jint
* result
);
341 * Reads a Java value from the given stream.
343 * @param stream The stream to read from.
344 * @param typeId The type to read.
345 * @param outValue The resultant value.
346 * @return On any error, if any.
349 sjme_errorCode
sjme_stream_inputReadValueJ(
350 sjme_attrInNotNull sjme_stream_input stream
,
351 sjme_attrInRange(0, SJME_NUM_BASIC_TYPE_IDS
)
352 sjme_basicTypeId typeId
,
353 sjme_attrOutNotNull sjme_jvalue
* outValue
);
356 * Closes an output stream.
358 * @param stream The stream to close.
359 * @param optResult Optional resultant output value, if any.
360 * @return Any resultant error, if any.
363 sjme_errorCode
sjme_stream_outputClose(
364 sjme_attrInNotNull sjme_stream_output stream
,
365 sjme_attrOutNullable sjme_pointer
* optResult
);
368 * Contains the result of the written byte array.
372 typedef struct sjme_stream_resultByteArray
374 /** The buffer where the data is. */
377 /** The length of the array. */
381 * If this is set to @c SJME_JNI_TRUE , then the close handler for
382 * the byte array will free the memory contained in the buffer. To prevent
383 * it from being freed, this should return @c SJME_JNI_FALSE in which case
384 * the @c optResult value will be written to with the final buffer.
388 /** The input whatever value. */
389 sjme_pointer whatever
;
391 /** The optional output result. */
392 sjme_pointer
* optResult
;
393 } sjme_stream_resultByteArray
;
396 * This function is called back when the output byte array stream has been
397 * closed, which provides the byte array for access.
399 * @param stream The stream that is finished and is about to be closed.
400 * @param result The result of the array operation.
401 * @return Any resultant error, if any.
404 typedef sjme_errorCode (*sjme_stream_outputByteArrayFinishFunc
)(
405 sjme_attrInNotNull sjme_stream_output stream
,
406 sjme_attrInNotNull sjme_stream_resultByteArray
* result
);
409 * Opens a dynamically resizing output byte array.
411 * @param inPool The pool to allocate within.
412 * @param outStream The resultant output stream.
413 * @param initialLimit The initial buffer limit.
414 * @param finish The function to call when the stream is closed.
415 * @param whatever Can be used to pass whatever is needed for the finish
417 * @return On any error, if any.
420 sjme_errorCode
sjme_stream_outputOpenByteArray(
421 sjme_attrInNotNull sjme_alloc_pool
* inPool
,
422 sjme_attrOutNotNull sjme_stream_output
* outStream
,
423 sjme_attrInPositive sjme_jint initialLimit
,
424 sjme_attrInNotNull sjme_stream_outputByteArrayFinishFunc finish
,
425 sjme_attrInNullable sjme_pointer whatever
);
428 * Opens an output stream which writes to the given block of memory, note that
429 * when it reaches the end of the block it will fail to write following it.
431 * @param inPool The pool to allocate within.
432 * @param outStream The resultant output stream.
433 * @param base The base memory address to write to.
434 * @param length The length of the memory region.
435 * @return Any resultant error, if any.
438 sjme_errorCode
sjme_stream_outputOpenMemory(
439 sjme_attrInNotNull sjme_alloc_pool
* inPool
,
440 sjme_attrOutNotNull sjme_stream_output
* outStream
,
441 sjme_attrInNotNull sjme_pointer base
,
442 sjme_attrInPositive sjme_jint length
);
445 * Writes to the given output stream.
447 * @param outStream The stream to write to.
448 * @param src The source bytes.
449 * @param length The number of bytes to write.
450 * @return Any resultant error, if any.
453 sjme_errorCode
sjme_stream_outputWrite(
454 sjme_attrInNotNull sjme_stream_output outStream
,
455 sjme_attrOutNotNullBuf(length
) sjme_pointer src
,
456 sjme_attrInPositive sjme_jint length
);
459 * Writes to the given output stream.
461 * @param stream The stream to write to.
462 * @param src The source bytes.
463 * @param offset The offset into the buffer.
464 * @param length The number of bytes to write.
465 * @return Any resultant error, if any.
468 sjme_errorCode
sjme_stream_outputWriteIter(
469 sjme_attrInNotNull sjme_stream_output stream
,
470 sjme_attrOutNotNullBuf(length
) sjme_pointer src
,
471 sjme_attrInPositive sjme_jint offset
,
472 sjme_attrInPositive sjme_jint length
);
475 * Writes a single byte to the output stream.
477 * @param outStream The stream to write to.
478 * @param value The value to write, only the lower eight bits are kept.
479 * @return Any resultant error, if any.
482 sjme_errorCode
sjme_stream_outputWriteSingle(
483 sjme_attrInNotNull sjme_stream_output outStream
,
484 sjme_attrInRange(0, 256) sjme_jint value
);
487 * Writes a Java value to the output stream.
489 * @param outStream The stream to write to.
490 * @param typeId The type to write.
491 * @param value The value to write.
492 * @return Any resultant error, if any.
495 sjme_errorCode
sjme_stream_outputWriteValueJP(
496 sjme_attrInNotNull sjme_stream_output outStream
,
497 sjme_attrInRange(0, SJME_NUM_BASIC_TYPE_IDS
) sjme_basicTypeId typeId
,
498 sjme_attrInNotNull
const sjme_jvalue
* value
);
501 * Writes a Java value to the output stream.
503 * @param outStream The stream to write to.
504 * @param typeId The type to write.
505 * @param ... The value to write.
506 * @return Any resultant error, if any.
509 sjme_errorCode
sjme_stream_outputWriteValueJ(
510 sjme_attrInNotNull sjme_stream_output outStream
,
511 sjme_attrInRange(0, SJME_NUM_BASIC_TYPE_IDS
) sjme_basicTypeId typeId
,
514 /*--------------------------------------------------------------------------*/
518 #ifdef SJME_CXX_SQUIRRELJME_STREAM_H
520 #undef SJME_CXX_SQUIRRELJME_STREAM_H
521 #undef SJME_CXX_IS_EXTERNED
522 #endif /* #ifdef SJME_CXX_SQUIRRELJME_STREAM_H */
523 #endif /* #ifdef __cplusplus */
525 #endif /* SQUIRRELJME_STREAM_H */