1 /*
2  * virtypedparam.h: managing typed parameters
3  *
4  * Copyright (C) 2011-2012 Red Hat, Inc.
5  *
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.
10  *
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.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library.  If not, see
18  * <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #pragma once
23 
24 #include "internal.h"
25 #include "virenum.h"
26 
27 /**
28  * VIR_TYPED_PARAM_MULTIPLE:
29  *
30  * Flag indicating that the params has multiple occurrences of the parameter.
31  * Only used as a flag for @type argument of the virTypedParamsValidate.
32  */
33 #define VIR_TYPED_PARAM_MULTIPLE (1U << 31)
34 
35 G_STATIC_ASSERT(!(VIR_TYPED_PARAM_LAST & VIR_TYPED_PARAM_MULTIPLE));
36 
37 typedef struct _virTypedParameterRemoteValue virTypedParameterRemoteValue;
38 struct _virTypedParameterRemoteValue {
39     int type;
40     union {
41         int i; /* exempt from syntax-check */
42         unsigned int ui;
43         long long int l;
44         unsigned long long int ul;
45         double d;
46         char b;
47         char *s;
48     } remote_typed_param_value;
49 };
50 
51 
52 struct _virTypedParameterRemote {
53     char *field;
54     virTypedParameterRemoteValue value;
55 };
56 
57 
58 int virTypedParamsValidate(virTypedParameterPtr params, int nparams,
59                            /* const char *name, int type ... */ ...)
60     G_GNUC_NULL_TERMINATED G_GNUC_WARN_UNUSED_RESULT;
61 
62 bool virTypedParamsCheck(virTypedParameterPtr params,
63                          int nparams,
64                          const char **names,
65                          int nnames);
66 
67 int
68 virTypedParamsGetStringList(virTypedParameterPtr params,
69                             int nparams,
70                             const char *name,
71                             const char ***values);
72 int
73 virTypedParamsFilter(virTypedParameterPtr params,
74                      int nparams,
75                      const char *name,
76                      virTypedParameterPtr **ret)
77     G_GNUC_WARN_UNUSED_RESULT;
78 
79 
80 int virTypedParameterAssign(virTypedParameterPtr param, const char *name,
81                             int type, /* TYPE arg */ ...)
82     G_GNUC_WARN_UNUSED_RESULT;
83 
84 int virTypedParamsReplaceString(virTypedParameterPtr *params,
85                                 int *nparams,
86                                 const char *name,
87                                 const char *value);
88 
89 int virTypedParamsCopy(virTypedParameterPtr *dst,
90                        virTypedParameterPtr src,
91                        int nparams);
92 
93 char *virTypedParameterToString(virTypedParameterPtr param);
94 
95 void virTypedParamsRemoteFree(struct _virTypedParameterRemote *remote_params_val,
96                               unsigned int remote_params_len);
97 
98 int virTypedParamsDeserialize(struct _virTypedParameterRemote *remote_params,
99                               unsigned int remote_params_len,
100                               int limit,
101                               virTypedParameterPtr *params,
102                               int *nparams);
103 
104 int virTypedParamsSerialize(virTypedParameterPtr params,
105                             int nparams,
106                             int limit,
107                             struct _virTypedParameterRemote **remote_params_val,
108                             unsigned int *remote_params_len,
109                             unsigned int flags);
110 
111 VIR_ENUM_DECL(virTypedParameter);
112 
113 #define VIR_TYPED_PARAMS_DEBUG(params, nparams) \
114     do { \
115         int _i; \
116         if (!params) \
117             break; \
118         for (_i = 0; _i < (nparams); _i++) { \
119             char *_value = virTypedParameterToString((params) + _i); \
120             VIR_DEBUG("params[\"%s\"]=(%s)%s", \
121                       (params)[_i].field, \
122                       virTypedParameterTypeToString((params)[_i].type), \
123                       NULLSTR(_value)); \
124             VIR_FREE(_value); \
125         } \
126     } while (0)
127 
128 typedef struct _virTypedParamList virTypedParamList;
129 struct _virTypedParamList {
130     virTypedParameterPtr par;
131     size_t npar;
132     size_t par_alloc;
133 };
134 
135 void virTypedParamListFree(virTypedParamList *list);
136 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virTypedParamList, virTypedParamListFree);
137 
138 size_t virTypedParamListStealParams(virTypedParamList *list,
139                                     virTypedParameterPtr *params);
140 
141 int virTypedParamListAddInt(virTypedParamList *list,
142                             int value,
143                             const char *namefmt,
144                             ...)
145     G_GNUC_PRINTF(3, 4) G_GNUC_WARN_UNUSED_RESULT;
146 int virTypedParamListAddUInt(virTypedParamList *list,
147                              unsigned int value,
148                              const char *namefmt,
149                              ...)
150     G_GNUC_PRINTF(3, 4) G_GNUC_WARN_UNUSED_RESULT;
151 int virTypedParamListAddLLong(virTypedParamList *list,
152                               long long value,
153                               const char *namefmt,
154                               ...)
155     G_GNUC_PRINTF(3, 4) G_GNUC_WARN_UNUSED_RESULT;
156 int virTypedParamListAddULLong(virTypedParamList *list,
157                                unsigned long long value,
158                                const char *namefmt,
159                                ...)
160     G_GNUC_PRINTF(3, 4) G_GNUC_WARN_UNUSED_RESULT;
161 int virTypedParamListAddString(virTypedParamList *list,
162                                const char *value,
163                                const char *namefmt,
164                                ...)
165     G_GNUC_PRINTF(3, 4) G_GNUC_WARN_UNUSED_RESULT;
166 int virTypedParamListAddBoolean(virTypedParamList *list,
167                                 bool value,
168                                 const char *namefmt,
169                                 ...)
170     G_GNUC_PRINTF(3, 4) G_GNUC_WARN_UNUSED_RESULT;
171 int virTypedParamListAddDouble(virTypedParamList *list,
172                                double value,
173                                const char *namefmt,
174                                ...)
175     G_GNUC_PRINTF(3, 4) G_GNUC_WARN_UNUSED_RESULT;
176