aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-06-03 22:15:53 +0200
committerDavid Robillard <d@drobilla.net>2019-04-13 19:15:32 +0200
commitd77ae556562490d6ce70fed690eca69a18ee4c46 (patch)
treeb1be64c2ce33e7c6fe217503fd173091829133b0 /src
parent1cfecadf30286bb146a9f60154dc9d4e48f8d1cb (diff)
downloadserd-d77ae556562490d6ce70fed690eca69a18ee4c46.tar.gz
serd-d77ae556562490d6ce70fed690eca69a18ee4c46.tar.bz2
serd-d77ae556562490d6ce70fed690eca69a18ee4c46.zip
Add SerdCursor to public API
Diffstat (limited to 'src')
-rw-r--r--src/byte_source.c23
-rw-r--r--src/byte_source.h17
-rw-r--r--src/cursor.c74
-rw-r--r--src/cursor.h28
-rw-r--r--src/reader.c18
-rw-r--r--src/serdi.c9
-rw-r--r--src/world.c12
7 files changed, 152 insertions, 29 deletions
diff --git a/src/byte_source.c b/src/byte_source.c
index 9477b558..96573683 100644
--- a/src/byte_source.c
+++ b/src/byte_source.c
@@ -47,20 +47,21 @@ serd_byte_source_open_source(SerdByteSource* source,
SerdStreamErrorFunc error_func,
SerdStreamCloseFunc close_func,
void* stream,
- const char* name,
+ const SerdNode* name,
size_t page_size)
{
- 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->page_size = page_size;
- source->cur = cur;
+ source->name = serd_node_copy(name);
source->from_stream = true;
+ const SerdCursor cur = { source->name, 1, 1 };
+ source->cur = cur;
+
if (page_size > 1) {
source->file_buf = (uint8_t*)serd_allocate_buffer(page_size);
source->read_buf = source->file_buf;
@@ -87,13 +88,18 @@ serd_byte_source_prepare(SerdByteSource* source)
}
SerdStatus
-serd_byte_source_open_string(SerdByteSource* source, const char* utf8)
+serd_byte_source_open_string(SerdByteSource* source,
+ const char* utf8,
+ const SerdNode* name)
{
- const Cursor cur = { "(string)", 1, 1 };
-
memset(source, '\0', sizeof(*source));
- source->cur = cur;
+
+ source->name = name ? serd_node_copy(name) : serd_new_string("string");
source->read_buf = (const uint8_t*)utf8;
+
+ const SerdCursor cur = {source->name, 1, 1};
+ source->cur = cur;
+
return SERD_SUCCESS;
}
@@ -108,6 +114,7 @@ serd_byte_source_close(SerdByteSource* source)
if (source->page_size > 1) {
free(source->file_buf);
}
+ serd_node_free(source->name);
memset(source, '\0', sizeof(*source));
return st;
}
diff --git a/src/byte_source.h b/src/byte_source.h
index 0edfd75c..f88fa6c9 100644
--- a/src/byte_source.h
+++ b/src/byte_source.h
@@ -17,6 +17,8 @@
#ifndef SERD_BYTE_SOURCE_H
#define SERD_BYTE_SOURCE_H
+#include "cursor.h"
+
#include "serd/serd.h"
#include <assert.h>
@@ -27,18 +29,13 @@
typedef int (*SerdStreamCloseFunc)(void*);
typedef struct {
- const char* filename;
- unsigned line;
- unsigned col;
-} Cursor;
-
-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
- Cursor cur; ///< Cursor for error reporting
+ SerdNode* name; ///< Name of stream (referenced by cur)
+ SerdCursor cur; ///< Cursor for error reporting
uint8_t* file_buf; ///< Buffer iff reading pages from a file
const uint8_t* read_buf; ///< Pointer to file_buf or read_byte
size_t read_head; ///< Offset into read_buf
@@ -49,7 +46,9 @@ typedef struct {
} SerdByteSource;
SerdStatus
-serd_byte_source_open_string(SerdByteSource* source, const char* utf8);
+serd_byte_source_open_string(SerdByteSource* source,
+ const char* utf8,
+ const SerdNode* name);
SerdStatus
serd_byte_source_open_source(SerdByteSource* source,
@@ -57,7 +56,7 @@ serd_byte_source_open_source(SerdByteSource* source,
SerdStreamErrorFunc error_func,
SerdStreamCloseFunc close_func,
void* stream,
- const char* name,
+ const SerdNode* name,
size_t page_size);
SerdStatus
diff --git a/src/cursor.c b/src/cursor.c
new file mode 100644
index 00000000..ac9b24a9
--- /dev/null
+++ b/src/cursor.c
@@ -0,0 +1,74 @@
+/*
+ Copyright 2018 David Robillard <http://drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#include "cursor.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+SerdCursor*
+serd_cursor_new(const SerdNode* name, unsigned line, unsigned col)
+{
+ SerdCursor* cursor = (SerdCursor*)malloc(sizeof(SerdCursor));
+
+ cursor->file = name;
+ cursor->line = line;
+ cursor->col = col;
+ return cursor;
+}
+
+SerdCursor*
+serd_cursor_copy(const SerdCursor* cursor)
+{
+ if (!cursor) {
+ return NULL;
+ }
+
+ SerdCursor* copy = (SerdCursor*)malloc(sizeof(SerdCursor));
+ memcpy(copy, cursor, sizeof(SerdCursor));
+ return copy;
+}
+
+void
+serd_cursor_free(SerdCursor* cursor)
+{
+ free(cursor);
+}
+
+bool
+serd_cursor_equals(const SerdCursor* l, const SerdCursor* r)
+{
+ return (l == r || (l && r && serd_node_equals(l->file, r->file) &&
+ l->line == r->line && l->col == r->col));
+}
+
+const SerdNode*
+serd_cursor_get_name(const SerdCursor* cursor)
+{
+ return cursor->file;
+}
+
+unsigned
+serd_cursor_get_line(const SerdCursor* cursor)
+{
+ return cursor->line;
+}
+
+unsigned
+serd_cursor_get_column(const SerdCursor* cursor)
+{
+ return cursor->col;
+}
diff --git a/src/cursor.h b/src/cursor.h
new file mode 100644
index 00000000..51fbf6e8
--- /dev/null
+++ b/src/cursor.h
@@ -0,0 +1,28 @@
+/*
+ Copyright 2018 David Robillard <http://drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#ifndef SERD_CURSOR_H
+#define SERD_CURSOR_H
+
+#include "serd/serd.h"
+
+struct SerdCursorImpl {
+ const SerdNode* file;
+ unsigned line;
+ unsigned col;
+};
+
+#endif // SERD_CURSOR_H
diff --git a/src/reader.c b/src/reader.c
index 40c48a41..79e2315e 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -39,8 +39,7 @@ r_err(SerdReader* reader, SerdStatus st, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
- const Cursor* const cur = &reader->source.cur;
- const SerdError e = { st, cur->filename, cur->line, cur->col, fmt, &args };
+ const SerdError e = { st, &reader->source.cur, fmt, &args };
serd_world_error(reader->world, &e);
va_end(args);
return st;
@@ -246,7 +245,7 @@ serd_reader_start_stream(SerdReader* reader,
SerdReadFunc read_func,
SerdStreamErrorFunc error_func,
void* stream,
- const char* name,
+ const SerdNode* name,
size_t page_size)
{
return serd_byte_source_open_source(
@@ -267,20 +266,25 @@ serd_reader_start_file(SerdReader* reader, const char* uri, bool bulk)
return SERD_ERR_UNKNOWN;
}
- return serd_byte_source_open_source(
+ SerdNode* const name = serd_new_uri(uri);
+ const SerdStatus st = serd_byte_source_open_source(
&reader->source,
bulk ? (SerdReadFunc)fread : serd_file_read_byte,
(SerdStreamErrorFunc)ferror,
(SerdStreamCloseFunc)fclose,
fd,
- uri,
+ name,
bulk ? SERD_PAGE_SIZE : 1);
+ serd_node_free(name);
+ return st;
}
SerdStatus
-serd_reader_start_string(SerdReader* reader, const char* utf8)
+serd_reader_start_string(SerdReader* reader,
+ const char* utf8,
+ const SerdNode* name)
{
- return serd_byte_source_open_string(&reader->source, utf8);
+ return serd_byte_source_open_string(&reader->source, utf8, name);
}
static SerdStatus
diff --git a/src/serdi.c b/src/serdi.c
index e2cdc0c1..a385e665 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -247,15 +247,18 @@ main(int argc, char** argv)
serd_reader_add_blank_prefix(reader, add_prefix);
serd_node_free(root);
- SerdStatus status = SERD_SUCCESS;
+ SerdStatus status = SERD_SUCCESS;
+ SerdNode* input_name = NULL;
if (from_string) {
- status = serd_reader_start_string(reader, input);
+ input_name = serd_new_string("string");
+ status = serd_reader_start_string(reader, input, input_name);
} else if (from_stdin) {
+ input_name = serd_new_string("stdin");
status = serd_reader_start_stream(reader,
serd_file_read_byte,
(SerdStreamErrorFunc)ferror,
stdin,
- "(stdin)",
+ input_name,
1);
} else {
status = serd_reader_start_file(reader, input, bulk_read);
diff --git a/src/world.c b/src/world.c
index 27feeac6..d43a2909 100644
--- a/src/world.c
+++ b/src/world.c
@@ -18,6 +18,7 @@
#include "world.h"
+#include "cursor.h"
#include "node.h"
#include "serd_config.h"
@@ -54,7 +55,14 @@ serd_world_error(const SerdWorld* world, const SerdError* e)
if (world->error_sink) {
world->error_sink(world->error_handle, e);
} else {
- fprintf(stderr, "error: %s:%u:%u: ", e->filename, e->line, e->col);
+ fprintf(stderr, "error: ");
+ if (e->cursor) {
+ fprintf(stderr,
+ "%s:%u:%u: ",
+ serd_node_get_string(e->cursor->file),
+ e->cursor->line,
+ e->cursor->col);
+ }
vfprintf(stderr, e->fmt, *e->args);
}
return e->status;
@@ -65,7 +73,7 @@ serd_world_errorf(const SerdWorld* world, SerdStatus st, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
- const SerdError e = { st, NULL, 0, 0, fmt, &args };
+ const SerdError e = { st, NULL, fmt, &args };
serd_world_error(world, &e);
va_end(args);
return st;