core: stream: add function to open a b_stream from a FILE pointer
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
#include <blue/core/misc.h>
|
#include <blue/core/misc.h>
|
||||||
#include <blue/core/status.h>
|
#include <blue/core/status.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#define b_stdin (z__b_stream_get_stdin())
|
#define b_stdin (z__b_stream_get_stdin())
|
||||||
#define b_stdout (z__b_stream_get_stdout())
|
#define b_stdout (z__b_stream_get_stdout())
|
||||||
@@ -64,6 +65,8 @@ BLUE_API b_status b_stream_pipeline_init(
|
|||||||
void *p, size_t len, b_stream_pipeline *out);
|
void *p, size_t len, b_stream_pipeline *out);
|
||||||
BLUE_API b_status b_stream_pipeline_destroy(b_stream_pipeline *pipeline);
|
BLUE_API b_status b_stream_pipeline_destroy(b_stream_pipeline *pipeline);
|
||||||
|
|
||||||
|
BLUE_API b_stream *b_stream_open_fp(FILE *fp);
|
||||||
|
|
||||||
BLUE_API b_status b_stream_close(b_stream *stream);
|
BLUE_API b_status b_stream_close(b_stream *stream);
|
||||||
BLUE_API b_status b_stream_reserve(b_stream *stream, size_t len);
|
BLUE_API b_status b_stream_reserve(b_stream *stream, size_t len);
|
||||||
BLUE_API b_status b_stream_seek(
|
BLUE_API b_status b_stream_seek(
|
||||||
|
|||||||
@@ -13,21 +13,12 @@ static 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_ptr;
|
||||||
size_t count = 0;
|
|
||||||
enum b_status status = B_SUCCESS;
|
enum b_status status = B_SUCCESS;
|
||||||
|
|
||||||
for (size_t i = 0; i < max; i++) {
|
size_t count = fread(out, 1, max, fp);
|
||||||
int c = fgetc(fp);
|
|
||||||
if (ferror(fp)) {
|
|
||||||
status = B_ERR_IO_FAILURE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == -1) {
|
if (ferror(fp)) {
|
||||||
break;
|
status = B_ERR_IO_FAILURE;
|
||||||
}
|
|
||||||
|
|
||||||
out[count++] = c;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*nr_read = count;
|
*nr_read = count;
|
||||||
@@ -40,17 +31,10 @@ static b_status stdio_write(
|
|||||||
{
|
{
|
||||||
FILE *fp = stream->s_ptr;
|
FILE *fp = stream->s_ptr;
|
||||||
enum b_status status = B_SUCCESS;
|
enum b_status status = B_SUCCESS;
|
||||||
size_t w = 0;
|
size_t w = fwrite(data, 1, count, fp);
|
||||||
|
|
||||||
for (size_t i = 0; i < count; i++) {
|
if (ferror(fp)) {
|
||||||
fputc(data[i], fp);
|
status = B_ERR_IO_FAILURE;
|
||||||
|
|
||||||
if (ferror(fp)) {
|
|
||||||
status = B_ERR_IO_FAILURE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
w++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*nr_written = w;
|
*nr_written = w;
|
||||||
@@ -141,6 +125,23 @@ enum b_status b_stream_pipeline_destroy(struct b_stream_pipeline *pipeline)
|
|||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct b_stream *b_stream_open_fp(FILE *fp)
|
||||||
|
{
|
||||||
|
struct b_stream *stream = malloc(sizeof *stream);
|
||||||
|
if (!stream) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(stream, 0x0, sizeof *stream);
|
||||||
|
|
||||||
|
stream->s_mode = B_STREAM_READ | B_STREAM_WRITE;
|
||||||
|
stream->s_ptr = fp;
|
||||||
|
stream->s_read = stdio_read;
|
||||||
|
stream->s_write = stdio_write;
|
||||||
|
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
enum b_status b_stream_close(b_stream *stream)
|
enum b_status b_stream_close(b_stream *stream)
|
||||||
{
|
{
|
||||||
if (stream->s_istack) {
|
if (stream->s_istack) {
|
||||||
|
|||||||
Reference in New Issue
Block a user