missing check
[oweals/gnunet.git] / src / include / gnunet_env_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /** 
22  * @file include/gnunet_env_lib.h
23  * @brief Library providing operations for the @e environment of
24  *        PSYC and Social messages, and for (de)serializing variable values.
25  * @author Gabor X Toth
26  */
27
28
29 /** 
30  * Possible operations on PSYC state (persistent) and transient variables (per message).
31  */
32 enum GNUNET_ENV_Operator
33 {
34   /** 
35    * Set value of a transient variable.
36    */
37   GNUNET_ENV_OP_SET = ':',
38
39   /** 
40    * Assign value for a persistent state variable.
41    *
42    * If an assigned value is NULL, the variable is deleted.
43    */
44   GNUNET_ENV_OP_ASSIGN = '=',
45
46   /** 
47    * Augment state variable.
48    *
49    * Used for appending strings, adding numbers, and adding new items to a list or dictionary.
50    */
51   GNUNET_ENV_OP_AUGMENT = '+',
52
53   /** 
54    * Diminish state variable.
55    *
56    * Used for subtracting numbers, and removing items from a list or dictionary.
57    */
58   GNUNET_ENV_OP_DIMINISH = '-',
59
60   /** 
61    * Update state variable.
62    *
63    * Used for modifying a single item of a list or dictionary.
64    */
65   GNUNET_ENV_OP_UPDATE = '@',
66 };
67
68
69 /** 
70  * PSYC variable types.
71  */
72 enum GNUNET_ENV_Type
73 {
74   GNUNET_ENV_TYPE_DATA = 0,
75   GNUNET_ENV_TYPE_NUMBER,
76   GNUNET_ENV_TYPE_LIST,
77   GNUNET_ENV_TYPE_DICT
78 };
79
80
81 /** 
82  * PSYC state modifier.
83  */
84 struct GNUNET_ENV_Modifier {
85   /** 
86    * State operation.
87    */
88   GNUNET_ENV_Operator oper;
89
90   /** 
91    * Variable name.
92    */
93   const char *name;
94
95   /** 
96    * Size of @a value.
97    */
98   size_t value_size;
99
100   /** 
101    * Value of variable.
102    */
103   const void *value;
104 };
105
106
107 /** 
108  * Environment for a message.
109  *
110  * Contains modifiers.
111  */
112 struct GNUNET_ENV_Environment;
113
114
115 /** 
116  * Create an environment.
117  * 
118  * @return A newly allocated environment.
119  */
120 struct GNUNET_ENV_Environment *
121 GNUNET_ENV_environment_create ();
122
123
124 /** 
125  * Add an operation on a variable to the environment.
126  *
127  * @param env The environment.
128  * @param oper Operation to perform.
129  * @param name Name of the variable.
130  * @param value_size Size of @a value.
131  * @param value Value of the variable.
132  * 
133  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error.
134  */
135 int
136 GNUNET_ENV_environment_operation (struct GNUNET_ENV_Environment *env,
137                                   enum GNUNET_ENV_Operator oper,
138                                   const char *name,
139                                   size_t value_size, const void *value);
140
141
142 /** 
143  * Get all modifiers in the environment.
144  *
145  * FIXME: use an iterator instead, as we'll likely use a SList to store the
146  *        modifiers in the environment.
147  *
148  * @param env The environment.
149  * @param[out] modifier_count Set to the number of returned modifiers.
150  * 
151  * @return Array of modifiers.
152  */
153 const struct GNUNET_ENV_Modifier *
154 GNUNET_ENV_environment_get_modifiers (const struct GNUNET_ENV_Environment *env,
155                                       size_t *modifier_count);
156
157
158 /** 
159  * Add list of modifiers to the environment.
160  *
161  * @param env The environment.
162  * @param modifier_count Number of @a modifiers.
163  * @param modifiers Array of modifiers to add.
164  * 
165  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error.
166  */
167 int
168 GNUNET_ENV_environment_set_modifiers (const struct GNUNET_ENV_Environment *env,
169                                       size_t modifier_count,
170                                       const struct GNUNET_ENV_Modifier *modifiers);
171
172
173 /** 
174  * Destroy an environment.
175  *
176  * @param env The environment to destroy.
177  */
178 void
179 GNUNET_ENV_environment_destroy (struct GNUNET_ENV_Environment *env);
180
181
182 /** 
183  * Get the type of variable.
184  *
185  * @param name Name of the variable.
186  * 
187  * @return Variable type.
188  */
189 enum GNUNET_ENV_Type
190 GNUNET_ENV_var_get_type (char *name);
191
192
193 /** 
194  * Get the variable's value as an integer.
195  *
196  * @param size Size of value.
197  * @param value Raw value of variable.
198  * @param[out] number Value converted to a 64-bit integer.
199  *
200  * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
201  */
202 int
203 GNUNET_ENV_value_to_number (size_t size, const void *value, int64_t *number);
204
205
206 /** 
207  * Get the variable's value as a list.
208  *
209  * @param size Size of value.
210  * @param value Raw value of variable.
211  * @param[out] list A newly created list holding the elements.
212  *
213  * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
214  */
215 int
216 GNUNET_ENV_value_to_list (size_t size, const void *value, GNUNET_CONTAINER_SList **list);
217
218
219 /** 
220  * Get the variable's value as a dictionary.
221  *
222  * @param size Size of value.
223  * @param value Raw value of variable.
224  * @param[out] dict A newly created hashmap holding the elements of the dictionary.
225  *
226  * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
227  */
228 int
229 GNUNET_ENV_value_to_dict (size_t size, const void *value, GNUNET_CONTAINER_MultiHashMap **dict);
230
231
232 /** 
233  * Create a PSYC variable value from an integer.
234  *
235  * @param number The number to convert.
236  * @param[out] value_size Size of returned value.
237  * 
238  * @return A newly allocated value or NULL on error.
239  */
240 void *
241 GNUNET_ENV_value_from_number (int64_t number, size_t *value_size);
242
243
244 /** 
245  * Create a PSYC variable value from a list.
246  *
247  * @param list The list to convert.
248  * @param[out] value_size Size of returned value.
249  * 
250  * @return A newly allocated value or NULL on error.
251  */
252 void *
253 GNUNET_ENV_value_from_list (GNUNET_CONTAINER_SList *list, size_t *value_size);
254
255
256 /** 
257  * Create a PSYC variable value from a dictionary.
258  *
259  * @param dict The dict to convert.
260  * @param[out] value_size Size of returned value.
261  * 
262  * @return A newly allocated value or NULL on error.
263  */
264 void *
265 GNUNET_ENV_value_from_dict (GNUNET_CONTAINER_MultiHashMap *dict, size_t *value_size);