diff options
author | David Robillard <d@drobilla.net> | 2011-04-30 17:41:11 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2011-04-30 17:41:11 +0000 |
commit | 3ef3583423f162dc1860523deed95fa215ff5c10 (patch) | |
tree | 7a0e1703a023a47a2f740c67da4b12066cc23c7e | |
parent | 6dba0dd5cf94285437d2a0808ee2dec971302058 (diff) | |
download | serd-3ef3583423f162dc1860523deed95fa215ff5c10.tar.gz serd-3ef3583423f162dc1860523deed95fa215ff5c10.tar.bz2 serd-3ef3583423f162dc1860523deed95fa215ff5c10.zip |
Use descriptive return codes instead of bool
git-svn-id: http://svn.drobilla.net/serd/trunk@154 490d8e77-9747-427b-9fa3-0b8f29cee8a0
-rw-r--r-- | serd/serd.h | 62 | ||||
-rw-r--r-- | src/env.c | 8 | ||||
-rw-r--r-- | src/node.c | 4 | ||||
-rw-r--r-- | src/reader.c | 63 | ||||
-rw-r--r-- | src/serdi.c | 28 | ||||
-rw-r--r-- | src/uri.c | 4 | ||||
-rw-r--r-- | src/writer.c | 30 |
7 files changed, 100 insertions, 99 deletions
diff --git a/serd/serd.h b/serd/serd.h index d4326c2f..7580207d 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -90,6 +90,18 @@ typedef struct SerdReadStateImpl SerdReadState; typedef struct SerdWriterImpl SerdWriter; /** + Return status code. +*/ +typedef enum { + SERD_SUCCESS = 0, /**< No error */ + SERD_FAILURE = 1, /**< Non-fatal failure */ + SERD_ERR_UNKNOWN = 2, /**< Unknown error */ + SERD_ERR_BAD_SYNTAX = 3, /**< Invalid syntax */ + SERD_ERR_BAD_ARG = 3, /**< Invalid argument */ + SERD_ERR_NOT_FOUND = 4 /**< Not found */ +} SerdStatus; + +/** RDF syntax type. */ typedef enum { @@ -249,7 +261,7 @@ serd_uri_string_has_scheme(const uint8_t* utf8); Parse @a utf8, writing result to @a out. */ SERD_API -bool +SerdStatus serd_uri_parse(const uint8_t* utf8, SerdURI* out); /** @@ -357,30 +369,30 @@ serd_node_free(SerdNode* node); Called whenever the base URI of the serialisation changes. */ -typedef bool (*SerdBaseSink)(void* handle, - const SerdNode* uri); +typedef SerdStatus (*SerdBaseSink)(void* handle, + const SerdNode* uri); /** Sink (callback) for namespace definitions. Called whenever a prefix is defined in the serialisation. */ -typedef bool (*SerdPrefixSink)(void* handle, - const SerdNode* name, - const SerdNode* uri); +typedef SerdStatus (*SerdPrefixSink)(void* handle, + const SerdNode* name, + const SerdNode* uri); /** Sink (callback) for statements. Called for every RDF statement in the serialisation. */ -typedef bool (*SerdStatementSink)(void* handle, - const SerdNode* graph, - const SerdNode* subject, - const SerdNode* predicate, - const SerdNode* object, - const SerdNode* object_datatype, - const SerdNode* object_lang); +typedef SerdStatus (*SerdStatementSink)(void* handle, + const SerdNode* graph, + const SerdNode* subject, + const SerdNode* predicate, + const SerdNode* object, + const SerdNode* object_datatype, + const SerdNode* object_lang); /** Sink (callback) for anonymous node end markers. @@ -389,8 +401,8 @@ typedef bool (*SerdStatementSink)(void* handle, @a value will no longer be referred to by any future statements (i.e. the anonymous serialisation of the node is finished). */ -typedef bool (*SerdEndSink)(void* handle, - const SerdNode* node); +typedef SerdStatus (*SerdEndSink)(void* handle, + const SerdNode* node); /** @} @@ -435,7 +447,7 @@ serd_env_qualify(const SerdEnv* env, Expand @a curie. */ SERD_API -bool +SerdStatus serd_env_expand(const SerdEnv* env, const SerdNode* curie, SerdChunk* uri_prefix, @@ -486,7 +498,7 @@ serd_reader_set_blank_prefix(SerdReader* reader, Read @a file. */ SERD_API -bool +SerdStatus serd_reader_read_file(SerdReader* reader, FILE* file, const uint8_t* name); @@ -495,7 +507,7 @@ serd_reader_read_file(SerdReader* reader, Read @a utf8. */ SERD_API -bool +SerdStatus serd_reader_read_string(SerdReader* me, const uint8_t* utf8); /** @@ -543,7 +555,7 @@ serd_read_state_get_base_uri(SerdReadState* state, Set the current base URI. */ SERD_API -bool +SerdStatus serd_read_state_set_base_uri(SerdReadState* state, const SerdNode* uri_node); @@ -551,7 +563,7 @@ serd_read_state_set_base_uri(SerdReadState* state, Set a namespace prefix. */ SERD_API -bool +SerdStatus serd_read_state_set_prefix(SerdReadState* state, const SerdNode* name, const SerdNode* uri_node); @@ -585,7 +597,7 @@ serd_writer_free(SerdWriter* writer); Set the current output base URI (and emit directive if applicable). */ SERD_API -void +SerdStatus serd_writer_set_base_uri(SerdWriter* writer, const SerdURI* uri); @@ -593,7 +605,7 @@ serd_writer_set_base_uri(SerdWriter* writer, Set a namespace prefix (and emit directive if applicable). */ SERD_API -bool +SerdStatus serd_writer_set_prefix(SerdWriter* writer, const SerdNode* name, const SerdNode* uri); @@ -602,7 +614,7 @@ serd_writer_set_prefix(SerdWriter* writer, Write a statement. */ SERD_API -bool +SerdStatus serd_writer_write_statement(SerdWriter* writer, const SerdNode* graph, const SerdNode* subject, @@ -615,7 +627,7 @@ serd_writer_write_statement(SerdWriter* writer, Mark the end of an anonymous node's description. */ SERD_API -bool +SerdStatus serd_writer_end_anon(SerdWriter* writer, const SerdNode* node); @@ -623,7 +635,7 @@ serd_writer_end_anon(SerdWriter* writer, Finish a write. */ SERD_API -void +SerdStatus serd_writer_finish(SerdWriter* writer); /** @@ -112,7 +112,7 @@ serd_env_qualify(const SerdEnv* env, } SERD_API -bool +SerdStatus serd_env_expand(const SerdEnv* env, const SerdNode* qname, SerdChunk* uri_prefix, @@ -120,7 +120,7 @@ serd_env_expand(const SerdEnv* env, { const uint8_t* const colon = memchr(qname->buf, ':', qname->n_bytes); if (!colon) { - return false; // Illegal qname + return SERD_ERR_BAD_ARG; // Illegal qname } const size_t name_len = colon - qname->buf; @@ -130,9 +130,9 @@ serd_env_expand(const SerdEnv* env, uri_prefix->len = prefix->uri.n_bytes - 1; uri_suffix->buf = colon + 1; uri_suffix->len = qname->n_bytes - (colon - qname->buf) - 2; - return true; + return SERD_SUCCESS; } - return false; + return SERD_ERR_NOT_FOUND; } SERD_API @@ -100,7 +100,7 @@ serd_node_new_uri_from_string(const uint8_t* str, return serd_node_new_uri(base, NULL, out); // Empty URI => Base URI } else { SerdURI uri; - if (serd_uri_parse(str, &uri)) { + if (!serd_uri_parse(str, &uri)) { return serd_node_new_uri(&uri, base, out); // Resolve/Serialise } } @@ -129,7 +129,7 @@ serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out) node.n_chars = actual_len; // FIXME: double parse - if (!serd_uri_parse(buf, out)) { + if (serd_uri_parse(buf, out)) { fprintf(stderr, "Failed to parse URI <%s>\n", buf); return SERD_NODE_NULL; } diff --git a/src/reader.c b/src/reader.c index 8dec46c7..6d9cc599 100644 --- a/src/reader.c +++ b/src/reader.c @@ -98,12 +98,6 @@ struct SerdReadStateImpl { SerdURI base_uri; }; -typedef enum { - SERD_SUCCESS = 0, ///< Completed successfully - SERD_FAILURE = 1, ///< Non-fatal failure - SERD_ERROR = 2, ///< Fatal error -} SerdStatus; - static int error(SerdReader* reader, const char* fmt, ...) { @@ -301,13 +295,13 @@ emit_statement(SerdReader* reader, const SerdNode object = public_node(reader, o); const SerdNode object_datatype = public_node_from_ref(reader, SERD_URI, o->datatype); const SerdNode object_lang = public_node_from_ref(reader, SERD_LITERAL, o->lang); - return reader->statement_sink(reader->handle, - &graph, - &subject, - &predicate, - &object, - &object_datatype, - &object_lang); + return !reader->statement_sink(reader->handle, + &graph, + &subject, + &predicate, + &object, + &object_datatype, + &object_lang); } static bool read_collection(SerdReader* reader, ReadContext ctx, Node* dest); @@ -449,11 +443,11 @@ read_character(SerdReader* reader, Ref dest) switch (c) { case '\0': error(reader, "unexpected end of file\n", peek_byte(reader)); - return SERD_ERROR; + return SERD_ERR_BAD_SYNTAX; default: if (c < 0x20) { // ASCII control character error(reader, "unexpected control character\n"); - return SERD_ERROR; + return SERD_ERR_BAD_SYNTAX; } else if (c <= 0x7E) { // Printable ASCII push_byte(reader, dest, eat_byte(reader, c)); return SERD_SUCCESS; @@ -467,7 +461,7 @@ read_character(SerdReader* reader, Ref dest) size = 4; } else { error(reader, "invalid character\n"); - return SERD_ERROR; + return SERD_ERR_BAD_SYNTAX; } for (unsigned i = 0; i < size; ++i) { push_byte(reader, dest, eat_byte(reader, peek_byte(reader))); @@ -489,7 +483,7 @@ read_echaracter(SerdReader* reader, Ref dest) return SERD_SUCCESS; } else { error(reader, "illegal escape `\\%c'\n", peek_byte(reader)); - return SERD_ERROR; + return SERD_ERR_BAD_SYNTAX; } default: return read_character(reader, dest); @@ -520,7 +514,7 @@ read_lcharacter(SerdReader* reader, Ref dest) return SERD_SUCCESS; } else { error(reader, "illegal escape `\\%c'\n", peek_byte(reader)); - return SERD_ERROR; + return SERD_ERR_BAD_SYNTAX; } case 0x9: case 0xA: case 0xD: push_byte(reader, dest, eat_byte(reader, c)); @@ -542,7 +536,7 @@ read_scharacter(SerdReader* reader, Ref dest) return SERD_SUCCESS; } else { error(reader, "illegal escape `\\%c'\n", peek_byte(reader)); - return SERD_ERROR; + return SERD_ERR_BAD_SYNTAX; } case '\"': return SERD_FAILURE; @@ -622,7 +616,7 @@ read_longString(SerdReader* reader) Ref str = push_string(reader, "", 1); SerdStatus st; while (!(st = read_lcharacter(reader, str))) {} - if (st != SERD_ERROR) { + if (st < SERD_ERR_UNKNOWN) { return str; } pop_string(reader, str); @@ -637,7 +631,7 @@ read_string(SerdReader* reader) Ref str = push_string(reader, "", 1); SerdStatus st; while (!(st = read_scharacter(reader, str))) {} - if (st != SERD_ERROR) { + if (st < SERD_ERR_UNKNOWN) { eat_byte(reader, '\"'); return str; } @@ -670,7 +664,7 @@ read_relativeURI(SerdReader* reader) Ref str = push_string(reader, "", 1); SerdStatus st; while (!(st = read_ucharacter(reader, str))) {} - if (st != SERD_ERROR) { + if (st < SERD_ERR_UNKNOWN) { return str; } pop_string(reader, str); @@ -1292,7 +1286,7 @@ read_prefixID(SerdReader* reader) TRY_THROW(uri = read_uriref(reader)); const SerdNode name_node = public_node_from_ref(reader, SERD_LITERAL, name); const SerdNode uri_node = public_node_from_ref(reader, SERD_URI, uri); - ret = reader->prefix_sink(reader->handle, &name_node, &uri_node); + ret = !reader->prefix_sink(reader->handle, &name_node, &uri_node); pop_string(reader, uri); except: pop_string(reader, name); @@ -1409,7 +1403,7 @@ serd_reader_set_blank_prefix(SerdReader* reader, } SERD_API -bool +SerdStatus serd_reader_read_file(SerdReader* me, FILE* file, const uint8_t* name) { const Cursor cur = { name, 1, 1 }; @@ -1432,11 +1426,11 @@ serd_reader_read_file(SerdReader* me, FILE* file, const uint8_t* name) free(me->read_buf - READ_BUF_LEN); me->fd = 0; me->read_buf = NULL; - return ret; + return ret ? SERD_SUCCESS : SERD_ERR_UNKNOWN; } SERD_API -bool +SerdStatus serd_reader_read_string(SerdReader* me, const uint8_t* utf8) { const Cursor cur = { (const uint8_t*)"(string)", 1, 1 }; @@ -1450,7 +1444,7 @@ serd_reader_read_string(SerdReader* me, const uint8_t* utf8) const bool ret = read_turtleDoc(me); me->read_buf = NULL; - return ret; + return ret ? SERD_SUCCESS : SERD_ERR_UNKNOWN; } SERD_API @@ -1508,7 +1502,7 @@ serd_read_state_get_base_uri(SerdReadState* state, } SERD_API -bool +SerdStatus serd_read_state_set_base_uri(SerdReadState* state, const SerdNode* uri_node) { @@ -1522,13 +1516,13 @@ serd_read_state_set_base_uri(SerdReadState* state, serd_node_free(&state->base_uri_node); state->base_uri_node = base_uri_node; state->base_uri = base_uri; - return true; + return SERD_SUCCESS; } - return false; + return SERD_ERR_BAD_ARG; } SERD_API -bool +SerdStatus serd_read_state_set_prefix(SerdReadState* state, const SerdNode* name, const SerdNode* uri_node) @@ -1536,7 +1530,6 @@ serd_read_state_set_prefix(SerdReadState* state, if (serd_uri_string_has_scheme(uri_node->buf)) { // Set prefix to absolute URI serd_env_add(state->env, name, uri_node); - return true; } else { // Resolve relative URI and create a new node and URI for it SerdURI abs_uri; @@ -1544,14 +1537,12 @@ serd_read_state_set_prefix(SerdReadState* state, uri_node, &state->base_uri, &abs_uri); if (!abs_uri_node.buf) { - return false; + return SERD_ERR_BAD_ARG; } // Set prefix to resolved (absolute) URI serd_env_add(state->env, name, &abs_uri_node); serd_node_free(&abs_uri_node); - return true; } - return false; + return SERD_SUCCESS; } - diff --git a/src/serdi.c b/src/serdi.c index 0f843344..e3d774ef 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -27,21 +27,21 @@ typedef struct { SerdWriter* writer; } State; -static bool +static SerdStatus event_base(void* handle, const SerdNode* uri_node) { State* const state = (State*)handle; - if (serd_read_state_set_base_uri(state->read_state, uri_node)) { + if (!serd_read_state_set_base_uri(state->read_state, uri_node)) { SerdURI base_uri; serd_read_state_get_base_uri(state->read_state, &base_uri); serd_writer_set_base_uri(state->writer, &base_uri); - return true; + return SERD_SUCCESS; } - return false; + return SERD_ERR_UNKNOWN; } -static bool +static SerdStatus event_prefix(void* handle, const SerdNode* name, const SerdNode* uri_node) @@ -49,10 +49,10 @@ event_prefix(void* handle, State* const state = (State*)handle; serd_read_state_set_prefix(state->read_state, name, uri_node); serd_writer_set_prefix(state->writer, name, uri_node); - return true; + return SERD_SUCCESS; } -static bool +static SerdStatus event_statement(void* handle, const SerdNode* graph, const SerdNode* subject, @@ -66,7 +66,7 @@ event_statement(void* handle, graph, subject, predicate, object, object_datatype, object_lang); } -static bool +static SerdStatus event_end(void* handle, const SerdNode* node) { @@ -182,7 +182,7 @@ main(int argc, char** argv) SerdURI base_uri; if (a < argc) { // Base URI given on command line const uint8_t* const in_base_uri = (const uint8_t*)argv[a++]; - if (!serd_uri_parse((const uint8_t*)in_base_uri, &base_uri)) { + if (serd_uri_parse((const uint8_t*)in_base_uri, &base_uri)) { fprintf(stderr, "Invalid base URI <%s>\n", argv[2]); return 1; } @@ -193,7 +193,7 @@ main(int argc, char** argv) base_uri_str = (const uint8_t*)""; } - if (!serd_uri_parse(base_uri_str, &base_uri)) { + if (serd_uri_parse(base_uri_str, &base_uri)) { fprintf(stderr, "Invalid base URI <%s>\n", base_uri_str); } @@ -220,7 +220,7 @@ main(int argc, char** argv) SERD_TURTLE, &state, event_base, event_prefix, event_statement, event_end); - const bool success = (from_file) + const SerdStatus status = (from_file) ? serd_reader_read_file(reader, in_fd, input) : serd_reader_read_string(reader, input); @@ -235,9 +235,5 @@ main(int argc, char** argv) serd_read_state_free(state.read_state); serd_env_free(state.env); - if (success) { - return 0; - } - - return 1; + return (status == SERD_SUCCESS) ? 0 : 1; } @@ -69,7 +69,7 @@ serd_uri_dump(const SerdURI* uri, FILE* file) #endif SERD_API -bool +SerdStatus serd_uri_parse(const uint8_t* utf8, SerdURI* uri) { *uri = SERD_URI_NULL; @@ -181,7 +181,7 @@ end: fprintf(stderr, "\n"); #endif - return true; + return SERD_SUCCESS; } SERD_API diff --git a/src/writer.c b/src/writer.c index 67b69166..6794439a 100644 --- a/src/writer.c +++ b/src/writer.c @@ -205,7 +205,7 @@ write_node(SerdWriter* writer, case SERD_CURIE: switch (writer->syntax) { case SERD_NTRIPLES: - if (!serd_env_expand(writer->env, node, &uri_prefix, &uri_suffix)) { + if (serd_env_expand(writer->env, node, &uri_prefix, &uri_suffix)) { fprintf(stderr, "Undefined namespace prefix `%s'\n", node->buf); return false; } @@ -257,7 +257,7 @@ write_node(SerdWriter* writer, } else if ((writer->style & SERD_STYLE_RESOLVED) && !serd_uri_string_has_scheme(node->buf)) { SerdURI uri; - if (serd_uri_parse(node->buf, &uri)) { + if (!serd_uri_parse(node->buf, &uri)) { SerdURI abs_uri; serd_uri_resolve(&uri, &writer->base_uri, &abs_uri); writer->sink("<", 1, writer->stream); @@ -275,7 +275,7 @@ write_node(SerdWriter* writer, } SERD_API -bool +SerdStatus serd_writer_write_statement(SerdWriter* writer, const SerdNode* graph, const SerdNode* subject, @@ -292,10 +292,10 @@ serd_writer_write_statement(SerdWriter* writer, write_node(writer, predicate, NULL, NULL); writer->sink(" ", 1, writer->stream); if (!write_node(writer, object, object_datatype, object_lang)) { - return false; + return SERD_ERR_UNKNOWN; } writer->sink(" .\n", 3, writer->stream); - return true; + return SERD_SUCCESS; case SERD_TURTLE: break; } @@ -362,20 +362,20 @@ serd_writer_write_statement(SerdWriter* writer, serd_node_copy(predicate) }; reset_context(writer); writer->context = new_context; - return true; + return SERD_SUCCESS; } SERD_API -bool +SerdStatus serd_writer_end_anon(SerdWriter* writer, const SerdNode* node) { if (writer->syntax == SERD_NTRIPLES) { - return true; + return SERD_SUCCESS; } if (serd_stack_is_empty(&writer->anon_stack)) { fprintf(stderr, "Unexpected end of anonymous node\n"); - return false; + return SERD_ERR_UNKNOWN; } assert(writer->indent > 0); --writer->indent; @@ -387,17 +387,18 @@ serd_writer_end_anon(SerdWriter* writer, if (!writer->context.subject.buf) { // End of anonymous subject writer->context.subject = serd_node_copy(node); } - return true; + return SERD_SUCCESS; } SERD_API -void +SerdStatus serd_writer_finish(SerdWriter* writer) { if (writer->context.subject.buf) { writer->sink(" .\n", 3, writer->stream); } reset_context(writer); + return SERD_SUCCESS; } SERD_API @@ -424,7 +425,7 @@ serd_writer_new(SerdSyntax syntax, } SERD_API -void +SerdStatus serd_writer_set_base_uri(SerdWriter* writer, const SerdURI* uri) { @@ -439,10 +440,11 @@ serd_writer_set_base_uri(SerdWriter* writer, writer->sink("> .\n", 4, writer->stream); } reset_context(writer); + return SERD_SUCCESS; } SERD_API -bool +SerdStatus serd_writer_set_prefix(SerdWriter* writer, const SerdNode* name, const SerdNode* uri) @@ -459,7 +461,7 @@ serd_writer_set_prefix(SerdWriter* writer, writer->sink("> .\n", 4, writer->stream); } reset_context(writer); - return true; + return SERD_SUCCESS; } SERD_API |