From: Bert Vermeulen Date: Tue, 29 May 2012 10:12:51 +0000 (+0200) Subject: cli: loose string comparison helper X-Git-Tag: sigrok-cli-0.3.1~14 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=432de709d1560fdc861901bc64f19b68a4b2011b;p=sigrok-cli.git cli: loose string comparison helper --- diff --git a/parsers.c b/parsers.c index a01f041..707bcdd 100644 --- a/parsers.c +++ b/parsers.c @@ -181,3 +181,36 @@ struct sr_dev *parse_devstring(const char *devstring) return dev; } + +char *strcanon(char *str) +{ + int p0, p1; + char *s; + + /* Returns newly allocated string. */ + s = g_ascii_strdown(str, -1); + for (p0 = p1 = 0; str[p0]; p0++) { + if ((s[p0] >= 'a' && s[p0] <= 'z') + || (s[p0] >= '0' && s[p0] <= '9')) + s[p1++] = s[p0]; + } + s[p1] = '\0'; + + return s; +} + + +int canon_cmp(char *str1, char *str2) +{ + int ret; + char *s1, *s2; + + s1 = strcanon(str1); + s2 = strcanon(str2); + ret = g_ascii_strcasecmp(s1, s2); + g_free(s2); + g_free(s1); + + return ret; +} + diff --git a/sigrok-cli.h b/sigrok-cli.h index d231670..8c57ef3 100644 --- a/sigrok-cli.h +++ b/sigrok-cli.h @@ -29,6 +29,8 @@ char **sr_parse_triggerstring(struct sr_dev *dev, const char *triggerstring); GHashTable *parse_generic_arg(const char *arg); struct sr_dev *parse_devstring(const char *devstring); uint64_t sr_parse_timestring(const char *timestring); +char *strcanon(char *str); +int canon_cmp(char *str1, char *str2); /* anykey.c */ void add_anykey(void);