2 * This file is part of GNUnet.
3 * (C) 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
29 #ifndef GNUNET_ENV_LIB_H
30 #define GNUNET_ENV_LIB_H
35 #if 0 /* keep Emacsens' auto-indent happy */
42 * Possible operations on PSYC state (persistent) and transient variables (per message).
44 enum GNUNET_ENV_Operator
47 * Set value of a transient variable.
49 GNUNET_ENV_OP_SET = ':',
52 * Assign value for a persistent state variable.
54 * If an assigned value is NULL, the variable is deleted.
56 GNUNET_ENV_OP_ASSIGN = '=',
59 * Augment state variable.
61 * Used for appending strings, adding numbers, and adding new items to a list or dictionary.
63 GNUNET_ENV_OP_AUGMENT = '+',
66 * Diminish state variable.
68 * Used for subtracting numbers, and removing items from a list or dictionary.
70 GNUNET_ENV_OP_DIMINISH = '-',
73 * Update state variable.
75 * Used for modifying a single item of a list or dictionary.
77 GNUNET_ENV_OP_UPDATE = '@',
82 * PSYC variable types.
86 GNUNET_ENV_TYPE_DATA = 0,
87 GNUNET_ENV_TYPE_NUMBER,
94 * PSYC state modifier.
96 struct GNUNET_ENV_Modifier {
100 enum GNUNET_ENV_Operator oper;
120 struct GNUNET_ENV_Modifier *next;
125 struct GNUNET_ENV_Modifier *prev;
130 * Environment for a message.
132 * Contains modifiers.
134 struct GNUNET_ENV_Environment;
138 * Create an environment.
140 * @return A newly allocated environment.
142 struct GNUNET_ENV_Environment *
143 GNUNET_ENV_environment_create ();
147 * Add a modifier to the environment.
149 * @param env The environment.
150 * @param oper Operation to perform.
151 * @param name Name of the variable.
152 * @param value Value of the variable.
153 * @param value_size Size of @a value.
156 GNUNET_ENV_environment_add_mod (struct GNUNET_ENV_Environment *env,
157 enum GNUNET_ENV_Operator oper, const char *name,
158 const void *value, size_t value_size);
162 * Iterator for modifiers in the environment.
164 * @param cls Closure.
165 * @param mod Modifier.
167 * @return #GNUNET_YES to continue iterating,
168 * #GNUNET_NO to stop.
171 (*GNUNET_ENV_Iterator) (void *cls, struct GNUNET_ENV_Modifier *mod);
175 * Iterate through all modifiers in the environment.
177 * @param env The environment.
178 * @param it Iterator.
179 * @param it_cls Closure for iterator.
182 GNUNET_ENV_environment_iterate (const struct GNUNET_ENV_Environment *env,
183 GNUNET_ENV_Iterator it, void *it_cls);
187 * Get the number of modifiers in the environment.
189 * @param env The environment.
191 * @return Number of modifiers.
194 GNUNET_ENV_environment_get_mod_count (const struct GNUNET_ENV_Environment *env);
198 * Destroy an environment.
200 * @param env The environment to destroy.
203 GNUNET_ENV_environment_destroy (struct GNUNET_ENV_Environment *env);
207 * Get the type of variable.
209 * @param name Name of the variable.
211 * @return Variable type.
214 GNUNET_ENV_var_get_type (char *name);
218 * Perform an operation on a variable.
220 * @param name Name of variable.
221 * @param current_value Current value of variable.
222 * @param current_value_size Size of @a current_value.
223 * @param oper Operator.
224 * @param args Arguments for the operation.
225 * @param args_size Size of @a args.
226 * @param return_value Return value.
227 * @param return_value_size Size of @a return_value.
229 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
232 GNUNET_ENV_operation (char *name, void *current_value, size_t current_value_size,
233 enum GNUNET_ENV_Operator oper, void *args, size_t args_size,
234 void **return_value, size_t *return_value_size);
238 * Get the variable's value as an integer.
240 * @param size Size of value.
241 * @param value Raw value of variable.
242 * @param[out] number Value converted to a 64-bit integer.
244 * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
247 GNUNET_ENV_value_to_number (size_t size, const void *value, int64_t *number);
251 * Get the variable's value as a list.
253 * @param size Size of value.
254 * @param value Raw value of variable.
255 * @param[out] list A newly created list holding the elements.
257 * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
260 GNUNET_ENV_value_to_list (size_t size, const void *value, struct GNUNET_CONTAINER_SList **list);
264 * Get the variable's value as a dictionary.
266 * @param size Size of value.
267 * @param value Raw value of variable.
268 * @param[out] dict A newly created hashmap holding the elements of the dictionary.
270 * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
273 GNUNET_ENV_value_to_dict (size_t size, const void *value, struct GNUNET_CONTAINER_MultiHashMap **dict);
277 * Create a PSYC variable value from an integer.
279 * @param number The number to convert.
280 * @param[out] value_size Size of returned value.
282 * @return A newly allocated value or NULL on error.
285 GNUNET_ENV_value_from_number (int64_t number, size_t *value_size);
289 * Create a PSYC variable value from a list.
291 * @param list The list to convert.
292 * @param[out] value_size Size of returned value.
294 * @return A newly allocated value or NULL on error.
297 GNUNET_ENV_value_from_list (struct GNUNET_CONTAINER_SList *list, size_t *value_size);
301 * Create a PSYC variable value from a dictionary.
303 * @param dict The dict to convert.
304 * @param[out] value_size Size of returned value.
306 * @return A newly allocated value or NULL on error.
309 GNUNET_ENV_value_from_dict (struct GNUNET_CONTAINER_MultiHashMap *dict, size_t *value_size);
312 #if 0 /* keep Emacsens' auto-indent happy */
319 /* ifndef GNUNET_ENV_LIB_H */
321 /* end of gnunet_env_lib.h */