]> sigrok.org Git - libsigrok.git/blobdiff - output/output_vcd.c
Fix all warnings and re-enable -Wextra.
[libsigrok.git] / output / output_vcd.c
index 0d8d9d311090ce85f93e86863eb9ef628f5f40f5..4a46e854a881b9cdffad1257ad61c8dcc422bb8e 100644 (file)
@@ -53,10 +53,12 @@ static int init(struct output *o)
        GSList *l;
        uint64_t samplerate;
        int i, b, num_probes;
-       char *c;
-       char sbuf[10], wbuf[1000];
+       char *c, *samplerate_s;
+       char wbuf[1000];
 
        ctx = malloc(sizeof(struct context));
+       if (ctx == NULL)
+               return SIGROK_ERR_MALLOC;
        o->internal = ctx;
        ctx->num_enabled_probes = 0;
        for (l = o->device->probes; l; l = l->next) {
@@ -71,34 +73,35 @@ static int init(struct output *o)
        /* TODO: Allow for configuration via o->param. */
 
        ctx->header = calloc(1, MAX_HEADER_LEN + 1);
+       if (ctx->header == NULL)
+               return SIGROK_ERR_MALLOC;
        num_probes = g_slist_length(o->device->probes);
+       /* TODO: Handle num_probes == 0, too many probes, etc. */
        samplerate = *((uint64_t *) o->device->plugin->get_device_info(
                        o->device->plugin_index, DI_CUR_SAMPLERATE));
 
-       /* Samplerate string */
-       if (samplerate >= GHZ(1))
-               snprintf(sbuf, 10, "%"PRIu64" GHz", samplerate / 1000000000);
-       else if (samplerate >= MHZ(1))
-               snprintf(sbuf, 10, "%"PRIu64" MHz", samplerate / 1000000);
-       else if (samplerate >= KHZ(1))
-               snprintf(sbuf, 10, "%"PRIu64" KHz", samplerate / 1000);
-       else
-               snprintf(sbuf, 10, "%"PRIu64" Hz", samplerate);
+       if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
+               return -1; // FIXME
 
        /* Wires / channels */
        wbuf[0] = '\0';
-       for (i = 0; i < num_probes; i++) {
+       for (i = 0; i < ctx->num_enabled_probes; i++) {
                c = (char *)&wbuf + strlen((char *)&wbuf);
-               sprintf(c, "$var wire 1 %c channel%i $end\n",
-                        (char)('!' + i), i);
+               sprintf(c, "$var wire 1 %c channel%s $end\n",
+                        (char)('!' + i), ctx->probelist[i]);
        }
 
        /* TODO: date: File or signals? Make y/n configurable. */
        b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date",
                     PACKAGE_STRING, ctx->num_enabled_probes, num_probes,
-                    (char *)&sbuf, 1, "ns", PACKAGE, (char *)&wbuf);
+                    samplerate_s, 1, "ns", PACKAGE, (char *)&wbuf);
+       /* TODO: Handle snprintf errors. */
+
+       free(samplerate_s);
 
        ctx->prevbits = calloc(sizeof(int), num_probes);
+       if (ctx->prevbits == NULL)
+               return SIGROK_ERR_MALLOC;
 
        return 0;
 }
@@ -117,7 +120,9 @@ static int event(struct output *o, int event_type, char **data_out,
        case DF_END:
                outlen = strlen("$dumpoff\n$end\n");
                outbuf = malloc(outlen + 1);
-               snprintf(outbuf, outlen, "$dumpoff\n$end\n");
+               if (outbuf == NULL)
+                       return SIGROK_ERR_MALLOC;
+               snprintf(outbuf, outlen + 1, "$dumpoff\n$end\n");
                *data_out = outbuf;
                *length_out = outlen;
                free(o->internal);
@@ -132,13 +137,16 @@ static int data(struct output *o, char *data_in, uint64_t length_in,
                char **data_out, uint64_t *length_out)
 {
        struct context *ctx;
-       int offset, outsize, p, curbit, prevbit;
+       unsigned int offset, outsize;
+       int p, curbit, prevbit;
        uint64_t sample, prevsample;
        char *outbuf, *c;
 
        ctx = o->internal;
        outsize = strlen(ctx->header);
        outbuf = calloc(1, outsize + 1 + 10000); // FIXME: Use realloc().
+       if (outbuf == NULL)
+               return SIGROK_ERR_MALLOC;
        if (ctx->header) {
                /* The header is still here, this must be the first packet. */
                strncpy(outbuf, ctx->header, outsize);