]> sigrok.org Git - libsigrok.git/blame - strutil.c
sr: Prefix log messages with subsystem string.
[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 *
38ba2522 30 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz".
25e7d9b1
UH
31 *
32 * @param samplerate The samplerate in Hz.
133a37bf
UH
33 * @return A g_try_malloc()ed string representation of the samplerate value,
34 * or NULL upon errors. The caller is responsible to g_free() the
35 * memory.
25e7d9b1 36 */
1a081ca6 37SR_API char *sr_samplerate_string(uint64_t samplerate)
25e7d9b1
UH
38{
39 char *o;
40 int r;
41
133a37bf
UH
42 /* Allocate enough for a uint64_t as string + " GHz". */
43 if (!(o = g_try_malloc0(30 + 1))) {
44 sr_err("strutil: %s: o malloc failed", __func__);
25e7d9b1 45 return NULL;
133a37bf 46 }
25e7d9b1 47
59df0c77 48 if (samplerate >= SR_GHZ(1))
99c1fc59 49 r = snprintf(o, 30, "%" PRIu64 " GHz", samplerate / 1000000000);
59df0c77 50 else if (samplerate >= SR_MHZ(1))
99c1fc59 51 r = snprintf(o, 30, "%" PRIu64 " MHz", samplerate / 1000000);
59df0c77 52 else if (samplerate >= SR_KHZ(1))
38ba2522 53 r = snprintf(o, 30, "%" PRIu64 " kHz", samplerate / 1000);
25e7d9b1 54 else
99c1fc59 55 r = snprintf(o, 30, "%" PRIu64 " Hz", samplerate);
25e7d9b1
UH
56
57 if (r < 0) {
58 /* Something went wrong... */
133a37bf 59 g_free(o);
25e7d9b1
UH
60 return NULL;
61 }
62
63 return o;
64}
2a3f9541 65
2a3f9541 66/**
dfcc0bf9 67 * Convert a numeric frequency value to the "natural" string representation
2a3f9541
BV
68 * of its period.
69 *
2507648e 70 * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
2a3f9541
BV
71 *
72 * @param frequency The frequency in Hz.
133a37bf
UH
73 * @return A g_try_malloc()ed string representation of the frequency value,
74 * or NULL upon errors. The caller is responsible to g_free() the
75 * memory.
2a3f9541 76 */
1a081ca6 77SR_API char *sr_period_string(uint64_t frequency)
2a3f9541
BV
78{
79 char *o;
80 int r;
81
133a37bf
UH
82 /* Allocate enough for a uint64_t as string + " ms". */
83 if (!(o = g_try_malloc0(30 + 1))) {
84 sr_err("strutil: %s: o malloc failed", __func__);
2a3f9541 85 return NULL;
133a37bf 86 }
2a3f9541 87
59df0c77 88 if (frequency >= SR_GHZ(1))
2a3f9541 89 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
59df0c77 90 else if (frequency >= SR_MHZ(1))
2507648e 91 r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000);
59df0c77 92 else if (frequency >= SR_KHZ(1))
2a3f9541
BV
93 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
94 else
95 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
96
97 if (r < 0) {
98 /* Something went wrong... */
133a37bf 99 g_free(o);
2a3f9541
BV
100 return NULL;
101 }
102
103 return o;
104}
40f5ddac 105
dfcc0bf9
UH
106/**
107 * TODO
108 *
109 * @param device TODO
110 * @param triggerstring TODO
111 * @return TODO
112 */
1a081ca6
UH
113SR_API char **sr_parse_triggerstring(struct sr_device *device,
114 const char *triggerstring)
40f5ddac
BV
115{
116 GSList *l;
1afe8989 117 struct sr_probe *probe;
40f5ddac
BV
118 int max_probes, probenum, i;
119 char **tokens, **triggerlist, *trigger, *tc, *trigger_types;
120 gboolean error;
121
122 max_probes = g_slist_length(device->probes);
123 error = FALSE;
b53738ba
UH
124
125 if (!(triggerlist = g_try_malloc0(max_probes * sizeof(char *)))) {
126 sr_err("session file: %s: metafile malloc failed", __func__);
127 return NULL;
128 }
129
40f5ddac
BV
130 tokens = g_strsplit(triggerstring, ",", max_probes);
131 trigger_types = device->plugin->get_device_info(0, SR_DI_TRIGGER_TYPES);
132 if (trigger_types == NULL)
133 return NULL;
134
135 for (i = 0; tokens[i]; i++) {
136 if (tokens[i][0] < '0' || tokens[i][0] > '9') {
137 /* Named probe */
138 probenum = 0;
139 for (l = device->probes; l; l = l->next) {
1afe8989 140 probe = (struct sr_probe *)l->data;
40f5ddac
BV
141 if (probe->enabled
142 && !strncmp(probe->name, tokens[i],
143 strlen(probe->name))) {
144 probenum = probe->index;
145 break;
146 }
147 }
148 } else {
149 probenum = strtol(tokens[i], NULL, 10);
150 }
151
152 if (probenum < 1 || probenum > max_probes) {
a562c3a2 153 sr_err("Invalid probe.\n");
40f5ddac
BV
154 error = TRUE;
155 break;
156 }
157
158 if ((trigger = strchr(tokens[i], '='))) {
159 for (tc = ++trigger; *tc; tc++) {
160 if (strchr(trigger_types, *tc) == NULL) {
a562c3a2 161 sr_err("Unsupported trigger type "
40f5ddac
BV
162 "'%c'\n", *tc);
163 error = TRUE;
164 break;
165 }
166 }
167 if (!error)
168 triggerlist[probenum - 1] = g_strdup(trigger);
169 }
170 }
171 g_strfreev(tokens);
172
173 if (error) {
174 for (i = 0; i < max_probes; i++)
66410a86 175 g_free(triggerlist[i]);
40f5ddac
BV
176 g_free(triggerlist);
177 triggerlist = NULL;
178 }
179
180 return triggerlist;
181}
182
dfcc0bf9
UH
183/**
184 * Convert a "natural" string representation of a size value to uint64_t.
185 *
186 * E.g. a value of "3k" or "3 K" would be converted to 3000, a value
187 * of "15M" would be converted to 15000000.
188 *
189 * Value representations other than decimal (such as hex or octal) are not
190 * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported.
191 * Spaces (but not other whitespace) between value and suffix are allowed.
192 *
193 * @param sizestring A string containing a (decimal) size value.
f64c1414
BV
194 * @param size Pointer to uint64_t which will contain the string's size value.
195 * @return SR_OK or error code
dfcc0bf9 196 *
dfcc0bf9 197 */
1a081ca6 198SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
40f5ddac 199{
f64c1414 200 int multiplier, done;
40f5ddac
BV
201 char *s;
202
f64c1414 203 *size = strtoull(sizestring, &s, 10);
40f5ddac 204 multiplier = 0;
f64c1414
BV
205 done = FALSE;
206 while (s && *s && multiplier == 0 && !done) {
40f5ddac
BV
207 switch (*s) {
208 case ' ':
209 break;
210 case 'k':
211 case 'K':
59df0c77 212 multiplier = SR_KHZ(1);
40f5ddac
BV
213 break;
214 case 'm':
215 case 'M':
59df0c77 216 multiplier = SR_MHZ(1);
40f5ddac
BV
217 break;
218 case 'g':
219 case 'G':
59df0c77 220 multiplier = SR_GHZ(1);
40f5ddac
BV
221 break;
222 default:
f64c1414
BV
223 done = TRUE;
224 s--;
40f5ddac
BV
225 }
226 s++;
227 }
228 if (multiplier > 0)
f64c1414 229 *size *= multiplier;
40f5ddac 230
f64c1414
BV
231 if (*s && strcasecmp(s, "Hz"))
232 return SR_ERR;
233
234 return SR_OK;
40f5ddac
BV
235}
236
dfcc0bf9
UH
237/**
238 * Convert a "natural" string representation of a time value to an
239 * uint64_t value in milliseconds.
240 *
241 * E.g. a value of "3s" or "3 s" would be converted to 3000, a value
242 * of "15ms" would be converted to 15.
243 *
244 * Value representations other than decimal (such as hex or octal) are not
245 * supported. Only lower-case "s" and "ms" time suffixes are supported.
246 * Spaces (but not other whitespace) between value and suffix are allowed.
247 *
248 * @param timestring A string containing a (decimal) time value.
249 * @return The string's time value as uint64_t, in milliseconds.
250 *
251 * TODO: Error handling.
252 * TODO: Add support for "m" (minutes) and others.
253 * TODO: picoseconds?
254 * TODO: Allow both lower-case and upper-case.
255 */
1a081ca6 256SR_API uint64_t sr_parse_timestring(const char *timestring)
40f5ddac
BV
257{
258 uint64_t time_msec;
259 char *s;
260
261 time_msec = strtoull(timestring, &s, 10);
262 if (time_msec == 0 && s == timestring)
263 return 0;
264
265 if (s && *s) {
266 while (*s == ' ')
267 s++;
268 if (!strcmp(s, "s"))
269 time_msec *= 1000;
270 else if (!strcmp(s, "ms"))
271 ; /* redundant */
272 else
273 return 0;
274 }
275
276 return time_msec;
277}
4d436e71 278
1a081ca6 279SR_API gboolean sr_parse_boolstring(const char *boolstr)
4d436e71
GM
280{
281 if (!boolstr)
282 return FALSE;
283
1a081ca6 284 if (!g_strcasecmp(boolstr, "true") ||
4d436e71
GM
285 !g_strcasecmp(boolstr, "yes") ||
286 !g_strcasecmp(boolstr, "on") ||
1a081ca6 287 !g_strcasecmp(boolstr, "1"))
4d436e71
GM
288 return TRUE;
289
290 return FALSE;
291}