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
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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @author Gabor X Toth
25 * PSYC Environment library
27 * @defgroup psyc-util-env PSYC Utilities library: Environment
28 * Environment data structure operations for PSYC and Social messages.
30 * Library providing operations for the @e environment of
31 * PSYC and Social messages, and for (de)serializing variable values.
37 #ifndef GNUNET_PSYC_ENV_H
38 #define GNUNET_PSYC_ENV_H
43 #if 0 /* keep Emacsens' auto-indent happy */
50 * Possible operations on PSYC state (persistent) and transient variables (per message).
52 enum GNUNET_PSYC_Operator
55 * Set value of a transient variable.
57 GNUNET_PSYC_OP_SET = ':',
60 * Assign value for a persistent state variable.
62 * If an assigned value is NULL, the variable is deleted.
64 GNUNET_PSYC_OP_ASSIGN = '=',
67 * Augment state variable.
69 * Used for appending strings, adding numbers, and adding new items to a list or dictionary.
71 GNUNET_PSYC_OP_AUGMENT = '+',
74 * Diminish state variable.
76 * Used for subtracting numbers, and removing items from a list or dictionary.
78 GNUNET_PSYC_OP_DIMINISH = '-',
81 * Update state variable.
83 * Used for modifying a single item of a list or dictionary.
85 GNUNET_PSYC_OP_UPDATE = '@',
90 * PSYC variable types.
94 GNUNET_PSYC_TYPE_DATA = 0,
95 GNUNET_PSYC_TYPE_NUMBER,
96 GNUNET_PSYC_TYPE_LIST,
102 * PSYC state modifier.
104 struct GNUNET_PSYC_Modifier
109 enum GNUNET_PSYC_Operator oper;
129 struct GNUNET_PSYC_Modifier *next;
134 struct GNUNET_PSYC_Modifier *prev;
139 * Environment for a message.
141 * Contains modifiers.
143 struct GNUNET_PSYC_Environment;
147 * Create an environment.
149 * @return A newly allocated environment.
151 struct GNUNET_PSYC_Environment *
152 GNUNET_PSYC_env_create ();
156 * Add a modifier to the environment.
158 * @param env The environment.
159 * @param oper Operation to perform.
160 * @param name Name of the variable.
161 * @param value Value of the variable.
162 * @param value_size Size of @a value.
165 GNUNET_PSYC_env_add (struct GNUNET_PSYC_Environment *env,
166 enum GNUNET_PSYC_Operator oper, const char *name,
167 const void *value, size_t value_size);
171 * Get the first modifier of the environment.
173 struct GNUNET_PSYC_Modifier *
174 GNUNET_PSYC_env_head (const struct GNUNET_PSYC_Environment *env);
179 * Get the last modifier of the environment.
181 struct GNUNET_PSYC_Modifier *
182 GNUNET_PSYC_env_tail (const struct GNUNET_PSYC_Environment *env);
186 * Remove a modifier from the environment.
189 GNUNET_PSYC_env_remove (struct GNUNET_PSYC_Environment *env,
190 struct GNUNET_PSYC_Modifier *mod);
194 * Remove a modifier at the beginning of the environment.
197 GNUNET_PSYC_env_shift (struct GNUNET_PSYC_Environment *env,
198 enum GNUNET_PSYC_Operator *oper, const char **name,
199 const void **value, size_t *value_size);
203 * Iterator for modifiers in the environment.
205 * @param cls Closure.
206 * @param mod Modifier.
208 * @return #GNUNET_YES to continue iterating,
209 * #GNUNET_NO to stop.
212 (*GNUNET_PSYC_Iterator) (void *cls, enum GNUNET_PSYC_Operator oper,
213 const char *name, const char *value,
214 uint32_t value_size);
218 * Iterate through all modifiers in the environment.
220 * @param env The environment.
221 * @param it Iterator.
222 * @param it_cls Closure for iterator.
225 GNUNET_PSYC_env_iterate (const struct GNUNET_PSYC_Environment *env,
226 GNUNET_PSYC_Iterator it, void *it_cls);
230 * Get the number of modifiers in the environment.
232 * @param env The environment.
234 * @return Number of modifiers.
237 GNUNET_PSYC_env_get_count (const struct GNUNET_PSYC_Environment *env);
241 * Destroy an environment.
243 * @param env The environment to destroy.
246 GNUNET_PSYC_env_destroy (struct GNUNET_PSYC_Environment *env);
250 * Get the type of variable.
252 * @param name Name of the variable.
254 * @return Variable type.
256 enum GNUNET_PSYC_Type
257 GNUNET_PSYC_var_get_type (char *name);
261 * Perform an operation on a variable.
263 * @param name Name of variable.
264 * @param current_value Current value of variable.
265 * @param current_value_size Size of @a current_value.
266 * @param oper Operator.
267 * @param args Arguments for the operation.
268 * @param args_size Size of @a args.
269 * @param return_value Return value.
270 * @param return_value_size Size of @a return_value.
272 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
275 GNUNET_PSYC_operation (char *name, void *current_value, size_t current_value_size,
276 enum GNUNET_PSYC_Operator oper, void *args, size_t args_size,
277 void **return_value, size_t *return_value_size);
281 * Get the variable's value as an integer.
283 * @param size Size of value.
284 * @param value Raw value of variable.
285 * @param[out] number Value converted to a 64-bit integer.
287 * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
290 GNUNET_PSYC_value_to_number (size_t size, const void *value, int64_t *number);
294 * Get the variable's value as a dictionary.
296 * @param size Size of value.
297 * @param value Raw value of variable.
298 * @param[out] dict A newly created hashmap holding the elements of the dictionary.
300 * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
303 GNUNET_PSYC_value_to_dict (size_t size, const void *value, struct GNUNET_CONTAINER_MultiHashMap **dict);
307 * Create a PSYC variable value from an integer.
309 * @param number The number to convert.
310 * @param[out] value_size Size of returned value.
312 * @return A newly allocated value or NULL on error.
315 GNUNET_PSYC_value_from_number (int64_t number, size_t *value_size);
319 * Create a PSYC variable value from a dictionary.
321 * @param dict The dict to convert.
322 * @param[out] value_size Size of returned value.
324 * @return A newly allocated value or NULL on error.
327 GNUNET_PSYC_value_from_dict (struct GNUNET_CONTAINER_MultiHashMap *dict, size_t *value_size);
330 #if 0 /* keep Emacsens' auto-indent happy */
337 /* ifndef GNUNET_PSYC_ENV_H */
340 /** @} */ /* end of group */