2 * This file is part of GNUnet.
3 * Copyright (C) 2013 GNUnet e.V.
5 * GNUnet is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License,
8 * or (at your 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 * Affero General Public License for more details.
17 * @author Gabor X Toth
20 * PSYC Environment library
22 * @defgroup psyc-util-env PSYC Utilities library: Environment
23 * Environment data structure operations for PSYC and Social messages.
25 * Library providing operations for the @e environment of
26 * PSYC and Social messages, and for (de)serializing variable values.
32 #ifndef GNUNET_PSYC_ENV_H
33 #define GNUNET_PSYC_ENV_H
38 #if 0 /* keep Emacsens' auto-indent happy */
45 * Possible operations on PSYC state (persistent) and transient variables (per message).
47 enum GNUNET_PSYC_Operator
50 * Set value of a transient variable.
52 GNUNET_PSYC_OP_SET = ':',
55 * Assign value for a persistent state variable.
57 * If an assigned value is NULL, the variable is deleted.
59 GNUNET_PSYC_OP_ASSIGN = '=',
62 * Augment state variable.
64 * Used for appending strings, adding numbers, and adding new items to a list or dictionary.
66 GNUNET_PSYC_OP_AUGMENT = '+',
69 * Diminish state variable.
71 * Used for subtracting numbers, and removing items from a list or dictionary.
73 GNUNET_PSYC_OP_DIMINISH = '-',
76 * Update state variable.
78 * Used for modifying a single item of a list or dictionary.
80 GNUNET_PSYC_OP_UPDATE = '@',
85 * PSYC variable types.
89 GNUNET_PSYC_TYPE_DATA = 0,
90 GNUNET_PSYC_TYPE_NUMBER,
91 GNUNET_PSYC_TYPE_LIST,
97 * PSYC state modifier.
99 struct GNUNET_PSYC_Modifier
104 enum GNUNET_PSYC_Operator oper;
124 struct GNUNET_PSYC_Modifier *next;
129 struct GNUNET_PSYC_Modifier *prev;
134 * Environment for a message.
136 * Contains modifiers.
138 struct GNUNET_PSYC_Environment;
142 * Create an environment.
144 * @return A newly allocated environment.
146 struct GNUNET_PSYC_Environment *
147 GNUNET_PSYC_env_create ();
151 * Add a modifier to the environment.
153 * @param env The environment.
154 * @param oper Operation to perform.
155 * @param name Name of the variable.
156 * @param value Value of the variable.
157 * @param value_size Size of @a value.
160 GNUNET_PSYC_env_add (struct GNUNET_PSYC_Environment *env,
161 enum GNUNET_PSYC_Operator oper, const char *name,
162 const void *value, size_t value_size);
166 * Get the first modifier of the environment.
168 struct GNUNET_PSYC_Modifier *
169 GNUNET_PSYC_env_head (const struct GNUNET_PSYC_Environment *env);
174 * Get the last modifier of the environment.
176 struct GNUNET_PSYC_Modifier *
177 GNUNET_PSYC_env_tail (const struct GNUNET_PSYC_Environment *env);
181 * Remove a modifier from the environment.
184 GNUNET_PSYC_env_remove (struct GNUNET_PSYC_Environment *env,
185 struct GNUNET_PSYC_Modifier *mod);
189 * Remove a modifier at the beginning of the environment.
192 GNUNET_PSYC_env_shift (struct GNUNET_PSYC_Environment *env,
193 enum GNUNET_PSYC_Operator *oper, const char **name,
194 const void **value, size_t *value_size);
198 * Iterator for modifiers in the environment.
200 * @param cls Closure.
201 * @param mod Modifier.
203 * @return #GNUNET_YES to continue iterating,
204 * #GNUNET_NO to stop.
207 (*GNUNET_PSYC_Iterator) (void *cls, enum GNUNET_PSYC_Operator oper,
208 const char *name, const char *value,
209 uint32_t value_size);
213 * Iterate through all modifiers in the environment.
215 * @param env The environment.
216 * @param it Iterator.
217 * @param it_cls Closure for iterator.
220 GNUNET_PSYC_env_iterate (const struct GNUNET_PSYC_Environment *env,
221 GNUNET_PSYC_Iterator it, void *it_cls);
225 * Get the number of modifiers in the environment.
227 * @param env The environment.
229 * @return Number of modifiers.
232 GNUNET_PSYC_env_get_count (const struct GNUNET_PSYC_Environment *env);
236 * Destroy an environment.
238 * @param env The environment to destroy.
241 GNUNET_PSYC_env_destroy (struct GNUNET_PSYC_Environment *env);
245 * Get the type of variable.
247 * @param name Name of the variable.
249 * @return Variable type.
251 enum GNUNET_PSYC_Type
252 GNUNET_PSYC_var_get_type (char *name);
256 * Perform an operation on a variable.
258 * @param name Name of variable.
259 * @param current_value Current value of variable.
260 * @param current_value_size Size of @a current_value.
261 * @param oper Operator.
262 * @param args Arguments for the operation.
263 * @param args_size Size of @a args.
264 * @param return_value Return value.
265 * @param return_value_size Size of @a return_value.
267 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
270 GNUNET_PSYC_operation (char *name, void *current_value, size_t current_value_size,
271 enum GNUNET_PSYC_Operator oper, void *args, size_t args_size,
272 void **return_value, size_t *return_value_size);
276 * Get the variable's value as an integer.
278 * @param size Size of value.
279 * @param value Raw value of variable.
280 * @param[out] number Value converted to a 64-bit integer.
282 * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
285 GNUNET_PSYC_value_to_number (size_t size, const void *value, int64_t *number);
289 * Get the variable's value as a dictionary.
291 * @param size Size of value.
292 * @param value Raw value of variable.
293 * @param[out] dict A newly created hashmap holding the elements of the dictionary.
295 * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
298 GNUNET_PSYC_value_to_dict (size_t size, const void *value, struct GNUNET_CONTAINER_MultiHashMap **dict);
302 * Create a PSYC variable value from an integer.
304 * @param number The number to convert.
305 * @param[out] value_size Size of returned value.
307 * @return A newly allocated value or NULL on error.
310 GNUNET_PSYC_value_from_number (int64_t number, size_t *value_size);
314 * Create a PSYC variable value from a dictionary.
316 * @param dict The dict to convert.
317 * @param[out] value_size Size of returned value.
319 * @return A newly allocated value or NULL on error.
322 GNUNET_PSYC_value_from_dict (struct GNUNET_CONTAINER_MultiHashMap *dict, size_t *value_size);
325 #if 0 /* keep Emacsens' auto-indent happy */
332 /* ifndef GNUNET_PSYC_ENV_H */
335 /** @} */ /* end of group */