summaryrefslogtreecommitdiff
path: root/src/util/virenum.c
blob: c6936e58ff071bbb958f2e5595439274a231745b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * virenum.c: enum value conversion helpers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <config.h>

#include "virenum.h"

#define VIR_FROM_THIS VIR_FROM_NONE

VIR_ENUM_IMPL(virTristateBool,
              VIR_TRISTATE_BOOL_LAST,
              "default",
              "yes",
              "no",
);

VIR_ENUM_IMPL(virTristateSwitch,
              VIR_TRISTATE_SWITCH_LAST,
              "default",
              "on",
              "off",
);


virTristateBool
virTristateBoolFromBool(bool val)
{
    if (val)
        return VIR_TRISTATE_BOOL_YES;
    else
        return VIR_TRISTATE_BOOL_NO;
}


/**
 * virTristateBoolToBool:
 * @t: a virTristateBool value
 * @b: pointer to a boolean to be updated according to the value of @t
 *
 * The value pointed to by @b is updated if the tristate value @t is not absent.
 */
void
virTristateBoolToBool(virTristateBool t,
                      bool *b)
{
    switch (t) {
    case VIR_TRISTATE_BOOL_YES:
        *b = true;
        break;

    case VIR_TRISTATE_BOOL_NO:
        *b = false;
        break;

    case VIR_TRISTATE_BOOL_ABSENT:
    case VIR_TRISTATE_BOOL_LAST:
        break;
    }
}


virTristateSwitch
virTristateSwitchFromBool(bool val)
{
    if (val)
        return VIR_TRISTATE_SWITCH_ON;
    else
        return VIR_TRISTATE_SWITCH_OFF;
}


/**
 * virTristateSwitchToBool:
 * @t: a virTristateSwitch value
 * @b: pointer to a boolean to be updated according to the value of @t
 *
 * The value pointed to by @b is updated if the tristate value @t is not absent.
 */
void
virTristateSwitchToBool(virTristateSwitch t,
                        bool *b)
{
    switch (t) {
    case VIR_TRISTATE_SWITCH_ON:
        *b = true;
        break;

    case VIR_TRISTATE_SWITCH_OFF:
        *b = false;
        break;

    case VIR_TRISTATE_SWITCH_ABSENT:
    case VIR_TRISTATE_SWITCH_LAST:
        break;
    }
}


int
virEnumFromString(const char * const *types,
                  unsigned int ntypes,
                  const char *type)
{
    size_t i;
    if (!type)
        return -1;

    for (i = 0; i < ntypes; i++)
        if (STREQ(types[i], type))
            return i;

    return -1;
}


const char *
virEnumToString(const char * const *types,
                unsigned int ntypes,
                int type)
{
    if (type < 0 || type >= ntypes)
        return NULL;

    return types[type];
}