aboutsummaryrefslogtreecommitdiffstats
path: root/tools/console.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/console.c')
-rw-r--r--tools/console.c34
1 files changed, 34 insertions, 0 deletions
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 <io.h>
#endif
+#include <stdint.h>
+#include <string.h>
+
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);
+}