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.
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
29 #include "gnunet_util_lib.h"
30 #include "gnunet_env_lib.h"
33 * Environment for a message.
37 struct GNUNET_ENV_Environment
39 struct GNUNET_ENV_Modifier *mod_head;
40 struct GNUNET_ENV_Modifier *mod_tail;
46 * Create an environment.
48 * @return A newly allocated environment.
50 struct GNUNET_ENV_Environment *
51 GNUNET_ENV_environment_create ()
53 return GNUNET_new (struct GNUNET_ENV_Environment);
58 * Add a modifier to the environment.
60 * @param env The environment.
61 * @param oper Operation to perform.
62 * @param name Name of the variable.
63 * @param value Value of the variable.
64 * @param value_size Size of @a value.
67 GNUNET_ENV_environment_add (struct GNUNET_ENV_Environment *env,
68 enum GNUNET_ENV_Operator oper, const char *name,
69 const void *value, size_t value_size)
71 struct GNUNET_ENV_Modifier *mod = GNUNET_new (struct GNUNET_ENV_Modifier);
75 mod->value_size = value_size;
76 GNUNET_CONTAINER_DLL_insert_tail (env->mod_head, env->mod_tail, mod);
82 * Get the modifier at the beginning of an environment.
93 GNUNET_ENV_environment_head (struct GNUNET_ENV_Environment *env,
94 enum GNUNET_ENV_Operator *oper, const char **name,
95 const void **value, size_t *value_size)
97 if (NULL == env->mod_head)
100 struct GNUNET_ENV_Modifier *mod = env->mod_head;
104 *value_size = mod->value_size;
110 * Get the modifier at the beginning of an environment and remove it.
121 GNUNET_ENV_environment_shift (struct GNUNET_ENV_Environment *env,
122 enum GNUNET_ENV_Operator *oper, const char **name,
123 const void **value, size_t *value_size)
125 if (NULL == env->mod_head)
128 struct GNUNET_ENV_Modifier *mod = env->mod_head;
132 *value_size = mod->value_size;
134 GNUNET_CONTAINER_DLL_remove (env->mod_head, env->mod_tail, mod);
143 * Iterate through all modifiers in the environment.
145 * @param env The environment.
146 * @param it Iterator.
147 * @param it_cls Closure for iterator.
150 GNUNET_ENV_environment_iterate (const struct GNUNET_ENV_Environment *env,
151 GNUNET_ENV_Iterator it, void *it_cls)
153 struct GNUNET_ENV_Modifier *mod;
154 for (mod = env->mod_head; NULL != mod; mod = mod->next)
155 it (it_cls, mod->oper, mod->name, mod->value, mod->value_size);
160 * Get the number of modifiers in the environment.
162 * @param env The environment.
164 * @return Number of modifiers.
167 GNUNET_ENV_environment_get_count (const struct GNUNET_ENV_Environment *env)
169 return env->mod_count;
174 * Destroy an environment.
176 * @param env The environment to destroy.
179 GNUNET_ENV_environment_destroy (struct GNUNET_ENV_Environment *env)
181 struct GNUNET_ENV_Modifier *mod, *prev = NULL;
182 for (mod = env->mod_head; NULL != mod; mod = mod->next)