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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @author Gabor X Toth
25 * Library providing operations for the @e environment of
26 * PSYC and Social messages.
30 #include "gnunet_util_lib.h"
31 #include "gnunet_psyc_env.h"
34 * Environment for a message.
38 struct GNUNET_PSYC_Environment
40 struct GNUNET_PSYC_Modifier *mod_head;
41 struct GNUNET_PSYC_Modifier *mod_tail;
47 * Create an environment.
49 * @return A newly allocated environment.
51 struct GNUNET_PSYC_Environment *
52 GNUNET_PSYC_env_create ()
54 return GNUNET_new (struct GNUNET_PSYC_Environment);
59 * Add a modifier to the environment.
61 * @param env The environment.
62 * @param oper Operation to perform.
63 * @param name Name of the variable.
64 * @param value Value of the variable.
65 * @param value_size Size of @a value.
68 GNUNET_PSYC_env_add (struct GNUNET_PSYC_Environment *env,
69 enum GNUNET_PSYC_Operator oper, const char *name,
70 const void *value, size_t value_size)
72 struct GNUNET_PSYC_Modifier *mod = GNUNET_new (struct GNUNET_PSYC_Modifier);
76 mod->value_size = value_size;
77 GNUNET_CONTAINER_DLL_insert_tail (env->mod_head, env->mod_tail, mod);
83 * Get the first modifier of the environment.
85 struct GNUNET_PSYC_Modifier *
86 GNUNET_PSYC_env_head (const struct GNUNET_PSYC_Environment *env)
93 * Get the last modifier of the environment.
95 struct GNUNET_PSYC_Modifier *
96 GNUNET_PSYC_env_tail (const struct GNUNET_PSYC_Environment *env)
103 * Remove a modifier from the environment.
106 GNUNET_PSYC_env_remove (struct GNUNET_PSYC_Environment *env,
107 struct GNUNET_PSYC_Modifier *mod)
109 GNUNET_CONTAINER_DLL_remove (env->mod_head, env->mod_tail, mod);
114 * Get the modifier at the beginning of an environment and remove it.
125 GNUNET_PSYC_env_shift (struct GNUNET_PSYC_Environment *env,
126 enum GNUNET_PSYC_Operator *oper, const char **name,
127 const void **value, size_t *value_size)
129 if (NULL == env->mod_head)
132 struct GNUNET_PSYC_Modifier *mod = env->mod_head;
136 *value_size = mod->value_size;
138 GNUNET_CONTAINER_DLL_remove (env->mod_head, env->mod_tail, mod);
147 * Iterate through all modifiers in the environment.
149 * @param env The environment.
150 * @param it Iterator.
151 * @param it_cls Closure for iterator.
154 GNUNET_PSYC_env_iterate (const struct GNUNET_PSYC_Environment *env,
155 GNUNET_PSYC_Iterator it, void *it_cls)
157 struct GNUNET_PSYC_Modifier *mod;
158 for (mod = env->mod_head; NULL != mod; mod = mod->next)
159 it (it_cls, mod->oper, mod->name, mod->value, mod->value_size);
164 * Get the number of modifiers in the environment.
166 * @param env The environment.
168 * @return Number of modifiers.
171 GNUNET_PSYC_env_get_count (const struct GNUNET_PSYC_Environment *env)
173 return env->mod_count;
178 * Destroy an environment.
180 * @param env The environment to destroy.
183 GNUNET_PSYC_env_destroy (struct GNUNET_PSYC_Environment *env)
185 struct GNUNET_PSYC_Modifier *mod, *prev = NULL;
186 for (mod = env->mod_head; NULL != mod; mod = mod->next)