aboutsummaryrefslogtreecommitdiffstats
path: root/tools/console.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2023-03-29 23:34:40 -0400
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:08 -0500
commit4e1850852a74fe6904beabf793ea8d9915cab246 (patch)
tree5fff5fb4aa6161d7e69434e6d3ca587a971c35ea /tools/console.c
parent5796f49ec8ff1933f4a3c258c16f140b39cc1c03 (diff)
downloadserd-4e1850852a74fe6904beabf793ea8d9915cab246.tar.gz
serd-4e1850852a74fe6904beabf793ea8d9915cab246.tar.bz2
serd-4e1850852a74fe6904beabf793ea8d9915cab246.zip
Add support for reading multiple files at once
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);
+}