core: stream: update generic pointer usage for stdio streams

This commit is contained in:
2025-10-19 11:26:04 +01:00
parent b3de59ae24
commit ff2ff6f136

View File

@@ -12,7 +12,7 @@
static enum b_status stdio_read( static enum b_status stdio_read(
struct b_stream *stream, unsigned char *out, size_t max, size_t *nr_read) struct b_stream *stream, unsigned char *out, size_t max, size_t *nr_read)
{ {
FILE *fp = stream->s_ptr; FILE *fp = stream->s_ptr0;
enum b_status status = B_SUCCESS; enum b_status status = B_SUCCESS;
size_t count = fread(out, 1, max, fp); size_t count = fread(out, 1, max, fp);
@@ -29,7 +29,7 @@ static enum b_status stdio_write(
struct b_stream *stream, const unsigned char *data, size_t count, struct b_stream *stream, const unsigned char *data, size_t count,
size_t *nr_written) size_t *nr_written)
{ {
FILE *fp = stream->s_ptr; FILE *fp = stream->s_ptr0;
enum b_status status = B_SUCCESS; enum b_status status = B_SUCCESS;
size_t w = fwrite(data, 1, count, fp); size_t w = fwrite(data, 1, count, fp);
@@ -44,7 +44,7 @@ static enum b_status stdio_write(
static enum b_status stdio_seek( static enum b_status stdio_seek(
struct b_stream *stream, long long offset, b_stream_seek_origin origin) struct b_stream *stream, long long offset, b_stream_seek_origin origin)
{ {
FILE *fp = stream->s_ptr; FILE *fp = stream->s_ptr0;
int whence = 0; int whence = 0;
switch (origin) { switch (origin) {
case B_STREAM_SEEK_START: case B_STREAM_SEEK_START:
@@ -70,7 +70,7 @@ static enum b_status stdio_seek(
static enum b_status stdio_tell(const struct b_stream *stream, size_t *cursor) static enum b_status stdio_tell(const struct b_stream *stream, size_t *cursor)
{ {
FILE *fp = stream->s_ptr; FILE *fp = stream->s_ptr0;
long pos = ftell(fp); long pos = ftell(fp);
if (pos == -1L) { if (pos == -1L) {
return B_ERR_NOT_SUPPORTED; return B_ERR_NOT_SUPPORTED;
@@ -86,39 +86,39 @@ static struct b_stream stdio[] = {
.s_read = stdio_read, .s_read = stdio_read,
.s_seek = stdio_seek, .s_seek = stdio_seek,
.s_tell = stdio_tell, .s_tell = stdio_tell,
.s_ptr = NULL, /* set to stdin (stdio.h) at runtime */ .s_ptr0 = NULL, /* set to stdin (stdio.h) at runtime */
}, },
[IDX_STDOUT] = { [IDX_STDOUT] = {
.s_mode = B_STREAM_WRITE, .s_mode = B_STREAM_WRITE,
.s_write = stdio_write, .s_write = stdio_write,
.s_seek = stdio_seek, .s_seek = stdio_seek,
.s_tell = stdio_tell, .s_tell = stdio_tell,
.s_ptr = NULL, /* set to stdout (stdio.h) at runtime */ .s_ptr0 = NULL, /* set to stdout (stdio.h) at runtime */
}, },
[IDX_STDERR] = { [IDX_STDERR] = {
.s_mode = B_STREAM_WRITE, .s_mode = B_STREAM_WRITE,
.s_write = stdio_write, .s_write = stdio_write,
.s_seek = stdio_seek, .s_seek = stdio_seek,
.s_tell = stdio_tell, .s_tell = stdio_tell,
.s_ptr = NULL, /* set to stderr (stdio.h) at runtime */ .s_ptr0 = NULL, /* set to stderr (stdio.h) at runtime */
}, },
}; };
struct b_stream *z__b_stream_get_stdin(void) struct b_stream *z__b_stream_get_stdin(void)
{ {
stdio[IDX_STDIN].s_ptr = stdin; stdio[IDX_STDIN].s_ptr0 = stdin;
return &stdio[IDX_STDIN]; return &stdio[IDX_STDIN];
} }
struct b_stream *z__b_stream_get_stdout(void) struct b_stream *z__b_stream_get_stdout(void)
{ {
stdio[IDX_STDOUT].s_ptr = stdout; stdio[IDX_STDOUT].s_ptr0 = stdout;
return &stdio[IDX_STDOUT]; return &stdio[IDX_STDOUT];
} }
struct b_stream *z__b_stream_get_stderr(void) struct b_stream *z__b_stream_get_stderr(void)
{ {
stdio[IDX_STDERR].s_ptr = stderr; stdio[IDX_STDERR].s_ptr0 = stderr;
return &stdio[IDX_STDERR]; return &stdio[IDX_STDERR];
} }
@@ -180,7 +180,7 @@ struct b_stream *b_stream_open_fp(FILE *fp)
memset(stream, 0x0, sizeof *stream); memset(stream, 0x0, sizeof *stream);
stream->s_mode = B_STREAM_READ | B_STREAM_WRITE; stream->s_mode = B_STREAM_READ | B_STREAM_WRITE;
stream->s_ptr = fp; stream->s_ptr0 = fp;
stream->s_read = stdio_read; stream->s_read = stdio_read;
stream->s_write = stdio_write; stream->s_write = stdio_write;
stream->s_seek = stdio_seek; stream->s_seek = stdio_seek;