Support stdin and stdout in file stream utils
This commit is contained in:
parent
ff984c1551
commit
cb6930d9a8
|
@ -5,7 +5,7 @@
|
|||
#include <stdio.h>
|
||||
#include "../error/error.c"
|
||||
|
||||
#define HBU_FSTREAM_BUILD_INFRA(type, mode, noun, verb) \
|
||||
#define HBU_FSTREAM_BUILD_INFRA(type, mode, noun, verb, std) \
|
||||
typedef struct hbu_fstream##type##_s { \
|
||||
const char *name; \
|
||||
FILE *fd; \
|
||||
|
@ -13,16 +13,22 @@
|
|||
\
|
||||
hbu_fstream##type##_t hbu_fstream##type##_create(char *path) { \
|
||||
hbu_fstream##type##_t fstream = malloc(sizeof(struct hbu_fstream##type##_s)); \
|
||||
fstream->name = path; \
|
||||
\
|
||||
FILE *fd = fopen(path, mode); \
|
||||
\
|
||||
if (fd == NULL) { \
|
||||
hbe_fatal(HBE_IO_FOPEN_FAIL, "Failed to open file %s for " verb " with error %d", fstream->name, errno); \
|
||||
if (path == NULL) { \
|
||||
fstream->name = #std; \
|
||||
fstream->fd = std; \
|
||||
} else { \
|
||||
fstream->name = path; \
|
||||
\
|
||||
FILE *fd = fopen(path, mode); \
|
||||
\
|
||||
if (fd == NULL) { \
|
||||
hbe_fatal(HBE_IO_FOPEN_FAIL, "Failed to open file %s for " verb " with error %d", fstream->name, errno); \
|
||||
} \
|
||||
\
|
||||
fstream->fd = fd; \
|
||||
} \
|
||||
\
|
||||
fstream->fd = fd; \
|
||||
\
|
||||
return fstream; \
|
||||
} \
|
||||
\
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include "../error/error.c"
|
||||
#include "fstream.h"
|
||||
|
||||
HBU_FSTREAM_BUILD_INFRA(in, "r", "read", "reading")
|
||||
HBU_FSTREAM_BUILD_INFRA(in, "r", "read", "reading", stdin)
|
||||
|
||||
hb_eod_char_t hbu_fstreamin_read(hbu_fstreamin_t fstreamin) {
|
||||
hb_char_t c;
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
#include "../error/error.c"
|
||||
#include "fstream.h"
|
||||
|
||||
HBU_FSTREAM_BUILD_INFRA(out, "w", "write", "writing")
|
||||
HBU_FSTREAM_BUILD_INFRA(out, "w", "write", "writing", stdout)
|
||||
|
||||
void hbu_fstreamout_write(hbu_fstreamout_t fstreamout, hb_char_t c) {
|
||||
if (fwrite(&c, SIZEOF_CHAR, 1, fstreamout->fd) != SIZEOF_CHAR) {
|
||||
|
|
Loading…
Reference in New Issue