Browse Source

Add serd_chunk_sink for easy writing to a string.

git-svn-id: http://svn.drobilla.net/serd/trunk@329 490d8e77-9747-427b-9fa3-0b8f29cee8a0
zrythm_meson
David Robillard 11 years ago
parent
commit
7b022006c4
  1. 3
      ChangeLog
  2. 24
      serd/serd.h
  3. 19
      src/writer.c
  4. 21
      tests/serd_test.c
  5. 2
      wscript

3
ChangeLog

@ -15,7 +15,8 @@ serd (UNRELEASED) unstable; urgency=low @@ -15,7 +15,8 @@ serd (UNRELEASED) unstable; urgency=low
other tools fail to parse.
* Add serd_strtod(), serd_node_new_decimal(), and serd_node_new_integer()
for locale-independent numeric node parsing/serialising.
* Add serd_file_sink for the common case of writing to a FILE* stream.
* Add serd_file_sink for easy writing to a FILE* stream.
* Add serd_chunk_sink for easy writing to a string.
* Escape ASCII control characters in output (e.g. fix problems with string
literals that start with a backspace)
* Improve URI resolution to cover most of the abnormal cases from RFC3986

24
serd/serd.h

@ -696,12 +696,34 @@ serd_writer_free(SerdWriter* writer); @@ -696,12 +696,34 @@ serd_writer_free(SerdWriter* writer);
A convenience sink function for writing to a FILE*.
This function can be used as a SerdSink when writing to a FILE*. The
associated @c stream parameter must be a FILE* opened for writing.
@c stream parameter must be a FILE* opened for writing.
*/
SERD_API
size_t
serd_file_sink(const void* buf, size_t len, void* stream);
/**
A convenience sink function for writing to a string.
This function can be used as a SerdSink to write to a SerdChunk which is
resized as necessary with realloc(). The @c stream parameter must point to
an initialized SerdChunk. When the write is finished, the string should be
retrieved with serd_chunk_sink_finish().
*/
SERD_API
size_t
serd_chunk_sink(const void* buf, size_t len, void* stream);
/**
Finish a serialisation to a chunk with serd_chunk_sink().
The returned string is the result of the serialisation, which is NULL
terminated (by this function) and owned by the caller.
*/
SERD_API
uint8_t*
serd_chunk_sink_finish(SerdChunk* stream);
/**
Set a prefix to be removed from matching blank node identifiers.
*/

19
src/writer.c

@ -714,3 +714,22 @@ serd_file_sink(const void* buf, size_t len, void* stream) @@ -714,3 +714,22 @@ serd_file_sink(const void* buf, size_t len, void* stream)
{
return fwrite(buf, 1, len, (FILE*)stream);
}
SERD_API
size_t
serd_chunk_sink(const void* buf, size_t len, void* stream)
{
SerdChunk* chunk = (SerdChunk*)stream;
chunk->buf = realloc((uint8_t*)chunk->buf, chunk->len + len);
memcpy((uint8_t*)chunk->buf + chunk->len, buf, len);
chunk->len += len;
return len;
}
SERD_API
uint8_t*
serd_chunk_sink_finish(SerdChunk* stream)
{
serd_chunk_sink("", 1, stream);
return (uint8_t*)stream->buf;
}

21
tests/serd_test.c

@ -443,6 +443,27 @@ main() @@ -443,6 +443,27 @@ main()
}
serd_writer_free(writer);
// Test chunk sink
SerdChunk chunk = { NULL, 0 };
writer = serd_writer_new(
SERD_TURTLE, (SerdStyle)0, env, NULL, serd_chunk_sink, &chunk);
o = serd_node_from_string(SERD_URI, USTR("http://example.org/base"));
if (serd_writer_set_base_uri(writer, &o)) {
return failure("Failed to write to chunk sink\n");
}
serd_writer_free(writer);
uint8_t* out = serd_chunk_sink_finish(&chunk);
if (strcmp((const char*)out, "@base <http://example.org/base> .\n")) {
return failure("Incorrect chunk output:\n%s\n", chunk.buf);
}
free(out);
// Rewind and test reader
fseek(fd, 0, SEEK_SET);
SerdReader* reader = serd_reader_new(

2
wscript

@ -9,7 +9,7 @@ from waflib.extras import autowaf as autowaf @@ -9,7 +9,7 @@ from waflib.extras import autowaf as autowaf
import waflib.Logs as Logs, waflib.Options as Options
# Version of this package (even if built as a child)
SERD_VERSION = '0.10.0'
SERD_VERSION = '0.11.0'
SERD_MAJOR_VERSION = '0'
# Library version (UNIX style major, minor, micro)

Loading…
Cancel
Save