From: Bert Vermeulen Date: Tue, 15 May 2012 18:46:14 +0000 (+0200) Subject: sr: add period parser X-Git-Tag: dsupstream~946 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=76f4c61086b314e66e92dd571f37defd3f714554;p=libsigrok.git sr: add period parser --- diff --git a/sigrok-proto.h b/sigrok-proto.h index df31b90d..d5a9adb9 100644 --- a/sigrok-proto.h +++ b/sigrok-proto.h @@ -119,6 +119,7 @@ SR_API char **sr_parse_triggerstring(struct sr_dev *dev, SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size); SR_API uint64_t sr_parse_timestring(const char *timestring); SR_API gboolean sr_parse_boolstring(const char *boolstring); +SR_API int sr_parse_period(const char *periodstr, struct sr_rational *r); /*--- version.c -------------------------------------------------------------*/ diff --git a/strutil.c b/strutil.c index cb51b2ab..ea69a2af 100644 --- a/strutil.c +++ b/strutil.c @@ -314,3 +314,33 @@ SR_API gboolean sr_parse_boolstring(const char *boolstr) return FALSE; } + +SR_API int sr_parse_period(const char *periodstr, struct sr_rational *r) +{ + char *s; + + r->p = strtoull(periodstr, &s, 10); + if (r->p == 0 && s == periodstr) + /* No digits found. */ + return SR_ERR_ARG; + + if (s && *s) { + while (*s == ' ') + s++; + if (!strcmp(s, "ns")) + r->q = 1000000000L; + else if (!strcmp(s, "us")) + r->q = 1000000; + else if (!strcmp(s, "ms")) + r->q = 1000; + else if (!strcmp(s, "s")) + r->q = 1; + else + /* Must have a time suffix. */ + return SR_ERR_ARG; + } + + return SR_OK; +} + +