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 * Library providing operations for the @e environment of
24 * PSYC and Social messages.
28 #include "gnunet_util_lib.h"
29 #include "gnunet_psyc_env.h"
32 * Environment for a message.
36 struct GNUNET_PSYC_Environment
38 struct GNUNET_PSYC_Modifier *mod_head;
39 struct GNUNET_PSYC_Modifier *mod_tail;
45 * Create an environment.
47 * @return A newly allocated environment.
49 struct GNUNET_PSYC_Environment *
50 GNUNET_PSYC_env_create ()
52 return GNUNET_new (struct GNUNET_PSYC_Environment);
57 * Add a modifier to the environment.
59 * @param env The environment.
60 * @param oper Operation to perform.
61 * @param name Name of the variable.
62 * @param value Value of the variable.
63 * @param value_size Size of @a value.
66 GNUNET_PSYC_env_add (struct GNUNET_PSYC_Environment *env,
67 enum GNUNET_PSYC_Operator oper, const char *name,
68 const void *value, size_t value_size)
70 struct GNUNET_PSYC_Modifier *mod = GNUNET_new (struct GNUNET_PSYC_Modifier);
74 mod->value_size = value_size;
75 GNUNET_CONTAINER_DLL_insert_tail (env->mod_head, env->mod_tail, mod);
81 * Get the first modifier of the environment.
83 struct GNUNET_PSYC_Modifier *
84 GNUNET_PSYC_env_head (const struct GNUNET_PSYC_Environment *env)
91 * Get the last modifier of the environment.
93 struct GNUNET_PSYC_Modifier *
94 GNUNET_PSYC_env_tail (const struct GNUNET_PSYC_Environment *env)
101 * Remove a modifier from the environment.
104 GNUNET_PSYC_env_remove (struct GNUNET_PSYC_Environment *env,
105 struct GNUNET_PSYC_Modifier *mod)
107 GNUNET_CONTAINER_DLL_remove (env->mod_head, env->mod_tail, mod);
112 * Get the modifier at the beginning of an environment and remove it.
123 GNUNET_PSYC_env_shift (struct GNUNET_PSYC_Environment *env,
124 enum GNUNET_PSYC_Operator *oper, const char **name,
125 const void **value, size_t *value_size)
127 if (NULL == env->mod_head)
130 struct GNUNET_PSYC_Modifier *mod = env->mod_head;
134 *value_size = mod->value_size;
136 GNUNET_CONTAINER_DLL_remove (env->mod_head, env->mod_tail, mod);
145 * Iterate through all modifiers in the environment.
147 * @param env The environment.
148 * @param it Iterator.
149 * @param it_cls Closure for iterator.
152 GNUNET_PSYC_env_iterate (const struct GNUNET_PSYC_Environment *env,
153 GNUNET_PSYC_Iterator it, void *it_cls)
155 struct GNUNET_PSYC_Modifier *mod;
156 for (mod = env->mod_head; NULL != mod; mod = mod->next)
157 it (it_cls, mod->oper, mod->name, mod->value, mod->value_size);
162 * Get the number of modifiers in the environment.
164 * @param env The environment.
166 * @return Number of modifiers.
169 GNUNET_PSYC_env_get_count (const struct GNUNET_PSYC_Environment *env)
171 return env->mod_count;
176 * Destroy an environment.
178 * @param env The environment to destroy.
181 GNUNET_PSYC_env_destroy (struct GNUNET_PSYC_Environment *env)
183 struct GNUNET_PSYC_Modifier *mod, *prev = NULL;
184 for (mod = env->mod_head; NULL != mod; mod = mod->next)