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
30 * Possible operations on PSYC state (persistent) and transient variables (per message).
32 enum GNUNET_ENV_Operator
35 * Set value of a transient variable.
37 GNUNET_ENV_OP_SET = ':',
40 * Assign value for a persistent state variable.
42 * If an assigned value is NULL, the variable is deleted.
44 GNUNET_ENV_OP_ASSIGN = '=',
47 * Augment state variable.
49 * Used for appending strings, adding numbers, and adding new items to a list or dictionary.
51 GNUNET_ENV_OP_AUGMENT = '+',
54 * Diminish state variable.
56 * Used for subtracting numbers, and removing items from a list or dictionary.
58 GNUNET_ENV_OP_DIMINISH = '-',
61 * Update state variable.
63 * Used for modifying a single item of a list or dictionary.
65 GNUNET_ENV_OP_UPDATE = '@',
70 * PSYC variable types.
74 GNUNET_ENV_TYPE_DATA = 0,
75 GNUNET_ENV_TYPE_NUMBER,
82 * PSYC state modifier.
84 struct GNUNET_ENV_Modifier {
88 GNUNET_ENV_Operator oper;
108 * Environment for a message.
110 * Contains modifiers.
112 struct GNUNET_ENV_Environment;
116 * Create an environment.
118 * @return A newly allocated environment.
120 struct GNUNET_ENV_Environment *
121 GNUNET_ENV_environment_create ();
125 * Add an operation on a variable to the environment.
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.
133 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error.
136 GNUNET_ENV_environment_operation (struct GNUNET_ENV_Environment *env,
137 enum GNUNET_ENV_Operator oper,
139 size_t value_size, const void *value);
143 * Get all modifiers in the environment.
145 * FIXME: use an iterator instead, as we'll likely use a SList to store the
146 * modifiers in the environment.
148 * @param env The environment.
149 * @param[out] modifier_count Set to the number of returned modifiers.
151 * @return Array of modifiers.
153 const struct GNUNET_ENV_Modifier *
154 GNUNET_ENV_environment_get_modifiers (const struct GNUNET_ENV_Environment *env,
155 size_t *modifier_count);
159 * Add list of modifiers to the environment.
161 * @param env The environment.
162 * @param modifier_count Number of @a modifiers.
163 * @param modifiers Array of modifiers to add.
165 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error.
168 GNUNET_ENV_environment_set_modifiers (const struct GNUNET_ENV_Environment *env,
169 size_t modifier_count,
170 const struct GNUNET_ENV_Modifier *modifiers);
174 * Destroy an environment.
176 * @param env The environment to destroy.
179 GNUNET_ENV_environment_destroy (struct GNUNET_ENV_Environment *env);
183 * Get the type of variable.
185 * @param name Name of the variable.
187 * @return Variable type.
190 GNUNET_ENV_var_get_type (char *name);
194 * Get the variable's value as an integer.
196 * @param size Size of value.
197 * @param value Raw value of variable.
198 * @param[out] number Value converted to a 64-bit integer.
200 * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
203 GNUNET_ENV_value_to_number (size_t size, const void *value, int64_t *number);
207 * Get the variable's value as a list.
209 * @param size Size of value.
210 * @param value Raw value of variable.
211 * @param[out] list A newly created list holding the elements.
213 * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
216 GNUNET_ENV_value_to_list (size_t size, const void *value, GNUNET_CONTAINER_SList **list);
220 * Get the variable's value as a dictionary.
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.
226 * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
229 GNUNET_ENV_value_to_dict (size_t size, const void *value, GNUNET_CONTAINER_MultiHashMap **dict);
233 * Create a PSYC variable value from an integer.
235 * @param number The number to convert.
236 * @param[out] value_size Size of returned value.
238 * @return A newly allocated value or NULL on error.
241 GNUNET_ENV_value_from_number (int64_t number, size_t *value_size);
245 * Create a PSYC variable value from a list.
247 * @param list The list to convert.
248 * @param[out] value_size Size of returned value.
250 * @return A newly allocated value or NULL on error.
253 GNUNET_ENV_value_from_list (GNUNET_CONTAINER_SList *list, size_t *value_size);
257 * Create a PSYC variable value from a dictionary.
259 * @param dict The dict to convert.
260 * @param[out] value_size Size of returned value.
262 * @return A newly allocated value or NULL on error.
265 GNUNET_ENV_value_from_dict (GNUNET_CONTAINER_MultiHashMap *dict, size_t *value_size);