diff options
Diffstat (limited to 'src/env/env.c')
-rw-r--r-- | src/env/env.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/src/env/env.c b/src/env/env.c new file mode 100644 index 000000000..c977572ab --- /dev/null +++ b/src/env/env.c | |||
@@ -0,0 +1,131 @@ | |||
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_mod (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_malloc (sizeof (*mod)); | ||
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 | * Iterate through all modifiers in the environment. | ||
83 | * | ||
84 | * @param env The environment. | ||
85 | * @param it Iterator. | ||
86 | * @param it_cls Closure for iterator. | ||
87 | */ | ||
88 | void | ||
89 | GNUNET_ENV_environment_iterate (const struct GNUNET_ENV_Environment *env, | ||
90 | GNUNET_ENV_Iterator it, void *it_cls) | ||
91 | { | ||
92 | struct GNUNET_ENV_Modifier *mod; | ||
93 | for (mod = env->mod_head; NULL != mod; mod = mod->next) | ||
94 | it (it_cls, mod); | ||
95 | } | ||
96 | |||
97 | |||
98 | /** | ||
99 | * Get the number of modifiers in the environment. | ||
100 | * | ||
101 | * @param env The environment. | ||
102 | * | ||
103 | * @return Number of modifiers. | ||
104 | */ | ||
105 | size_t | ||
106 | GNUNET_ENV_environment_get_mod_count (const struct GNUNET_ENV_Environment *env) | ||
107 | { | ||
108 | return env->mod_count; | ||
109 | } | ||
110 | |||
111 | |||
112 | /** | ||
113 | * Destroy an environment. | ||
114 | * | ||
115 | * @param env The environment to destroy. | ||
116 | */ | ||
117 | void | ||
118 | GNUNET_ENV_environment_destroy (struct GNUNET_ENV_Environment *env) | ||
119 | { | ||
120 | struct GNUNET_ENV_Modifier *mod, *prev = NULL; | ||
121 | for (mod = env->mod_head; NULL != mod; mod = mod->next) | ||
122 | { | ||
123 | if (NULL != prev) | ||
124 | GNUNET_free (prev); | ||
125 | prev = mod; | ||
126 | } | ||
127 | if (NULL != prev) | ||
128 | GNUNET_free (prev); | ||
129 | |||
130 | GNUNET_free (env); | ||
131 | } | ||