From: Arran Cudbard-Bell Date: Mon, 31 Oct 2011 10:31:06 +0000 (+0100) Subject: Add function to compare substrings to FR_NAME_NUMBER arrays X-Git-Tag: release_3_0_0_beta0~535 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c4424b9bd41172bf52dcf1ef491d77a29c0c0b62;p=thirdparty%2Ffreeradius-server.git Add function to compare substrings to FR_NAME_NUMBER arrays --- diff --git a/src/include/token.h b/src/include/token.h index f90860117a7..ab96fd5920b 100644 --- a/src/include/token.h +++ b/src/include/token.h @@ -72,6 +72,7 @@ typedef struct FR_NAME_NUMBER { } FR_NAME_NUMBER; int fr_str2int(const FR_NAME_NUMBER *table, const char *name, int def); +int fr_substr2int(const FR_NAME_NUMBER *table, const char *name, int def, int len); const char *fr_int2str(const FR_NAME_NUMBER *table, int number, const char *def); diff --git a/src/lib/token.c b/src/lib/token.c index 5f0fbe33d1f..2e04ae390b8 100644 --- a/src/lib/token.c +++ b/src/lib/token.c @@ -269,6 +269,28 @@ int fr_str2int(const FR_NAME_NUMBER *table, const char *name, int def) return def; } +/* + * Convert a string matching part of name to an integer. + */ +int fr_substr2int(const FR_NAME_NUMBER *table, const char *name, int def, int len) +{ + const FR_NAME_NUMBER *this; + size_t max; + + for (this = table; this->name != NULL; this++) { + /* + * Match up to the length of the table entry if len is < 0. + */ + max = (len < 0) ? strlen(this->name) : (unsigned)len; + + if (strncasecmp(this->name, name, max) == 0) { + return this->number; + } + } + + return def; +} + /* * Convert an integer to a string. */