]>
Commit | Line | Data |
---|---|---|
25e7d9b1 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
25e7d9b1 UH |
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> | |
9e4f8cf9 | 24 | #include <errno.h> |
45c59c8b BV |
25 | #include "libsigrok.h" |
26 | #include "libsigrok-internal.h" | |
25e7d9b1 | 27 | |
29a27196 UH |
28 | /* Message logging helpers with subsystem-specific prefix string. */ |
29 | #define LOG_PREFIX "strutil: " | |
30 | #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args) | |
31 | #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args) | |
32 | #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args) | |
33 | #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args) | |
34 | #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args) | |
35 | #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args) | |
a885ce3e | 36 | |
393fb9cb UH |
37 | /** |
38 | * @file | |
39 | * | |
40 | * Helper functions for handling or converting libsigrok-related strings. | |
41 | */ | |
42 | ||
7b870c38 UH |
43 | /** |
44 | * @defgroup grp_strutil String utilities | |
45 | * | |
46 | * Helper functions for handling or converting libsigrok-related strings. | |
47 | * | |
48 | * @{ | |
49 | */ | |
50 | ||
9e4f8cf9 DJ |
51 | /** |
52 | * Convert a string representation of a numeric value to a long integer. The | |
53 | * conversion is strict and will fail if the complete string does not represent | |
54 | * a valid long integer. The function sets errno according to the details of the | |
55 | * failure. | |
56 | * | |
57 | * @param str The string representation to convert. | |
58 | * @param ret Pointer to long where the result of the conversion will be stored. | |
59 | * | |
60 | * @return SR_OK if conversion is successful, otherwise SR_ERR. | |
61 | * | |
62 | * @since 0.3.0 | |
63 | */ | |
8d558c7a | 64 | SR_PRIV int sr_atol(const char *str, long *ret) |
9e4f8cf9 DJ |
65 | { |
66 | long tmp; | |
67 | char *endptr = NULL; | |
68 | ||
69 | errno = 0; | |
70 | tmp = strtol(str, &endptr, 0); | |
71 | ||
72 | if (!endptr || *endptr || errno) { | |
73 | if (!errno) | |
74 | errno = EINVAL; | |
75 | return SR_ERR; | |
76 | } | |
77 | ||
78 | *ret = tmp; | |
79 | return SR_OK; | |
80 | } | |
81 | ||
82 | /** | |
83 | * Convert a string representation of a numeric value to an integer. The | |
84 | * conversion is strict and will fail if the complete string does not represent | |
85 | * a valid integer. The function sets errno according to the details of the | |
86 | * failure. | |
87 | * | |
88 | * @param str The string representation to convert. | |
89 | * @param ret Pointer to int where the result of the conversion will be stored. | |
90 | * | |
91 | * @return SR_OK if conversion is successful, otherwise SR_ERR. | |
92 | * | |
93 | * @since 0.3.0 | |
94 | */ | |
8d558c7a | 95 | SR_PRIV int sr_atoi(const char *str, int *ret) |
9e4f8cf9 DJ |
96 | { |
97 | long tmp; | |
98 | ||
99 | if (sr_atol(str, &tmp) != SR_OK) | |
100 | return SR_ERR; | |
101 | ||
102 | if ((int) tmp != tmp) { | |
103 | errno = ERANGE; | |
104 | return SR_ERR; | |
105 | } | |
106 | ||
107 | *ret = (int) tmp; | |
108 | return SR_OK; | |
109 | } | |
110 | ||
111 | /** | |
112 | * Convert a string representation of a numeric value to a double. The | |
113 | * conversion is strict and will fail if the complete string does not represent | |
114 | * a valid double. The function sets errno according to the details of the | |
115 | * failure. | |
116 | * | |
117 | * @param str The string representation to convert. | |
118 | * @param ret Pointer to double where the result of the conversion will be stored. | |
119 | * | |
120 | * @return SR_OK if conversion is successful, otherwise SR_ERR. | |
121 | * | |
122 | * @since 0.3.0 | |
123 | */ | |
8d558c7a | 124 | SR_PRIV int sr_atod(const char *str, double *ret) |
9e4f8cf9 DJ |
125 | { |
126 | double tmp; | |
127 | char *endptr = NULL; | |
128 | ||
129 | errno = 0; | |
130 | tmp = strtof(str, &endptr); | |
131 | ||
132 | if (!endptr || *endptr || errno) { | |
133 | if (!errno) | |
134 | errno = EINVAL; | |
135 | return SR_ERR; | |
136 | } | |
137 | ||
138 | *ret = tmp; | |
139 | return SR_OK; | |
140 | } | |
141 | ||
142 | /** | |
143 | * Convert a string representation of a numeric value to a float. The | |
144 | * conversion is strict and will fail if the complete string does not represent | |
145 | * a valid float. The function sets errno according to the details of the | |
146 | * failure. | |
147 | * | |
148 | * @param str The string representation to convert. | |
149 | * @param ret Pointer to float where the result of the conversion will be stored. | |
150 | * | |
151 | * @return SR_OK if conversion is successful, otherwise SR_ERR. | |
152 | * | |
153 | * @since 0.3.0 | |
154 | */ | |
8d558c7a | 155 | SR_PRIV int sr_atof(const char *str, float *ret) |
9e4f8cf9 DJ |
156 | { |
157 | double tmp; | |
158 | ||
159 | if (sr_atod(str, &tmp) != SR_OK) | |
160 | return SR_ERR; | |
161 | ||
162 | if ((float) tmp != tmp) { | |
163 | errno = ERANGE; | |
164 | return SR_ERR; | |
165 | } | |
166 | ||
167 | *ret = (float) tmp; | |
168 | return SR_OK; | |
169 | } | |
170 | ||
25e7d9b1 | 171 | /** |
b07b42f3 UH |
172 | * Convert a numeric value value to its "natural" string representation |
173 | * in SI units. | |
25e7d9b1 | 174 | * |
4cc9aea1 JH |
175 | * E.g. a value of 3000000, with units set to "W", would be converted |
176 | * to "3 MW", 20000 to "20 kW", 31500 would become "31.5 kW". | |
25e7d9b1 | 177 | * |
4cc9aea1 JH |
178 | * @param x The value to convert. |
179 | * @param unit The unit to append to the string, or NULL if the string | |
180 | * has no units. | |
44dae539 | 181 | * |
133a37bf UH |
182 | * @return A g_try_malloc()ed string representation of the samplerate value, |
183 | * or NULL upon errors. The caller is responsible to g_free() the | |
184 | * memory. | |
25e7d9b1 | 185 | */ |
4cc9aea1 | 186 | SR_API char *sr_si_string_u64(uint64_t x, const char *unit) |
25e7d9b1 | 187 | { |
094e6b81 PS |
188 | uint8_t i; |
189 | uint64_t quot, divisor[] = { | |
b07b42f3 UH |
190 | SR_HZ(1), SR_KHZ(1), SR_MHZ(1), SR_GHZ(1), |
191 | SR_GHZ(1000), SR_GHZ(1000 * 1000), SR_GHZ(1000 * 1000 * 1000), | |
094e6b81 PS |
192 | }; |
193 | const char *p, prefix[] = "\0kMGTPE"; | |
194 | char fmt[8], fract[20] = "", *f; | |
195 | ||
a885ce3e | 196 | if (unit == NULL) |
4cc9aea1 | 197 | unit = ""; |
25e7d9b1 | 198 | |
094e6b81 PS |
199 | for (i = 0; (quot = x / divisor[i]) >= 1000; i++); |
200 | ||
201 | if (i) { | |
202 | sprintf(fmt, ".%%0%dlu", i * 3); | |
203 | f = fract + sprintf(fract, fmt, x % divisor[i]) - 1; | |
204 | ||
205 | while (f >= fract && strchr("0.", *f)) | |
206 | *f-- = 0; | |
133a37bf | 207 | } |
25e7d9b1 | 208 | |
094e6b81 PS |
209 | p = prefix + i; |
210 | ||
211 | return g_strdup_printf("%" PRIu64 "%s %.1s%s", quot, fract, p, unit); | |
4cc9aea1 | 212 | } |
25e7d9b1 | 213 | |
4cc9aea1 JH |
214 | /** |
215 | * Convert a numeric samplerate value to its "natural" string representation. | |
216 | * | |
217 | * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz", | |
218 | * 31500 would become "31.5 kHz". | |
219 | * | |
220 | * @param samplerate The samplerate in Hz. | |
221 | * | |
222 | * @return A g_try_malloc()ed string representation of the samplerate value, | |
223 | * or NULL upon errors. The caller is responsible to g_free() the | |
224 | * memory. | |
225 | */ | |
226 | SR_API char *sr_samplerate_string(uint64_t samplerate) | |
227 | { | |
228 | return sr_si_string_u64(samplerate, "Hz"); | |
25e7d9b1 | 229 | } |
2a3f9541 | 230 | |
2a3f9541 | 231 | /** |
dfcc0bf9 | 232 | * Convert a numeric frequency value to the "natural" string representation |
2a3f9541 BV |
233 | * of its period. |
234 | * | |
2507648e | 235 | * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms". |
2a3f9541 BV |
236 | * |
237 | * @param frequency The frequency in Hz. | |
44dae539 | 238 | * |
133a37bf UH |
239 | * @return A g_try_malloc()ed string representation of the frequency value, |
240 | * or NULL upon errors. The caller is responsible to g_free() the | |
241 | * memory. | |
2a3f9541 | 242 | */ |
1a081ca6 | 243 | SR_API char *sr_period_string(uint64_t frequency) |
2a3f9541 BV |
244 | { |
245 | char *o; | |
246 | int r; | |
247 | ||
133a37bf UH |
248 | /* Allocate enough for a uint64_t as string + " ms". */ |
249 | if (!(o = g_try_malloc0(30 + 1))) { | |
a885ce3e | 250 | sr_err("%s: o malloc failed", __func__); |
2a3f9541 | 251 | return NULL; |
133a37bf | 252 | } |
2a3f9541 | 253 | |
59df0c77 | 254 | if (frequency >= SR_GHZ(1)) |
2a3f9541 | 255 | r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000); |
59df0c77 | 256 | else if (frequency >= SR_MHZ(1)) |
2507648e | 257 | r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000); |
59df0c77 | 258 | else if (frequency >= SR_KHZ(1)) |
2a3f9541 BV |
259 | r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000); |
260 | else | |
261 | r = snprintf(o, 30, "%" PRIu64 " s", frequency); | |
262 | ||
263 | if (r < 0) { | |
264 | /* Something went wrong... */ | |
133a37bf | 265 | g_free(o); |
2a3f9541 BV |
266 | return NULL; |
267 | } | |
268 | ||
269 | return o; | |
270 | } | |
40f5ddac | 271 | |
79afc8ca | 272 | /** |
e0e15067 BV |
273 | * Convert a numeric voltage value to the "natural" string representation |
274 | * of its voltage value. The voltage is specified as a rational number's | |
275 | * numerator and denominator. | |
79afc8ca BV |
276 | * |
277 | * E.g. a value of 300000 would be converted to "300mV", 2 to "2V". | |
278 | * | |
e0e15067 BV |
279 | * @param v_p The voltage numerator. |
280 | * @param v_q The voltage denominator. | |
79afc8ca BV |
281 | * |
282 | * @return A g_try_malloc()ed string representation of the voltage value, | |
283 | * or NULL upon errors. The caller is responsible to g_free() the | |
284 | * memory. | |
285 | */ | |
e0e15067 | 286 | SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q) |
79afc8ca | 287 | { |
79afc8ca | 288 | int r; |
d5a669a9 | 289 | char *o; |
79afc8ca BV |
290 | |
291 | if (!(o = g_try_malloc0(30 + 1))) { | |
a885ce3e | 292 | sr_err("%s: o malloc failed", __func__); |
79afc8ca BV |
293 | return NULL; |
294 | } | |
295 | ||
e0e15067 BV |
296 | if (v_q == 1000) |
297 | r = snprintf(o, 30, "%" PRIu64 "mV", v_p); | |
298 | else if (v_q == 1) | |
299 | r = snprintf(o, 30, "%" PRIu64 "V", v_p); | |
79afc8ca | 300 | else |
e0e15067 | 301 | r = snprintf(o, 30, "%gV", (float)v_p / (float)v_q); |
79afc8ca BV |
302 | |
303 | if (r < 0) { | |
304 | /* Something went wrong... */ | |
305 | g_free(o); | |
306 | return NULL; | |
307 | } | |
308 | ||
309 | return o; | |
310 | } | |
311 | ||
dfcc0bf9 | 312 | /** |
bf978d35 | 313 | * Parse a trigger specification string. |
dfcc0bf9 | 314 | * |
9c5332d2 UH |
315 | * @param sdi The device instance for which the trigger specification is |
316 | * intended. Must not be NULL. Also, sdi->driver and | |
317 | * sdi->driver->info_get must not be NULL. | |
bf978d35 UH |
318 | * @param triggerstring The string containing the trigger specification for |
319 | * one or more probes of this device. Entries for multiple probes are | |
320 | * comma-separated. Triggers are specified in the form key=value, | |
321 | * where the key is a probe number (or probe name) and the value is | |
322 | * the requested trigger type. Valid trigger types currently | |
323 | * include 'r' (rising edge), 'f' (falling edge), 'c' (any pin value | |
324 | * change), '0' (low value), or '1' (high value). | |
325 | * Example: "1=r,sck=f,miso=0,7=c" | |
44dae539 | 326 | * |
bf978d35 UH |
327 | * @return Pointer to a list of trigger types (strings), or NULL upon errors. |
328 | * The pointer list (if non-NULL) has as many entries as the | |
329 | * respective device has probes (all physically available probes, | |
330 | * not just enabled ones). Entries of the list which don't have | |
331 | * a trigger value set in 'triggerstring' are NULL, the other entries | |
332 | * contain the respective trigger type which is requested for the | |
333 | * respective probe (e.g. "r", "c", and so on). | |
dfcc0bf9 | 334 | */ |
92ae7984 | 335 | SR_API char **sr_parse_triggerstring(const struct sr_dev_inst *sdi, |
1a081ca6 | 336 | const char *triggerstring) |
40f5ddac BV |
337 | { |
338 | GSList *l; | |
003595ac | 339 | GVariant *gvar; |
1afe8989 | 340 | struct sr_probe *probe; |
40f5ddac | 341 | int max_probes, probenum, i; |
b7f578be JH |
342 | char **tokens, **triggerlist, *trigger, *tc; |
343 | const char *trigger_types; | |
40f5ddac BV |
344 | gboolean error; |
345 | ||
92ae7984 | 346 | max_probes = g_slist_length(sdi->probes); |
40f5ddac | 347 | error = FALSE; |
b53738ba UH |
348 | |
349 | if (!(triggerlist = g_try_malloc0(max_probes * sizeof(char *)))) { | |
a885ce3e | 350 | sr_err("%s: triggerlist malloc failed", __func__); |
b53738ba UH |
351 | return NULL; |
352 | } | |
353 | ||
8f996b89 ML |
354 | if (sdi->driver->config_list(SR_CONF_TRIGGER_TYPE, |
355 | &gvar, sdi, NULL) != SR_OK) { | |
a885ce3e | 356 | sr_err("%s: Device doesn't support any triggers.", __func__); |
40f5ddac | 357 | return NULL; |
bf978d35 | 358 | } |
003595ac | 359 | trigger_types = g_variant_get_string(gvar, NULL); |
40f5ddac | 360 | |
31fc1fbc | 361 | tokens = g_strsplit(triggerstring, ",", max_probes); |
40f5ddac | 362 | for (i = 0; tokens[i]; i++) { |
17ff1124 BV |
363 | probenum = -1; |
364 | for (l = sdi->probes; l; l = l->next) { | |
365 | probe = (struct sr_probe *)l->data; | |
366 | if (probe->enabled | |
367 | && !strncmp(probe->name, tokens[i], | |
368 | strlen(probe->name))) { | |
369 | probenum = probe->index; | |
370 | break; | |
40f5ddac | 371 | } |
40f5ddac BV |
372 | } |
373 | ||
31fc1fbc | 374 | if (probenum < 0 || probenum >= max_probes) { |
a885ce3e | 375 | sr_err("Invalid probe."); |
40f5ddac BV |
376 | error = TRUE; |
377 | break; | |
378 | } | |
379 | ||
380 | if ((trigger = strchr(tokens[i], '='))) { | |
381 | for (tc = ++trigger; *tc; tc++) { | |
382 | if (strchr(trigger_types, *tc) == NULL) { | |
a885ce3e | 383 | sr_err("Unsupported trigger " |
0aeb0ccd | 384 | "type '%c'.", *tc); |
40f5ddac BV |
385 | error = TRUE; |
386 | break; | |
387 | } | |
388 | } | |
389 | if (!error) | |
31fc1fbc | 390 | triggerlist[probenum] = g_strdup(trigger); |
40f5ddac BV |
391 | } |
392 | } | |
393 | g_strfreev(tokens); | |
003595ac | 394 | g_variant_unref(gvar); |
40f5ddac BV |
395 | |
396 | if (error) { | |
397 | for (i = 0; i < max_probes; i++) | |
66410a86 | 398 | g_free(triggerlist[i]); |
40f5ddac BV |
399 | g_free(triggerlist); |
400 | triggerlist = NULL; | |
401 | } | |
402 | ||
403 | return triggerlist; | |
404 | } | |
405 | ||
dfcc0bf9 UH |
406 | /** |
407 | * Convert a "natural" string representation of a size value to uint64_t. | |
408 | * | |
409 | * E.g. a value of "3k" or "3 K" would be converted to 3000, a value | |
410 | * of "15M" would be converted to 15000000. | |
411 | * | |
412 | * Value representations other than decimal (such as hex or octal) are not | |
413 | * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported. | |
414 | * Spaces (but not other whitespace) between value and suffix are allowed. | |
415 | * | |
416 | * @param sizestring A string containing a (decimal) size value. | |
f64c1414 | 417 | * @param size Pointer to uint64_t which will contain the string's size value. |
dfcc0bf9 | 418 | * |
44dae539 | 419 | * @return SR_OK upon success, SR_ERR upon errors. |
dfcc0bf9 | 420 | */ |
1a081ca6 | 421 | SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size) |
40f5ddac | 422 | { |
f64c1414 | 423 | int multiplier, done; |
40f5ddac BV |
424 | char *s; |
425 | ||
f64c1414 | 426 | *size = strtoull(sizestring, &s, 10); |
40f5ddac | 427 | multiplier = 0; |
f64c1414 BV |
428 | done = FALSE; |
429 | while (s && *s && multiplier == 0 && !done) { | |
40f5ddac BV |
430 | switch (*s) { |
431 | case ' ': | |
432 | break; | |
433 | case 'k': | |
434 | case 'K': | |
59df0c77 | 435 | multiplier = SR_KHZ(1); |
40f5ddac BV |
436 | break; |
437 | case 'm': | |
438 | case 'M': | |
59df0c77 | 439 | multiplier = SR_MHZ(1); |
40f5ddac BV |
440 | break; |
441 | case 'g': | |
442 | case 'G': | |
59df0c77 | 443 | multiplier = SR_GHZ(1); |
40f5ddac BV |
444 | break; |
445 | default: | |
f64c1414 BV |
446 | done = TRUE; |
447 | s--; | |
40f5ddac BV |
448 | } |
449 | s++; | |
450 | } | |
451 | if (multiplier > 0) | |
f64c1414 | 452 | *size *= multiplier; |
40f5ddac | 453 | |
f64c1414 BV |
454 | if (*s && strcasecmp(s, "Hz")) |
455 | return SR_ERR; | |
456 | ||
457 | return SR_OK; | |
40f5ddac BV |
458 | } |
459 | ||
dfcc0bf9 UH |
460 | /** |
461 | * Convert a "natural" string representation of a time value to an | |
462 | * uint64_t value in milliseconds. | |
463 | * | |
464 | * E.g. a value of "3s" or "3 s" would be converted to 3000, a value | |
465 | * of "15ms" would be converted to 15. | |
466 | * | |
467 | * Value representations other than decimal (such as hex or octal) are not | |
468 | * supported. Only lower-case "s" and "ms" time suffixes are supported. | |
469 | * Spaces (but not other whitespace) between value and suffix are allowed. | |
470 | * | |
471 | * @param timestring A string containing a (decimal) time value. | |
472 | * @return The string's time value as uint64_t, in milliseconds. | |
473 | * | |
6b2d8d3e UH |
474 | * @todo Add support for "m" (minutes) and others. |
475 | * @todo Add support for picoseconds? | |
476 | * @todo Allow both lower-case and upper-case? If no, document it. | |
dfcc0bf9 | 477 | */ |
1a081ca6 | 478 | SR_API uint64_t sr_parse_timestring(const char *timestring) |
40f5ddac BV |
479 | { |
480 | uint64_t time_msec; | |
481 | char *s; | |
482 | ||
6b2d8d3e UH |
483 | /* TODO: Error handling, logging. */ |
484 | ||
40f5ddac BV |
485 | time_msec = strtoull(timestring, &s, 10); |
486 | if (time_msec == 0 && s == timestring) | |
487 | return 0; | |
488 | ||
489 | if (s && *s) { | |
490 | while (*s == ' ') | |
491 | s++; | |
492 | if (!strcmp(s, "s")) | |
493 | time_msec *= 1000; | |
494 | else if (!strcmp(s, "ms")) | |
495 | ; /* redundant */ | |
496 | else | |
497 | return 0; | |
498 | } | |
499 | ||
500 | return time_msec; | |
501 | } | |
4d436e71 | 502 | |
1a081ca6 | 503 | SR_API gboolean sr_parse_boolstring(const char *boolstr) |
4d436e71 GM |
504 | { |
505 | if (!boolstr) | |
506 | return FALSE; | |
507 | ||
993526f8 BV |
508 | if (!g_ascii_strncasecmp(boolstr, "true", 4) || |
509 | !g_ascii_strncasecmp(boolstr, "yes", 3) || | |
510 | !g_ascii_strncasecmp(boolstr, "on", 2) || | |
511 | !g_ascii_strncasecmp(boolstr, "1", 1)) | |
4d436e71 GM |
512 | return TRUE; |
513 | ||
514 | return FALSE; | |
515 | } | |
76f4c610 | 516 | |
76e107d6 | 517 | SR_API int sr_parse_period(const char *periodstr, uint64_t *p, uint64_t *q) |
76f4c610 BV |
518 | { |
519 | char *s; | |
520 | ||
76e107d6 BV |
521 | *p = strtoull(periodstr, &s, 10); |
522 | if (*p == 0 && s == periodstr) | |
76f4c610 BV |
523 | /* No digits found. */ |
524 | return SR_ERR_ARG; | |
525 | ||
526 | if (s && *s) { | |
527 | while (*s == ' ') | |
528 | s++; | |
8c012adb | 529 | if (!strcmp(s, "fs")) |
76e107d6 | 530 | *q = 1000000000000000ULL; |
8c012adb | 531 | else if (!strcmp(s, "ps")) |
76e107d6 | 532 | *q = 1000000000000ULL; |
8c012adb | 533 | else if (!strcmp(s, "ns")) |
76e107d6 | 534 | *q = 1000000000ULL; |
76f4c610 | 535 | else if (!strcmp(s, "us")) |
76e107d6 | 536 | *q = 1000000; |
76f4c610 | 537 | else if (!strcmp(s, "ms")) |
76e107d6 | 538 | *q = 1000; |
76f4c610 | 539 | else if (!strcmp(s, "s")) |
76e107d6 | 540 | *q = 1; |
76f4c610 BV |
541 | else |
542 | /* Must have a time suffix. */ | |
543 | return SR_ERR_ARG; | |
544 | } | |
545 | ||
546 | return SR_OK; | |
547 | } | |
548 | ||
549 | ||
76e107d6 | 550 | SR_API int sr_parse_voltage(const char *voltstr, uint64_t *p, uint64_t *q) |
79afc8ca BV |
551 | { |
552 | char *s; | |
553 | ||
76e107d6 BV |
554 | *p = strtoull(voltstr, &s, 10); |
555 | if (*p == 0 && s == voltstr) | |
79afc8ca BV |
556 | /* No digits found. */ |
557 | return SR_ERR_ARG; | |
558 | ||
559 | if (s && *s) { | |
560 | while (*s == ' ') | |
561 | s++; | |
562 | if (!strcasecmp(s, "mv")) | |
76e107d6 | 563 | *q = 1000L; |
79afc8ca | 564 | else if (!strcasecmp(s, "v")) |
76e107d6 | 565 | *q = 1; |
79afc8ca BV |
566 | else |
567 | /* Must have a base suffix. */ | |
568 | return SR_ERR_ARG; | |
569 | } | |
570 | ||
571 | return SR_OK; | |
572 | } | |
573 | ||
7b870c38 | 574 | /** @} */ |