glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / include / gnunet_psyc_env.h
1 /*
2  * This file is part of GNUnet.
3  * Copyright (C) 2013 GNUnet e.V.
4  *
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.
9  *
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.
14  */
15
16 /**
17  * @author Gabor X Toth
18  *
19  * @file
20  * PSYC Environment library
21  *
22  * @defgroup psyc-util-env  PSYC Utilities library: Environment
23  * Environment data structure operations for PSYC and Social messages.
24  *
25  * Library providing operations for the @e environment of
26  * PSYC and Social messages, and for (de)serializing variable values.
27  *
28  * @{
29  */
30
31
32 #ifndef GNUNET_PSYC_ENV_H
33 #define GNUNET_PSYC_ENV_H
34
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #if 0                           /* keep Emacsens' auto-indent happy */
39 }
40 #endif
41 #endif
42
43
44 /**
45  * Possible operations on PSYC state (persistent) and transient variables (per message).
46  */
47 enum GNUNET_PSYC_Operator
48 {
49   /**
50    * Set value of a transient variable.
51    */
52   GNUNET_PSYC_OP_SET = ':',
53
54   /**
55    * Assign value for a persistent state variable.
56    *
57    * If an assigned value is NULL, the variable is deleted.
58    */
59   GNUNET_PSYC_OP_ASSIGN = '=',
60
61   /**
62    * Augment state variable.
63    *
64    * Used for appending strings, adding numbers, and adding new items to a list or dictionary.
65    */
66   GNUNET_PSYC_OP_AUGMENT = '+',
67
68   /**
69    * Diminish state variable.
70    *
71    * Used for subtracting numbers, and removing items from a list or dictionary.
72    */
73   GNUNET_PSYC_OP_DIMINISH = '-',
74
75   /**
76    * Update state variable.
77    *
78    * Used for modifying a single item of a list or dictionary.
79    */
80   GNUNET_PSYC_OP_UPDATE = '@',
81 };
82
83
84 /**
85  * PSYC variable types.
86  */
87 enum GNUNET_PSYC_Type
88 {
89   GNUNET_PSYC_TYPE_DATA = 0,
90   GNUNET_PSYC_TYPE_NUMBER,
91   GNUNET_PSYC_TYPE_LIST,
92   GNUNET_PSYC_TYPE_DICT
93 };
94
95
96 /**
97  * PSYC state modifier.
98  */
99 struct GNUNET_PSYC_Modifier
100 {
101   /**
102    * State operation.
103    */
104   enum GNUNET_PSYC_Operator oper;
105
106   /**
107    * Variable name.
108    */
109   const char *name;
110
111   /**
112    * Size of @a value.
113    */
114   size_t value_size;
115
116   /**
117    * Value of variable.
118    */
119   const void *value;
120
121   /**
122    * Next modifier.
123    */
124   struct GNUNET_PSYC_Modifier *next;
125
126   /**
127    * Previous modifier.
128    */
129   struct GNUNET_PSYC_Modifier *prev;
130 };
131
132
133 /**
134  * Environment for a message.
135  *
136  * Contains modifiers.
137  */
138 struct GNUNET_PSYC_Environment;
139
140
141 /**
142  * Create an environment.
143  *
144  * @return A newly allocated environment.
145  */
146 struct GNUNET_PSYC_Environment *
147 GNUNET_PSYC_env_create ();
148
149
150 /**
151  * Add a modifier to the environment.
152  *
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.
158  */
159 void
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);
163
164
165 /**
166  * Get the first modifier of the environment.
167  */
168 struct GNUNET_PSYC_Modifier *
169 GNUNET_PSYC_env_head (const struct GNUNET_PSYC_Environment *env);
170
171
172
173 /**
174  * Get the last modifier of the environment.
175  */
176 struct GNUNET_PSYC_Modifier *
177 GNUNET_PSYC_env_tail (const struct GNUNET_PSYC_Environment *env);
178
179
180 /**
181  * Remove a modifier from the environment.
182  */
183 void
184 GNUNET_PSYC_env_remove (struct GNUNET_PSYC_Environment *env,
185                         struct GNUNET_PSYC_Modifier *mod);
186
187
188 /**
189  * Remove a modifier at the beginning of the environment.
190  */
191 int
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);
195
196
197 /**
198  * Iterator for modifiers in the environment.
199  *
200  * @param cls Closure.
201  * @param mod Modifier.
202  *
203  * @return #GNUNET_YES to continue iterating,
204  *         #GNUNET_NO to stop.
205  */
206 typedef int
207 (*GNUNET_PSYC_Iterator) (void *cls, enum GNUNET_PSYC_Operator oper,
208                          const char *name, const char *value,
209                          uint32_t value_size);
210
211
212 /**
213  * Iterate through all modifiers in the environment.
214  *
215  * @param env The environment.
216  * @param it Iterator.
217  * @param it_cls Closure for iterator.
218  */
219 void
220 GNUNET_PSYC_env_iterate (const struct GNUNET_PSYC_Environment *env,
221                          GNUNET_PSYC_Iterator it, void *it_cls);
222
223
224 /**
225  * Get the number of modifiers in the environment.
226  *
227  * @param env The environment.
228  *
229  * @return Number of modifiers.
230  */
231 size_t
232 GNUNET_PSYC_env_get_count (const struct GNUNET_PSYC_Environment *env);
233
234
235 /**
236  * Destroy an environment.
237  *
238  * @param env The environment to destroy.
239  */
240 void
241 GNUNET_PSYC_env_destroy (struct GNUNET_PSYC_Environment *env);
242
243
244 /**
245  * Get the type of variable.
246  *
247  * @param name Name of the variable.
248  *
249  * @return Variable type.
250  */
251 enum GNUNET_PSYC_Type
252 GNUNET_PSYC_var_get_type (char *name);
253
254
255 /**
256  * Perform an operation on a variable.
257  *
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.
266  *
267  * @return #GNUNET_OK on success, else #GNUNET_SYSERR
268  */
269 int
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);
273
274
275 /**
276  * Get the variable's value as an integer.
277  *
278  * @param size Size of value.
279  * @param value Raw value of variable.
280  * @param[out] number Value converted to a 64-bit integer.
281  *
282  * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
283  */
284 int
285 GNUNET_PSYC_value_to_number (size_t size, const void *value, int64_t *number);
286
287
288 /**
289  * Get the variable's value as a dictionary.
290  *
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.
294  *
295  * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
296  */
297 int
298 GNUNET_PSYC_value_to_dict (size_t size, const void *value, struct GNUNET_CONTAINER_MultiHashMap **dict);
299
300
301 /**
302  * Create a PSYC variable value from an integer.
303  *
304  * @param number The number to convert.
305  * @param[out] value_size Size of returned value.
306  *
307  * @return A newly allocated value or NULL on error.
308  */
309 void *
310 GNUNET_PSYC_value_from_number (int64_t number, size_t *value_size);
311
312
313 /**
314  * Create a PSYC variable value from a dictionary.
315  *
316  * @param dict The dict to convert.
317  * @param[out] value_size Size of returned value.
318  *
319  * @return A newly allocated value or NULL on error.
320  */
321 void *
322 GNUNET_PSYC_value_from_dict (struct GNUNET_CONTAINER_MultiHashMap *dict, size_t *value_size);
323
324
325 #if 0                           /* keep Emacsens' auto-indent happy */
326 {
327 #endif
328 #ifdef __cplusplus
329 }
330 #endif
331
332 /* ifndef GNUNET_PSYC_ENV_H */
333 #endif
334
335 /** @} */  /* end of group */