make IMC/IMV pairs independent of libcharon
[strongswan.git] / src / libstrongswan / enum.c
1 /*
2 * Copyright (C) 2006 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <stddef.h>
17 #include <stdio.h>
18
19 #include <library.h>
20
21 #include "enum.h"
22
23 /**
24 * See header.
25 */
26 char *enum_to_name(enum_name_t *e, int val)
27 {
28 do
29 {
30 if (val >= e->first && val <= e->last)
31 {
32 return e->names[val - e->first];
33 }
34 }
35 while ((e = e->next));
36 return NULL;
37 }
38
39 /**
40 * See header.
41 */
42 int enum_from_name(enum_name_t *e, char *name)
43 {
44 do
45 {
46 int i, count = e->last - e->first + 1;
47
48 for (i = 0; i < count; i++)
49 {
50 if (strcaseeq(name, e->names[i]))
51 {
52 return e->first + i;
53 }
54 }
55 }
56 while ((e = e->next));
57 return -1;
58 }
59
60 /**
61 * Described in header.
62 */
63 int enum_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
64 const void *const *args)
65 {
66 enum_name_t *ed = *((enum_name_t**)(args[0]));
67 int val = *((int*)(args[1]));
68
69 char *name = enum_to_name(ed, val);
70
71 if (name == NULL)
72 {
73 return print_in_hook(dst, len, "(%d)", val);
74 }
75 else
76 {
77 return print_in_hook(dst, len, "%s", name);
78 }
79 }