blob: 0306ea7c91f940002861e2ce114654e6a82b0b39 [file] [log] [blame]
Bert Vermeulen40f5dda2011-01-31 22:29:40 +01001/*
Uwe Hermann50985c22013-04-23 22:24:30 +02002 * This file is part of the libsigrok project.
Bert Vermeulen40f5dda2011-01-31 22:29:40 +01003 *
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>
poljar (Damir Jelić)9e4f8cf2013-11-01 18:40:06 +010024#include <errno.h>
Bert Vermeulen45c59c82012-07-05 00:55:07 +020025#include "libsigrok.h"
26#include "libsigrok-internal.h"
Bert Vermeulen40f5dda2011-01-31 22:29:40 +010027
Uwe Hermann2ad1deb2014-04-25 18:40:59 +020028/** @cond PRIVATE */
Martin Ling3544f842013-12-23 03:38:35 +000029#define LOG_PREFIX "strutil"
Uwe Hermann2ad1deb2014-04-25 18:40:59 +020030/** @endcond */
Uwe Hermanna885ce32012-11-11 12:44:16 +010031
Bert Vermeulen40f5dda2011-01-31 22:29:40 +010032/**
Uwe Hermann393fb9c2012-10-22 00:30:12 +020033 * @file
34 *
35 * Helper functions for handling or converting libsigrok-related strings.
36 */
37
38/**
Uwe Hermann7b870c32012-10-21 16:13:36 +020039 * @defgroup grp_strutil String utilities
40 *
41 * Helper functions for handling or converting libsigrok-related strings.
42 *
43 * @{
44 */
45
46/**
Uwe Hermanndf823ac2013-12-27 16:18:28 +010047 * @private
48 *
Matthias Heidbrink1ba4a1c2014-05-19 19:40:53 +020049 * Convert a string representation of a numeric value (base 10) to a long integer. The
poljar (Damir Jelić)9e4f8cf2013-11-01 18:40:06 +010050 * conversion is strict and will fail if the complete string does not represent
51 * a valid long integer. The function sets errno according to the details of the
52 * failure.
53 *
54 * @param str The string representation to convert.
55 * @param ret Pointer to long where the result of the conversion will be stored.
56 *
Matthias Heidbrink1ba4a1c2014-05-19 19:40:53 +020057 * @retval SR_OK Conversion successful.
58 * @retval SR_ERR Failure.
poljar (Damir Jelić)9e4f8cf2013-11-01 18:40:06 +010059 *
60 * @since 0.3.0
61 */
Uwe Hermann8d558c72013-12-03 17:16:59 +010062SR_PRIV int sr_atol(const char *str, long *ret)
poljar (Damir Jelić)9e4f8cf2013-11-01 18:40:06 +010063{
64 long tmp;
65 char *endptr = NULL;
66
67 errno = 0;
Matthias Heidbrink1ba4a1c2014-05-19 19:40:53 +020068 tmp = strtol(str, &endptr, 10);
poljar (Damir Jelić)9e4f8cf2013-11-01 18:40:06 +010069
70 if (!endptr || *endptr || errno) {
71 if (!errno)
72 errno = EINVAL;
73 return SR_ERR;
74 }
75
76 *ret = tmp;
77 return SR_OK;
78}
79
80/**
Uwe Hermanndf823ac2013-12-27 16:18:28 +010081 * @private
82 *
Matthias Heidbrink1ba4a1c2014-05-19 19:40:53 +020083 * Convert a string representation of a numeric value (base 10) to an integer. The
poljar (Damir Jelić)9e4f8cf2013-11-01 18:40:06 +010084 * 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 *
Matthias Heidbrink1ba4a1c2014-05-19 19:40:53 +020091 * @retval SR_OK Conversion successful.
92 * @retval SR_ERR Failure.
poljar (Damir Jelić)9e4f8cf2013-11-01 18:40:06 +010093 *
94 * @since 0.3.0
95 */
Uwe Hermann8d558c72013-12-03 17:16:59 +010096SR_PRIV int sr_atoi(const char *str, int *ret)
poljar (Damir Jelić)9e4f8cf2013-11-01 18:40:06 +010097{
98 long tmp;
99
100 if (sr_atol(str, &tmp) != SR_OK)
101 return SR_ERR;
102
103 if ((int) tmp != tmp) {
104 errno = ERANGE;
105 return SR_ERR;
106 }
107
108 *ret = (int) tmp;
109 return SR_OK;
110}
111
112/**
Uwe Hermanndf823ac2013-12-27 16:18:28 +0100113 * @private
114 *
poljar (Damir Jelić)9e4f8cf2013-11-01 18:40:06 +0100115 * Convert a string representation of a numeric value to a double. The
116 * conversion is strict and will fail if the complete string does not represent
117 * a valid double. The function sets errno according to the details of the
118 * failure.
119 *
120 * @param str The string representation to convert.
121 * @param ret Pointer to double where the result of the conversion will be stored.
122 *
Matthias Heidbrink1ba4a1c2014-05-19 19:40:53 +0200123 * @retval SR_OK Conversion successful.
124 * @retval SR_ERR Failure.
poljar (Damir Jelić)9e4f8cf2013-11-01 18:40:06 +0100125 *
126 * @since 0.3.0
127 */
Uwe Hermann8d558c72013-12-03 17:16:59 +0100128SR_PRIV int sr_atod(const char *str, double *ret)
poljar (Damir Jelić)9e4f8cf2013-11-01 18:40:06 +0100129{
130 double tmp;
131 char *endptr = NULL;
132
133 errno = 0;
134 tmp = strtof(str, &endptr);
135
136 if (!endptr || *endptr || errno) {
137 if (!errno)
138 errno = EINVAL;
139 return SR_ERR;
140 }
141
142 *ret = tmp;
143 return SR_OK;
144}
145
146/**
Uwe Hermanndf823ac2013-12-27 16:18:28 +0100147 * @private
148 *
poljar (Damir Jelić)9e4f8cf2013-11-01 18:40:06 +0100149 * Convert a string representation of a numeric value to a float. The
150 * conversion is strict and will fail if the complete string does not represent
151 * a valid float. The function sets errno according to the details of the
152 * failure.
153 *
154 * @param str The string representation to convert.
155 * @param ret Pointer to float where the result of the conversion will be stored.
156 *
Matthias Heidbrink1ba4a1c2014-05-19 19:40:53 +0200157 * @retval SR_OK Conversion successful.
158 * @retval SR_ERR Failure.
poljar (Damir Jelić)9e4f8cf2013-11-01 18:40:06 +0100159 *
160 * @since 0.3.0
161 */
Uwe Hermann8d558c72013-12-03 17:16:59 +0100162SR_PRIV int sr_atof(const char *str, float *ret)
poljar (Damir Jelić)9e4f8cf2013-11-01 18:40:06 +0100163{
164 double tmp;
165
166 if (sr_atod(str, &tmp) != SR_OK)
167 return SR_ERR;
168
169 if ((float) tmp != tmp) {
170 errno = ERANGE;
171 return SR_ERR;
172 }
173
174 *ret = (float) tmp;
175 return SR_OK;
176}
177
178/**
poljar (Damir Jelić)9806c2d2014-01-16 15:28:48 +0100179 * @private
180 *
181 * Convert a string representation of a numeric value to a float. The
182 * conversion is strict and will fail if the complete string does not represent
183 * a valid float. The function sets errno according to the details of the
184 * failure. This version ignores the locale.
185 *
186 * @param str The string representation to convert.
187 * @param ret Pointer to float where the result of the conversion will be stored.
188 *
Matthias Heidbrink1ba4a1c2014-05-19 19:40:53 +0200189 * @retval SR_OK Conversion successful.
190 * @retval SR_ERR Failure.
poljar (Damir Jelić)9806c2d2014-01-16 15:28:48 +0100191 *
192 * @since 0.3.0
193 */
194SR_PRIV int sr_atof_ascii(const char *str, float *ret)
195{
196 double tmp;
197 char *endptr = NULL;
198
199 errno = 0;
200 tmp = g_ascii_strtod(str, &endptr);
201
202 if (!endptr || *endptr || errno) {
203 if (!errno)
204 errno = EINVAL;
205 return SR_ERR;
206 }
207
208 /* FIXME This fails unexpectedly. Some other method to safel downcast
209 * needs to be found. Checking against FLT_MAX doesn't work as well. */
210 /*
211 if ((float) tmp != tmp) {
212 errno = ERANGE;
213 sr_dbg("ERANGEEEE %e != %e", (float) tmp, tmp);
214 return SR_ERR;
215 }
216 */
217
218 *ret = (float) tmp;
219 return SR_OK;
220}
221
222/**
Uwe Hermannb07b42f2013-08-07 00:18:36 +0200223 * Convert a numeric value value to its "natural" string representation
224 * in SI units.
Joel Holdsworth4cc9aea2012-07-07 09:32:00 +0100225 *
226 * E.g. a value of 3000000, with units set to "W", would be converted
227 * to "3 MW", 20000 to "20 kW", 31500 would become "31.5 kW".
228 *
229 * @param x The value to convert.
230 * @param unit The unit to append to the string, or NULL if the string
231 * has no units.
232 *
233 * @return A g_try_malloc()ed string representation of the samplerate value,
234 * or NULL upon errors. The caller is responsible to g_free() the
235 * memory.
Uwe Hermann47117242014-05-04 20:51:05 +0200236 *
237 * @since 0.2.0
Joel Holdsworth4cc9aea2012-07-07 09:32:00 +0100238 */
239SR_API char *sr_si_string_u64(uint64_t x, const char *unit)
240{
Peter Stuge094e6b82013-06-04 02:20:57 +0200241 uint8_t i;
242 uint64_t quot, divisor[] = {
Uwe Hermannb07b42f2013-08-07 00:18:36 +0200243 SR_HZ(1), SR_KHZ(1), SR_MHZ(1), SR_GHZ(1),
244 SR_GHZ(1000), SR_GHZ(1000 * 1000), SR_GHZ(1000 * 1000 * 1000),
Peter Stuge094e6b82013-06-04 02:20:57 +0200245 };
246 const char *p, prefix[] = "\0kMGTPE";
Bert Vermeulen69d83be2014-01-13 22:22:40 +0100247 char fmt[16], fract[20] = "", *f;
Peter Stuge094e6b82013-06-04 02:20:57 +0200248
Uwe Hermanna885ce32012-11-11 12:44:16 +0100249 if (unit == NULL)
Joel Holdsworth4cc9aea2012-07-07 09:32:00 +0100250 unit = "";
251
Peter Stuge094e6b82013-06-04 02:20:57 +0200252 for (i = 0; (quot = x / divisor[i]) >= 1000; i++);
253
254 if (i) {
Bert Vermeulen69d83be2014-01-13 22:22:40 +0100255 sprintf(fmt, ".%%0%d"PRIu64, i * 3);
Peter Stuge094e6b82013-06-04 02:20:57 +0200256 f = fract + sprintf(fract, fmt, x % divisor[i]) - 1;
257
258 while (f >= fract && strchr("0.", *f))
259 *f-- = 0;
Joel Holdsworth4cc9aea2012-07-07 09:32:00 +0100260 }
261
Peter Stuge094e6b82013-06-04 02:20:57 +0200262 p = prefix + i;
263
264 return g_strdup_printf("%" PRIu64 "%s %.1s%s", quot, fract, p, unit);
Joel Holdsworth4cc9aea2012-07-07 09:32:00 +0100265}
266
267/**
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100268 * Convert a numeric samplerate value to its "natural" string representation.
269 *
Uwe Hermannc69e35a2012-02-27 22:27:33 +0100270 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz",
271 * 31500 would become "31.5 kHz".
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100272 *
273 * @param samplerate The samplerate in Hz.
Uwe Hermann44dae532012-02-17 20:44:19 +0100274 *
Uwe Hermann133a37b2012-02-11 20:06:46 +0100275 * @return A g_try_malloc()ed string representation of the samplerate value,
276 * or NULL upon errors. The caller is responsible to g_free() the
277 * memory.
Uwe Hermann47117242014-05-04 20:51:05 +0200278 *
279 * @since 0.1.0
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100280 */
Uwe Hermann1a081ca2012-02-01 23:40:35 +0100281SR_API char *sr_samplerate_string(uint64_t samplerate)
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100282{
Joel Holdsworth4cc9aea2012-07-07 09:32:00 +0100283 return sr_si_string_u64(samplerate, "Hz");
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100284}
285
286/**
Uwe Hermanndfcc0bf2011-02-08 21:47:50 +0100287 * Convert a numeric frequency value to the "natural" string representation
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100288 * of its period.
289 *
290 * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
291 *
292 * @param frequency The frequency in Hz.
Uwe Hermann44dae532012-02-17 20:44:19 +0100293 *
Uwe Hermann133a37b2012-02-11 20:06:46 +0100294 * @return A g_try_malloc()ed string representation of the frequency value,
295 * or NULL upon errors. The caller is responsible to g_free() the
296 * memory.
Uwe Hermann47117242014-05-04 20:51:05 +0200297 *
298 * @since 0.1.0
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100299 */
Uwe Hermann1a081ca2012-02-01 23:40:35 +0100300SR_API char *sr_period_string(uint64_t frequency)
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100301{
302 char *o;
303 int r;
304
Uwe Hermann133a37b2012-02-11 20:06:46 +0100305 /* Allocate enough for a uint64_t as string + " ms". */
306 if (!(o = g_try_malloc0(30 + 1))) {
Uwe Hermanna885ce32012-11-11 12:44:16 +0100307 sr_err("%s: o malloc failed", __func__);
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100308 return NULL;
Uwe Hermann133a37b2012-02-11 20:06:46 +0100309 }
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100310
Uwe Hermann59df0c72011-02-22 17:57:03 +0100311 if (frequency >= SR_GHZ(1))
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100312 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
Uwe Hermann59df0c72011-02-22 17:57:03 +0100313 else if (frequency >= SR_MHZ(1))
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100314 r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000);
Uwe Hermann59df0c72011-02-22 17:57:03 +0100315 else if (frequency >= SR_KHZ(1))
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100316 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
317 else
318 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
319
320 if (r < 0) {
321 /* Something went wrong... */
Uwe Hermann133a37b2012-02-11 20:06:46 +0100322 g_free(o);
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100323 return NULL;
324 }
325
326 return o;
327}
328
Uwe Hermanndfcc0bf2011-02-08 21:47:50 +0100329/**
Bert Vermeulene0e15062013-03-31 10:27:15 +0200330 * Convert a numeric voltage value to the "natural" string representation
331 * of its voltage value. The voltage is specified as a rational number's
332 * numerator and denominator.
Bert Vermeulen79afc8c2012-05-17 01:54:57 +0200333 *
334 * E.g. a value of 300000 would be converted to "300mV", 2 to "2V".
335 *
Bert Vermeulene0e15062013-03-31 10:27:15 +0200336 * @param v_p The voltage numerator.
337 * @param v_q The voltage denominator.
Bert Vermeulen79afc8c2012-05-17 01:54:57 +0200338 *
339 * @return A g_try_malloc()ed string representation of the voltage value,
340 * or NULL upon errors. The caller is responsible to g_free() the
341 * memory.
Uwe Hermann47117242014-05-04 20:51:05 +0200342 *
343 * @since 0.2.0
Bert Vermeulen79afc8c2012-05-17 01:54:57 +0200344 */
Bert Vermeulene0e15062013-03-31 10:27:15 +0200345SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q)
Bert Vermeulen79afc8c2012-05-17 01:54:57 +0200346{
Bert Vermeulen79afc8c2012-05-17 01:54:57 +0200347 int r;
Bert Vermeulend5a669a2013-02-04 13:36:23 +0100348 char *o;
Bert Vermeulen79afc8c2012-05-17 01:54:57 +0200349
350 if (!(o = g_try_malloc0(30 + 1))) {
Uwe Hermanna885ce32012-11-11 12:44:16 +0100351 sr_err("%s: o malloc failed", __func__);
Bert Vermeulen79afc8c2012-05-17 01:54:57 +0200352 return NULL;
353 }
354
Bert Vermeulene0e15062013-03-31 10:27:15 +0200355 if (v_q == 1000)
356 r = snprintf(o, 30, "%" PRIu64 "mV", v_p);
357 else if (v_q == 1)
358 r = snprintf(o, 30, "%" PRIu64 "V", v_p);
Bert Vermeulen79afc8c2012-05-17 01:54:57 +0200359 else
Bert Vermeulene0e15062013-03-31 10:27:15 +0200360 r = snprintf(o, 30, "%gV", (float)v_p / (float)v_q);
Bert Vermeulen79afc8c2012-05-17 01:54:57 +0200361
362 if (r < 0) {
363 /* Something went wrong... */
364 g_free(o);
365 return NULL;
366 }
367
368 return o;
369}
370
371/**
Uwe Hermanndfcc0bf2011-02-08 21:47:50 +0100372 * Convert a "natural" string representation of a size value to uint64_t.
373 *
374 * E.g. a value of "3k" or "3 K" would be converted to 3000, a value
375 * of "15M" would be converted to 15000000.
376 *
377 * Value representations other than decimal (such as hex or octal) are not
378 * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported.
379 * Spaces (but not other whitespace) between value and suffix are allowed.
380 *
381 * @param sizestring A string containing a (decimal) size value.
Bert Vermeulenf64c1412011-11-27 19:31:25 +0100382 * @param size Pointer to uint64_t which will contain the string's size value.
Uwe Hermanndfcc0bf2011-02-08 21:47:50 +0100383 *
Uwe Hermann44dae532012-02-17 20:44:19 +0100384 * @return SR_OK upon success, SR_ERR upon errors.
Uwe Hermann47117242014-05-04 20:51:05 +0200385 *
386 * @since 0.1.0
Uwe Hermanndfcc0bf2011-02-08 21:47:50 +0100387 */
Uwe Hermann1a081ca2012-02-01 23:40:35 +0100388SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100389{
Bert Vermeulenf64c1412011-11-27 19:31:25 +0100390 int multiplier, done;
poljar (Damir Jelić)580f3092014-01-16 02:53:40 +0100391 double frac_part;
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100392 char *s;
393
Bert Vermeulenf64c1412011-11-27 19:31:25 +0100394 *size = strtoull(sizestring, &s, 10);
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100395 multiplier = 0;
poljar (Damir Jelić)580f3092014-01-16 02:53:40 +0100396 frac_part = 0;
Bert Vermeulenf64c1412011-11-27 19:31:25 +0100397 done = FALSE;
398 while (s && *s && multiplier == 0 && !done) {
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100399 switch (*s) {
400 case ' ':
401 break;
poljar (Damir Jelić)580f3092014-01-16 02:53:40 +0100402 case '.':
403 frac_part = g_ascii_strtod(s, &s);
404 break;
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100405 case 'k':
406 case 'K':
Uwe Hermann59df0c72011-02-22 17:57:03 +0100407 multiplier = SR_KHZ(1);
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100408 break;
409 case 'm':
410 case 'M':
Uwe Hermann59df0c72011-02-22 17:57:03 +0100411 multiplier = SR_MHZ(1);
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100412 break;
413 case 'g':
414 case 'G':
Uwe Hermann59df0c72011-02-22 17:57:03 +0100415 multiplier = SR_GHZ(1);
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100416 break;
417 default:
Bert Vermeulenf64c1412011-11-27 19:31:25 +0100418 done = TRUE;
419 s--;
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100420 }
421 s++;
422 }
poljar (Damir Jelić)580f3092014-01-16 02:53:40 +0100423 if (multiplier > 0) {
Bert Vermeulenf64c1412011-11-27 19:31:25 +0100424 *size *= multiplier;
poljar (Damir Jelić)580f3092014-01-16 02:53:40 +0100425 *size += frac_part * multiplier;
426 } else
427 *size += frac_part;
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100428
Bert Vermeulen19e43ab2014-10-02 12:57:19 +0200429 if (s && *s && strcasecmp(s, "Hz"))
Bert Vermeulenf64c1412011-11-27 19:31:25 +0100430 return SR_ERR;
431
432 return SR_OK;
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100433}
434
Uwe Hermanndfcc0bf2011-02-08 21:47:50 +0100435/**
436 * Convert a "natural" string representation of a time value to an
437 * uint64_t value in milliseconds.
438 *
439 * E.g. a value of "3s" or "3 s" would be converted to 3000, a value
440 * of "15ms" would be converted to 15.
441 *
442 * Value representations other than decimal (such as hex or octal) are not
443 * supported. Only lower-case "s" and "ms" time suffixes are supported.
444 * Spaces (but not other whitespace) between value and suffix are allowed.
445 *
446 * @param timestring A string containing a (decimal) time value.
447 * @return The string's time value as uint64_t, in milliseconds.
448 *
Uwe Hermann6b2d8d32012-10-21 23:24:42 +0200449 * @todo Add support for "m" (minutes) and others.
450 * @todo Add support for picoseconds?
451 * @todo Allow both lower-case and upper-case? If no, document it.
Uwe Hermann47117242014-05-04 20:51:05 +0200452 *
453 * @since 0.1.0
Uwe Hermanndfcc0bf2011-02-08 21:47:50 +0100454 */
Uwe Hermann1a081ca2012-02-01 23:40:35 +0100455SR_API uint64_t sr_parse_timestring(const char *timestring)
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100456{
457 uint64_t time_msec;
458 char *s;
459
Uwe Hermann6b2d8d32012-10-21 23:24:42 +0200460 /* TODO: Error handling, logging. */
461
Bert Vermeulen40f5dda2011-01-31 22:29:40 +0100462 time_msec = strtoull(timestring, &s, 10);
463 if (time_msec == 0 && s == timestring)
464 return 0;
465
466 if (s && *s) {
467 while (*s == ' ')
468 s++;
469 if (!strcmp(s, "s"))
470 time_msec *= 1000;
471 else if (!strcmp(s, "ms"))
472 ; /* redundant */
473 else
474 return 0;
475 }
476
477 return time_msec;
478}
Gareth McMullin4d436e72011-11-19 13:41:41 +1300479
Uwe Hermann47117242014-05-04 20:51:05 +0200480/** @since 0.1.0 */
Uwe Hermann1a081ca2012-02-01 23:40:35 +0100481SR_API gboolean sr_parse_boolstring(const char *boolstr)
Gareth McMullin4d436e72011-11-19 13:41:41 +1300482{
483 if (!boolstr)
484 return FALSE;
485
Bert Vermeulen993526f2012-04-23 15:31:41 +0200486 if (!g_ascii_strncasecmp(boolstr, "true", 4) ||
487 !g_ascii_strncasecmp(boolstr, "yes", 3) ||
488 !g_ascii_strncasecmp(boolstr, "on", 2) ||
489 !g_ascii_strncasecmp(boolstr, "1", 1))
Gareth McMullin4d436e72011-11-19 13:41:41 +1300490 return TRUE;
491
492 return FALSE;
493}
Bert Vermeulen76f4c612012-05-15 20:46:14 +0200494
Uwe Hermann47117242014-05-04 20:51:05 +0200495/** @since 0.2.0 */
Bert Vermeulen76e107d2013-03-30 14:41:01 +0100496SR_API int sr_parse_period(const char *periodstr, uint64_t *p, uint64_t *q)
Bert Vermeulen76f4c612012-05-15 20:46:14 +0200497{
498 char *s;
499
Bert Vermeulen76e107d2013-03-30 14:41:01 +0100500 *p = strtoull(periodstr, &s, 10);
501 if (*p == 0 && s == periodstr)
Bert Vermeulen76f4c612012-05-15 20:46:14 +0200502 /* No digits found. */
503 return SR_ERR_ARG;
504
505 if (s && *s) {
506 while (*s == ' ')
507 s++;
Petteri Aimonen8c012ad2012-11-20 21:02:14 +0200508 if (!strcmp(s, "fs"))
Bert Vermeulen76e107d2013-03-30 14:41:01 +0100509 *q = 1000000000000000ULL;
Petteri Aimonen8c012ad2012-11-20 21:02:14 +0200510 else if (!strcmp(s, "ps"))
Bert Vermeulen76e107d2013-03-30 14:41:01 +0100511 *q = 1000000000000ULL;
Petteri Aimonen8c012ad2012-11-20 21:02:14 +0200512 else if (!strcmp(s, "ns"))
Bert Vermeulen76e107d2013-03-30 14:41:01 +0100513 *q = 1000000000ULL;
Bert Vermeulen76f4c612012-05-15 20:46:14 +0200514 else if (!strcmp(s, "us"))
Bert Vermeulen76e107d2013-03-30 14:41:01 +0100515 *q = 1000000;
Bert Vermeulen76f4c612012-05-15 20:46:14 +0200516 else if (!strcmp(s, "ms"))
Bert Vermeulen76e107d2013-03-30 14:41:01 +0100517 *q = 1000;
Bert Vermeulen76f4c612012-05-15 20:46:14 +0200518 else if (!strcmp(s, "s"))
Bert Vermeulen76e107d2013-03-30 14:41:01 +0100519 *q = 1;
Bert Vermeulen76f4c612012-05-15 20:46:14 +0200520 else
521 /* Must have a time suffix. */
522 return SR_ERR_ARG;
523 }
524
525 return SR_OK;
526}
527
Uwe Hermann47117242014-05-04 20:51:05 +0200528/** @since 0.2.0 */
Bert Vermeulen76e107d2013-03-30 14:41:01 +0100529SR_API int sr_parse_voltage(const char *voltstr, uint64_t *p, uint64_t *q)
Bert Vermeulen79afc8c2012-05-17 01:54:57 +0200530{
531 char *s;
532
Bert Vermeulen76e107d2013-03-30 14:41:01 +0100533 *p = strtoull(voltstr, &s, 10);
534 if (*p == 0 && s == voltstr)
Bert Vermeulen79afc8c2012-05-17 01:54:57 +0200535 /* No digits found. */
536 return SR_ERR_ARG;
537
538 if (s && *s) {
539 while (*s == ' ')
540 s++;
541 if (!strcasecmp(s, "mv"))
Bert Vermeulen76e107d2013-03-30 14:41:01 +0100542 *q = 1000L;
Bert Vermeulen79afc8c2012-05-17 01:54:57 +0200543 else if (!strcasecmp(s, "v"))
Bert Vermeulen76e107d2013-03-30 14:41:01 +0100544 *q = 1;
Bert Vermeulen79afc8c2012-05-17 01:54:57 +0200545 else
546 /* Must have a base suffix. */
547 return SR_ERR_ARG;
548 }
549
550 return SR_OK;
551}
552
Uwe Hermann7b870c32012-10-21 16:13:36 +0200553/** @} */