Beispiel #1
0
static void
reset_context(SerdWriter* writer)
{
	if (writer->context.graph.buf)
		serd_node_free(&writer->context.graph);
	if (writer->context.subject.buf)
		serd_node_free(&writer->context.subject);
	if (writer->context.predicate.buf)
		serd_node_free(&writer->context.predicate);
	writer->context = WRITE_CONTEXT_NULL;
	writer->empty   = false;
}
Beispiel #2
0
SERD_API
void
serd_env_free(SerdEnv* env)
{
	for (size_t i = 0; i < env->n_prefixes; ++i) {
		serd_node_free(&env->prefixes[i].name);
		serd_node_free(&env->prefixes[i].uri);
	}
	free(env->prefixes);
	serd_node_free(&env->base_uri_node);
	free(env);
}
Beispiel #3
0
static int
add_state_to_manifest(const LilvNode* plugin_uri,
                      const char*     manifest_path,
                      const char*     state_uri,
                      const char*     state_path)
{
	FILE* fd = fopen((char*)manifest_path, "a");
	if (!fd) {
		LILV_ERRORF("Failed to open %s (%s)\n",
		            manifest_path, strerror(errno));
		return 4;
	}

	lilv_flock(fd, true);

	SerdNode    file     = serd_node_new_file_uri(USTR(state_path), 0, 0, 0);
	SerdNode    manifest = serd_node_new_file_uri(USTR(manifest_path), 0, 0, 0);
	SerdEnv*    env      = NULL;
	SerdWriter* writer   = ttl_file_writer(fd, &manifest, &env);

	if (!state_uri) {
		state_uri = (const char*)file.buf;
	}

	// <state> a pset:Preset
	SerdNode s = serd_node_from_string(SERD_URI, USTR(state_uri));
	SerdNode p = serd_node_from_string(SERD_URI, USTR(LILV_NS_RDF "type"));
	SerdNode o = serd_node_from_string(SERD_URI, USTR(LV2_PRESETS__Preset));
	serd_writer_write_statement(writer, 0, NULL, &s, &p, &o, NULL, NULL);

	// <state> rdfs:seeAlso <file>
	p = serd_node_from_string(SERD_URI, USTR(LILV_NS_RDFS "seeAlso"));
	serd_writer_write_statement(writer, 0, NULL, &s, &p, &file, NULL, NULL);

	// <state> lv2:appliesTo <plugin>
	p = serd_node_from_string(SERD_URI, USTR(LV2_CORE__appliesTo));
	o = serd_node_from_string(
		SERD_URI, USTR(lilv_node_as_string(plugin_uri)));
	serd_writer_write_statement(writer, 0, NULL, &s, &p, &o, NULL, NULL);

	serd_node_free(&file);
	serd_node_free(&manifest);
	serd_writer_free(writer);
	serd_env_free(env);

	lilv_flock(fd, false);
	fclose(fd);

	return 0;
}
Beispiel #4
0
int
main(int argc, char** argv)
{
	if (argc != 2) {
		fprintf(stderr, "USAGE: %s BUNDLE\n", argv[0]);
		return 1;
	}

	const char* bundle_path = argv[1];
	LilvWorld*  world       = lilv_world_new();

	// Load test plugin bundle
	uint8_t*  abs_bundle = (uint8_t*)lilv_path_absolute(bundle_path);
	SerdNode  bundle     = serd_node_new_file_uri(abs_bundle, 0, 0, true);
	LilvNode* bundle_uri = lilv_new_uri(world, (const char*)bundle.buf);
	lilv_world_load_bundle(world, bundle_uri);
	free(abs_bundle);
	serd_node_free(&bundle);

	LilvNode*          plugin_uri = lilv_new_uri(world, PLUGIN_URI);
	const LilvPlugins* plugins    = lilv_world_get_all_plugins(world);
	const LilvPlugin*  plugin     = lilv_plugins_get_by_uri(plugins, plugin_uri);
	TEST_ASSERT(plugin);

	LilvInstance* instance = lilv_plugin_instantiate(plugin, 48000.0, NULL);
	TEST_ASSERT(instance);

	lilv_world_free(world);

	return 0;
}
void RDFParserSerd::doParse(const char *fileName, const char *baseUri, RDFNotation notation, bool ignoreErrors, RDFCallback *callback) {

    this->callback = callback;
    this->numByte = fileUtil::getSize(fileName);

    // Create Base URI and environment
    SerdURI  base_uri = SERD_URI_NULL;
    SerdNode base = serd_node_new_file_uri((const uint8_t *)fileName, NULL, &base_uri, false);
    env = serd_env_new(&base);

    SerdReader* reader = serd_reader_new(
                getParserType(notation), this, NULL,
                (SerdBaseSink)hdtserd_basechanged,
                (SerdPrefixSink)hdtserd_prefixchanged,
                (SerdStatementSink)hdtserd_process_triple,
                (SerdEndSink)hdtserd_end);

    serd_reader_set_error_sink(reader, hdtserd_error, NULL);


    const uint8_t* input=serd_uri_to_path((const uint8_t *)fileName);
    FILE *in_fd = fopen((const char*)input, "r");
    // TODO: fadvise sequential
    if(in_fd==NULL) {
        throw "Could not open input file for parsing";
    }
    SerdStatus status = serd_reader_read_file_handle(reader, in_fd, (const uint8_t *)fileName);

    serd_reader_free(reader);

    fclose(in_fd);

    serd_env_free(env);
    serd_node_free(&base);
}
Beispiel #6
0
SERD_API
SerdStatus
serd_writer_end_anon(SerdWriter*     writer,
                     const SerdNode* node)
{
	if (writer->syntax == SERD_NTRIPLES) {
		return SERD_SUCCESS;
	}
	if (serd_stack_is_empty(&writer->anon_stack)) {
		fprintf(stderr, "Unexpected end of anonymous node\n");
		return SERD_ERR_UNKNOWN;
	}
	assert(writer->indent > 0);
	--writer->indent;
	serd_writer_write_delim(writer, '\n');
	writer->sink("]", 1, writer->stream);
	reset_context(writer);
	writer->context = *anon_stack_top(writer);
	serd_stack_pop(&writer->anon_stack, sizeof(WriteContext));
	const bool is_subject = serd_node_equals(node, &writer->context.subject);
	if (is_subject) {
		serd_node_free(&writer->context.predicate);
		writer->context.subject   = serd_node_copy(node);
		writer->context.predicate = SERD_NODE_NULL;
	}
	return SERD_SUCCESS;
}
Beispiel #7
0
SRATOM_API
void
sratom_free(Sratom* sratom)
{
	serd_node_free(&sratom->base_uri);
	free(sratom);
}
Beispiel #8
0
static SerdStatus
reset_context(SerdWriter* writer, bool del)
{
	if (del) {
		serd_node_free(&writer->context.graph);
		serd_node_free(&writer->context.subject);
		serd_node_free(&writer->context.predicate);
		writer->context = WRITE_CONTEXT_NULL;
	} else {
		writer->context.graph.type     = SERD_NOTHING;
		writer->context.subject.type   = SERD_NOTHING;
		writer->context.predicate.type = SERD_NOTHING;
	}
	writer->empty = false;
	return SERD_SUCCESS;
}
Beispiel #9
0
SERD_API
void
serd_reader_set_default_graph(SerdReader*     reader,
                              const SerdNode* graph)
{
	serd_node_free(&reader->default_graph);
	reader->default_graph = serd_node_copy(graph);
}
Beispiel #10
0
LILV_API
int
lilv_state_save(LilvWorld*       world,
                LV2_URID_Map*    map,
                LV2_URID_Unmap*  unmap,
                const LilvState* state,
                const char*      uri,
                const char*      dir,
                const char*      filename)
{
	if (!filename || !dir || lilv_mkdir_p(dir)) {
		return 1;
	}

	char*       abs_dir = absolute_dir(dir);
	char* const path    = lilv_path_join(abs_dir, filename);
	FILE*       fd      = fopen(path, "w");
	if (!fd) {
		LILV_ERRORF("Failed to open %s (%s)\n", path, strerror(errno));
		free(abs_dir);
		free(path);
		return 4;
	}

	// FIXME: make parameter non-const?
	if (state->dir && strcmp(state->dir, abs_dir)) {
		free(state->dir);
		((LilvState*)state)->dir = lilv_strdup(abs_dir);
	}

	// Create symlinks to files if necessary
	lilv_state_make_links(state, abs_dir);

	// Write state to Turtle file
	SerdNode    file   = serd_node_new_file_uri(USTR(path), NULL, NULL, false);
	SerdEnv*    env    = NULL;
	SerdWriter* writer = ttl_file_writer(fd, &file, &env);

	SerdNode node = uri ? serd_node_from_string(SERD_URI, USTR(uri)) : file;
	int ret       = lilv_state_write(
		world, map, unmap, state, writer, (const char*)node.buf, dir);

	serd_node_free(&file);
	serd_writer_free(writer);
	serd_env_free(env);
	fclose(fd);

	char* const manifest = lilv_path_join(abs_dir, "manifest.ttl");
	add_state_to_manifest(state->plugin_uri, manifest, uri, path);

	free(manifest);
	free(abs_dir);
	free(path);
	return ret;
}
Beispiel #11
0
LILV_API LilvNode*
lilv_new_file_uri(LilvWorld* world, const char* host, const char* path)
{
	char*    abs_path = lilv_path_absolute(path);
	SerdNode s        = serd_node_new_file_uri(
		(const uint8_t*)abs_path, (const uint8_t*)host, NULL, true);

	LilvNode* ret = lilv_node_new(world, LILV_VALUE_URI, (const char*)s.buf);
	serd_node_free(&s);
	free(abs_path);
	return ret;
}
Beispiel #12
0
SERD_API
void
serd_writer_free(SerdWriter* writer)
{
	serd_writer_finish(writer);
	serd_stack_free(&writer->anon_stack);
	free(writer->bprefix);
	if (writer->style & SERD_STYLE_BULK) {
		serd_bulk_sink_free(&writer->bulk_sink);
	}
	serd_node_free(&writer->root_node);
	free(writer);
}
Beispiel #13
0
SERD_API
SerdStatus
serd_writer_set_root_uri(SerdWriter*     writer,
                         const SerdNode* uri)
{
	serd_node_free(&writer->root_node);
	if (uri && uri->buf) {
		writer->root_node = serd_node_copy(uri);
		serd_uri_parse(uri->buf, &writer->root_uri);
	} else {
		writer->root_node = SERD_NODE_NULL;
		writer->root_uri  = SERD_URI_NULL;
	}
	return SERD_SUCCESS;
}
Beispiel #14
0
SRATOM_API
void
sratom_set_sink(Sratom*           sratom,
                const char*       base_uri,
                SerdStatementSink write_statement,
                SerdEndSink       end_anon,
                void*             handle)
{
	if (base_uri) {
		serd_node_free(&sratom->base_uri);
		sratom->base_uri = serd_node_new_uri_from_string(
			USTR(base_uri), NULL, NULL);
	}
	sratom->write_statement = write_statement;
	sratom->end_anon        = end_anon;
	sratom->handle          = handle;
}
Beispiel #15
0
static void
serd_env_add(SerdEnv*        env,
             const SerdNode* name,
             const SerdNode* uri)
{
	SerdPrefix* const prefix = serd_env_find(env, name->buf, name->n_bytes);
	if (prefix) {
		SerdNode old_prefix_uri = prefix->uri;
		prefix->uri = serd_node_copy(uri);
		serd_node_free(&old_prefix_uri);
	} else {
		env->prefixes = (SerdPrefix*)realloc(
			env->prefixes, (++env->n_prefixes) * sizeof(SerdPrefix));
		env->prefixes[env->n_prefixes - 1].name = serd_node_copy(name);
		env->prefixes[env->n_prefixes - 1].uri  = serd_node_copy(uri);
	}
}
Beispiel #16
0
SordNode*
sord_new_relative_uri(SordWorld*     world,
                      const uint8_t* str,
                      const uint8_t* base_str)
{
	if (serd_uri_string_has_scheme(str)) {
		return sord_new_uri(world, str);
	}
	SerdURI  buri = SERD_URI_NULL;
	SerdNode base = serd_node_new_uri_from_string(base_str, NULL, &buri);
	SerdNode node = serd_node_new_uri_from_string(str, &buri, NULL);

	SordNode* ret = sord_new_uri_counted(
		world, node.buf, node.n_bytes, node.n_chars, false);

	serd_node_free(&base);
	return ret;
}
Beispiel #17
0
SERD_API
void
serd_reader_free(SerdReader* reader)
{
	pop_node(reader, reader->rdf_nil);
	pop_node(reader, reader->rdf_rest);
	pop_node(reader, reader->rdf_first);
	serd_node_free(&reader->default_graph);

#ifdef SERD_STACK_CHECK
	free(reader->allocs);
#endif
	free(reader->stack.buf);
	free(reader->bprefix);
	if (reader->free_handle) {
		reader->free_handle(reader->handle);
	}
	free(reader);
}
Beispiel #18
0
LILV_API
LilvState*
lilv_state_new_from_file(LilvWorld*          world,
                         const LV2_URID_Map* map,
                         const LilvNode*     subject,
                         const char*         path)
{
	if (subject && !lilv_node_is_uri(subject)
	    && !lilv_node_is_blank(subject)) {
		LILV_ERRORF("Subject `%s' is not a URI or blank node.\n",
		            lilv_node_as_string(subject));
		return NULL;
	}

	uint8_t*    abs_path = (uint8_t*)lilv_path_absolute(path);
	SerdNode    node     = serd_node_new_file_uri(abs_path, NULL, NULL, 0);
	SerdEnv*    env      = serd_env_new(&node);
	SordModel*  model    = sord_new(world->world, SORD_SPO, false);
	SerdReader* reader   = sord_new_reader(model, env, SERD_TURTLE, NULL);

	serd_reader_read_file(reader, node.buf);

	SordNode* subject_node = (subject)
		? subject->node
		: sord_node_from_serd_node(world->world, env, &node, NULL, NULL);

	char* dirname   = lilv_dirname(path);
	char* real_path = lilv_realpath(dirname);
	LilvState* state = new_state_from_model(
		world, map, model, subject_node, real_path);
	free(dirname);
	free(real_path);

	serd_node_free(&node);
	free(abs_path);
	serd_reader_free(reader);
	sord_free(model);
	serd_env_free(env);
	return state;
}
Beispiel #19
0
SERD_API
SerdStatus
serd_env_set_prefix(SerdEnv*        env,
                    const SerdNode* name,
                    const SerdNode* uri_node)
{
	if (!name->buf || uri_node->type != SERD_URI) {
		return SERD_ERR_BAD_ARG;
	} else if (serd_uri_string_has_scheme(uri_node->buf)) {
		// Set prefix to absolute URI
		serd_env_add(env, name, uri_node);
	} else {
		// Resolve relative URI and create a new node and URI for it
		SerdURI  abs_uri;
		SerdNode abs_uri_node = serd_node_new_uri_from_node(
			uri_node, &env->base_uri, &abs_uri);

		// Set prefix to resolved (absolute) URI
		serd_env_add(env, name, &abs_uri_node);
		serd_node_free(&abs_uri_node);
	}
	return SERD_SUCCESS;
}
Beispiel #20
0
SERD_API
SerdStatus
serd_env_set_base_uri(SerdEnv*        env,
                      const SerdNode* uri_node)
{
	if (!env || !uri_node) {
		return SERD_ERR_BAD_ARG;
	}

	// Resolve base URI and create a new node and URI for it
	SerdURI  base_uri;
	SerdNode base_uri_node = serd_node_new_uri_from_node(
		uri_node, &env->base_uri, &base_uri);

	if (base_uri_node.buf) {
		// Replace the current base URI
		serd_node_free(&env->base_uri_node);
		env->base_uri_node = base_uri_node;
		env->base_uri      = base_uri;
		return SERD_SUCCESS;
	}
	return SERD_ERR_BAD_ARG;
}
Beispiel #21
0
SERD_API
SerdStatus
serd_writer_write_statement(SerdWriter*        writer,
                            SerdStatementFlags flags,
                            const SerdNode*    graph,
                            const SerdNode*    subject,
                            const SerdNode*    predicate,
                            const SerdNode*    object,
                            const SerdNode*    object_datatype,
                            const SerdNode*    object_lang)
{
	assert(subject && predicate && object);
	switch (writer->syntax) {
	case SERD_NTRIPLES:
		write_node(writer, subject, NULL, NULL, FIELD_SUBJECT, flags);
		writer->sink(" ", 1, writer->stream);
		write_node(writer, predicate, NULL, NULL, FIELD_PREDICATE, flags);
		writer->sink(" ", 1, writer->stream);
		if (!write_node(writer, object, object_datatype, object_lang,
		                FIELD_OBJECT, flags)) {
			return SERD_ERR_UNKNOWN;
		}
		writer->sink(" .\n", 3, writer->stream);
		return SERD_SUCCESS;
	case SERD_TURTLE:
		break;
	}
	if (serd_node_equals(subject, &writer->context.subject)) {
		if (serd_node_equals(predicate, &writer->context.predicate)) {
			// Abbreviate S P
			if ((flags & SERD_ANON_O_BEGIN)) {
				writer->sink(" , ", 3, writer->stream);  // ] , [
			} else {
				++writer->indent;
				serd_writer_write_delim(writer, ',');
			}
			write_node(writer, object, object_datatype, object_lang,
			           FIELD_OBJECT, flags);
			if (!(flags & SERD_ANON_O_BEGIN)) {
				--writer->indent;
			}
		} else {
			// Abbreviate S
			if (writer->context.predicate.buf) {
				serd_writer_write_delim(writer, ';');
			} else {
				serd_writer_write_delim(writer, '\n');
			}
			write_node(writer, predicate, NULL, NULL, FIELD_PREDICATE, flags);
			if (writer->context.predicate.buf)
				serd_node_free(&writer->context.predicate);
			writer->context.predicate = serd_node_copy(predicate);
			writer->sink(" ", 1, writer->stream);
			write_node(writer, object, object_datatype, object_lang,
			           FIELD_OBJECT, flags);
		}
	} else {
		if (writer->context.subject.buf) {
			if (writer->indent > 0) {
				--writer->indent;
			}
			if (serd_stack_is_empty(&writer->anon_stack)) {
				serd_writer_write_delim(writer, '.');
				serd_writer_write_delim(writer, '\n');
			}
		} else if (!writer->empty) {
			serd_writer_write_delim(writer, '\n');
		}

		if (!(flags & SERD_ANON_CONT)) {
			write_node(writer, subject, NULL, NULL, FIELD_SUBJECT, flags);
			++writer->indent;
			if (!(flags & SERD_ANON_S_BEGIN)) {
				serd_writer_write_delim(writer, '\n');
			}
		} else {
			++writer->indent;
		}

		reset_context(writer);
		writer->context.subject   = serd_node_copy(subject);
		writer->context.predicate = SERD_NODE_NULL;

		write_node(writer, predicate, NULL, NULL, FIELD_PREDICATE, flags);
		writer->context.predicate = serd_node_copy(predicate);
		writer->sink(" ", 1, writer->stream);

		write_node(writer, object, object_datatype, object_lang,
		           FIELD_OBJECT, flags);
	}

	if (writer->syntax != SERD_NTRIPLES
	    && ((flags & SERD_ANON_S_BEGIN) || (flags & SERD_ANON_O_BEGIN))) {
		WriteContext* ctx = (WriteContext*)serd_stack_push(
			&writer->anon_stack, sizeof(WriteContext));
		*ctx = writer->context;
		writer->context = WRITE_CONTEXT_NULL;  // Prevent deletion...
	}

	const WriteContext new_context = { serd_node_copy(graph),
	                                   serd_node_copy(subject),
	                                   serd_node_copy(predicate) };
	reset_context(writer);  // ... here
	writer->context = new_context;
	return SERD_SUCCESS;
}
Beispiel #22
0
SRATOM_API
int
sratom_write(Sratom*         sratom,
             LV2_URID_Unmap* unmap,
             uint32_t        flags,
             const SerdNode* subject,
             const SerdNode* predicate,
             uint32_t        type_urid,
             uint32_t        size,
             const void*     body)
{
	const char* const type        = unmap->unmap(unmap->handle, type_urid);
	uint8_t           idbuf[12]   = "b0000000000";
	SerdNode          id          = serd_node_from_string(SERD_BLANK, idbuf);
	uint8_t           nodebuf[12] = "b0000000000";
	SerdNode          node        = serd_node_from_string(SERD_BLANK, nodebuf);
	SerdNode          object      = SERD_NODE_NULL;
	SerdNode          datatype    = SERD_NODE_NULL;
	SerdNode          language    = SERD_NODE_NULL;
	bool              new_node    = false;
	if (type_urid == 0 && size == 0) {
		object = serd_node_from_string(SERD_URI, USTR(NS_RDF "nil"));
	} else if (type_urid == sratom->forge.String) {
		object = serd_node_from_string(SERD_LITERAL, (const uint8_t*)body);
	} else if (type_urid == sratom->forge.Chunk) {
		datatype = serd_node_from_string(SERD_URI, NS_XSD "base64Binary");
		object   = serd_node_new_blob(body, size, true);
		new_node = true;
	} else if (type_urid == sratom->forge.Literal) {
		const LV2_Atom_Literal_Body* lit = (const LV2_Atom_Literal_Body*)body;
		const uint8_t*         str = USTR(lit + 1);
		object = serd_node_from_string(SERD_LITERAL, str);
		if (lit->datatype) {
			datatype = serd_node_from_string(
				SERD_URI, USTR(unmap->unmap(unmap->handle, lit->datatype)));
		} else if (lit->lang) {
			const char*  lang       = unmap->unmap(unmap->handle, lit->lang);
			const char*  prefix     = "http://lexvo.org/id/iso639-3/";
			const size_t prefix_len = strlen(prefix);
			if (lang && !strncmp(lang, prefix, prefix_len)) {
				language = serd_node_from_string(
					SERD_LITERAL, USTR(lang + prefix_len));
			} else {
				fprintf(stderr, "Unknown language URID %d\n", lit->lang);
			}
		}
	} else if (type_urid == sratom->forge.URID) {
		const uint32_t urid = *(const uint32_t*)body;
		const uint8_t* str  = USTR(unmap->unmap(unmap->handle, urid));
		object = serd_node_from_string(SERD_URI, str);
	} else if (type_urid == sratom->forge.Path) {
		const uint8_t* str = USTR(body);
		if (path_is_absolute((const char*)str)) {
			new_node = true;
			object   = serd_node_new_file_uri(str, NULL, NULL, false);
		} else {
			SerdURI base_uri = SERD_URI_NULL;
			if (!sratom->base_uri.buf ||
			    strncmp((const char*)sratom->base_uri.buf, "file://", 7)) {
				fprintf(stderr, "warning: Relative path but base is not a file URI.\n");
				fprintf(stderr, "warning: Writing ambiguous atom:Path literal.\n");
				object   = serd_node_from_string(SERD_LITERAL, str);
				datatype = serd_node_from_string(SERD_URI, USTR(LV2_ATOM__Path));
			} else {
				if (sratom->base_uri.buf) {
					serd_uri_parse(sratom->base_uri.buf, &base_uri);
				}
				new_node = true;
				SerdNode rel = serd_node_new_file_uri(str, NULL, NULL, false);
				object = serd_node_new_uri_from_node(&rel, &base_uri, NULL);
				serd_node_free(&rel);
			}
		}
	} else if (type_urid == sratom->forge.URI) {
		const uint8_t* str = USTR(body);
		object = serd_node_from_string(SERD_URI, str);
	} else if (type_urid == sratom->forge.Int) {
		new_node = true;
		object   = serd_node_new_integer(*(const int32_t*)body);
		datatype = serd_node_from_string(SERD_URI, (sratom->pretty_numbers)
		                                 ? NS_XSD "integer" : NS_XSD "int");
	} else if (type_urid == sratom->forge.Long) {
		new_node = true;
		object   = serd_node_new_integer(*(const int64_t*)body);
		datatype = serd_node_from_string(SERD_URI, (sratom->pretty_numbers)
		                                 ? NS_XSD "integer" : NS_XSD "long");
	} else if (type_urid == sratom->forge.Float) {
		new_node = true;
		object   = serd_node_new_decimal(*(const float*)body, 8);
		datatype = serd_node_from_string(SERD_URI, (sratom->pretty_numbers)
		                                 ? NS_XSD "decimal" : NS_XSD "float");
	} else if (type_urid == sratom->forge.Double) {
		new_node = true;
		object   = serd_node_new_decimal(*(const double*)body, 16);
		datatype = serd_node_from_string(SERD_URI, (sratom->pretty_numbers)
		                                 ? NS_XSD "decimal" : NS_XSD "double");
	} else if (type_urid == sratom->forge.Bool) {
		const int32_t val = *(const int32_t*)body;
		datatype = serd_node_from_string(SERD_URI, NS_XSD "boolean");
		object   = serd_node_from_string(SERD_LITERAL,
		                                 USTR(val ? "true" : "false"));
	} else if (type_urid == sratom->midi_MidiEvent) {
		new_node = true;
		datatype = serd_node_from_string(SERD_URI, USTR(LV2_MIDI__MidiEvent));
		uint8_t* str = (uint8_t*)calloc(size * 2 + 1, 1);
		for (uint32_t i = 0; i < size; ++i) {
			snprintf((char*)str + (2 * i), size * 2 + 1, "%02X",
			         (unsigned)(uint8_t)*((const uint8_t*)body + i));
		}
		object = serd_node_from_string(SERD_LITERAL, USTR(str));
	} else if (type_urid == sratom->atom_Event) {
		const LV2_Atom_Event* ev = (const LV2_Atom_Event*)body;
		gensym(&id, 'e', sratom->next_id++);
		start_object(sratom, &flags, subject, predicate, &id, NULL);
		// TODO: beat time
		SerdNode time = serd_node_new_integer(ev->time.frames);
		SerdNode p    = serd_node_from_string(SERD_URI,
		                                      USTR(LV2_ATOM__frameTime));
		datatype = serd_node_from_string(SERD_URI, NS_XSD "decimal");
		sratom->write_statement(sratom->handle, SERD_ANON_CONT, NULL,
		                        &id, &p, &time, &datatype, &language);
		serd_node_free(&time);

		p = serd_node_from_string(SERD_URI, NS_RDF "value");
		sratom_write(sratom, unmap, SERD_ANON_CONT, &id, &p,
		             ev->body.type, ev->body.size, LV2_ATOM_BODY(&ev->body));
		if (sratom->end_anon) {
			sratom->end_anon(sratom->handle, &id);
		}
	} else if (type_urid == sratom->forge.Tuple) {
		gensym(&id, 't', sratom->next_id++);
		start_object(sratom, &flags, subject, predicate, &id, type);
		SerdNode p = serd_node_from_string(SERD_URI, NS_RDF "value");
		flags |= SERD_LIST_O_BEGIN;
		LV2_ATOM_TUPLE_BODY_FOREACH(body, size, i) {
			list_append(sratom, unmap, &flags, &id, &p, &node,
			            i->size, i->type, LV2_ATOM_BODY(i));
		}
Beispiel #23
0
int
main(void)
{
#define MAX       1000000
#define NUM_TESTS 1000
	for (int i = 0; i < NUM_TESTS; ++i) {
		double dbl = rand() % MAX;
		dbl += (rand() % MAX) / (double)MAX;

		if (!test_strtod(dbl, 1 / (double)MAX)) {
			return 1;
		}
	}

	const double expt_test_nums[] = {
		2.0E18, -5e19, +8e20, 2e+34, -5e-5, 8e0, 9e-0, 2e+0
	};

	const char* expt_test_strs[] = {
		"02e18", "-5e019", "+8e20", "2E+34", "-5E-5", "8E0", "9e-0", " 2e+0"
	};

	for (unsigned i = 0; i < sizeof(expt_test_nums) / sizeof(double); ++i) {
		const double num   = serd_strtod(expt_test_strs[i], NULL);
		const double delta = fabs(num - expt_test_nums[i]);
		if (delta > DBL_EPSILON) {
			return failure("Parsed `%s' %lf != %lf (delta %lf)\n",
			               expt_test_strs[i], num, expt_test_nums[i], delta);
		}
	}

	// Test serd_node_new_decimal

	const double dbl_test_nums[] = {
		0.0, 9.0, 10.0, .01, 2.05, -16.00001, 5.000000005, 0.0000000001, NAN, INFINITY
	};

	const char* dbl_test_strs[] = {
		"0.0", "9.0", "10.0", "0.01", "2.05", "-16.00001", "5.00000001", "0.0", NULL, NULL
	};

	for (unsigned i = 0; i < sizeof(dbl_test_nums) / sizeof(double); ++i) {
		SerdNode   node = serd_node_new_decimal(dbl_test_nums[i], 8);
		const bool pass = (node.buf && dbl_test_strs[i])
			? !strcmp((const char*)node.buf, (const char*)dbl_test_strs[i])
			: ((const char*)node.buf == dbl_test_strs[i]);
		if (!pass) {
			return failure("Serialised `%s' != %s\n",
			               node.buf, dbl_test_strs[i]);
		}
		const size_t len = node.buf ? strlen((const char*)node.buf) : 0;
		if (node.n_bytes != len || node.n_chars != len) {
			return failure("Length %zu,%zu != %zu\n",
			               node.n_bytes, node.n_chars, len);
		}
		serd_node_free(&node);
	}

	// Test serd_node_new_integer

	const long int_test_nums[] = {
		0, -0, -23, 23, -12340, 1000, -1000
	};

	const char* int_test_strs[] = {
		"0", "0", "-23", "23", "-12340", "1000", "-1000"
	};

	for (unsigned i = 0; i < sizeof(int_test_nums) / sizeof(double); ++i) {
		SerdNode node = serd_node_new_integer(int_test_nums[i]);
		if (strcmp((const char*)node.buf, (const char*)int_test_strs[i])) {
			return failure("Serialised `%s' != %s\n",
			               node.buf, int_test_strs[i]);
		}
		const size_t len = strlen((const char*)node.buf);
		if (node.n_bytes != len || node.n_chars != len) {
			return failure("Length %zu,%zu != %zu\n",
			               node.n_bytes, node.n_chars, len);
		}
		serd_node_free(&node);
	}

	// Test serd_node_new_blob
	for (size_t size = 0; size < 256; ++size) {
		uint8_t* data = (uint8_t*)malloc(size);
		for (size_t i = 0; i < size; ++i) {
			data[i] = (uint8_t)(rand() % 256);
		}

		SerdNode blob = serd_node_new_blob(data, size, size % 5);

		if (blob.n_bytes != blob.n_chars) {
			return failure("Blob %zu bytes != %zu chars\n",
			               blob.n_bytes, blob.n_chars);
		}

		size_t   out_size;
		uint8_t* out = (uint8_t*)serd_base64_decode(
			blob.buf, blob.n_bytes, &out_size);
		if (out_size != size) {
			return failure("Blob size %zu != %zu\n", out_size, size);
		}

		for (size_t i = 0; i < size; ++i) {
			if (out[i] != data[i]) {
				return failure("Corrupt blob at byte %zu\n", i);
			}
		}

		serd_node_free(&blob);
		free(out);
		free(data);
	}

	// Test serd_strlen

	const uint8_t str[] = { '"', '5', 0xE2, 0x82, 0xAC, '"', '\n', 0 };

	size_t        n_bytes;
	SerdNodeFlags flags;
	size_t        len = serd_strlen(str, &n_bytes, &flags);
	if (len != 5 || n_bytes != 7
	    || flags != (SERD_HAS_QUOTE|SERD_HAS_NEWLINE)) {
		return failure("Bad serd_strlen(%s) len=%zu n_bytes=%zu flags=%u\n",
		        str, len, n_bytes, flags);
	}
	len = serd_strlen(str, NULL, &flags);
	if (len != 5) {
		return failure("Bad serd_strlen(%s) len=%zu flags=%u\n",
		        str, len, flags);
	}

	// Test serd_strerror

	const uint8_t* msg = NULL;
	if (strcmp((const char*)(msg = serd_strerror(SERD_SUCCESS)), "Success")) {
		return failure("Bad message `%s' for SERD_SUCCESS\n", msg);
	}
	for (int i = SERD_FAILURE; i <= SERD_ERR_INTERNAL; ++i) {
		msg = serd_strerror((SerdStatus)i);
		if (!strcmp((const char*)msg, "Success")) {
			return failure("Bad message `%s' for (SerdStatus)%d\n", msg, i);
		}
	}
	msg = serd_strerror((SerdStatus)-1);

	// Test serd_uri_to_path

	const uint8_t* uri = (const uint8_t*)"file:///home/user/foo.ttl";
	if (strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")) {
		return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri);
	}
	uri = (const uint8_t*)"file://localhost/home/user/foo.ttl";
	if (strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")) {
		return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri);
	}
	uri = (const uint8_t*)"file:illegal/file/uri";
	if (serd_uri_to_path(uri)) {
		return failure("Converted invalid URI `%s' to path `%s'\n",
		        uri, serd_uri_to_path(uri));
	}
	uri = (const uint8_t*)"file:///c:/awful/system";
	if (strcmp((const char*)serd_uri_to_path(uri), "c:/awful/system")) {
		return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri);
	}
	uri = (const uint8_t*)"file:///c:awful/system";
	if (strcmp((const char*)serd_uri_to_path(uri), "/c:awful/system")) {
		return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri);
	}
	uri = (const uint8_t*)"file:///0/1";
	if (strcmp((const char*)serd_uri_to_path(uri), "/0/1")) {
		return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri);
	}
	uri = (const uint8_t*)"C:\\Windows\\Sucks";
	if (strcmp((const char*)serd_uri_to_path(uri), "C:\\Windows\\Sucks")) {
		return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri);
	}
	uri = (const uint8_t*)"C|/Windows/Sucks";
	if (strcmp((const char*)serd_uri_to_path(uri), "C|/Windows/Sucks")) {
		return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri);
	}

	// Test serd_node_new_file_uri and serd_file_uri_parse
	SerdURI        furi;
	const uint8_t* path_str  = USTR("C:/My 100%");
	SerdNode       file_node = serd_node_new_file_uri(path_str, 0, &furi, true);
	uint8_t*       hostname  = NULL;
	uint8_t*       out_path  = serd_file_uri_parse(file_node.buf, &hostname);
	if (strcmp((const char*)file_node.buf, "file:///C:/My%20100%%")) {
		return failure("Bad URI %s\n", file_node.buf);
	} else if (hostname) {
		return failure("hostname `%s' shouldn't exist\n", hostname);
	} else if (strcmp((const char*)path_str, (const char*)out_path)) {
		return failure("path=>URI=>path failure %s => %s => %s\n",
		               path_str, file_node.buf, out_path);
	}
	free(out_path);
	serd_node_free(&file_node);

	path_str  = USTR("C:\\Pointless Space");
	file_node = serd_node_new_file_uri(path_str, USTR("pwned"), 0, true);
	hostname  = NULL;
	out_path  = serd_file_uri_parse(file_node.buf, &hostname);
	if (strcmp((const char*)file_node.buf, "file://pwned/C:/Pointless%20Space")) {
		return failure("Bad URI %s\n", file_node.buf);
	} else if (!hostname || strcmp((const char*)hostname, "pwned")) {
		return failure("Bad hostname `%s'\n", hostname);
	} else if (strcmp((const char*)out_path, "C:/Pointless Space")) {
		return failure("path=>URI=>path failure %s => %s => %s\n",
		               path_str, file_node.buf, out_path);
	}
	free(hostname);
	free(out_path);
	serd_node_free(&file_node);

	path_str  = USTR("/foo/bar");
	file_node = serd_node_new_file_uri(path_str, 0, 0, true);
	hostname  = NULL;
	out_path  = serd_file_uri_parse(file_node.buf, &hostname);
	if (strcmp((const char*)file_node.buf, "file:///foo/bar")) {
		return failure("Bad URI %s\n", file_node.buf);
	} else if (hostname) {
		return failure("hostname `%s' shouldn't exist\n", hostname);
	} else if (strcmp((const char*)path_str, (const char*)out_path)) {
		return failure("path=>URI=>path failure %s => %s => %s\n",
		               path_str, file_node.buf, out_path);
	}
	free(out_path);
	serd_node_free(&file_node);

	path_str  = USTR("/foo/bar");
	file_node = serd_node_new_file_uri(path_str, USTR("localhost"), 0, true);
	out_path  = serd_file_uri_parse(file_node.buf, &hostname);
	if (strcmp((const char*)file_node.buf, "file://localhost/foo/bar")) {
		return failure("Bad URI %s\n", file_node.buf);
	} else if (strcmp((const char*)hostname, "localhost")) {
		return failure("incorrect hostname `%s'\n", hostname);
	} else if (strcmp((const char*)path_str, (const char*)out_path)) {
		return failure("path=>URI=>path failure %s => %s => %s\n",
		               path_str, file_node.buf, out_path);
	}
	free(hostname);
	free(out_path);
	serd_node_free(&file_node);

	path_str  = USTR("a/relative path");
	file_node = serd_node_new_file_uri(path_str, 0, 0, false);
	out_path  = serd_file_uri_parse(file_node.buf, &hostname);
	if (strcmp((const char*)file_node.buf, "a/relative path")) {
		return failure("Bad URI %s\n", file_node.buf);
	} else if (hostname) {
		return failure("hostname `%s' shouldn't exist\n", hostname);
	} else if (strcmp((const char*)path_str, (const char*)out_path)) {
		return failure("path=>URI=>path failure %s => %s => %s\n",
		               path_str, file_node.buf, out_path);
	}
	free(hostname);
	free(out_path);
	serd_node_free(&file_node);

	if (serd_file_uri_parse(USTR("file://invalid"), NULL)) {
		return failure("successfully parsed bogus URI <file://invalid>\n");
	}

	out_path = serd_file_uri_parse(USTR("file://host/foo/%XYbar"), NULL);
	if (strcmp((const char*)out_path, "/foo/bar")) {
		return failure("bad tolerance of junk escape: `%s'\n", out_path);
	}
	free(out_path);
	out_path = serd_file_uri_parse(USTR("file://host/foo/%0Abar"), NULL);
	if (strcmp((const char*)out_path, "/foo/bar")) {
		return failure("bad tolerance of junk escape: `%s'\n", out_path);
	}
	free(out_path);

	// Test serd_node_equals

	const uint8_t replacement_char_str[] = { 0xEF, 0xBF, 0xBD, 0 };
	SerdNode lhs = serd_node_from_string(SERD_LITERAL, replacement_char_str);
	SerdNode rhs = serd_node_from_string(SERD_LITERAL, USTR("123"));
	if (serd_node_equals(&lhs, &rhs)) {
		return failure("%s == %s\n", lhs.buf, rhs.buf);
	}

	SerdNode qnode = serd_node_from_string(SERD_CURIE, USTR("foo:bar"));
	if (serd_node_equals(&lhs, &qnode)) {
		return failure("%s == %s\n", lhs.buf, qnode.buf);
	}

	if (!serd_node_equals(&lhs, &lhs)) {
		return failure("%s != %s\n", lhs.buf, lhs.buf);
	}

	SerdNode null_copy = serd_node_copy(&SERD_NODE_NULL);
	if (!serd_node_equals(&SERD_NODE_NULL, &null_copy)) {
		return failure("copy of null node != null node\n");
	}

	// Test serd_node_from_string

	SerdNode node = serd_node_from_string(SERD_LITERAL, (const uint8_t*)"hello\"");
	if (node.n_bytes != 6 || node.n_chars != 6 || node.flags != SERD_HAS_QUOTE
	    || strcmp((const char*)node.buf, "hello\"")) {
		return failure("Bad node %s %zu %zu %d %d\n",
		        node.buf, node.n_bytes, node.n_chars, node.flags, node.type);
	}

	node = serd_node_from_string(SERD_URI, NULL);
	if (!serd_node_equals(&node, &SERD_NODE_NULL)) {
		return failure("Creating node from NULL string failed\n");
	}

	// Test serd_node_new_uri_from_string

	SerdURI base_uri;
	SerdNode base = serd_node_new_uri_from_string(USTR("http://example.org/"),
	                                              NULL, &base_uri);
	SerdNode nil = serd_node_new_uri_from_string(NULL, &base_uri, NULL);
	SerdNode nil2 = serd_node_new_uri_from_string(USTR(""), &base_uri, NULL);
	if (nil.type != SERD_URI || strcmp((const char*)nil.buf, (const char*)base.buf) ||
	    nil2.type != SERD_URI || strcmp((const char*)nil2.buf, (const char*)base.buf)) {
		return failure("URI %s != base %s\n", nil.buf, base.buf);
	}
	serd_node_free(&base);
	serd_node_free(&nil);
	serd_node_free(&nil2);

	// Test SerdEnv

	SerdNode u   = serd_node_from_string(SERD_URI, USTR("http://example.org/foo"));
	SerdNode b   = serd_node_from_string(SERD_CURIE, USTR("invalid"));
	SerdNode c   = serd_node_from_string(SERD_CURIE, USTR("eg.2:b"));
	SerdEnv* env = serd_env_new(NULL);
	serd_env_set_prefix_from_strings(env, USTR("eg.2"), USTR("http://example.org/"));

	if (!serd_env_set_base_uri(env, &node)) {
		return failure("Set base URI to %s\n", node.buf);
	}

	if (!serd_node_equals(serd_env_get_base_uri(env, NULL), &node)) {
		return failure("Base URI mismatch\n");
	}

	SerdChunk prefix, suffix;
	if (!serd_env_expand(env, &b, &prefix, &suffix)) {
		return failure("Expanded invalid curie %s\n", b.buf);
	}

	SerdNode xnode = serd_env_expand_node(env, &node);
	if (!serd_node_equals(&xnode, &SERD_NODE_NULL)) {
		return failure("Expanded %s to %s\n", c.buf, xnode.buf);
	}

	SerdNode xu = serd_env_expand_node(env, &u);
	if (strcmp((const char*)xu.buf, "http://example.org/foo")) {
		return failure("Expanded %s to %s\n", c.buf, xu.buf);
	}
	serd_node_free(&xu);

	SerdNode badpre = serd_node_from_string(SERD_CURIE, USTR("hm:what"));
	SerdNode xbadpre = serd_env_expand_node(env, &badpre);
	if (!serd_node_equals(&xbadpre, &SERD_NODE_NULL)) {
		return failure("Expanded invalid curie %s\n", badpre.buf);
	}

	SerdNode xc = serd_env_expand_node(env, &c);
	if (strcmp((const char*)xc.buf, "http://example.org/b")) {
		return failure("Expanded %s to %s\n", c.buf, xc.buf);
	}
	serd_node_free(&xc);

	if (!serd_env_set_prefix(env, &SERD_NODE_NULL, &SERD_NODE_NULL)) {
		return failure("Set NULL prefix\n");
	}

	const SerdNode lit = serd_node_from_string(SERD_LITERAL, USTR("hello"));
	if (!serd_env_set_prefix(env, &b, &lit)) {
		return failure("Set prefix to literal\n");
	}

	int n_prefixes = 0;
	serd_env_set_prefix_from_strings(env, USTR("eg.2"), USTR("http://example.org/"));
	serd_env_foreach(env, count_prefixes, &n_prefixes);
	if (n_prefixes != 1) {
		return failure("Bad prefix count %d\n", n_prefixes);
	}

	SerdNode shorter_uri = serd_node_from_string(SERD_URI, USTR("urn:foo"));
	SerdNode prefix_name;
	if (serd_env_qualify(env, &shorter_uri, &prefix_name, &suffix)) {
		return failure("Qualified %s\n", shorter_uri.buf);
	}

	// Test SerdReader and SerdWriter

	const char* path = "serd_test.ttl";
	FILE* fd = fopen(path, "w");
	if (!fd) {
		return failure("Failed to open file %s\n", path);
	}

	SerdWriter* writer = serd_writer_new(
		SERD_TURTLE, (SerdStyle)0, env, NULL, serd_file_sink, fd);
	if (!writer) {
		return failure("Failed to create writer\n");
	}

	serd_writer_chop_blank_prefix(writer, USTR("tmp"));
	serd_writer_chop_blank_prefix(writer, NULL);

	if (!serd_writer_set_base_uri(writer, &lit)) {
		return failure("Set base URI to %s\n", lit.buf);
	}

	if (!serd_writer_set_prefix(writer, &lit, &lit)) {
		return failure("Set prefix %s to %s\n", lit.buf, lit.buf);
	}

	if (!serd_writer_end_anon(writer, NULL)) {
		return failure("Ended non-existent anonymous node\n");
	}

	if (serd_writer_get_env(writer) != env) {
		return failure("Writer has incorrect env\n");
	}

	uint8_t buf[] = { 0x80, 0, 0, 0, 0 };
	SerdNode s = serd_node_from_string(SERD_URI, USTR(""));
	SerdNode p = serd_node_from_string(SERD_URI, USTR("http://example.org/pred"));
	SerdNode o = serd_node_from_string(SERD_LITERAL, buf);

	// Write 3 invalid statements (should write nothing)
	const SerdNode* junk[][5] = { { &s, &p, NULL, NULL, NULL },
	                              { &s, NULL, &o, NULL, NULL },
	                              { NULL, &p, &o, NULL, NULL },
	                              { &s, &p, &SERD_NODE_NULL, NULL, NULL },
	                              { &s, &SERD_NODE_NULL, &o, NULL, NULL },
	                              { &SERD_NODE_NULL, &p, &o, NULL, NULL },
	                              { &s, &o, &o, NULL, NULL },
	                              { &o, &p, &o, NULL, NULL },
	                              { NULL, NULL, NULL, NULL, NULL } };
	for (unsigned i = 0; i < sizeof(junk) / (sizeof(SerdNode*) * 5); ++i) {
		if (!serd_writer_write_statement(
			    writer, 0, NULL,
			    junk[i][0], junk[i][1], junk[i][2], junk[i][3], junk[i][4])) {
			return failure("Successfully wrote junk statement %d\n", i);
		}
	}

	const SerdNode t = serd_node_from_string(SERD_URI, USTR("urn:Type"));
	const SerdNode l = serd_node_from_string(SERD_LITERAL, USTR("en"));
	const SerdNode* good[][5] = { { &s, &p, &o, NULL, NULL },
	                              { &s, &p, &o, &SERD_NODE_NULL, &SERD_NODE_NULL },
	                              { &s, &p, &o, &t, NULL },
	                              { &s, &p, &o, NULL, &l },
	                              { &s, &p, &o, &t, &l },
	                              { &s, &p, &o, &t, &SERD_NODE_NULL },
	                              { &s, &p, &o, &SERD_NODE_NULL, &l },
	                              { &s, &p, &o, NULL, &SERD_NODE_NULL },
	                              { &s, &p, &o, &SERD_NODE_NULL, NULL },
	                              { &s, &p, &o, &SERD_NODE_NULL, NULL } };
	for (unsigned i = 0; i < sizeof(good) / (sizeof(SerdNode*) * 5); ++i) {
		if (serd_writer_write_statement(
			    writer, 0, NULL,
			    good[i][0], good[i][1], good[i][2], good[i][3], good[i][4])) {
			return failure("Failed to write good statement %d\n", i);
		}
	}

	// Write 1 statement with bad UTF-8 (should be replaced)
	if (serd_writer_write_statement(writer, 0, NULL,
	                                &s, &p, &o, NULL, NULL)) {
		return failure("Failed to write junk UTF-8\n");
	}

	// Write 1 valid statement
	o = serd_node_from_string(SERD_LITERAL, USTR("hello"));
	if (serd_writer_write_statement(writer, 0, NULL,
	                                &s, &p, &o, NULL, NULL)) {
		return failure("Failed to write valid statement\n");
	}

	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);

	ReaderTest* rt   = (ReaderTest*)malloc(sizeof(ReaderTest));
	rt->n_statements = 0;
	rt->graph        = NULL;

	SerdReader* reader = serd_reader_new(
		SERD_TURTLE, rt, free,
		NULL, NULL, test_sink, NULL);
	if (!reader) {
		return failure("Failed to create reader\n");
	}
	if (serd_reader_get_handle(reader) != rt) {
		return failure("Corrupt reader handle\n");
	}

	SerdNode g = serd_node_from_string(SERD_URI, USTR("http://example.org/"));
	serd_reader_set_default_graph(reader, &g);
	serd_reader_add_blank_prefix(reader, USTR("tmp"));
	serd_reader_add_blank_prefix(reader, NULL);

	if (!serd_reader_read_file(reader, USTR("http://notafile"))) {
		return failure("Apparently read an http URI\n");
	}
	if (!serd_reader_read_file(reader, USTR("file:///better/not/exist"))) {
		return failure("Apprently read a non-existent file\n");
	}
	SerdStatus st = serd_reader_read_file(reader, USTR(path));
	if (st) {
		return failure("Error reading file (%s)\n", serd_strerror(st));
	}

	if (rt->n_statements != 12) {
		return failure("Bad statement count %d\n", rt->n_statements);
	} else if (!rt->graph || !rt->graph->buf ||
	           strcmp((const char*)rt->graph->buf, "http://example.org/")) {
		return failure("Bad graph %p\n", rt->graph);
	}

	if (!serd_reader_read_string(reader, USTR("This isn't Turtle at all."))) {
		return failure("Parsed invalid string successfully.\n");
	}

	serd_reader_free(reader);
	fclose(fd);

	serd_env_free(env);

	printf("Success\n");
	return 0;
}
Beispiel #24
0
int
main(int argc, char** argv)
{
	if (argc < 2) {
		return print_usage(argv[0], true);
	}

	int a = 1;
	for (; a < argc && argv[a][0] == '-'; ++a) {
		if (argv[a][1] == 'l') {
			one_line_errors = true;
		} else if (argv[a][1] == 'v') {
			return print_version();
		} else {
			fprintf(stderr, "%s: Unknown option `%s'\n", argv[0], argv[a]);
			return print_usage(argv[0], true);
		}
	}

	SordWorld*  world  = sord_world_new();
	SordModel*  model  = sord_new(world, SORD_SPO|SORD_OPS, false);
	SerdEnv*    env    = serd_env_new(&SERD_NODE_NULL);
	SerdReader* reader = sord_new_reader(model, env, SERD_TURTLE, NULL);

	for (; a < argc; ++a) {
		const uint8_t* input   = (const uint8_t*)argv[a];
		uint8_t*       in_path = absolute_path(serd_uri_to_path(input));

		if (!in_path) {
			fprintf(stderr, "Skipping file %s\n", input);
			continue;
		}

		SerdURI  base_uri;
		SerdNode base_uri_node = serd_node_new_file_uri(
			in_path, NULL, &base_uri, false);

		serd_env_set_base_uri(env, &base_uri_node);
		const SerdStatus st = serd_reader_read_file(reader, in_path);
		if (st) {
			fprintf(stderr, "error reading %s: %s\n",
			        in_path, serd_strerror(st));
		}

		serd_node_free(&base_uri_node);
		free(in_path);
	}
	serd_reader_free(reader);
	serd_env_free(env);

#define URI(prefix, suffix) \
	uris.prefix##_##suffix = sord_new_uri(world, NS_##prefix #suffix)

	URIs uris;
	URI(foaf, Document);
	URI(owl, AnnotationProperty);
	URI(owl, Class);
	URI(owl, DatatypeProperty);
	URI(owl, FunctionalProperty);
	URI(owl, InverseFunctionalProperty);
	URI(owl, ObjectProperty);
	URI(owl, OntologyProperty);
	URI(owl, Restriction);
	URI(owl, Thing);
	URI(owl, cardinality);
	URI(owl, equivalentClass);
	URI(owl, minCardinality);
	URI(owl, onDatatype);
	URI(owl, onProperty);
	URI(owl, someValuesFrom);
	URI(owl, withRestrictions);
	URI(rdf, PlainLiteral);
	URI(rdf, Property);
	URI(rdf, first);
	URI(rdf, rest);
	URI(rdf, type);
	URI(rdfs, Class);
	URI(rdfs, Literal);
	URI(rdfs, Resource);
	URI(rdfs, domain);
	URI(rdfs, label);
	URI(rdfs, range);
	URI(rdfs, subClassOf);
	URI(xsd, anyURI);
	URI(xsd, decimal);
	URI(xsd, maxInclusive);
	URI(xsd, minInclusive);
	URI(xsd, pattern);
	URI(xsd, string);

#ifndef HAVE_PCRE
	fprintf(stderr, "warning: Built without PCRE, datatypes not checked.\n");
#endif

	const int prop_st = check_properties(model, &uris);
	const int inst_st = check_instances(model, &uris);

	printf("Found %d errors among %d files (checked %d restrictions)\n",
	       n_errors, argc - 1, n_restrictions);

	sord_free(model);
	sord_world_free(world);
	return prop_st || inst_st;
}
Beispiel #25
0
int
main(int argc, char** argv)
{
	if (argc < 2) {
		return print_usage(argv[0], true);
	}

	FILE*          in_fd         = NULL;
	SerdSyntax     input_syntax  = SERD_TURTLE;
	SerdSyntax     output_syntax = SERD_NTRIPLES;
	bool           from_file     = true;
	bool           bulk_read     = true;
	bool           bulk_write    = false;
	bool           full_uris     = false;
	bool           lax           = false;
	bool           quiet         = false;
	const uint8_t* in_name       = NULL;
	const uint8_t* add_prefix    = NULL;
	const uint8_t* chop_prefix   = NULL;
	const uint8_t* root_uri      = NULL;
	int            a             = 1;
	for (; a < argc && argv[a][0] == '-'; ++a) {
		if (argv[a][1] == '\0') {
			in_name = (const uint8_t*)"(stdin)";
			in_fd   = stdin;
			break;
		} else if (argv[a][1] == 'b') {
			bulk_write = true;
		} else if (argv[a][1] == 'e') {
			bulk_read = false;
		} else if (argv[a][1] == 'f') {
			full_uris = true;
		} else if (argv[a][1] == 'h') {
			return print_usage(argv[0], false);
		} else if (argv[a][1] == 'l') {
			lax = true;
		} else if (argv[a][1] == 'q') {
			quiet = true;
		} else if (argv[a][1] == 'v') {
			return print_version();
		} else if (argv[a][1] == 's') {
			in_name = (const uint8_t*)"(string)";
			from_file = false;
			++a;
			break;
		} else if (argv[a][1] == 'i') {
			if (++a == argc) {
				return missing_arg(argv[0], 'i');
			} else if (!set_syntax(&input_syntax, argv[a])) {
				return print_usage(argv[0], true);
			}
		} else if (argv[a][1] == 'o') {
			if (++a == argc) {
				return missing_arg(argv[0], 'o');
			} else if (!set_syntax(&output_syntax, argv[a])) {
				return print_usage(argv[0], true);
			}
		} else if (argv[a][1] == 'p') {
			if (++a == argc) {
				return missing_arg(argv[0], 'p');
			}
			add_prefix = (const uint8_t*)argv[a];
		} else if (argv[a][1] == 'c') {
			if (++a == argc) {
				return missing_arg(argv[0], 'c');
			}
			chop_prefix = (const uint8_t*)argv[a];
		} else if (argv[a][1] == 'r') {
			if (++a == argc) {
				return missing_arg(argv[0], 'r');
			}
			root_uri = (const uint8_t*)argv[a];
		} else {
			SERDI_ERRORF("invalid option -- '%s'\n", argv[a] + 1);
			return print_usage(argv[0], true);
		}
	}

	if (a == argc) {
		SERDI_ERROR("missing input\n");
		return 1;
	}

	const uint8_t* input = (const uint8_t*)argv[a++];
	if (from_file) {
		in_name = in_name ? in_name : input;
		if (!in_fd) {
			input = serd_uri_to_path(in_name);
			if (!input || !(in_fd = serd_fopen((const char*)input, "r"))) {
				return 1;
			}
		}
	}

	SerdURI  base_uri = SERD_URI_NULL;
	SerdNode base     = SERD_NODE_NULL;
	if (a < argc) {  // Base URI given on command line
		base = serd_node_new_uri_from_string(
			(const uint8_t*)argv[a], NULL, &base_uri);
	} else if (from_file && in_fd != stdin) {  // Use input file URI
		base = serd_node_new_file_uri(input, NULL, &base_uri, false);
	}

	FILE*    out_fd = stdout;
	SerdEnv* env    = serd_env_new(&base);

	int output_style = 0;
	if (output_syntax == SERD_NTRIPLES) {
		output_style |= SERD_STYLE_ASCII;
	} else {
		output_style |= SERD_STYLE_ABBREVIATED;
		if (!full_uris) {
			output_style |= SERD_STYLE_CURIED;
		}
	}

	if (input_syntax != SERD_NTRIPLES || (output_style & SERD_STYLE_CURIED)) {
		// Base URI may change and/or we're abbreviating URIs, so must resolve
		output_style |= SERD_STYLE_RESOLVED;  // Base may chan
	}

	if (bulk_write) {
		output_style |= SERD_STYLE_BULK;
	}

	SerdWriter* writer = serd_writer_new(
		output_syntax, (SerdStyle)output_style,
		env, &base_uri, serd_file_sink, out_fd);

	SerdReader* reader = serd_reader_new(
		input_syntax, writer, NULL,
		(SerdBaseSink)serd_writer_set_base_uri,
		(SerdPrefixSink)serd_writer_set_prefix,
		(SerdStatementSink)serd_writer_write_statement,
		(SerdEndSink)serd_writer_end_anon);

	serd_reader_set_strict(reader, !lax);
	if (quiet) {
		serd_reader_set_error_sink(reader, quiet_error_sink, NULL);
		serd_writer_set_error_sink(writer, quiet_error_sink, NULL);
	}

	SerdNode root = serd_node_from_string(SERD_URI, root_uri);
	serd_writer_set_root_uri(writer, &root);
	serd_writer_chop_blank_prefix(writer, chop_prefix);
	serd_reader_add_blank_prefix(reader, add_prefix);

	SerdStatus status = SERD_SUCCESS;
	if (!from_file) {
		status = serd_reader_read_string(reader, input);
	} else if (bulk_read) {
		status = serd_reader_read_file_handle(reader, in_fd, in_name);
	} else {
		status = serd_reader_start_stream(reader, in_fd, in_name, false);
		while (!status) {
			status = serd_reader_read_chunk(reader);
		}
		serd_reader_end_stream(reader);
	}

	serd_reader_free(reader);

	if (from_file) {
		fclose(in_fd);
	}

	serd_writer_finish(writer);
	serd_writer_free(writer);
	serd_env_free(env);
	serd_node_free(&base);

	return (status > SERD_FAILURE) ? 1 : 0;
}
Beispiel #26
0
SordNode*
sord_node_from_serd_node(SordWorld*      world,
                         SerdEnv*        env,
                         const SerdNode* sn,
                         const SerdNode* datatype,
                         const SerdNode* lang)
{
	if (!sn) {
		return NULL;
	}

	SordNode* datatype_node = NULL;
	SordNode* ret           = NULL;
	switch (sn->type) {
	case SERD_NOTHING:
		return NULL;
	case SERD_LITERAL:
		datatype_node = sord_node_from_serd_node(
			world, env, datatype, NULL, NULL),
		ret = sord_new_literal_counted(
			world,
			datatype_node,
			sn->buf,
			sn->n_bytes,
			sn->n_chars,
			sn->flags,
			lang ? (const char*)lang->buf : NULL);
		sord_node_free(world, datatype_node);
		return ret;
	case SERD_URI:
		if (serd_uri_string_has_scheme(sn->buf)) {
			return sord_new_uri_counted(
				world, sn->buf, sn->n_bytes, sn->n_chars, true);
		} else {
			SerdURI base_uri;
			serd_env_get_base_uri(env, &base_uri);
			SerdURI  abs_uri;
			SerdNode abs_uri_node = serd_node_new_uri_from_node(
				sn, &base_uri, &abs_uri);
			ret = sord_new_uri_counted(world,
			                           abs_uri_node.buf,
			                           abs_uri_node.n_bytes,
			                           abs_uri_node.n_chars,
			                           true);
			serd_node_free(&abs_uri_node);
			return ret;
		}
	case SERD_CURIE: {
		SerdChunk uri_prefix;
		SerdChunk uri_suffix;
		if (serd_env_expand(env, sn, &uri_prefix, &uri_suffix)) {
			error(world, SERD_ERR_BAD_CURIE,
			      "failed to expand CURIE `%s'\n", sn->buf);
			return NULL;
		}
		const size_t uri_len = uri_prefix.len + uri_suffix.len;
		uint8_t*     buf     = (uint8_t*)malloc(uri_len + 1);
		memcpy(buf,                  uri_prefix.buf, uri_prefix.len);
		memcpy(buf + uri_prefix.len, uri_suffix.buf, uri_suffix.len);
		buf[uri_len] = '\0';
		ret = sord_new_uri_counted(
			world, buf, uri_prefix.len + uri_suffix.len,
			uri_prefix.len + uri_suffix.len, false);  // FIXME: UTF-8
		return ret;
	}
	case SERD_BLANK:
		return sord_new_blank_counted(world, sn->buf, sn->n_bytes, sn->n_chars);
	}
	return NULL;
}
Beispiel #27
0
int
main(int argc, char** argv)
{
	if (argc < 2) {
		return print_usage(argv[0], true);
	}

	FILE*          in_fd         = NULL;
	SerdSyntax     input_syntax  = SERD_TURTLE;
	SerdSyntax     output_syntax = SERD_NTRIPLES;
	bool           from_file     = true;
	const uint8_t* in_name       = NULL;
	int a = 1;
	for (; a < argc && argv[a][0] == '-'; ++a) {
		if (argv[a][1] == '\0') {
			in_name = (const uint8_t*)"(stdin)";
			in_fd   = stdin;
			break;
		} else if (argv[a][1] == 'h') {
			return print_usage(argv[0], false);
		} else if (argv[a][1] == 'v') {
			return print_version();
		} else if (argv[a][1] == 's') {
			in_name = (const uint8_t*)"(string)";
			from_file = false;
			++a;
			break;
		} else if (argv[a][1] == 'i') {
			if (++a == argc) {
				fprintf(stderr, "Missing value for -i\n");
				return 1;
			}
			if (!set_syntax(&input_syntax, argv[a])) {
				return 1;
			}
		} else if (argv[a][1] == 'o') {
			if (++a == argc) {
				fprintf(stderr, "Missing value for -o\n");
				return 1;
			}
			if (!set_syntax(&output_syntax, argv[a])) {
				return 1;
			}
		} else {
			fprintf(stderr, "Unknown option `%s'\n", argv[a]);
			return print_usage(argv[0], true);
		}
	}

	if (a == argc) {
		fprintf(stderr, "Missing input\n");
		return 1;
	}

	const uint8_t* input   = (const uint8_t*)argv[a++];
	uint8_t*       in_path = NULL;
	if (from_file) {
		in_name = in_name ? in_name : input;
		if (!in_fd) {
			in_path = absolute_path(serd_uri_to_path(in_name));
			if (!in_path || !(in_fd = fopen((const char*)in_path, "rb"))) {
				return 1;
			}
		}
	}

	SerdURI  base_uri      = SERD_URI_NULL;
	SerdNode base_uri_node = SERD_NODE_NULL;
	if (a < argc) {  // Base URI given on command line
		base_uri_node = serd_node_new_uri_from_string(
			(const uint8_t*)argv[a], NULL, &base_uri);
	} else if (from_file) {  // Use input file URI
		base_uri_node = serd_node_new_file_uri(in_path, NULL, &base_uri, false);
	}

	if (!base_uri_node.buf) {
		fprintf(stderr, "Missing base URI\n");
		return 1;
	}

	SordWorld*  world  = sord_world_new();
	SordModel*  sord   = sord_new(world, SORD_SPO|SORD_OPS, false);
	SerdEnv*    env    = serd_env_new(&base_uri_node);
	SerdReader* reader = sord_new_reader(sord, env, input_syntax, NULL);

	const SerdStatus status = (from_file)
		? serd_reader_read_file_handle(reader, in_fd, in_name)
		: serd_reader_read_string(reader, input);

	serd_reader_free(reader);

	fprintf(stderr, "Loaded %lu statements\n",
	        (unsigned long)sord_num_quads(sord));

	SerdEnv* write_env = serd_env_new(&base_uri_node);

	int output_style = SERD_STYLE_RESOLVED;
	if (output_syntax == SERD_NTRIPLES) {
		output_style |= SERD_STYLE_ASCII;
	} else {
		output_style |= SERD_STYLE_CURIED | SERD_STYLE_ABBREVIATED;
	}

	SerdWriter* writer = serd_writer_new(
		output_syntax,
		(SerdStyle)output_style,
		write_env, &base_uri, serd_file_sink, stdout);

	// Write @prefix directives
	serd_env_foreach(env,
	                 (SerdPrefixSink)serd_writer_set_prefix,
	                 writer);

	// Write statements
	sord_write(sord, writer, NULL);

	serd_writer_finish(writer);
	serd_writer_free(writer);

	serd_env_free(env);
	serd_env_free(write_env);
	serd_node_free(&base_uri_node);

	sord_free(sord);
	sord_world_free(world);

	return (status > SERD_FAILURE) ? 1 : 0;
}
Beispiel #28
0
int
main(int argc, char** argv)
{
	if (argc < 2) {
		return print_usage(argv[0], true);
	}

	FILE*          in_fd         = NULL;
	SerdSyntax     input_syntax  = SERD_TURTLE;
	SerdSyntax     output_syntax = SERD_NTRIPLES;
	bool           from_file     = true;
	bool           bulk_write    = false;
	bool           full_uris     = false;
	const uint8_t* in_name       = NULL;
	const uint8_t* add_prefix    = NULL;
	const uint8_t* chop_prefix   = NULL;
	const uint8_t* root_uri      = NULL;
	int            a             = 1;
	for (; a < argc && argv[a][0] == '-'; ++a) {
		if (argv[a][1] == '\0') {
			in_name = (const uint8_t*)"(stdin)";
			in_fd   = stdin;
			break;
		} else if (argv[a][1] == 'b') {
			bulk_write = true;
		} else if (argv[a][1] == 'f') {
			full_uris = true;
		} else if (argv[a][1] == 'h') {
			return print_usage(argv[0], false);
		} else if (argv[a][1] == 'v') {
			return print_version();
		} else if (argv[a][1] == 's') {
			in_name = (const uint8_t*)"(string)";
			from_file = false;
			++a;
			break;
		} else if (argv[a][1] == 'i') {
			if (++a == argc || !set_syntax(&input_syntax, argv[a])) {
				return bad_arg(argv[0], 'i');
			}
		} else if (argv[a][1] == 'o') {
			if (++a == argc || !set_syntax(&output_syntax, argv[a])) {
				return bad_arg(argv[0], 'o');
			}
		} else if (argv[a][1] == 'p') {
			if (++a == argc) {
				return bad_arg(argv[0], 'p');
			}
			add_prefix = (const uint8_t*)argv[a];
		} else if (argv[a][1] == 'c') {
			if (++a == argc) {
				return bad_arg(argv[0], 'c');
			}
			chop_prefix = (const uint8_t*)argv[a];
		} else if (argv[a][1] == 'r') {
			if (++a == argc) {
				return bad_arg(argv[0], 'r');
			}
			root_uri = (const uint8_t*)argv[a];
		} else {
			fprintf(stderr, "%s: Unknown option `%s'\n", argv[0], argv[a]);
			return print_usage(argv[0], true);
		}
	}

	if (a == argc) {
		fprintf(stderr, "%s: Missing input\n", argv[0]);
		return 1;
	}

	const uint8_t* input = (const uint8_t*)argv[a++];
	if (from_file) {
		in_name = in_name ? in_name : input;
		if (!in_fd) {
			input = serd_uri_to_path(in_name);
			if (!input || !(in_fd = serd_fopen((const char*)input, "r"))) {
				return 1;
			}
		}
	}

	SerdURI  base_uri = SERD_URI_NULL;
	SerdNode base     = SERD_NODE_NULL;
	if (a < argc) {  // Base URI given on command line
		base = serd_node_new_uri_from_string(
			(const uint8_t*)argv[a], NULL, &base_uri);
	} else if (from_file && in_fd != stdin) {  // Use input file URI
		base = serd_node_new_file_uri(input, NULL, &base_uri, false);
	}

	FILE*    out_fd = stdout;
	SerdEnv* env    = serd_env_new(&base);

	int output_style = 0;
	if (output_syntax == SERD_NTRIPLES) {
		output_style |= SERD_STYLE_ASCII;
	} else {
		output_style |= SERD_STYLE_ABBREVIATED;
		if (!full_uris) {
			output_style |= SERD_STYLE_CURIED;
		}
	}

	if (input_syntax != SERD_NTRIPLES  // Base URI may change (@base)
	    || (output_syntax == SERD_TURTLE)) {
		output_style |= SERD_STYLE_RESOLVED;
	}

	if (bulk_write) {
		output_style |= SERD_STYLE_BULK;
	}

	SerdWriter* writer = serd_writer_new(
		output_syntax, (SerdStyle)output_style,
		env, &base_uri, serd_file_sink, out_fd);

	SerdReader* reader = serd_reader_new(
		input_syntax, writer, NULL,
		(SerdBaseSink)serd_writer_set_base_uri,
		(SerdPrefixSink)serd_writer_set_prefix,
		(SerdStatementSink)serd_writer_write_statement,
		(SerdEndSink)serd_writer_end_anon);

	SerdNode root = serd_node_from_string(SERD_URI, root_uri);
	serd_writer_set_root_uri(writer, &root);
	serd_writer_chop_blank_prefix(writer, chop_prefix);
	serd_reader_add_blank_prefix(reader, add_prefix);

	const SerdStatus status = (from_file)
		? serd_reader_read_file_handle(reader, in_fd, in_name)
		: serd_reader_read_string(reader, input);

	serd_reader_free(reader);

	if (from_file) {
		fclose(in_fd);
	}

	serd_writer_finish(writer);
	serd_writer_free(writer);
	serd_env_free(env);
	serd_node_free(&base);

	return (status > SERD_FAILURE) ? 1 : 0;
}