X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=decode.c;h=4ddf34afbbaf00db414a041f4ef2e61ddc27bc0f;hb=2b29fb3954da8ede79ade2e4940f95b3ef836d8f;hp=06400f36e87516caa56a97d2cd14e5e9bf88d9fb;hpb=54614916a903af4afc2582db793eb306e39ddd0a;p=sigrok-cli.git diff --git a/decode.c b/decode.c index 06400f3..4ddf34a 100644 --- a/decode.c +++ b/decode.c @@ -139,7 +139,7 @@ static int register_pd(char *opt_pds, char *opt_pd_annotations) pdtokens = g_strsplit(opt_pds, ",", 0); for (pdtok = pdtokens; *pdtok; pdtok++) { - if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE))) { + if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE, NULL))) { g_critical("Invalid protocol decoder option '%s'.", *pdtok); break; } @@ -487,27 +487,56 @@ int setup_pd_binary(char *opt_pd_binary) * the output and late flush (and to keep the state strictly local to the * routine). Some additional complexity due to JSON's inability to handle * a trailing comma at the last item. Code phrased such that text literals - * are kept in their order of appearance in the output text. + * are kept in their order of appearance in the output (where possible). */ -static void jsontrace_open_close(gboolean is_close_req) +static void jsontrace_open_close(gboolean is_close_req, + gboolean open_item, gboolean close_item) { - static gboolean is_opened; + static gboolean is_file_open; + static gboolean is_item_open; - if (!is_close_req) { - if (!is_opened) { + if (is_close_req && is_item_open) + close_item = TRUE; + + /* Automatic file header, and array item separation. */ + if (open_item) { + if (!is_file_open) printf("{\"traceEvents\": [\n"); - is_opened = TRUE; - } else { - printf(",\n"); + if (is_item_open) { + printf("}"); + is_item_open = FALSE; } - } else { - if (is_opened) { - printf("\n"); - printf("]}\n"); - fflush(stdout); + if (is_file_open) { + printf(",\n"); } - is_opened = FALSE; + is_file_open = TRUE; + } + + /* Array item open/append/close. */ + if (open_item) { + printf("{"); + is_item_open = TRUE; + } + if (!open_item && !close_item && !is_close_req) { + printf(", "); + is_item_open = TRUE; } + if (close_item) { + printf("}"); + is_item_open = FALSE; + } + + /* Automatic file footer on shutdown. */ + if (is_close_req && is_file_open) { + printf("\n"); + printf("]}\n"); + } + if (is_close_req) + is_file_open = FALSE; + + /* Flush at end of lines, or end of file. */ + if (close_item || is_close_req) + fflush(stdout); } /* Convert uint64 sample number to double timestamp in microseconds. */ @@ -562,32 +591,34 @@ static void jsontrace_annotation(struct srd_decoder *dec, * annotation row's description. The 'ts' (timestamp) is in * microseconds. Set 'name' to the longest annotation text. * - * BEWARE of the unfortunate JSON format comma limitation. And - * some of the output formatting is motivated by the desire to - * further reduce text size, by eliminating some of the spaces. - * - * This implementation is strictly compatible to the initial - * implementation. Which might change in the future to increase - * readability of the output to humans, by generating a layout - * which is closer to other output modes. + * BEWARE of the unfortunate JSON format limitation, which + * clutters data output calls with format helper calls. + * TODO Want to introduce a cJSON dependency to delegate the + * construction of output text? */ - jsontrace_open_close(FALSE); - printf("{"); + jsontrace_open_close(FALSE, TRUE, FALSE); + printf("\"%s\": \"%s\"", "ph", "B"); + jsontrace_open_close(FALSE, FALSE, FALSE); + printf("\"%s\": %lf", "ts", jsontrace_ts_usec(pdata->start_sample)); + jsontrace_open_close(FALSE, FALSE, FALSE); + printf("\"%s\": \"%s\"", "pid", pdata->pdo->proto_id); + jsontrace_open_close(FALSE, FALSE, FALSE); + printf("\"%s\": \"%s\"", "tid", row_text); + jsontrace_open_close(FALSE, FALSE, FALSE); printf("\"%s\": \"%s\"", "name", pda->ann_text[0]); - printf(", \"%s\": \"%s\"", "ph", "B"); - printf(", \"%s\": \"%s\"", "pid", pdata->pdo->proto_id); - printf(", \"%s\": \"%s\"", "tid", row_text); - printf(", \"%s\": %lf", "ts", jsontrace_ts_usec(pdata->start_sample)); - printf("}"); - - jsontrace_open_close(FALSE); - printf("{"); + + jsontrace_open_close(FALSE, TRUE, FALSE); + printf("\"%s\": \"%s\"", "ph", "E"); + jsontrace_open_close(FALSE, FALSE, FALSE); + printf("\"%s\": %lf", "ts", jsontrace_ts_usec(pdata->end_sample)); + jsontrace_open_close(FALSE, FALSE, FALSE); + printf("\"%s\": \"%s\"", "pid", pdata->pdo->proto_id); + jsontrace_open_close(FALSE, FALSE, FALSE); + printf("\"%s\": \"%s\"", "tid", row_text); + jsontrace_open_close(FALSE, FALSE, FALSE); printf("\"%s\": \"%s\"", "name", pda->ann_text[0]); - printf(", \"%s\": \"%s\"", "ph", "E"); - printf(", \"%s\": \"%s\"", "pid", pdata->pdo->proto_id); - printf(", \"%s\": \"%s\"", "tid", row_text); - printf(", \"%s\": %lf", "ts", jsontrace_ts_usec(pdata->end_sample)); - printf("}"); + + jsontrace_open_close(FALSE, FALSE, TRUE); } void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data) @@ -721,12 +752,12 @@ void show_pd_binary(struct srd_proto_data *pdata, void *cb_data) void show_pd_prepare(void) { if (opt_pd_jsontrace) - jsontrace_open_close(TRUE); + jsontrace_open_close(TRUE, FALSE, FALSE); } void show_pd_close(void) { if (opt_pd_jsontrace) - jsontrace_open_close(TRUE); + jsontrace_open_close(TRUE, FALSE, FALSE); } #endif