1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
/*
* This file is part of GNUnet.
* Copyright (C) 2013 GNUnet e.V.
*
* GNUnet is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 3, or (at your
* option) any later version.
*
* GNUnet is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNUnet; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* @author Gabor X Toth
*
* @file
* Library providing operations for the @e environment of
* PSYC and Social messages.
*/
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_psyc_env.h"
/**
* Environment for a message.
*
* Contains modifiers.
*/
struct GNUNET_PSYC_Environment
{
struct GNUNET_PSYC_Modifier *mod_head;
struct GNUNET_PSYC_Modifier *mod_tail;
size_t mod_count;
};
/**
* Create an environment.
*
* @return A newly allocated environment.
*/
struct GNUNET_PSYC_Environment *
GNUNET_PSYC_env_create ()
{
return GNUNET_new (struct GNUNET_PSYC_Environment);
}
/**
* Add a modifier to the environment.
*
* @param env The environment.
* @param oper Operation to perform.
* @param name Name of the variable.
* @param value Value of the variable.
* @param value_size Size of @a value.
*/
void
GNUNET_PSYC_env_add (struct GNUNET_PSYC_Environment *env,
enum GNUNET_PSYC_Operator oper, const char *name,
const void *value, size_t value_size)
{
struct GNUNET_PSYC_Modifier *mod = GNUNET_new (struct GNUNET_PSYC_Modifier);
mod->oper = oper;
mod->name = name;
mod->value = value;
mod->value_size = value_size;
GNUNET_CONTAINER_DLL_insert_tail (env->mod_head, env->mod_tail, mod);
env->mod_count++;
}
/**
* Get the first modifier of the environment.
*/
struct GNUNET_PSYC_Modifier *
GNUNET_PSYC_env_head (const struct GNUNET_PSYC_Environment *env)
{
return env->mod_head;
}
/**
* Get the last modifier of the environment.
*/
struct GNUNET_PSYC_Modifier *
GNUNET_PSYC_env_tail (const struct GNUNET_PSYC_Environment *env)
{
return env->mod_tail;
}
/**
* Remove a modifier from the environment.
*/
void
GNUNET_PSYC_env_remove (struct GNUNET_PSYC_Environment *env,
struct GNUNET_PSYC_Modifier *mod)
{
GNUNET_CONTAINER_DLL_remove (env->mod_head, env->mod_tail, mod);
}
/**
* Get the modifier at the beginning of an environment and remove it.
*
* @param env
* @param oper
* @param name
* @param value
* @param value_size
*
* @return
*/
int
GNUNET_PSYC_env_shift (struct GNUNET_PSYC_Environment *env,
enum GNUNET_PSYC_Operator *oper, const char **name,
const void **value, size_t *value_size)
{
if (NULL == env->mod_head)
return GNUNET_NO;
struct GNUNET_PSYC_Modifier *mod = env->mod_head;
*oper = mod->oper;
*name = mod->name;
*value = mod->value;
*value_size = mod->value_size;
GNUNET_CONTAINER_DLL_remove (env->mod_head, env->mod_tail, mod);
GNUNET_free (mod);
env->mod_count--;
return GNUNET_YES;
}
/**
* Iterate through all modifiers in the environment.
*
* @param env The environment.
* @param it Iterator.
* @param it_cls Closure for iterator.
*/
void
GNUNET_PSYC_env_iterate (const struct GNUNET_PSYC_Environment *env,
GNUNET_PSYC_Iterator it, void *it_cls)
{
struct GNUNET_PSYC_Modifier *mod;
for (mod = env->mod_head; NULL != mod; mod = mod->next)
it (it_cls, mod->oper, mod->name, mod->value, mod->value_size);
}
/**
* Get the number of modifiers in the environment.
*
* @param env The environment.
*
* @return Number of modifiers.
*/
size_t
GNUNET_PSYC_env_get_count (const struct GNUNET_PSYC_Environment *env)
{
return env->mod_count;
}
/**
* Destroy an environment.
*
* @param env The environment to destroy.
*/
void
GNUNET_PSYC_env_destroy (struct GNUNET_PSYC_Environment *env)
{
struct GNUNET_PSYC_Modifier *mod, *prev = NULL;
for (mod = env->mod_head; NULL != mod; mod = mod->next)
{
if (NULL != prev)
GNUNET_free (prev);
prev = mod;
}
if (NULL != prev)
GNUNET_free (prev);
GNUNET_free (env);
}
|