// Copyright 2011-2022 David Robillard // SPDX-License-Identifier: ISC #ifndef SERD_WRITER_H #define SERD_WRITER_H #include "serd/attributes.h" #include "serd/env.h" #include "serd/node.h" #include "serd/sink.h" #include "serd/status.h" #include "serd/stream.h" #include "serd/syntax.h" #include "serd/world.h" #include SERD_BEGIN_DECLS /** @defgroup serd_writer Writer @ingroup serd_reading_writing @{ */ /// Streaming serialiser that writes a text stream as statements are pushed typedef struct SerdWriterImpl SerdWriter; /** Writer style options. These flags allow more precise control of writer output style. Note that some options are only supported for some syntaxes, for example, NTriples does not support abbreviation and is always ASCII. */ typedef enum { SERD_WRITE_ABBREVIATED = 1U << 0U, ///< Abbreviate triples when possible SERD_WRITE_ASCII = 1U << 1U, ///< Escape all non-ASCII characters SERD_WRITE_RESOLVED = 1U << 2U, ///< Resolve URIs against base URI SERD_WRITE_CURIED = 1U << 3U, ///< Shorten URIs into CURIEs SERD_WRITE_BULK = 1U << 4U, ///< Write output in pages SERD_WRITE_STRICT = 1U << 5U, ///< Abort with error on lossy output } SerdWriterFlag; /// Bitwise OR of #SerdWriterFlag values typedef uint32_t SerdWriterFlags; /// Create a new RDF writer SERD_API SerdWriter* SERD_ALLOCATED serd_writer_new(SerdWorld* SERD_NONNULL world, SerdSyntax syntax, SerdWriterFlags flags, SerdEnv* SERD_NONNULL env, SerdWriteFunc SERD_NONNULL ssink, void* SERD_NULLABLE stream); /// Free `writer` SERD_API void serd_writer_free(SerdWriter* SERD_NULLABLE writer); /// Return a sink interface that emits statements via `writer` SERD_CONST_API const SerdSink* SERD_NONNULL serd_writer_sink(SerdWriter* SERD_NONNULL writer); /** Set a prefix to be removed from matching blank node identifiers. This is the counterpart to serd_reader_add_blank_prefix() which can be used to "undo" added prefixes. */ SERD_API void serd_writer_chop_blank_prefix(SerdWriter* SERD_NONNULL writer, const char* SERD_NULLABLE prefix); /** Set the current output base URI, and emit a directive if applicable. Note this function can be safely casted to #SerdBaseFunc. */ SERD_API SerdStatus serd_writer_set_base_uri(SerdWriter* SERD_NONNULL writer, const SerdNode* SERD_NULLABLE uri); /** Set the current root URI. The root URI should be a prefix of the base URI. The path of the root URI is the highest path any relative up-reference can refer to. For example, with root and base , will be written as <../>, but will be written non-relatively as . If the root is not explicitly set, it defaults to the base URI, so no up-references will be created at all. */ SERD_API SerdStatus serd_writer_set_root_uri(SerdWriter* SERD_NONNULL writer, const SerdNode* SERD_NULLABLE uri); /** Finish a write. This flushes any pending output, for example terminating punctuation, so that the output is a complete document. */ SERD_API SerdStatus serd_writer_finish(SerdWriter* SERD_NONNULL writer); /** @} */ SERD_END_DECLS #endif // SERD_WRITER_H