core: stream: add seek support to stdio streams

This commit is contained in:
2025-07-31 11:13:18 +01:00
parent 918bdad153
commit 771044a95a

View File

@@ -9,7 +9,7 @@
#define IDX_STDOUT 1
#define IDX_STDERR 2
static b_status stdio_read(
static enum b_status stdio_read(
struct b_stream *stream, unsigned char *out, size_t max, size_t *nr_read)
{
FILE *fp = stream->s_ptr;
@@ -25,7 +25,7 @@ static b_status stdio_read(
return status;
}
static b_status stdio_write(
static enum b_status stdio_write(
struct b_stream *stream, const unsigned char *data, size_t count,
size_t *nr_written)
{
@@ -41,20 +41,50 @@ static b_status stdio_write(
return status;
}
static enum b_status stdio_seek(
struct b_stream *stream, long long offset, b_stream_seek_origin origin)
{
FILE *fp = stream->s_ptr;
int whence = 0;
switch (origin) {
case B_STREAM_SEEK_START:
whence = SEEK_SET;
break;
case B_STREAM_SEEK_CURRENT:
whence = SEEK_CUR;
break;
case B_STREAM_SEEK_END:
whence = SEEK_END;
break;
default:
return B_ERR_INVALID_ARGUMENT;
}
int ret = fseek(fp, offset, whence);
if (ret != 0) {
return B_ERR_NOT_SUPPORTED;
}
return B_SUCCESS;
}
static struct b_stream stdio[] = {
[IDX_STDIN] = {
.s_mode = B_STREAM_READ,
.s_read = stdio_read,
.s_seek = stdio_seek,
.s_ptr = NULL, /* set to stdin (stdio.h) at runtime */
},
[IDX_STDOUT] = {
.s_mode = B_STREAM_WRITE,
.s_write = stdio_write,
.s_seek = stdio_seek,
.s_ptr = NULL, /* set to stdout (stdio.h) at runtime */
},
[IDX_STDERR] = {
.s_mode = B_STREAM_WRITE,
.s_write = stdio_write,
.s_seek = stdio_seek,
.s_ptr = NULL, /* set to stderr (stdio.h) at runtime */
},
};
@@ -138,6 +168,7 @@ struct b_stream *b_stream_open_fp(FILE *fp)
stream->s_ptr = fp;
stream->s_read = stdio_read;
stream->s_write = stdio_write;
stream->s_seek = stdio_seek;
return stream;
}