aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-12-26 19:25:51 -0500
committerDavid Robillard <d@drobilla.net>2019-12-19 20:52:34 -0500
commite61d7f710465ca8c87274d9664afeb0b125597a4 (patch)
treee90661112ed48f7e0e44932b21f59c2c148f1e25
parent5522f5ff705374d8d7bba3d21a05674640967c3e (diff)
downloadserd-e61d7f710465ca8c87274d9664afeb0b125597a4.tar.gz
serd-e61d7f710465ca8c87274d9664afeb0b125597a4.tar.bz2
serd-e61d7f710465ca8c87274d9664afeb0b125597a4.zip
Hide fopen wrapper and use reader interface consistently
-rw-r--r--serd/serd.h14
-rw-r--r--src/byte_source.c15
-rw-r--r--src/byte_source.h9
-rw-r--r--src/reader.c70
-rw-r--r--src/serd_internal.h25
-rw-r--r--src/serdi.c78
-rw-r--r--src/world.c23
-rw-r--r--src/world.h2
-rw-r--r--tests/serd_test.c24
9 files changed, 115 insertions, 145 deletions
diff --git a/serd/serd.h b/serd/serd.h
index 2a79ff25..37a90750 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -915,12 +915,11 @@ serd_reader_set_default_graph(SerdReader* reader,
const SerdNode* graph);
/**
- Read a file at a given `uri`.
+ Prepare to read from the file at a local file `uri`.
*/
SERD_API
SerdStatus
-serd_reader_read_file(SerdReader* reader,
- const char* uri);
+serd_reader_start_file(SerdReader* reader, const char* uri, bool bulk);
/**
Prepare to read from a stream.
@@ -972,14 +971,7 @@ serd_reader_read_document(SerdReader* reader);
*/
SERD_API
SerdStatus
-serd_reader_end_stream(SerdReader* reader);
-
-/**
- Read `utf8`.
-*/
-SERD_API
-SerdStatus
-serd_reader_read_string(SerdReader* reader, const char* utf8);
+serd_reader_finish(SerdReader* reader);
/**
Free `reader`.
diff --git a/src/byte_source.c b/src/byte_source.c
index af01411e..2e2499f5 100644
--- a/src/byte_source.c
+++ b/src/byte_source.c
@@ -40,6 +40,7 @@ SerdStatus
serd_byte_source_open_source(SerdByteSource* source,
SerdReadFunc read_func,
SerdStreamErrorFunc error_func,
+ SerdStreamCloseFunc close_func,
void* stream,
const char* name,
size_t page_size)
@@ -47,13 +48,14 @@ serd_byte_source_open_source(SerdByteSource* source,
const Cursor cur = { name, 1, 1 };
memset(source, '\0', sizeof(*source));
+ source->read_func = read_func;
+ source->error_func = error_func;
+ source->close_func = close_func;
source->stream = stream;
- source->from_stream = true;
source->page_size = page_size;
source->buf_size = page_size;
source->cur = cur;
- source->error_func = error_func;
- source->read_func = read_func;
+ source->from_stream = true;
if (page_size > 1) {
source->file_buf = (uint8_t*)serd_bufalloc(page_size);
@@ -94,9 +96,14 @@ serd_byte_source_open_string(SerdByteSource* source, const char* utf8)
SerdStatus
serd_byte_source_close(SerdByteSource* source)
{
+ SerdStatus st = SERD_SUCCESS;
+ if (source->close_func) {
+ st = source->close_func(source->stream) ? SERD_ERR_UNKNOWN
+ : SERD_SUCCESS;
+ }
if (source->page_size > 1) {
free(source->file_buf);
}
memset(source, '\0', sizeof(*source));
- return SERD_SUCCESS;
+ return st;
}
diff --git a/src/byte_source.h b/src/byte_source.h
index dcf97bac..7a04db8d 100644
--- a/src/byte_source.h
+++ b/src/byte_source.h
@@ -23,6 +23,8 @@
#include <stddef.h>
#include <stdio.h>
+typedef int (*SerdStreamCloseFunc)(void*);
+
typedef struct {
const char* filename;
unsigned line;
@@ -32,6 +34,7 @@ typedef struct {
typedef struct {
SerdReadFunc read_func; ///< Read function (e.g. fread)
SerdStreamErrorFunc error_func; ///< Error function (e.g. ferror)
+ SerdStreamCloseFunc close_func; ///< Function for closing stream
void* stream; ///< Stream (e.g. FILE)
size_t page_size; ///< Number of bytes to read at a time
size_t buf_size; ///< Number of bytes in file_buf
@@ -46,17 +49,13 @@ typedef struct {
} SerdByteSource;
SerdStatus
-serd_byte_source_open_file(SerdByteSource* source,
- FILE* file,
- bool bulk);
-
-SerdStatus
serd_byte_source_open_string(SerdByteSource* source, const char* utf8);
SerdStatus
serd_byte_source_open_source(SerdByteSource* source,
SerdReadFunc read_func,
SerdStreamErrorFunc error_func,
+ SerdStreamCloseFunc close_func,
void* stream,
const char* name,
size_t page_size);
diff --git a/src/reader.c b/src/reader.c
index 3a72a3e4..7758384b 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -219,37 +219,6 @@ serd_reader_set_default_graph(SerdReader* reader,
reader->default_graph = serd_node_copy(graph);
}
-SerdStatus
-serd_reader_read_file(SerdReader* reader,
- const char* uri)
-{
- char* const path = serd_file_uri_parse(uri, NULL);
- if (!path) {
- return SERD_ERR_BAD_ARG;
- }
-
- FILE* fd = serd_fopen(path, "rb");
- if (!fd) {
- serd_free(path);
- return SERD_ERR_UNKNOWN;
- }
-
- SerdStatus st = serd_reader_start_stream(
- reader, (SerdReadFunc)fread, (SerdStreamErrorFunc)ferror,
- fd, path, SERD_PAGE_SIZE);
-
- if (!st) {
- st = serd_reader_read_document(reader);
- }
-
- const SerdStatus est = serd_reader_end_stream(reader);
-
- fclose(fd);
- free(path);
-
- return st ? st : est;
-}
-
static SerdStatus
skip_bom(SerdReader* me)
{
@@ -276,7 +245,31 @@ serd_reader_start_stream(SerdReader* reader,
size_t page_size)
{
return serd_byte_source_open_source(
- &reader->source, read_func, error_func, stream, name, page_size);
+ &reader->source, read_func, error_func, NULL, stream, name, page_size);
+}
+
+SerdStatus
+serd_reader_start_file(SerdReader* reader, const char* uri, bool bulk)
+{
+ char* const path = serd_file_uri_parse(uri, NULL);
+ if (!path) {
+ return SERD_ERR_BAD_ARG;
+ }
+
+ FILE* fd = serd_world_fopen(reader->world, path, "rb");
+ free(path);
+ if (!fd) {
+ return SERD_ERR_UNKNOWN;
+ }
+
+ return serd_byte_source_open_source(
+ &reader->source,
+ bulk ? (SerdReadFunc)fread : serd_file_read_byte,
+ (SerdStreamErrorFunc)ferror,
+ (SerdStreamCloseFunc)fclose,
+ fd,
+ uri,
+ bulk ? SERD_PAGE_SIZE : 1);
}
SerdStatus
@@ -318,18 +311,7 @@ serd_reader_read_chunk(SerdReader* reader)
}
SerdStatus
-serd_reader_end_stream(SerdReader* reader)
+serd_reader_finish(SerdReader* reader)
{
return serd_byte_source_close(&reader->source);
}
-
-SerdStatus
-serd_reader_read_string(SerdReader* reader, const char* utf8)
-{
- serd_reader_start_string(reader, utf8);
-
- const SerdStatus st = serd_reader_read_document(reader);
- const SerdStatus est = serd_byte_source_close(&reader->source);
-
- return st ? st : est;
-}
diff --git a/src/serd_internal.h b/src/serd_internal.h
index 601af56c..f06c7051 100644
--- a/src/serd_internal.h
+++ b/src/serd_internal.h
@@ -29,7 +29,7 @@
#include "world.h"
-#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_FILENO)
+#if defined(HAVE_FILENO)
# include <fcntl.h>
#endif
@@ -42,19 +42,20 @@
# define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
-static inline FILE*
-serd_fopen(const char* path, const char* mode)
+/** fread-like wrapper for getc (which is faster). */
+static inline size_t
+serd_file_read_byte(void* buf, size_t size, size_t nmemb, void* stream)
{
- FILE* fd = fopen(path, mode);
- if (!fd) {
- fprintf(stderr, "error: failed to open file %s (%s)\n",
- path, strerror(errno));
- return NULL;
+ (void)size;
+ (void)nmemb;
+
+ const int c = getc((FILE*)stream);
+ if (c == EOF) {
+ *((uint8_t*)buf) = 0;
+ return 0;
}
-#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_FILENO)
- posix_fadvise(fileno(fd), 0, 0, POSIX_FADV_SEQUENTIAL);
-#endif
- return fd;
+ *((uint8_t*)buf) = (uint8_t)c;
+ return 1;
}
static inline void*
diff --git a/src/serdi.c b/src/serdi.c
index eda73c2c..0d0bce04 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -27,6 +27,7 @@
#include <string.h>
#include "string_utils.h"
+#include "world.h"
#define SERDI_ERROR(msg) fprintf(stderr, "serdi: " msg);
#define SERDI_ERRORF(fmt, ...) fprintf(stderr, "serdi: " fmt, __VA_ARGS__);
@@ -122,22 +123,6 @@ quiet_error_sink(void* handle, const SerdError* e)
return SERD_SUCCESS;
}
-/** 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;
-}
-
int
main(int argc, char** argv)
{
@@ -145,25 +130,23 @@ main(int argc, char** argv)
return print_usage(argv[0], true);
}
- FILE* in_fd = NULL;
SerdSyntax input_syntax = (SerdSyntax)0;
SerdSyntax output_syntax = (SerdSyntax)0;
- bool from_file = true;
+ bool from_string = false;
+ bool from_stdin = false;
bool ascii = false;
bool bulk_read = true;
bool bulk_write = false;
bool full_uris = false;
bool lax = false;
bool quiet = false;
- const char* in_name = NULL;
const char* add_prefix = NULL;
const char* chop_prefix = NULL;
const char* root_uri = NULL;
int a = 1;
for (; a < argc && argv[a][0] == '-'; ++a) {
if (argv[a][1] == '\0') {
- in_name = (const char*)"(stdin)";
- in_fd = stdin;
+ from_stdin = true;
break;
} else if (argv[a][1] == 'a') {
ascii = true;
@@ -182,8 +165,7 @@ main(int argc, char** argv)
} else if (argv[a][1] == 'v') {
return print_version();
} else if (argv[a][1] == 's') {
- in_name = (const char*)"(string)";
- from_file = false;
+ from_string = true;
++a;
break;
} else if (argv[a][1] == 'i') {
@@ -229,22 +211,9 @@ main(int argc, char** argv)
_setmode(fileno(stdout), _O_BINARY);
#endif
- char* input_path = NULL;
- const char* input = (const char*)argv[a++];
- if (from_file) {
- in_name = in_name ? in_name : input;
- if (!in_fd) {
- if (!strncmp(input, "file:", 5)) {
- input_path = serd_file_uri_parse(input, NULL);
- input = input_path;
- }
- if (!input || !(in_fd = serd_fopen(input, "rb"))) {
- return 1;
- }
- }
- }
+ const char* input = (const char*)argv[a++];
- if (!input_syntax && !(input_syntax = guess_syntax(in_name))) {
+ if (!input_syntax && !(input_syntax = guess_syntax(input))) {
input_syntax = SERD_TRIG;
}
@@ -260,7 +229,7 @@ main(int argc, char** argv)
if (a < argc) { // Base URI given on command line
base = serd_node_new_uri_from_string(
(const char*)argv[a], NULL, &base_uri);
- } else if (from_file && in_fd != stdin) { // Use input file URI
+ } else if (!from_string && !from_stdin) { // Use input file URI
base = serd_node_new_file_uri(input, NULL, &base_uri, true);
}
@@ -311,35 +280,32 @@ main(int argc, char** argv)
serd_node_free(root);
SerdStatus status = SERD_SUCCESS;
- if (!from_file) {
+ if (from_string) {
status = serd_reader_start_string(reader, input);
+ } else if (from_stdin) {
+ status = serd_reader_start_stream(reader,
+ serd_file_read_byte,
+ (SerdStreamErrorFunc)ferror,
+ stdin,
+ "(stdin)",
+ 1);
} else {
- status = serd_reader_start_stream(
- reader,
- bulk_read ? (SerdReadFunc)fread : serd_file_read_byte,
- (SerdStreamErrorFunc)ferror,
- in_fd,
- in_name,
- bulk_read ? SERD_PAGE_SIZE : 1);
+ status = serd_reader_start_file(reader, input, bulk_read);
}
- status = serd_reader_read_document(reader);
-
- serd_reader_end_stream(reader);
+ if (!status) {
+ status = serd_reader_read_document(reader);
+ }
+ serd_reader_finish(reader);
serd_reader_free(reader);
serd_writer_finish(writer);
serd_writer_free(writer);
serd_env_free(env);
serd_node_free(base);
serd_world_free(world);
- free(input_path);
-
- if (from_file) {
- fclose(in_fd);
- }
- if (fclose(out_fd)) {
+ if (fclose(stdout)) {
perror("serdi: write error");
status = SERD_ERR_UNKNOWN;
}
diff --git a/src/world.c b/src/world.c
index 0210760d..7ec483e8 100644
--- a/src/world.c
+++ b/src/world.c
@@ -14,12 +14,35 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#define _POSIX_C_SOURCE 200809L /* for posix_fadvise */
+
#include "serd_internal.h"
+#include "serd_config.h"
#include <stdarg.h>
+#include <stdio.h>
+#if defined(HAVE_POSIX_FADVISE)
+# include <fcntl.h>
+#endif
#include "world.h"
+FILE*
+serd_world_fopen(SerdWorld* world, const char* path, const char* mode)
+{
+ FILE* fd = fopen(path, mode);
+ if (!fd) {
+ serd_world_errorf(world, SERD_ERR_INTERNAL,
+ "failed to open file %s (%s)\n",
+ path, strerror(errno));
+ return NULL;
+ }
+#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_FILENO)
+ posix_fadvise(fileno(fd), 0, 0, POSIX_FADV_SEQUENTIAL);
+#endif
+ return fd;
+}
+
SerdStatus
serd_world_error(const SerdWorld* world, const SerdError* e)
{
diff --git a/src/world.h b/src/world.h
index 7e451b0e..aeea70c2 100644
--- a/src/world.h
+++ b/src/world.h
@@ -24,6 +24,8 @@ struct SerdWorldImpl {
void* error_handle;
};
+FILE* serd_world_fopen(SerdWorld* world, const char* path, const char* mode);
+
SerdStatus serd_world_error(const SerdWorld* world, const SerdError* e);
SerdStatus
diff --git a/tests/serd_test.c b/tests/serd_test.c
index 76ba1966..be4afe19 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -659,8 +659,8 @@ static void
test_reader(const char* path)
{
SerdWorld* world = serd_world_new();
- ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest));
- SerdSink sink = { rt, NULL, NULL, test_sink, NULL };
+ ReaderTest rt = { 0, NULL };
+ SerdSink sink = { &rt, NULL, NULL, test_sink, NULL };
SerdReader* reader = serd_reader_new(world, SERD_TURTLE, &sink);
assert(reader);
@@ -670,20 +670,18 @@ test_reader(const char* path)
serd_reader_add_blank_prefix(reader, NULL);
serd_node_free(g);
- assert(serd_reader_read_file(reader, "http://notafile"));
- assert(serd_reader_read_file(reader, "file:///better/not/exist"));
- assert(serd_reader_read_file(reader, "file://"));
+ assert(serd_reader_start_file(reader, "http://notafile", false));
+ assert(serd_reader_start_file(reader, "file://invalid", false));
+ assert(serd_reader_start_file(reader, "file:///nonexistant", false));
- const SerdStatus st = serd_reader_read_file(reader, path);
- assert(!st);
- assert(rt->n_statements == 13);
- assert(rt->graph && serd_node_get_string(rt->graph) &&
- !strcmp(serd_node_get_string(rt->graph), "http://example.org/"));
-
- assert(serd_reader_read_string(reader, "This isn't Turtle at all."));
+ assert(!serd_reader_start_file(reader, path, true));
+ assert(!serd_reader_read_document(reader));
+ assert(rt.n_statements == 13);
+ assert(rt.graph && serd_node_get_string(rt.graph) &&
+ !strcmp(serd_node_get_string(rt.graph), "http://example.org/"));
+ serd_reader_finish(reader);
serd_reader_free(reader);
- free(rt);
serd_world_free(world);
}