From 1e5e0c9fe2bed8d55cc6d113410c0fdea07f5373 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 4 Jul 2015 14:56:07 -0700 Subject: [PATCH] Make jq.h usable from C++ MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Previously, with clang++: jq.h:46:37: error: typedef redefinition with different types ('struct jq_util_input_state *' vs 'jq_util_input_state') With g++: jq.h:46:37: error: conflicting declaration ‘typedef struct jq_util_input_state* jq_util_input_state’ This typedef was added to libjq by commit 0d41447 which was after the 1.4 release, so although it is a public API, this is not a backcompat break because it has never been in a release. Specifying the "*" at all uses of jq_util_input_state is slightly tedious, but jq_state already works that way, so at least it will be consistent. --- jq.h | 14 +++++++------- main.c | 2 +- util.c | 28 ++++++++++++++-------------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/jq.h b/jq.h index 0e47d62..8678ef1 100644 --- a/jq.h +++ b/jq.h @@ -43,15 +43,15 @@ jv jq_get_attr(jq_state *, jv); * whereas jv string values must be in UTF-8. This way the caller * doesn't have to perform any codeset conversions. */ -typedef struct jq_util_input_state *jq_util_input_state; +typedef struct jq_util_input_state jq_util_input_state; typedef void (*jq_util_msg_cb)(void *, const char *); -jq_util_input_state jq_util_input_init(jq_util_msg_cb, void *); -void jq_util_input_set_parser(jq_util_input_state, jv_parser *, int); -void jq_util_input_free(jq_util_input_state *); -void jq_util_input_add_input(jq_util_input_state, const char *); -int jq_util_input_errors(jq_util_input_state); -jv jq_util_input_next_input(jq_util_input_state); +jq_util_input_state *jq_util_input_init(jq_util_msg_cb, void *); +void jq_util_input_set_parser(jq_util_input_state *, jv_parser *, int); +void jq_util_input_free(jq_util_input_state **); +void jq_util_input_add_input(jq_util_input_state *, const char *); +int jq_util_input_errors(jq_util_input_state *); +jv jq_util_input_next_input(jq_util_input_state *); jv jq_util_input_next_input_cb(jq_state *, void *); jv jq_util_input_get_position(jq_state*); jv jq_util_input_get_current_filename(jq_state*); diff --git a/main.c b/main.c index 168aa43..0ac5d62 100644 --- a/main.c +++ b/main.c @@ -207,7 +207,7 @@ int main(int argc, char* argv[]) { int dumpopts = JV_PRINT_INDENT_FLAGS(2); const char* program = 0; - jq_util_input_state input_state = jq_util_input_init(NULL, NULL); // XXX add err_cb + jq_util_input_state *input_state = jq_util_input_init(NULL, NULL); // XXX add err_cb int further_args_are_files = 0; int jq_flags = 0; diff --git a/util.c b/util.c index 62b5ba2..e490130 100644 --- a/util.c +++ b/util.c @@ -203,12 +203,12 @@ static void fprinter(void *data, const char *fname) { } // If parser == NULL -> RAW -jq_util_input_state jq_util_input_init(jq_util_msg_cb err_cb, void *err_cb_data) { +jq_util_input_state *jq_util_input_init(jq_util_msg_cb err_cb, void *err_cb_data) { if (err_cb == NULL) { err_cb = fprinter; err_cb_data = stderr; } - jq_util_input_state new_state = jv_mem_alloc(sizeof(*new_state)); + jq_util_input_state *new_state = jv_mem_alloc(sizeof(*new_state)); memset(new_state, 0, sizeof(*new_state)); new_state->err_cb = err_cb; new_state->err_cb_data = err_cb_data; @@ -226,7 +226,7 @@ jq_util_input_state jq_util_input_init(jq_util_msg_cb err_cb, void *err_cb_data) return new_state; } -void jq_util_input_set_parser(jq_util_input_state state, jv_parser *parser, int slurp) { +void jq_util_input_set_parser(jq_util_input_state *state, jv_parser *parser, int slurp) { assert(!jv_is_valid(state->slurped)); state->parser = parser; @@ -238,8 +238,8 @@ void jq_util_input_set_parser(jq_util_input_state state, jv_parser *parser, int state->slurped = jv_invalid(); } -void jq_util_input_free(jq_util_input_state *state) { - jq_util_input_state old_state = *state; +void jq_util_input_free(jq_util_input_state **state) { + jq_util_input_state *old_state = *state; *state = NULL; if (old_state == NULL) return; @@ -254,22 +254,22 @@ void jq_util_input_free(jq_util_input_state *state) { jv_mem_free(old_state); } -void jq_util_input_add_input(jq_util_input_state state, const char *fname) { +void jq_util_input_add_input(jq_util_input_state *state, const char *fname) { state->files = jv_mem_realloc(state->files, (state->nfiles + 1) * sizeof(state->files[0])); state->files[state->nfiles++] = jv_mem_strdup(fname); } -int jq_util_input_errors(jq_util_input_state state) { +int jq_util_input_errors(jq_util_input_state *state) { return state->failures; } -static const char *next_file(jq_util_input_state state) { +static const char *next_file(jq_util_input_state *state) { if (state->curr_file < state->nfiles) return state->files[state->curr_file++]; return NULL; } -static int jq_util_input_read_more(jq_util_input_state state) { +static int jq_util_input_read_more(jq_util_input_state *state) { if (!state->current_input || feof(state->current_input) || ferror(state->current_input)) { if (state->current_input && ferror(state->current_input)) { // System-level input error on the stream. It will be closed (below). @@ -360,7 +360,7 @@ static int jq_util_input_read_more(jq_util_input_state state) { } jv jq_util_input_next_input_cb(jq_state *jq, void *data) { - return jq_util_input_next_input((jq_util_input_state)data); + return jq_util_input_next_input((jq_util_input_state *)data); } // Return the current_filename:current_line @@ -371,7 +371,7 @@ jv jq_util_input_get_position(jq_state *jq) { assert(cb == jq_util_input_next_input_cb); if (cb != jq_util_input_next_input_cb) return jv_invalid_with_msg(jv_string("Invalid jq_util_input API usage")); - jq_util_input_state s = (jq_util_input_state)cb_data; + jq_util_input_state *s = (jq_util_input_state *)cb_data; // We can't assert that current_filename is a string because if // the error was a JSON parser error then we may not have set @@ -389,7 +389,7 @@ jv jq_util_input_get_current_filename(jq_state* jq) { jq_get_input_cb(jq, &cb, &cb_data); if (cb != jq_util_input_next_input_cb) return jv_invalid_with_msg(jv_string("Unknown input filename")); - jq_util_input_state s = (jq_util_input_state)cb_data; + jq_util_input_state *s = (jq_util_input_state *)cb_data; jv v = jv_copy(s->current_filename); return v; } @@ -400,7 +400,7 @@ jv jq_util_input_get_current_line(jq_state* jq) { jq_get_input_cb(jq, &cb, &cb_data); if (cb != jq_util_input_next_input_cb) return jv_invalid_with_msg(jv_string("Unknown input line number")); - jq_util_input_state s = (jq_util_input_state)cb_data; + jq_util_input_state *s = (jq_util_input_state *)cb_data; jv v = jv_number(s->current_line); return v; } @@ -408,7 +408,7 @@ jv jq_util_input_get_current_line(jq_state* jq) { // Blocks to read one more input from stdin and/or given files // When slurping, it returns just one value -jv jq_util_input_next_input(jq_util_input_state state) { +jv jq_util_input_next_input(jq_util_input_state *state) { int is_last = 0; jv value = jv_invalid(); // need more input do { -- 2.40.0