libclod
C library for interacting with NBTs, region files, LOD data and other things.
Loading...
Searching...
No Matches
clod_stream Struct Reference

#include <stream.h>

Public Attributes

uintptr_t impl
int(* read )(clod_stream *self, struct clod_string *dst)
int(* write )(clod_stream *self, struct clod_string *src)
int(* close )(clod_stream *self)

Detailed Description

Stream of data. It roughly replaces libc's FILE, but aims to support user implementations, and has additional implementations not supported by FILE such as networking.

All members are optional and may be omitted for streams to whom they have no relevance. A stream that supports no methods is valid, for example a directory for whom the user has no read permission can be opened, and such a directory is only useful as a relative path reference for opening files.

Negative error codes are standardised across all stream implementations and platforms, and are defined at the top of this file, while positive error codes are implementation and platform-dependent. The absolute minimum number of translated error codes shall be used, and only when there is a clear case for runtime code path switching should they be used. They are not to be used as debugging tools or user messages. They are exclusively for programs to change their behaviour based on the error code.

For example, distinguishing between EBADF, EFAULT, EINVAL... from linux file write syscall should not be implemented, as none of them provide useful information to the program at runtime. The only thing the program needs to know is "the file does not work" e.g. to avoid an infinite loop. On the other hand, distinguishing the normal end of a TCP connection from other errors can be important, as, instead of treating the connection as failed, the program may which to attempt to reconnect. To that end EPIPE is translated to CLOD_STREAM_EOF.

Definition at line 55 of file stream.h.

Member Data Documentation

◆ impl

uintptr_t clod_stream::impl

Implementation-defined data;

Definition at line 59 of file stream.h.

◆ read

int(* clod_stream::read) (clod_stream *self, struct clod_string *dst)

A stream's read method shall read a portion of data from the stream or return a non-zero error code. If read is null the stream does not support reading.

The new data is appended to the destination buffer using its free capacity.

Parameters
[in]selfImplementation-dependent data.
[in]dstDestination buffer.
Returns
0 on success, non-zero error code on failure.

Definition at line 72 of file stream.h.

◆ write

int(* clod_stream::write) (clod_stream *self, struct clod_string *src)

A stream's write method shall write the entire portion of data to the stream or return a non-zero error code. In such a case some data may have still been written. The amount of written data can be discerned through src, as the successfully written data is removed from the string. If write is null the stream does not support writing.

The written data is removed from the source buffer by incrementing its pointer and adjusting other parameters as such.

Parameters
[in]selfImplementation-dependent data.
[in]srcSource buffer.
Returns
0 on success, non-zero error code on failure.

Definition at line 88 of file stream.h.

◆ close

int(* clod_stream::close) (clod_stream *self)

A stream's close method shall close the stream and release any resources associated with it. If close is null the stream does not need to be closed. Notably, some streams which don't have an underlying close method will still need a close function to free the clod_stream object itself from memory.

Note
The stream cannot be interacted with following a call to close. Conversely, close should free the clod_stream from memory if relevant.
Parameters
[in]selfImplementation-dependent data.
Returns
0 on success, non-zero error code on failure.

Definition at line 102 of file stream.h.


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