aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2024-08-03 12:31:51 -0400
committerDavid Robillard <d@drobilla.net>2024-08-03 14:21:47 -0400
commitebe0b8697bca69abb7e7be1c731da4d54b10e462 (patch)
tree73f5b777ab632b2001a132eea82cd97499f330fa
parentf73aae2f97d1faa8e41a8b8627e1e186a1f900f4 (diff)
downloadserd-ebe0b8697bca69abb7e7be1c731da4d54b10e462.tar.gz
serd-ebe0b8697bca69abb7e7be1c731da4d54b10e462.tar.bz2
serd-ebe0b8697bca69abb7e7be1c731da4d54b10e462.zip
Fix reading chunks from files without trailing newlines
-rw-r--r--NEWS3
-rw-r--r--src/byte_source.c15
-rw-r--r--src/byte_source.h2
-rw-r--r--test/test_reader.c17
4 files changed, 20 insertions, 17 deletions
diff --git a/NEWS b/NEWS
index 3e9c71d3..8ccb7568 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,7 @@ serd (0.32.3) unstable; urgency=medium
* Fix library current_version on MacOS
* Fix overly permissive parsing of syntax names on the command line
* Fix parsing NQuads lines with no space before the final dot
+ * Fix reading chunks from files without trailing newlines
* Fix rewriting special literals when datatype URIs are prefixed names
* Gracefully handle errors while writing the end of anonymous nodes
* Improve test suite coverage
@@ -12,7 +13,7 @@ serd (0.32.3) unstable; urgency=medium
* Treat out of range unicode characters as errors
* Write blank lines between graphs and statements in TriG
- -- David Robillard <d@drobilla.net> Tue, 25 Jun 2024 22:38:16 +0000
+ -- David Robillard <d@drobilla.net> Sat, 03 Aug 2024 15:50:39 +0000
serd (0.32.2) stable; urgency=medium
diff --git a/src/byte_source.c b/src/byte_source.c
index 5443f4dd..122dc0f9 100644
--- a/src/byte_source.c
+++ b/src/byte_source.c
@@ -19,18 +19,17 @@ serd_byte_source_page(SerdByteSource* const source)
const size_t n_read =
source->read_func(source->file_buf, 1, source->page_size, source->stream);
+ source->buf_size = n_read;
+ if (n_read < source->page_size) {
+ source->file_buf[n_read] = '\0';
+ }
+
if (n_read == 0) {
- source->file_buf[0] = '\0';
- source->eof = true;
+ source->eof = true;
return (source->error_func(source->stream) ? SERD_ERR_UNKNOWN
: SERD_FAILURE);
}
- if (n_read < source->page_size) {
- source->file_buf[n_read] = '\0';
- source->buf_size = n_read;
- }
-
return SERD_SUCCESS;
}
@@ -52,7 +51,7 @@ serd_byte_source_open_source(SerdByteSource* const source,
source->stream = stream;
source->from_stream = true;
source->page_size = page_size;
- source->buf_size = page_size;
+ source->buf_size = 0U;
source->cur = cur;
source->error_func = error_func;
source->read_func = read_func;
diff --git a/src/byte_source.h b/src/byte_source.h
index bd6701c9..afd9ccb9 100644
--- a/src/byte_source.h
+++ b/src/byte_source.h
@@ -83,7 +83,7 @@ serd_byte_source_advance(SerdByteSource* source)
if (source->page_size > 1) {
if (++source->read_head == source->page_size) {
st = serd_byte_source_page(source);
- } else if (source->read_head == source->buf_size) {
+ } else if (source->read_head >= source->buf_size) {
source->eof = true;
}
} else {
diff --git a/test/test_reader.c b/test/test_reader.c
index cfaa8bed..18794f9e 100644
--- a/test/test_reader.c
+++ b/test/test_reader.c
@@ -219,6 +219,7 @@ test_read_eof_by_byte(void)
assert(serd_reader_read_chunk(reader) == SERD_SUCCESS);
assert(serd_reader_read_chunk(reader) == SERD_FAILURE);
+ serd_reader_end_stream(reader);
serd_reader_free(reader);
}
@@ -243,7 +244,7 @@ test_read_nquads_chunks(const char* const path)
fprintf(f,
"<http://example.org/s> <http://example.org/p3> "
- "<http://example.org/o3> .\n");
+ "<http://example.org/o3> .");
fseek(f, 0, SEEK_SET);
@@ -260,7 +261,9 @@ test_read_nquads_chunks(const char* const path)
assert(serd_reader_get_handle(reader) == &rt);
assert(f);
- SerdStatus st = serd_reader_start_stream(reader, f, NULL, false);
+ SerdStatus st = serd_reader_start_source_stream(
+ reader, (SerdSource)fread, (SerdStreamErrorFunc)ferror, f, NULL, 32U);
+
assert(st == SERD_SUCCESS);
// Read first statement
@@ -304,7 +307,7 @@ test_read_nquads_chunks(const char* const path)
assert(rt.n_end == 0);
assert(serd_reader_read_chunk(reader) == SERD_FAILURE);
-
+ serd_reader_end_stream(reader);
serd_reader_free(reader);
fclose(f);
remove(path);
@@ -323,8 +326,7 @@ test_read_turtle_chunks(const char* const path)
fprintf(f, "eg:s eg:p1 eg:o1 ;\n");
fprintf(f, " eg:p2 eg:o2 .\n");
fwrite(&null, sizeof(null), 1, f);
- fprintf(f, "eg:s eg:p [ eg:sp eg:so ] .\n");
- fwrite(&null, sizeof(null), 1, f);
+ fprintf(f, "eg:s eg:p [ eg:sp eg:so ] .");
fseek(f, 0, SEEK_SET);
ReaderTest rt = {0, 0, 0, 0};
@@ -340,7 +342,8 @@ test_read_turtle_chunks(const char* const path)
assert(serd_reader_get_handle(reader) == &rt);
assert(f);
- SerdStatus st = serd_reader_start_stream(reader, f, NULL, false);
+ SerdStatus st = serd_reader_start_source_stream(
+ reader, (SerdSource)fread, (SerdStreamErrorFunc)ferror, f, NULL, 32U);
assert(st == SERD_SUCCESS);
// Read base
@@ -400,7 +403,7 @@ test_read_turtle_chunks(const char* const path)
assert(rt.n_end == 1);
assert(serd_reader_read_chunk(reader) == SERD_FAILURE);
-
+ serd_reader_end_stream(reader);
serd_reader_free(reader);
fclose(f);
remove(path);