eaarl-io  1.0.1+71abbd4
EAARL Input/Output Library (Public API)
Data Fields
eaarlio_stream Struct Reference

Stream interface. More...

#include <stream.h>

Data Fields

eaarlio_error(* close )(struct eaarlio_stream *self)
 Close the stream. More...
 
eaarlio_error(* read )(struct eaarlio_stream *self, uint64_t len, unsigned char *buffer)
 Read bytes from a stream and place them in a buffer. More...
 
eaarlio_error(* write )(struct eaarlio_stream *self, uint64_t len, unsigned char const *buffer)
 Writes bytes from a buffer to a stream. More...
 
eaarlio_error(* seek )(struct eaarlio_stream *self, int64_t offset, int whence)
 Set the current position for the stream. More...
 
eaarlio_error(* tell )(struct eaarlio_stream *self, int64_t *position)
 Retrieve the current position in the stream. More...
 
void * data
 Internal data pointer. More...
 

Detailed Description

Stream interface.

This interface generalizes the operations needed to read from and write to a file. The intent is to make it possible to read from streams other than a "normal" file: for example, a compressed file.

Each function accepts as its first parameter a pointer to the eaarlio_stream. This gives the functions access to the stream's data field, which is a pointer to whatever the implementation may need for internal state.

All functions return an eaarlio_error and should use EAARLIO_SUCCESS to represet a successful outcome. If it is not plausible to provide an implementation for a given function, then it should return EAARLIO_STREAM_NOT_IMPL. Each function is documented with some suggested error codes for other failing conditions, but you are not restricted to those listed. You can return any valid eaarlio_error value as needed.

This interface is intended to support random-access reads and writes. Not all streams are suitable for random-access. In such cases, your stream should pretend to be random-access as much as is possible. For example, requests to seek should succeed if they're to a current position; requests to seek for a future position should simply discard data to that point. If possible, consider implementing random read access to permit backtracking by restarting the stream from the beginning as a last resort. Except where noted, library functions operate sequentially so backtracking shouldn't cause a significant performance penalty internally.

Examples:
example_edb_read.c, example_edb_write.c, example_file_tld_opener.c, and file_stream.c.

Field Documentation

◆ close

eaarlio_error(* eaarlio_stream::close) (struct eaarlio_stream *self)

Close the stream.

Parameters
[in,out]selfA pointer to the stream
Returns
Any eaarlio_error code. Recommended values are given below.
Return values
EAARLIO_SUCCESSon success
EAARLIO_NULLif provided a NULL pointer
EAARLIO_STREAM_INVALIDif stream is not valid
EAARLIO_STREAM_CLOSE_ERRORif an error is encountered while closing
EAARLIO_STREAM_NOT_IMPLif this function isn't implemented
Postcondition
On success, all fields of eaarlio_stream must be set to NULL.
Examples:
example_edb_read.c, example_edb_write.c, example_file_tld_opener.c, and file_stream.c.

◆ data

void* eaarlio_stream::data

Internal data pointer.

This is not used by calling code directly. It provides a means for the functions in the structure to maintain internal state.

Examples:
file_stream.c.

◆ read

eaarlio_error(* eaarlio_stream::read) (struct eaarlio_stream *self, uint64_t len, unsigned char *buffer)

Read bytes from a stream and place them in a buffer.

Parameters
[in]selfA pointer to the stream
[in]lenThe number of bytes to read
[out]bufferThe destination for the bytes read
Returns
Any eaarlio_error code. Recommended values are given below.
Return values
EAARLIO_SUCCESSon success
EAARLIO_NULLif provided a NULL pointer
EAARLIO_STREAM_INVALIDif stream is not valid
EAARLIO_STREAM_READ_SHORTif the read request returned fewer bytes than requested (this is generally interpreted as being at the end of the file)
EAARLIO_STREAM_READ_ERRORif an error is encountered during the read
EAARLIO_STREAM_NOT_IMPLif this function isn't implemented
Examples:
file_stream.c.

◆ seek

eaarlio_error( * eaarlio_stream::seek) (struct eaarlio_stream *self, int64_t offset, int whence)

Set the current position for the stream.

The position in the stream will be set based on the given values for offset and whence, possibly relative to the current stream position. The position is set based on the value of whence, using the same meaning as for the C standard library function fseek:

whence Interpretation
SEEK_SET offset is from the start of the file
SEEK_CUR offset is relative to the current position
SEEK_END offset is from the end of the file
Note
The library only requires SEEK_SET and SEEK_CUR. Your implementation can respond to SEEK_END with EAARLIO_STREAM_SEEK_INVALID if appropriate.
Parameters
[in]selfA pointer to the stream
[in]offsetOffset to apply, as indicated by whence
[in]whenceHow to apply offset to the current position. This should be one of SEEK_SET, SEEK_CUR, or SEEK_END.
Returns
Any eaarlio_error code. Recommended values are given below.
Return values
EAARLIO_SUCCESSon success
EAARLIO_NULLif provided a NULL pointer
EAARLIO_STREAM_INVALIDif stream is not valid
EAARLIO_STREAM_SEEK_INVALIDif an invalid value is provided for whence
EAARLIO_STREAM_SEEK_ERRORif an error is encountered during the seek
EAARLIO_STREAM_NOT_IMPLif this function isn't implemented
Examples:
file_stream.c.

◆ tell

eaarlio_error(* eaarlio_stream::tell) (struct eaarlio_stream *self, int64_t *position)

Retrieve the current position in the stream.

Parameters
[in]selfA pointer to the stream
[out]positionPointer to single position value to be populated
Returns
Any eaarlio_error code. Recommended values are given below.
Return values
EAARLIO_SUCCESSon success
EAARLIO_NULLif provided a NULL pointer
EAARLIO_STREAM_INVALIDif stream is not valid
EAARLIO_STREAM_TELL_ERRORif an error is encountered during the tell
EAARLIO_STREAM_NOT_IMPLif this function isn't implemented
Examples:
file_stream.c.

◆ write

eaarlio_error(* eaarlio_stream::write) (struct eaarlio_stream *self, uint64_t len, unsigned char const *buffer)

Writes bytes from a buffer to a stream.

Parameters
[in]selfA pointer to the stream
[in]lenThe number of bytes to write
[in]bufferThe data to write
Returns
Any eaarlio_error code. Recommended values are given below.
Return values
EAARLIO_SUCCESSon success
EAARLIO_NULLif provided a NULL pointer
EAARLIO_STREAM_INVALIDif stream is not valid
EAARLIO_STREAM_WRITE_SHORTif the write request wrote fewer bytes than requested
EAARLIO_STREAM_WRITE_ERRORif an error is encountered during the write
EAARLIO_STREAM_NOT_IMPLif this function isn't implemented
Examples:
file_stream.c.

The documentation for this struct was generated from the following file: