BoilerplateResponder.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "subnode/BoilerplateResponder.h"
  16. #include "benc/Dict.h"
  17. #include "util/Identity.h"
  18. struct BoilerplateResponder_pvt
  19. {
  20. struct BoilerplateResponder pub;
  21. struct EncodingScheme* myScheme;
  22. String* mySchemeStr;
  23. Identity
  24. };
  25. void BoilerplateResponder_addBoilerplate(struct BoilerplateResponder* br,
  26. Dict* msgDict,
  27. struct Address* src,
  28. struct Allocator* alloc)
  29. {
  30. struct BoilerplateResponder_pvt* brp = Identity_check((struct BoilerplateResponder_pvt*) br);
  31. Dict_putStringC(msgDict, "es", brp->mySchemeStr, alloc);
  32. int encIdx = EncodingScheme_getFormNum(brp->myScheme, src->path);
  33. Assert_true(encIdx != EncodingScheme_getFormNum_INVALID);
  34. Dict_putIntC(msgDict, "ei", encIdx, alloc);
  35. }
  36. struct BoilerplateResponder* BoilerplateResponder_new(struct EncodingScheme* myScheme,
  37. struct Allocator* allocator)
  38. {
  39. struct Allocator* alloc = Allocator_child(allocator);
  40. struct BoilerplateResponder_pvt* brp =
  41. Allocator_calloc(alloc, sizeof(struct BoilerplateResponder_pvt), 1);
  42. Identity_set(brp);
  43. brp->myScheme = myScheme;
  44. brp->mySchemeStr = EncodingScheme_serialize(myScheme, alloc);
  45. return &brp->pub;
  46. }