aboutsummaryrefslogtreecommitdiffstats
path: root/src/serdi.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2020-06-28 19:46:47 +0200
committerDavid Robillard <d@drobilla.net>2021-03-08 23:23:06 -0500
commitf7b993d5aff1274e010d45304b12109f6de7e120 (patch)
tree6fadbff8f2e696ce62ba1237bee16c07776cd03e /src/serdi.c
parent2fb247d2beb71539ceba8f2841d7c1bad933ab36 (diff)
downloadserd-f7b993d5aff1274e010d45304b12109f6de7e120.tar.gz
serd-f7b993d5aff1274e010d45304b12109f6de7e120.tar.bz2
serd-f7b993d5aff1274e010d45304b12109f6de7e120.zip
WIP: Make Writer always write to a ByteSink
Diffstat (limited to 'src/serdi.c')
-rw-r--r--src/serdi.c45
1 files changed, 28 insertions, 17 deletions
diff --git a/src/serdi.c b/src/serdi.c
index e546f463..8b72945a 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -110,6 +110,7 @@ main(int argc, char** argv)
const char* add_prefix = NULL;
const char* chop_prefix = NULL;
const char* root_uri = NULL;
+ const char* out_filename = NULL;
int a = 1;
for (; a < argc && argv[a][0] == '-'; ++a) {
if (argv[a][1] == '\0') {
@@ -188,6 +189,11 @@ main(int argc, char** argv)
}
root_uri = argv[a];
+ } else if (argv[a][1] == 'w') {
+ if (++a == argc) {
+ return missing_arg(argv[0], 'w');
+ }
+ out_filename = argv[a];
} else {
SERDI_ERRORF("invalid option -- '%s'\n", argv[a] + 1);
return print_usage(argv[0], true);
@@ -199,11 +205,6 @@ main(int argc, char** argv)
return 1;
}
-#ifdef _WIN32
- _setmode(_fileno(stdin), _O_BINARY);
- _setmode(_fileno(stdout), _O_BINARY);
-#endif
-
const char* input = argv[a++];
if (!input_syntax && !(input_syntax = serd_guess_syntax(input))) {
@@ -222,20 +223,29 @@ main(int argc, char** argv)
base = serd_new_file_uri(SERD_MEASURE_STRING(input), SERD_EMPTY_STRING());
}
- FILE* const out_fd = stdout;
- SerdWorld* const world = serd_world_new();
- SerdEnv* const env = serd_env_new(serd_node_string_view(base));
+ SerdWorld* const world = serd_world_new();
+ SerdEnv* const env = serd_env_new(serd_node_string_view(base));
+#ifdef _WIN32
+ _setmode(_fileno(stdin), _O_BINARY);
+ if (!out_filename) {
+ _setmode(_fileno(stdout), _O_BINARY);
+ }
+#endif
+
+ const size_t block_size = bulk_write ? 4096u : 1u;
SerdByteSink* const byte_sink =
- serd_byte_sink_new((SerdWriteFunc)fwrite, out_fd, bulk_write ? 4096u : 1u);
+ out_filename
+ ? serd_byte_sink_new_filename(out_filename, block_size)
+ : serd_byte_sink_new_function((SerdWriteFunc)fwrite, stdout, block_size);
+
+ if (!byte_sink) {
+ perror("serdi: error opening output file");
+ return 1;
+ }
SerdWriter* const writer =
- serd_writer_new(world,
- output_syntax,
- writer_flags,
- env,
- (SerdWriteFunc)serd_byte_sink_write,
- byte_sink);
+ serd_writer_new(world, output_syntax, writer_flags, env, byte_sink);
SerdReader* const reader = serd_reader_new(
world, input_syntax, reader_flags, serd_writer_sink(writer), stack_size);
@@ -275,15 +285,16 @@ main(int argc, char** argv)
serd_reader_free(reader);
serd_writer_free(writer);
serd_node_free(input_name);
- serd_byte_sink_free(byte_sink);
serd_env_free(env);
serd_node_free(base);
serd_world_free(world);
- if (fclose(stdout)) {
+ if (serd_byte_sink_close(byte_sink) || (!out_filename && fclose(stdout))) {
perror("serdi: write error");
st = SERD_ERR_UNKNOWN;
}
+ serd_byte_sink_free(byte_sink);
+
return (st > SERD_FAILURE) ? 1 : 0;
}