aboutsummaryrefslogtreecommitdiff
path: root/src/env
diff options
context:
space:
mode:
authorGabor X Toth <*@tg-x.net>2014-01-06 00:09:40 +0000
committerGabor X Toth <*@tg-x.net>2014-01-06 00:09:40 +0000
commit43d497d7c4ebb6efae37ae4bb2f812a68aa64a32 (patch)
tree71e72ac7ec937d18259098b0ab8c2f35f9d14cbc /src/env
parentc04d45b9738e1764d2e2c21efdbeb129f298d5d1 (diff)
downloadgnunet-43d497d7c4ebb6efae37ae4bb2f812a68aa64a32.tar.gz
gnunet-43d497d7c4ebb6efae37ae4bb2f812a68aa64a32.zip
env: added head() and shift()
Diffstat (limited to 'src/env')
-rw-r--r--src/env/env.c71
-rw-r--r--src/env/test_env.c25
2 files changed, 84 insertions, 12 deletions
diff --git a/src/env/env.c b/src/env/env.c
index 1a8c1a568..107be8827 100644
--- a/src/env/env.c
+++ b/src/env/env.c
@@ -64,9 +64,9 @@ GNUNET_ENV_environment_create ()
64 * @param value_size Size of @a value. 64 * @param value_size Size of @a value.
65 */ 65 */
66void 66void
67GNUNET_ENV_environment_add_mod (struct GNUNET_ENV_Environment *env, 67GNUNET_ENV_environment_add (struct GNUNET_ENV_Environment *env,
68 enum GNUNET_ENV_Operator oper, const char *name, 68 enum GNUNET_ENV_Operator oper, const char *name,
69 const void *value, size_t value_size) 69 const void *value, size_t value_size)
70{ 70{
71 struct GNUNET_ENV_Modifier *mod = GNUNET_new (struct GNUNET_ENV_Modifier); 71 struct GNUNET_ENV_Modifier *mod = GNUNET_new (struct GNUNET_ENV_Modifier);
72 mod->oper = oper; 72 mod->oper = oper;
@@ -78,6 +78,67 @@ GNUNET_ENV_environment_add_mod (struct GNUNET_ENV_Environment *env,
78} 78}
79 79
80 80
81/**
82 * Get the modifier at the beginning of an environment.
83 *
84 * @param env
85 * @param oper
86 * @param name
87 * @param value
88 * @param value_size
89 *
90 * @return
91 */
92int
93GNUNET_ENV_environment_head (struct GNUNET_ENV_Environment *env,
94 enum GNUNET_ENV_Operator *oper, const char **name,
95 const void **value, size_t *value_size)
96{
97 if (NULL == env->mod_head)
98 return GNUNET_NO;
99
100 struct GNUNET_ENV_Modifier *mod = env->mod_head;
101 *oper = mod->oper;
102 *name = mod->name;
103 *value = mod->value;
104 *value_size = mod->value_size;
105 return GNUNET_YES;
106}
107
108
109/**
110 * Get the modifier at the beginning of an environment and remove it.
111 *
112 * @param env
113 * @param oper
114 * @param name
115 * @param value
116 * @param value_size
117 *
118 * @return
119 */
120int
121GNUNET_ENV_environment_shift (struct GNUNET_ENV_Environment *env,
122 enum GNUNET_ENV_Operator *oper, const char **name,
123 const void **value, size_t *value_size)
124{
125 if (NULL == env->mod_head)
126 return GNUNET_NO;
127
128 struct GNUNET_ENV_Modifier *mod = env->mod_head;
129 *oper = mod->oper;
130 *name = mod->name;
131 *value = mod->value;
132 *value_size = mod->value_size;
133
134 GNUNET_CONTAINER_DLL_remove (env->mod_head, env->mod_tail, mod);
135 GNUNET_free (mod);
136 env->mod_count--;
137
138 return GNUNET_YES;
139}
140
141
81/** 142/**
82 * Iterate through all modifiers in the environment. 143 * Iterate through all modifiers in the environment.
83 * 144 *
@@ -91,7 +152,7 @@ GNUNET_ENV_environment_iterate (const struct GNUNET_ENV_Environment *env,
91{ 152{
92 struct GNUNET_ENV_Modifier *mod; 153 struct GNUNET_ENV_Modifier *mod;
93 for (mod = env->mod_head; NULL != mod; mod = mod->next) 154 for (mod = env->mod_head; NULL != mod; mod = mod->next)
94 it (it_cls, mod); 155 it (it_cls, mod->oper, mod->name, mod->value, mod->value_size);
95} 156}
96 157
97 158
@@ -103,7 +164,7 @@ GNUNET_ENV_environment_iterate (const struct GNUNET_ENV_Environment *env,
103 * @return Number of modifiers. 164 * @return Number of modifiers.
104 */ 165 */
105size_t 166size_t
106GNUNET_ENV_environment_get_mod_count (const struct GNUNET_ENV_Environment *env) 167GNUNET_ENV_environment_get_count (const struct GNUNET_ENV_Environment *env)
107{ 168{
108 return env->mod_count; 169 return env->mod_count;
109} 170}
diff --git a/src/env/test_env.c b/src/env/test_env.c
index 8207d7361..865338f2c 100644
--- a/src/env/test_env.c
+++ b/src/env/test_env.c
@@ -46,15 +46,16 @@ struct ItCls
46}; 46};
47 47
48int 48int
49iterator (void *cls, struct GNUNET_ENV_Modifier *mod) 49iterator (void *cls, enum GNUNET_ENV_Operator oper,
50 const char *name, const char *value, uint32_t value_size)
50{ 51{
51 struct ItCls *it_cls = cls; 52 struct ItCls *it_cls = cls;
52 struct GNUNET_ENV_Modifier *m = &mods[it_cls->n++]; 53 struct GNUNET_ENV_Modifier *m = &mods[it_cls->n++];
53 54
54 GNUNET_assert (mod->oper == m->oper); 55 GNUNET_assert (oper == m->oper);
55 GNUNET_assert (mod->value_size == m->value_size); 56 GNUNET_assert (value_size == m->value_size);
56 GNUNET_assert (0 == memcmp (mod->name, m->name, strlen (m->name))); 57 GNUNET_assert (0 == memcmp (name, m->name, strlen (m->name)));
57 GNUNET_assert (0 == memcmp (mod->value, m->value, m->value_size)); 58 GNUNET_assert (0 == memcmp (value, m->value, m->value_size));
58 59
59 return GNUNET_YES; 60 return GNUNET_YES;
60} 61}
@@ -70,14 +71,24 @@ main (int argc, char *argv[])
70 71
71 for (i = 0; i < len; i++) 72 for (i = 0; i < len; i++)
72 { 73 {
73 GNUNET_ENV_environment_add_mod (env, mods[i].oper, mods[i].name, 74 GNUNET_ENV_environment_add (env, mods[i].oper, mods[i].name,
74 mods[i].value, mods[i].value_size); 75 mods[i].value, mods[i].value_size);
75 } 76 }
76 77
77 struct ItCls it_cls = { .n = 0 }; 78 struct ItCls it_cls = { .n = 0 };
78 GNUNET_ENV_environment_iterate (env, iterator, &it_cls); 79 GNUNET_ENV_environment_iterate (env, iterator, &it_cls);
79 GNUNET_assert (len == it_cls.n); 80 GNUNET_assert (len == it_cls.n);
80 81
82 for (i = 0; i < len; i++)
83 {
84 enum GNUNET_ENV_Operator oper;
85 const char *name;
86 const void *value;
87 size_t value_size;
88 GNUNET_ENV_environment_shift (env, &oper, &name, &value, &value_size);
89 GNUNET_assert (len - i - 1 == GNUNET_ENV_environment_get_count (env));
90 }
91
81 GNUNET_ENV_environment_destroy (env); 92 GNUNET_ENV_environment_destroy (env);
82 93
83 return 0; 94 return 0;