#define printxval_search_ex(xlat__, val__, dflt__) \
printxval_searchn_ex((xlat__), ARRAY_SIZE(xlat__) - 1, (val__), \
(dflt__), XLAT_STYLE_DEFAULT)
+extern int printxval_indexn_ex(const struct xlat *xlat, size_t xlat_size,
+ uint64_t val, const char *dflt, enum xlat_style style);
+#define printxval_indexn(xlat_, xlat_size_, val_, dflt_) \
+ printxval_indexn_ex((xlat_), (xlat_size_), (val_), (dflt_), \
+ XLAT_STYLE_DEFAULT)
+#define printxval_index(xlat__, val__, dflt__) \
+ printxval_indexn(xlat__, ARRAY_SIZE(xlat__) - 1, val__, dflt__)
+#define printxval_index_ex(xlat__, val__, dflt__) \
+ printxval_indexn_ex((xlat__), ARRAY_SIZE(xlat__) - 1, (val__), \
+ (dflt__), XLAT_STYLE_DEFAULT)
extern int sprintxval_ex(char *buf, size_t size, const struct xlat *xlat,
unsigned int val, const char *dflt,
enum xlat_style style);
* @param dflt String (abbreviated in comment syntax) which should be
* emitted if no appropriate xlat value has been found.
* @param style Style in which xlat value should be printed.
+ * @param fn Search function.
* @return 1 if appropriate xlat value has been found, 0
* otherwise.
*/
-int
-printxval_searchn_ex(const struct xlat *xlat, size_t xlat_size, uint64_t val,
- const char *dflt, enum xlat_style style)
+static int
+printxval_sized(const struct xlat *xlat, size_t xlat_size, uint64_t val,
+ const char *dflt, enum xlat_style style,
+ const char *(* fn)(const struct xlat *, size_t, uint64_t))
{
style = get_xlat_style(style);
return 0;
}
- const char *s = xlat_search(xlat, xlat_size, val);
+ const char *s = fn(xlat, xlat_size, val);
if (s) {
if (xlat_verbose(style) == XLAT_STYLE_VERBOSE) {
return 0;
}
+int
+printxval_searchn_ex(const struct xlat *xlat, size_t xlat_size, uint64_t val,
+ const char *dflt, enum xlat_style style)
+{
+ return printxval_sized(xlat, xlat_size, val, dflt, style,
+ xlat_search);
+}
+
+static const char *
+xlat_idx(const struct xlat *xlat, size_t nmemb, uint64_t val)
+{
+ if (val >= nmemb)
+ return NULL;
+
+ if (val != xlat[val].val) {
+ error_func_msg("Unexpected xlat value %" PRIu64
+ " at index %" PRIu64,
+ xlat[val].val, val);
+ return NULL;
+ }
+
+ return xlat[val].str;
+}
+
+int
+printxval_indexn_ex(const struct xlat *xlat, size_t xlat_size, uint64_t val,
+ const char *dflt, enum xlat_style style)
+{
+ return printxval_sized(xlat, xlat_size, val, dflt, style, xlat_idx);
+}
+
/*
* Interpret `xlat' as an array of flags.
* Print to static string the entries whose bits are on in `flags'
local val
val="$1"; shift
+ [ 1 = "$value_indexed" ] && printf " [%s] =" "${val}"
if [ -z "${val_type-}" ]; then
echo " XLAT(${val}),"
else
val="$1"; shift
str="$1"; shift
+ [ 1 = "$value_indexed" ] && printf " [%s] =" "${val}"
if [ -z "${val_type-}" ]; then
echo " XLAT_PAIR(${val}, \"${str}\"),"
else
local decl="extern const struct xlat ${name}[];"
local in_defs= in_mpers=
+ value_indexed=0
+
if grep -F -x "$decl" "$defs" > /dev/null; then
in_defs=1
elif grep -F -x "$decl" "$mpers" > /dev/null; then
'#val_type '*)
# to be processed during 2nd pass
;;
+ '#value_indexed')
+ value_indexed=1
+ ;;
'#'*)
echo "${line}"
;;
'#unconditional')
unconditional=1
;;
+ '#value_indexed')
+ ;;
'#val_type '*)
val_type="${line#\#val_type }"
;;