]> sigrok.org Git - libsigrok.git/blame - strutil.c
sr: fx2lafw: Cosmetics.
[libsigrok.git] / strutil.c
CommitLineData
25e7d9b1
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdint.h>
22#include <stdlib.h>
23#include <string.h>
b7f09cf8
UH
24#include "sigrok.h"
25#include "sigrok-internal.h"
25e7d9b1
UH
26
27/**
28 * Convert a numeric samplerate value to its "natural" string representation.
29 *
c69e35a7
UH
30 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz",
31 * 31500 would become "31.5 kHz".
25e7d9b1
UH
32 *
33 * @param samplerate The samplerate in Hz.
44dae539 34 *
133a37bf
UH
35 * @return A g_try_malloc()ed string representation of the samplerate value,
36 * or NULL upon errors. The caller is responsible to g_free() the
37 * memory.
25e7d9b1 38 */
1a081ca6 39SR_API char *sr_samplerate_string(uint64_t samplerate)
25e7d9b1
UH
40{
41 char *o;
c69e35a7 42 uint64_t s = samplerate;
25e7d9b1 43
c69e35a7
UH
44 if ((s >= SR_GHZ(1)) && (s % SR_GHZ(1) == 0)) {
45 o = g_strdup_printf("%" PRIu64 " GHz", s / SR_GHZ(1));
46 } else if ((s >= SR_GHZ(1)) && (s % SR_GHZ(1) != 0)) {
47 o = g_strdup_printf("%" PRIu64 ".%" PRIu64 " GHz",
48 s / SR_GHZ(1), s % SR_GHZ(1));
49 } else if ((s >= SR_MHZ(1)) && (s % SR_MHZ(1) == 0)) {
50 o = g_strdup_printf("%" PRIu64 " MHz", s / SR_MHZ(1));
51 } else if ((s >= SR_MHZ(1)) && (s % SR_MHZ(1) != 0)) {
52 o = g_strdup_printf("%" PRIu64 ".%" PRIu64 " MHz",
53 s / SR_MHZ(1), s % SR_MHZ(1));
54 } else if ((s >= SR_KHZ(1)) && (s % SR_KHZ(1) == 0)) {
55 o = g_strdup_printf("%" PRIu64 " kHz", s / SR_KHZ(1));
56 } else if ((s >= SR_KHZ(1)) && (s % SR_KHZ(1) != 0)) {
57 o = g_strdup_printf("%" PRIu64 ".%" PRIu64 " kHz",
58 s / SR_KHZ(1), s % SR_KHZ(1));
59 } else {
60 o = g_strdup_printf("%" PRIu64 " Hz", s);
133a37bf 61 }
25e7d9b1 62
c69e35a7
UH
63 if (!o) {
64 sr_err("strutil: %s: Error creating samplerate string.",
65 __func__);
25e7d9b1
UH
66 return NULL;
67 }
68
69 return o;
70}
2a3f9541 71
2a3f9541 72/**
dfcc0bf9 73 * Convert a numeric frequency value to the "natural" string representation
2a3f9541
BV
74 * of its period.
75 *
2507648e 76 * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
2a3f9541
BV
77 *
78 * @param frequency The frequency in Hz.
44dae539 79 *
133a37bf
UH
80 * @return A g_try_malloc()ed string representation of the frequency value,
81 * or NULL upon errors. The caller is responsible to g_free() the
82 * memory.
2a3f9541 83 */
1a081ca6 84SR_API char *sr_period_string(uint64_t frequency)
2a3f9541
BV
85{
86 char *o;
87 int r;
88
133a37bf
UH
89 /* Allocate enough for a uint64_t as string + " ms". */
90 if (!(o = g_try_malloc0(30 + 1))) {
91 sr_err("strutil: %s: o malloc failed", __func__);
2a3f9541 92 return NULL;
133a37bf 93 }
2a3f9541 94
59df0c77 95 if (frequency >= SR_GHZ(1))
2a3f9541 96 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
59df0c77 97 else if (frequency >= SR_MHZ(1))
2507648e 98 r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000);
59df0c77 99 else if (frequency >= SR_KHZ(1))
2a3f9541
BV
100 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
101 else
102 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
103
104 if (r < 0) {
105 /* Something went wrong... */
133a37bf 106 g_free(o);
2a3f9541
BV
107 return NULL;
108 }
109
110 return o;
111}
40f5ddac 112
dfcc0bf9
UH
113/**
114 * TODO
115 *
bb7ef793 116 * @param dev TODO
dfcc0bf9 117 * @param triggerstring TODO
44dae539 118 *
dfcc0bf9
UH
119 * @return TODO
120 */
bb7ef793 121SR_API char **sr_parse_triggerstring(struct sr_dev *dev,
1a081ca6 122 const char *triggerstring)
40f5ddac
BV
123{
124 GSList *l;
1afe8989 125 struct sr_probe *probe;
40f5ddac
BV
126 int max_probes, probenum, i;
127 char **tokens, **triggerlist, *trigger, *tc, *trigger_types;
128 gboolean error;
129
bb7ef793 130 max_probes = g_slist_length(dev->probes);
40f5ddac 131 error = FALSE;
b53738ba
UH
132
133 if (!(triggerlist = g_try_malloc0(max_probes * sizeof(char *)))) {
134 sr_err("session file: %s: metafile malloc failed", __func__);
135 return NULL;
136 }
137
40f5ddac 138 tokens = g_strsplit(triggerstring, ",", max_probes);
c09f0b57 139 trigger_types = dev->driver->dev_info_get(0, SR_DI_TRIGGER_TYPES);
40f5ddac
BV
140 if (trigger_types == NULL)
141 return NULL;
142
143 for (i = 0; tokens[i]; i++) {
144 if (tokens[i][0] < '0' || tokens[i][0] > '9') {
145 /* Named probe */
146 probenum = 0;
bb7ef793 147 for (l = dev->probes; l; l = l->next) {
1afe8989 148 probe = (struct sr_probe *)l->data;
40f5ddac
BV
149 if (probe->enabled
150 && !strncmp(probe->name, tokens[i],
151 strlen(probe->name))) {
152 probenum = probe->index;
153 break;
154 }
155 }
156 } else {
157 probenum = strtol(tokens[i], NULL, 10);
158 }
159
160 if (probenum < 1 || probenum > max_probes) {
a562c3a2 161 sr_err("Invalid probe.\n");
40f5ddac
BV
162 error = TRUE;
163 break;
164 }
165
166 if ((trigger = strchr(tokens[i], '='))) {
167 for (tc = ++trigger; *tc; tc++) {
168 if (strchr(trigger_types, *tc) == NULL) {
a562c3a2 169 sr_err("Unsupported trigger type "
40f5ddac
BV
170 "'%c'\n", *tc);
171 error = TRUE;
172 break;
173 }
174 }
175 if (!error)
176 triggerlist[probenum - 1] = g_strdup(trigger);
177 }
178 }
179 g_strfreev(tokens);
180
181 if (error) {
182 for (i = 0; i < max_probes; i++)
66410a86 183 g_free(triggerlist[i]);
40f5ddac
BV
184 g_free(triggerlist);
185 triggerlist = NULL;
186 }
187
188 return triggerlist;
189}
190
dfcc0bf9
UH
191/**
192 * Convert a "natural" string representation of a size value to uint64_t.
193 *
194 * E.g. a value of "3k" or "3 K" would be converted to 3000, a value
195 * of "15M" would be converted to 15000000.
196 *
197 * Value representations other than decimal (such as hex or octal) are not
198 * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported.
199 * Spaces (but not other whitespace) between value and suffix are allowed.
200 *
201 * @param sizestring A string containing a (decimal) size value.
f64c1414 202 * @param size Pointer to uint64_t which will contain the string's size value.
dfcc0bf9 203 *
44dae539 204 * @return SR_OK upon success, SR_ERR upon errors.
dfcc0bf9 205 */
1a081ca6 206SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
40f5ddac 207{
f64c1414 208 int multiplier, done;
40f5ddac
BV
209 char *s;
210
f64c1414 211 *size = strtoull(sizestring, &s, 10);
40f5ddac 212 multiplier = 0;
f64c1414
BV
213 done = FALSE;
214 while (s && *s && multiplier == 0 && !done) {
40f5ddac
BV
215 switch (*s) {
216 case ' ':
217 break;
218 case 'k':
219 case 'K':
59df0c77 220 multiplier = SR_KHZ(1);
40f5ddac
BV
221 break;
222 case 'm':
223 case 'M':
59df0c77 224 multiplier = SR_MHZ(1);
40f5ddac
BV
225 break;
226 case 'g':
227 case 'G':
59df0c77 228 multiplier = SR_GHZ(1);
40f5ddac
BV
229 break;
230 default:
f64c1414
BV
231 done = TRUE;
232 s--;
40f5ddac
BV
233 }
234 s++;
235 }
236 if (multiplier > 0)
f64c1414 237 *size *= multiplier;
40f5ddac 238
f64c1414
BV
239 if (*s && strcasecmp(s, "Hz"))
240 return SR_ERR;
241
242 return SR_OK;
40f5ddac
BV
243}
244
dfcc0bf9
UH
245/**
246 * Convert a "natural" string representation of a time value to an
247 * uint64_t value in milliseconds.
248 *
249 * E.g. a value of "3s" or "3 s" would be converted to 3000, a value
250 * of "15ms" would be converted to 15.
251 *
252 * Value representations other than decimal (such as hex or octal) are not
253 * supported. Only lower-case "s" and "ms" time suffixes are supported.
254 * Spaces (but not other whitespace) between value and suffix are allowed.
255 *
256 * @param timestring A string containing a (decimal) time value.
257 * @return The string's time value as uint64_t, in milliseconds.
258 *
259 * TODO: Error handling.
260 * TODO: Add support for "m" (minutes) and others.
261 * TODO: picoseconds?
262 * TODO: Allow both lower-case and upper-case.
263 */
1a081ca6 264SR_API uint64_t sr_parse_timestring(const char *timestring)
40f5ddac
BV
265{
266 uint64_t time_msec;
267 char *s;
268
269 time_msec = strtoull(timestring, &s, 10);
270 if (time_msec == 0 && s == timestring)
271 return 0;
272
273 if (s && *s) {
274 while (*s == ' ')
275 s++;
276 if (!strcmp(s, "s"))
277 time_msec *= 1000;
278 else if (!strcmp(s, "ms"))
279 ; /* redundant */
280 else
281 return 0;
282 }
283
284 return time_msec;
285}
4d436e71 286
1a081ca6 287SR_API gboolean sr_parse_boolstring(const char *boolstr)
4d436e71
GM
288{
289 if (!boolstr)
290 return FALSE;
291
1a081ca6 292 if (!g_strcasecmp(boolstr, "true") ||
4d436e71
GM
293 !g_strcasecmp(boolstr, "yes") ||
294 !g_strcasecmp(boolstr, "on") ||
1a081ca6 295 !g_strcasecmp(boolstr, "1"))
4d436e71
GM
296 return TRUE;
297
298 return FALSE;
299}