2
* virtypedparam.c: utility functions for dealing with virTypedParameters
4
* Copyright (C) 2011-2012 Red Hat, Inc.
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
#include "virtypedparam.h"
29
#include "virterror_internal.h"
31
#define VIR_FROM_THIS VIR_FROM_NONE
33
#define virUtilError(code, ...) \
34
virReportErrorHelper(VIR_FROM_NONE, code, __FILE__, \
35
__FUNCTION__, __LINE__, __VA_ARGS__)
37
VIR_ENUM_DECL(virTypedParameter)
38
VIR_ENUM_IMPL(virTypedParameter, VIR_TYPED_PARAM_LAST,
49
virTypedParameterArrayClear(virTypedParameterPtr params, int nparams)
56
for (i = 0; i < nparams; i++) {
57
if (params[i].type == VIR_TYPED_PARAM_STRING)
58
VIR_FREE(params[i].value.s);
62
/* Validate that PARAMS contains only recognized parameter names with
63
* correct types, and with no duplicates. Pass in as many name/type
64
* pairs as appropriate, and pass NULL to end the list of accepted
65
* parameters. Return 0 on success, -1 on failure with error message
68
virTypedParameterArrayValidate(virTypedParameterPtr params, int nparams, ...)
76
va_start(ap, nparams);
78
/* Yes, this is quadratic, but since we reject duplicates and
79
* unknowns, it is constrained by the number of var-args passed
80
* in, which is expected to be small enough to not be
82
for (i = 0; i < nparams; i++) {
84
va_start(ap, nparams);
86
name = va_arg(ap, const char *);
88
type = va_arg(ap, int);
89
if (STREQ(params[i].field, name)) {
90
if (params[i].type != type) {
93
badtype = virTypedParameterTypeToString(params[i].type);
95
badtype = virTypedParameterTypeToString(0);
96
virUtilError(VIR_ERR_INVALID_ARG,
97
_("invalid type '%s' for parameter '%s', "
99
badtype, params[i].field,
100
virTypedParameterTypeToString(type));
104
name = va_arg(ap, const char *);
107
virUtilError(VIR_ERR_INVALID_ARG,
108
_("parameter '%s' not supported"),
112
for (j = 0; j < i; j++) {
113
if (STREQ(params[i].field, params[j].field)) {
114
virUtilError(VIR_ERR_INVALID_ARG,
115
_("parameter '%s' occurs multiple times"),
129
/* Assign name, type, and the appropriately typed arg to param; in the
130
* case of a string, the caller is assumed to have malloc'd a string,
131
* or can pass NULL to have this function malloc an empty string.
132
* Return 0 on success, -1 after an error message on failure. */
134
virTypedParameterAssign(virTypedParameterPtr param, const char *name,
142
if (virStrcpyStatic(param->field, name) == NULL) {
143
virUtilError(VIR_ERR_INTERNAL_ERROR, _("Field name '%s' too long"),
150
case VIR_TYPED_PARAM_INT:
151
param->value.i = va_arg(ap, int);
153
case VIR_TYPED_PARAM_UINT:
154
param->value.ui = va_arg(ap, unsigned int);
156
case VIR_TYPED_PARAM_LLONG:
157
param->value.l = va_arg(ap, long long int);
159
case VIR_TYPED_PARAM_ULLONG:
160
param->value.ul = va_arg(ap, unsigned long long int);
162
case VIR_TYPED_PARAM_DOUBLE:
163
param->value.d = va_arg(ap, double);
165
case VIR_TYPED_PARAM_BOOLEAN:
166
param->value.b = !!va_arg(ap, int);
168
case VIR_TYPED_PARAM_STRING:
169
param->value.s = va_arg(ap, char *);
171
param->value.s = strdup("");
172
if (!param->value.s) {
178
virUtilError(VIR_ERR_INTERNAL_ERROR,
179
_("unexpected type %d for field %s"), type, name);