- rename
[oweals/gnunet.git] / src / env / env.c
1 /*
2  * This file is part of GNUnet.
3  * (C) 2013 Christian Grothoff (and other contributing authors)
4  *
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.
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  * General Public License for more details.
14  *
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.
19  */
20
21 /**
22  * @file env/env.c
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
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_env_lib.h"
31
32 /**
33  * Environment for a message.
34  *
35  * Contains modifiers.
36  */
37 struct GNUNET_ENV_Environment
38 {
39   struct GNUNET_ENV_Modifier *mod_head;
40   struct GNUNET_ENV_Modifier *mod_tail;
41   size_t mod_count;
42 };
43
44
45 /**
46  * Create an environment.
47  *
48  * @return A newly allocated environment.
49  */
50 struct GNUNET_ENV_Environment *
51 GNUNET_ENV_environment_create ()
52 {
53   return GNUNET_new (struct GNUNET_ENV_Environment);
54 }
55
56
57 /**
58  * Add a modifier to the environment.
59  *
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.
65  */
66 void
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)
70 {
71   struct GNUNET_ENV_Modifier *mod = GNUNET_new (struct GNUNET_ENV_Modifier);
72   mod->oper = oper;
73   mod->name = name;
74   mod->value = value;
75   mod->value_size = value_size;
76   GNUNET_CONTAINER_DLL_insert_tail (env->mod_head, env->mod_tail, mod);
77   env->mod_count++;
78 }
79
80
81 /**
82  * Get the first modifier of the environment.
83  */
84 struct GNUNET_ENV_Modifier *
85 GNUNET_ENV_environment_head (const struct GNUNET_ENV_Environment *env)
86 {
87   return env->mod_head;
88 }
89
90
91 /**
92  * Get the last modifier of the environment.
93  */
94 struct GNUNET_ENV_Modifier *
95 GNUNET_ENV_environment_tail (const struct GNUNET_ENV_Environment *env)
96 {
97   return env->mod_tail;
98 }
99
100
101 /**
102  * Remove a modifier from the environment.
103  */
104 void
105 GNUNET_ENV_environment_remove (struct GNUNET_ENV_Environment *env,
106                                struct GNUNET_ENV_Modifier *mod)
107 {
108   GNUNET_CONTAINER_DLL_remove (env->mod_head, env->mod_tail, mod);
109 }
110
111
112 /**
113  * Get the modifier at the beginning of an environment and remove it.
114  *
115  * @param env
116  * @param oper
117  * @param name
118  * @param value
119  * @param value_size
120  *
121  * @return
122  */
123 int
124 GNUNET_ENV_environment_shift (struct GNUNET_ENV_Environment *env,
125                               enum GNUNET_ENV_Operator *oper, const char **name,
126                               const void **value, size_t *value_size)
127 {
128   if (NULL == env->mod_head)
129     return GNUNET_NO;
130
131   struct GNUNET_ENV_Modifier *mod = env->mod_head;
132   *oper = mod->oper;
133   *name = mod->name;
134   *value = mod->value;
135   *value_size = mod->value_size;
136
137   GNUNET_CONTAINER_DLL_remove (env->mod_head, env->mod_tail, mod);
138   GNUNET_free (mod);
139   env->mod_count--;
140
141   return GNUNET_YES;
142 }
143
144
145 /**
146  * Iterate through all modifiers in the environment.
147  *
148  * @param env The environment.
149  * @param it Iterator.
150  * @param it_cls Closure for iterator.
151  */
152 void
153 GNUNET_ENV_environment_iterate (const struct GNUNET_ENV_Environment *env,
154                                 GNUNET_ENV_Iterator it, void *it_cls)
155 {
156   struct GNUNET_ENV_Modifier *mod;
157   for (mod = env->mod_head; NULL != mod; mod = mod->next)
158     it (it_cls, mod->oper, mod->name, mod->value, mod->value_size);
159 }
160
161
162 /**
163  * Get the number of modifiers in the environment.
164  *
165  * @param env The environment.
166  *
167  * @return Number of modifiers.
168  */
169 size_t
170 GNUNET_ENV_environment_get_count (const struct GNUNET_ENV_Environment *env)
171 {
172   return env->mod_count;
173 }
174
175
176 /**
177  * Destroy an environment.
178  *
179  * @param env The environment to destroy.
180  */
181 void
182 GNUNET_ENV_environment_destroy (struct GNUNET_ENV_Environment *env)
183 {
184   struct GNUNET_ENV_Modifier *mod, *prev = NULL;
185   for (mod = env->mod_head; NULL != mod; mod = mod->next)
186   {
187     if (NULL != prev)
188       GNUNET_free (prev);
189     prev = mod;
190   }
191   if (NULL != prev)
192     GNUNET_free (prev);
193
194   GNUNET_free (env);
195 }