aboutsummaryrefslogtreecommitdiffstats
path: root/src/serdi.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/serdi.c')
-rw-r--r--src/serdi.c39
1 files changed, 31 insertions, 8 deletions
diff --git a/src/serdi.c b/src/serdi.c
index f0cf8f4b..0e59c5ee 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -35,6 +35,7 @@
#include <errno.h>
#include <stdbool.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -150,6 +151,22 @@ serd_fopen(const char* path, const char* mode)
return fd;
}
+/** fread-like wrapper for getc (which is faster). */
+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;
+}
+
static SerdWriterFlags
choose_style(const SerdSyntax input_syntax,
const SerdSyntax output_syntax,
@@ -345,17 +362,23 @@ main(int argc, char** argv)
SerdStatus st = SERD_SUCCESS;
if (!from_file) {
- st = serd_reader_read_string(reader, input);
- } else if (bulk_read) {
- st = serd_reader_read_file_handle(reader, in_fd, in_name);
+ st = serd_reader_start_string(reader, input);
} else {
- st = serd_reader_start_stream(reader, in_fd, in_name, false);
- while (!st) {
- st = serd_reader_read_chunk(reader);
- }
- serd_reader_end_stream(reader);
+ st = serd_reader_start_stream(reader,
+ bulk_read ? (SerdSource)fread
+ : serd_file_read_byte,
+ (SerdStreamErrorFunc)ferror,
+ in_fd,
+ in_name,
+ bulk_read ? 4096 : 1);
+ }
+
+ if (!st) {
+ st = serd_reader_read_document(reader);
}
+ serd_reader_end_stream(reader);
+
serd_reader_free(reader);
serd_writer_finish(writer);
serd_writer_free(writer);