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.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @author Gabor X Toth
23 * PSYC Environment library
25 * @defgroup psyc-util-env PSYC Utilities library: Environment
26 * Environment data structure operations for PSYC and Social messages.
28 * Library providing operations for the @e environment of
29 * PSYC and Social messages, and for (de)serializing variable values.
35 #ifndef GNUNET_PSYC_ENV_H
36 #define GNUNET_PSYC_ENV_H
41 #if 0 /* keep Emacsens' auto-indent happy */
48 * Possible operations on PSYC state (persistent) and transient variables (per message).
50 enum GNUNET_PSYC_Operator
53 * Set value of a transient variable.
55 GNUNET_PSYC_OP_SET = ':',
58 * Assign value for a persistent state variable.
60 * If an assigned value is NULL, the variable is deleted.
62 GNUNET_PSYC_OP_ASSIGN = '=',
65 * Augment state variable.
67 * Used for appending strings, adding numbers, and adding new items to a list or dictionary.
69 GNUNET_PSYC_OP_AUGMENT = '+',
72 * Diminish state variable.
74 * Used for subtracting numbers, and removing items from a list or dictionary.
76 GNUNET_PSYC_OP_DIMINISH = '-',
79 * Update state variable.
81 * Used for modifying a single item of a list or dictionary.
83 GNUNET_PSYC_OP_UPDATE = '@',
88 * PSYC variable types.
92 GNUNET_PSYC_TYPE_DATA = 0,
93 GNUNET_PSYC_TYPE_NUMBER,
94 GNUNET_PSYC_TYPE_LIST,
100 * PSYC state modifier.
102 struct GNUNET_PSYC_Modifier
107 enum GNUNET_PSYC_Operator oper;
127 struct GNUNET_PSYC_Modifier *next;
132 struct GNUNET_PSYC_Modifier *prev;
137 * Environment for a message.
139 * Contains modifiers.
141 struct GNUNET_PSYC_Environment;
145 * Create an environment.
147 * @return A newly allocated environment.
149 struct GNUNET_PSYC_Environment *
150 GNUNET_PSYC_env_create ();
154 * Add a modifier to the environment.
156 * @param env The environment.
157 * @param oper Operation to perform.
158 * @param name Name of the variable.
159 * @param value Value of the variable.
160 * @param value_size Size of @a value.
163 GNUNET_PSYC_env_add (struct GNUNET_PSYC_Environment *env,
164 enum GNUNET_PSYC_Operator oper, const char *name,
165 const void *value, size_t value_size);
169 * Get the first modifier of the environment.
171 struct GNUNET_PSYC_Modifier *
172 GNUNET_PSYC_env_head (const struct GNUNET_PSYC_Environment *env);
177 * Get the last modifier of the environment.
179 struct GNUNET_PSYC_Modifier *
180 GNUNET_PSYC_env_tail (const struct GNUNET_PSYC_Environment *env);
184 * Remove a modifier from the environment.
187 GNUNET_PSYC_env_remove (struct GNUNET_PSYC_Environment *env,
188 struct GNUNET_PSYC_Modifier *mod);
192 * Remove a modifier at the beginning of the environment.
195 GNUNET_PSYC_env_shift (struct GNUNET_PSYC_Environment *env,
196 enum GNUNET_PSYC_Operator *oper, const char **name,
197 const void **value, size_t *value_size);
201 * Iterator for modifiers in the environment.
203 * @param cls Closure.
204 * @param mod Modifier.
206 * @return #GNUNET_YES to continue iterating,
207 * #GNUNET_NO to stop.
210 (*GNUNET_PSYC_Iterator) (void *cls, enum GNUNET_PSYC_Operator oper,
211 const char *name, const char *value,
212 uint32_t value_size);
216 * Iterate through all modifiers in the environment.
218 * @param env The environment.
219 * @param it Iterator.
220 * @param it_cls Closure for iterator.
223 GNUNET_PSYC_env_iterate (const struct GNUNET_PSYC_Environment *env,
224 GNUNET_PSYC_Iterator it, void *it_cls);
228 * Get the number of modifiers in the environment.
230 * @param env The environment.
232 * @return Number of modifiers.
235 GNUNET_PSYC_env_get_count (const struct GNUNET_PSYC_Environment *env);
239 * Destroy an environment.
241 * @param env The environment to destroy.
244 GNUNET_PSYC_env_destroy (struct GNUNET_PSYC_Environment *env);
248 * Get the type of variable.
250 * @param name Name of the variable.
252 * @return Variable type.
254 enum GNUNET_PSYC_Type
255 GNUNET_PSYC_var_get_type (char *name);
259 * Perform an operation on a variable.
261 * @param name Name of variable.
262 * @param current_value Current value of variable.
263 * @param current_value_size Size of @a current_value.
264 * @param oper Operator.
265 * @param args Arguments for the operation.
266 * @param args_size Size of @a args.
267 * @param return_value Return value.
268 * @param return_value_size Size of @a return_value.
270 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
273 GNUNET_PSYC_operation (char *name, void *current_value, size_t current_value_size,
274 enum GNUNET_PSYC_Operator oper, void *args, size_t args_size,
275 void **return_value, size_t *return_value_size);
279 * Get the variable's value as an integer.
281 * @param size Size of value.
282 * @param value Raw value of variable.
283 * @param[out] number Value converted to a 64-bit integer.
285 * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
288 GNUNET_PSYC_value_to_number (size_t size, const void *value, int64_t *number);
292 * Get the variable's value as a dictionary.
294 * @param size Size of value.
295 * @param value Raw value of variable.
296 * @param[out] dict A newly created hashmap holding the elements of the dictionary.
298 * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
301 GNUNET_PSYC_value_to_dict (size_t size, const void *value, struct GNUNET_CONTAINER_MultiHashMap **dict);
305 * Create a PSYC variable value from an integer.
307 * @param number The number to convert.
308 * @param[out] value_size Size of returned value.
310 * @return A newly allocated value or NULL on error.
313 GNUNET_PSYC_value_from_number (int64_t number, size_t *value_size);
317 * Create a PSYC variable value from a dictionary.
319 * @param dict The dict to convert.
320 * @param[out] value_size Size of returned value.
322 * @return A newly allocated value or NULL on error.
325 GNUNET_PSYC_value_from_dict (struct GNUNET_CONTAINER_MultiHashMap *dict, size_t *value_size);
328 #if 0 /* keep Emacsens' auto-indent happy */
335 /* ifndef GNUNET_PSYC_ENV_H */
338 /** @} */ /* end of group */