4 * @brief enum value to string conversion functions.
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 #include <printf_hook.h>
31 * get the name of an enum value in a enum_name_t list
33 static char *enum_name(enum_name_t
*e
, int val
)
37 if (val
>= e
->first
&& val
<= e
->last
)
39 return e
->names
[val
- e
->first
];
42 while ((e
= e
->next
));
47 * output handler in printf() for enum names
49 static int print_enum(FILE *stream
, const struct printf_info
*info
,
50 const void *const *args
)
52 enum_name_t
*ed
= *((enum_name_t
**)(args
[0]));
53 int val
= *((int*)(args
[1]));
55 char *name
= enum_name(ed
, val
);
59 return fprintf(stream
, "(%d)", val
);
63 return fprintf(stream
, "%s", name
);
68 * register printf() handlers
70 static void __attribute__ ((constructor
))print_register()
72 register_printf_function(PRINTF_ENUM
, print_enum
, arginfo_ptr_int
);