From: Alan T. DeKok Date: Fri, 23 Dec 2011 14:15:53 +0000 (-0500) Subject: Added new method to get name of enum from values X-Git-Tag: release_2_2_0~226 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=905f6a409f5ba7f502cbfcb068e14d5426869b24;p=thirdparty%2Ffreeradius-server.git Added new method to get name of enum from values This is simpler than having duplicate code throughout the source. --- diff --git a/src/include/libradius.h b/src/include/libradius.h index b81205c63c8..e7d18379658 100644 --- a/src/include/libradius.h +++ b/src/include/libradius.h @@ -262,6 +262,7 @@ DICT_ATTR *dict_attrbyvalue(unsigned int attr); DICT_ATTR *dict_attrbyname(const char *attr); DICT_VALUE *dict_valbyattr(unsigned int attr, int val); DICT_VALUE *dict_valbyname(unsigned int attr, const char *val); +const char *dict_valnamebyattr(unsigned int attr, int value); int dict_vendorbyname(const char *name); DICT_VENDOR *dict_vendorbyvalue(int vendor); diff --git a/src/lib/dict.c b/src/lib/dict.c index d61efd084d0..439f80ff9a9 100644 --- a/src/lib/dict.c +++ b/src/lib/dict.c @@ -1838,6 +1838,34 @@ DICT_VALUE *dict_valbyattr(unsigned int attr, int value) return fr_hash_table_finddata(values_byvalue, &dval); } +/* + * Associate a value with an attribute and return it. + */ +const char *dict_valnamebyattr(unsigned int attr, int value) +{ + DICT_VALUE dval, *dv; + + /* + * First, look up aliases. + */ + dval.attr = attr; + dval.name[0] = '\0'; + + /* + * Look up the attribute alias target, and use + * the correct attribute number if found. + */ + dv = fr_hash_table_finddata(values_byname, &dval); + if (dv) dval.attr = dv->value; + + dval.value = value; + + dv = fr_hash_table_finddata(values_byvalue, &dval); + if (!dv) return ""; + + return dv->name; +} + /* * Get a value by its name, keyed off of an attribute. */