You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

197 lines
4.4 KiB

  1. /*
  2. * This file is part of GNUnet.
  3. * Copyright (C) 2013 GNUnet e.V.
  4. *
  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.
  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. * Affero General Public License for more details.
  14. *
  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/>.
  17. SPDX-License-Identifier: AGPL3.0-or-later
  18. */
  19. /**
  20. * @author Gabor X Toth
  21. *
  22. * @file
  23. * Library providing operations for the @e environment of
  24. * PSYC and Social messages.
  25. */
  26. #include "platform.h"
  27. #include "gnunet_util_lib.h"
  28. #include "gnunet_psyc_env.h"
  29. /**
  30. * Environment for a message.
  31. *
  32. * Contains modifiers.
  33. */
  34. struct GNUNET_PSYC_Environment
  35. {
  36. struct GNUNET_PSYC_Modifier *mod_head;
  37. struct GNUNET_PSYC_Modifier *mod_tail;
  38. size_t mod_count;
  39. };
  40. /**
  41. * Create an environment.
  42. *
  43. * @return A newly allocated environment.
  44. */
  45. struct GNUNET_PSYC_Environment *
  46. GNUNET_PSYC_env_create ()
  47. {
  48. return GNUNET_new (struct GNUNET_PSYC_Environment);
  49. }
  50. /**
  51. * Add a modifier to the environment.
  52. *
  53. * @param env The environment.
  54. * @param oper Operation to perform.
  55. * @param name Name of the variable.
  56. * @param value Value of the variable.
  57. * @param value_size Size of @a value.
  58. */
  59. void
  60. GNUNET_PSYC_env_add (struct GNUNET_PSYC_Environment *env,
  61. enum GNUNET_PSYC_Operator oper, const char *name,
  62. const void *value, size_t value_size)
  63. {
  64. struct GNUNET_PSYC_Modifier *mod = GNUNET_new (struct GNUNET_PSYC_Modifier);
  65. mod->oper = oper;
  66. mod->name = name;
  67. mod->value = value;
  68. mod->value_size = value_size;
  69. GNUNET_CONTAINER_DLL_insert_tail (env->mod_head, env->mod_tail, mod);
  70. env->mod_count++;
  71. }
  72. /**
  73. * Get the first modifier of the environment.
  74. */
  75. struct GNUNET_PSYC_Modifier *
  76. GNUNET_PSYC_env_head (const struct GNUNET_PSYC_Environment *env)
  77. {
  78. return env->mod_head;
  79. }
  80. /**
  81. * Get the last modifier of the environment.
  82. */
  83. struct GNUNET_PSYC_Modifier *
  84. GNUNET_PSYC_env_tail (const struct GNUNET_PSYC_Environment *env)
  85. {
  86. return env->mod_tail;
  87. }
  88. /**
  89. * Remove a modifier from the environment.
  90. */
  91. void
  92. GNUNET_PSYC_env_remove (struct GNUNET_PSYC_Environment *env,
  93. struct GNUNET_PSYC_Modifier *mod)
  94. {
  95. GNUNET_CONTAINER_DLL_remove (env->mod_head, env->mod_tail, mod);
  96. }
  97. /**
  98. * Get the modifier at the beginning of an environment and remove it.
  99. *
  100. * @param env
  101. * @param oper
  102. * @param name
  103. * @param value
  104. * @param value_size
  105. *
  106. * @return
  107. */
  108. int
  109. GNUNET_PSYC_env_shift (struct GNUNET_PSYC_Environment *env,
  110. enum GNUNET_PSYC_Operator *oper, const char **name,
  111. const void **value, size_t *value_size)
  112. {
  113. if (NULL == env->mod_head)
  114. return GNUNET_NO;
  115. struct GNUNET_PSYC_Modifier *mod = env->mod_head;
  116. *oper = mod->oper;
  117. *name = mod->name;
  118. *value = mod->value;
  119. *value_size = mod->value_size;
  120. GNUNET_CONTAINER_DLL_remove (env->mod_head, env->mod_tail, mod);
  121. GNUNET_free (mod);
  122. env->mod_count--;
  123. return GNUNET_YES;
  124. }
  125. /**
  126. * Iterate through all modifiers in the environment.
  127. *
  128. * @param env The environment.
  129. * @param it Iterator.
  130. * @param it_cls Closure for iterator.
  131. */
  132. void
  133. GNUNET_PSYC_env_iterate (const struct GNUNET_PSYC_Environment *env,
  134. GNUNET_PSYC_Iterator it, void *it_cls)
  135. {
  136. struct GNUNET_PSYC_Modifier *mod;
  137. for (mod = env->mod_head; NULL != mod; mod = mod->next)
  138. it (it_cls, mod->oper, mod->name, mod->value, mod->value_size);
  139. }
  140. /**
  141. * Get the number of modifiers in the environment.
  142. *
  143. * @param env The environment.
  144. *
  145. * @return Number of modifiers.
  146. */
  147. size_t
  148. GNUNET_PSYC_env_get_count (const struct GNUNET_PSYC_Environment *env)
  149. {
  150. return env->mod_count;
  151. }
  152. /**
  153. * Destroy an environment.
  154. *
  155. * @param env The environment to destroy.
  156. */
  157. void
  158. GNUNET_PSYC_env_destroy (struct GNUNET_PSYC_Environment *env)
  159. {
  160. struct GNUNET_PSYC_Modifier *mod, *prev = NULL;
  161. for (mod = env->mod_head; NULL != mod; mod = mod->next)
  162. {
  163. if (NULL != prev)
  164. GNUNET_free (prev);
  165. prev = mod;
  166. }
  167. if (NULL != prev)
  168. GNUNET_free (prev);
  169. GNUNET_free (env);
  170. }