]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add function to compare substrings to FR_NAME_NUMBER arrays
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 31 Oct 2011 10:31:06 +0000 (11:31 +0100)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 31 Oct 2011 10:34:55 +0000 (11:34 +0100)
src/include/token.h
src/lib/token.c

index f90860117a72ecbd4d2a802a892546bb300f89f7..ab96fd5920b20b23b4a449361797e9ff75982cba 100644 (file)
@@ -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);
 
index 5f0fbe33d1f74a355051fabdd16939dedbf9a77f..2e04ae390b804b44471fd8f765e2c9453d60d2b4 100644 (file)
@@ -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.
  */