From: Uwe Hermann Date: Wed, 7 Apr 2010 17:43:41 +0000 (+0200) Subject: Factor out common sigrok_samplerate_string(). X-Git-Tag: libsigrok-0.1.0~581 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=25e7d9b115e5ea08be2d92ffe286aa1bf95778f4;p=libsigrok.git Factor out common sigrok_samplerate_string(). --- diff --git a/Makefile.am b/Makefile.am index e0d0a4af..62c2e259 100644 --- a/Makefile.am +++ b/Makefile.am @@ -41,6 +41,7 @@ libsigrok_la_SOURCES = \ output/output_text.c \ output/output_vcd.c \ output/output_gnuplot.c \ + output/common.c \ output/output.c libsigrok_la_LIBADD = $(LIBOBJS) diff --git a/output/common.c b/output/common.c new file mode 100644 index 00000000..adb70e1a --- /dev/null +++ b/output/common.c @@ -0,0 +1,60 @@ +/* + * This file is part of the sigrok project. + * + * Copyright (C) 2010 Uwe Hermann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include + +/** + * Convert a numeric samplerate value to its "natural" string representation. + * + * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 KHz". + * + * @param samplerate The samplerate in Hz. + * @return A malloc()ed string representation of the samplerate value, + * or NULL upon errors. The caller is responsible to free() the memory. + */ +char *sigrok_samplerate_string(uint64_t samplerate) +{ + char *o; + int r; + + o = malloc(30 + 1); /* Enough for a uint64_t as string + " GHz". */ + if (o == NULL) + return NULL; + + if (samplerate >= GHZ(1)) + r = snprintf(o, 30, "%"PRIu64" GHz", samplerate / 1000000000); + else if (samplerate >= MHZ(1)) + r = snprintf(o, 30, "%"PRIu64" MHz", samplerate / 1000000); + else if (samplerate >= KHZ(1)) + r = snprintf(o, 30, "%"PRIu64" KHz", samplerate / 1000); + else + r = snprintf(o, 30, "%"PRIu64" Hz", samplerate); + + if (r < 0) { + /* Something went wrong... */ + free(o); + return NULL; + } + + return o; +} diff --git a/output/output_gnuplot.c b/output/output_gnuplot.c index 25306d8d..e79bd9f5 100644 --- a/output/output_gnuplot.c +++ b/output/output_gnuplot.c @@ -49,8 +49,8 @@ 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) @@ -75,16 +75,9 @@ static int init(struct output *o) /* 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 /* Columns / channels */ wbuf[0] = '\0'; @@ -97,7 +90,9 @@ static int init(struct output *o) /* TODO: Timescale */ b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header, PACKAGE_STRING, "TODO", ctx->num_enabled_probes, - num_probes, (char *)&sbuf, 1, "ns", (char *)&wbuf); + num_probes, samplerate_s, 1, "ns", (char *)&wbuf); + + free(samplerate_s); /* TODO: Handle snprintf errors. */ diff --git a/output/output_skeleton.c b/output/output_skeleton.c index 24cada4a..4a3f280e 100644 --- a/output/output_skeleton.c +++ b/output/output_skeleton.c @@ -18,11 +18,8 @@ */ #include - #include "sigrok.h" - - static int init(struct output *o) { return 0; diff --git a/output/output_text.c b/output/output_text.c index 914e51fc..ae7a75a9 100644 --- a/output/output_text.c +++ b/output/output_text.c @@ -21,7 +21,7 @@ #include #include #include -#include "sigrok.h" +#include #define DEFAULT_BPL_BIN 64 #define DEFAULT_BPL_HEX 256 @@ -73,6 +73,7 @@ static int init(struct output *o, int default_spl) GSList *l; uint64_t samplerate; int num_probes; + char *samplerate_s; ctx = malloc(sizeof(struct context)); o->internal = ctx; @@ -95,15 +96,11 @@ static int init(struct output *o, int default_spl) num_probes = g_slist_length(o->device->probes); samplerate = *((uint64_t *) o->device->plugin->get_device_info(o->device->plugin_index, DI_CUR_SAMPLERATE)); snprintf(ctx->header, 512, "Acquisition with %d/%d probes at ", ctx->num_enabled_probes, num_probes); - if(samplerate >= GHZ(1)) - snprintf(ctx->header + strlen(ctx->header), 512, "%"PRIu64" GHz", samplerate / 1000000000); - else if(samplerate >= MHZ(1)) - snprintf(ctx->header + strlen(ctx->header), 512, "%"PRIu64" MHz", samplerate / 1000000); - else if(samplerate >= KHZ(1)) - snprintf(ctx->header + strlen(ctx->header), 512, "%"PRIu64" KHz", samplerate / 1000); - else - snprintf(ctx->header + strlen(ctx->header), 512, "%"PRIu64" Hz", samplerate); - snprintf(ctx->header + strlen(ctx->header), 512, "\n"); + + if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL) + return -1; // FIXME + snprintf(ctx->header + strlen(ctx->header), 512, "%s\n", samplerate_s); + free(samplerate_s); ctx->linebuf_len = ctx->samples_per_line * 2; ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len); diff --git a/output/output_vcd.c b/output/output_vcd.c index 0122b615..767e398d 100644 --- a/output/output_vcd.c +++ b/output/output_vcd.c @@ -53,8 +53,8 @@ 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) @@ -80,15 +80,8 @@ static int init(struct output *o) 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'; @@ -101,9 +94,11 @@ static int init(struct output *o) /* 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; diff --git a/sigrok.h b/sigrok.h index 50fd0716..89109afd 100644 --- a/sigrok.h +++ b/sigrok.h @@ -127,6 +127,8 @@ int filter_probes(int in_unitsize, int out_unitsize, int *probelist, char *data_in, uint64_t length_in, char **data_out, uint64_t *length_out); +char *sigrok_samplerate_string(uint64_t samplerate); + /*--- analyzer.c ------------------------------------------------------------*/ struct analyzer {