From ae46d9dfd96b05ddf876465f2c5a8cdd5f742ab0 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 21 Feb 2012 00:24:59 +0000 Subject: Print command name in serdi error messages. Trim code. git-svn-id: http://svn.drobilla.net/serd/trunk@313 490d8e77-9747-427b-9fa3-0b8f29cee8a0 --- src/env.c | 8 ++------ src/serdi.c | 60 +++++++++++++++++++++++++----------------------------------- src/writer.c | 16 ++++++---------- 3 files changed, 33 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/env.c b/src/env.c index 8f0b4632..b963bc75 100644 --- a/src/env.c +++ b/src/env.c @@ -156,9 +156,7 @@ serd_env_set_prefix_from_strings(SerdEnv* env, const SerdNode name_node = serd_node_from_string(SERD_LITERAL, name); const SerdNode uri_node = serd_node_from_string(SERD_URI, uri); - SerdStatus ret = serd_env_set_prefix(env, &name_node, &uri_node); - - return ret; + return serd_env_set_prefix(env, &name_node, &uri_node); } static inline bool @@ -267,8 +265,6 @@ serd_env_foreach(const SerdEnv* env, void* handle) { for (size_t i = 0; i < env->n_prefixes; ++i) { - func(handle, - &env->prefixes[i].name, - &env->prefixes[i].uri); + func(handle, &env->prefixes[i].name, &env->prefixes[i].uri); } } diff --git a/src/serdi.c b/src/serdi.c index d49ec776..63822e94 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -26,7 +26,7 @@ typedef struct { SerdWriter* writer; } State; -int +static int print_version() { printf("serdi " SERD_VERSION " \n"); @@ -37,7 +37,7 @@ print_version() return 0; } -int +static int print_usage(const char* name, bool error) { FILE* const os = error ? stderr : stdout; @@ -56,7 +56,7 @@ print_usage(const char* name, bool error) return error ? 1 : 0; } -bool +static bool set_syntax(SerdSyntax* syntax, const char* name) { if (!strcmp(name, "turtle")) { @@ -70,6 +70,13 @@ set_syntax(SerdSyntax* syntax, const char* name) return true; } +static int +bad_arg(const char* name, char opt) +{ + fprintf(stderr, "%s: Bad or missing value for -%c\n", name, opt); + return 1; +} + int main(int argc, char** argv) { @@ -86,7 +93,7 @@ main(int argc, char** argv) const uint8_t* in_name = NULL; const uint8_t* add_prefix = NULL; const uint8_t* chop_prefix = NULL; - int a = 1; + int a = 1; for (; a < argc && argv[a][0] == '-'; ++a) { if (argv[a][1] == '\0') { in_name = (const uint8_t*)"(stdin)"; @@ -106,41 +113,31 @@ main(int argc, char** argv) ++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; + if (++a == argc || !set_syntax(&input_syntax, argv[a])) { + return bad_arg(argv[0], 'i'); } } 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; + if (++a == argc || !set_syntax(&output_syntax, argv[a])) { + return bad_arg(argv[0], 'o'); } } else if (argv[a][1] == 'p') { if (++a == argc) { - fprintf(stderr, "Missing value for -p\n"); - return 1; + return bad_arg(argv[0], 'p'); } add_prefix = (const uint8_t*)argv[a]; } else if (argv[a][1] == 'c') { if (++a == argc) { - fprintf(stderr, "Missing value for -c\n"); - return 1; + return bad_arg(argv[0], 'c'); } chop_prefix = (const uint8_t*)argv[a]; } else { - fprintf(stderr, "Unknown option `%s'\n", argv[a]); + fprintf(stderr, "%s: Unknown option `%s'\n", argv[0], argv[a]); return print_usage(argv[0], true); } } if (a == argc) { - fprintf(stderr, "Missing input\n"); + fprintf(stderr, "%s: Missing input\n", argv[0]); return 1; } @@ -193,22 +190,15 @@ main(int argc, char** argv) output_syntax, (SerdStyle)output_style, env, &base_uri, serd_file_sink, out_fd); - if (chop_prefix) { - serd_writer_chop_blank_prefix(writer, chop_prefix); - } - - State state = { env, writer }; - SerdReader* reader = serd_reader_new( - input_syntax, state.writer, NULL, + 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); - if (add_prefix) { - serd_reader_add_blank_prefix(reader, add_prefix); - } + 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) @@ -220,9 +210,9 @@ main(int argc, char** argv) fclose(in_fd); } - serd_writer_finish(state.writer); - serd_writer_free(state.writer); - serd_env_free(state.env); + serd_writer_finish(writer); + serd_writer_free(writer); + serd_env_free(env); serd_node_free(&base_uri_node); return (status == SERD_SUCCESS) ? 0 : 1; diff --git a/src/writer.c b/src/writer.c index 03ba9781..270bd16f 100644 --- a/src/writer.c +++ b/src/writer.c @@ -269,7 +269,7 @@ write_sep(SerdWriter* writer, const Sep sep) writer->last_sep = sep; } -static void +static SerdStatus reset_context(SerdWriter* writer, bool del) { if (del) { @@ -283,6 +283,7 @@ reset_context(SerdWriter* writer, bool del) writer->context.predicate.type = SERD_NOTHING; } writer->empty = false; + return SERD_SUCCESS; } typedef enum { @@ -625,8 +626,7 @@ serd_writer_finish(SerdWriter* writer) if (writer->style & SERD_STYLE_BULK) { serd_bulk_sink_flush(&writer->bulk_sink); } - reset_context(writer, true); - return SERD_SUCCESS; + return reset_context(writer, true); } SERD_API @@ -693,8 +693,7 @@ serd_writer_set_base_uri(SerdWriter* writer, sink(uri->buf, uri->n_bytes, writer); sink("> .\n", 4, writer); } - reset_context(writer, false); - return SERD_SUCCESS; + return reset_context(writer, false); } return SERD_ERR_UNKNOWN; } @@ -717,8 +716,7 @@ serd_writer_set_prefix(SerdWriter* writer, write_text(writer, WRITE_URI, uri->buf, uri->n_bytes, '>'); sink("> .\n", 4, writer); } - reset_context(writer, false); - return SERD_SUCCESS; + return reset_context(writer, false); } return SERD_ERR_UNKNOWN; } @@ -740,7 +738,5 @@ SERD_API size_t serd_file_sink(const void* buf, size_t len, void* stream) { - FILE* file = (FILE*)stream; - return fwrite(buf, 1, len, file); + return fwrite(buf, 1, len, (FILE*)stream); } - -- cgit v1.2.1