From 4e1850852a74fe6904beabf793ea8d9915cab246 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 29 Mar 2023 23:34:40 -0400 Subject: Add support for reading multiple files at once --- tools/console.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'tools/console.c') diff --git a/tools/console.c b/tools/console.c index 4a127c3e..f6a15ecb 100644 --- a/tools/console.c +++ b/tools/console.c @@ -13,6 +13,9 @@ # include #endif +#include +#include + void serd_set_stream_utf8_mode(FILE* const stream) { @@ -39,3 +42,34 @@ serd_print_version(const char* const program) return 0; } + +/// Wrapper for getc that is compatible with SerdReadFunc but faster than fread +static size_t +serd_file_read_byte(void* buf, size_t size, size_t nmemb, void* stream) +{ + (void)size; + (void)nmemb; + + const int c = getc((FILE*)stream); + if (c == EOF) { + *((uint8_t*)buf) = 0; + return 0; + } + + *((uint8_t*)buf) = (uint8_t)c; + return 1; +} + +SerdInputStream +serd_open_tool_input(const char* const filename) +{ + if (!strcmp(filename, "-")) { + const SerdInputStream in = serd_open_input_stream( + serd_file_read_byte, (SerdErrorFunc)ferror, NULL, stdin); + + serd_set_stream_utf8_mode(stdin); + return in; + } + + return serd_open_input_file(filename); +} -- cgit v1.2.1