implode.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @file implode.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Synopsis:
  32. * implode(string glue, list(string) pieces)
  33. *
  34. * Variables:
  35. * string (empty) - concatenation of strings in 'pieces', with 'glue' in between
  36. * every two elements.
  37. */
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <misc/expstring.h>
  41. #include <ncd/NCDModule.h>
  42. #include <ncd/static_strings.h>
  43. #include <generated/blog_channel_ncd_implode.h>
  44. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  45. struct instance {
  46. NCDModuleInst *i;
  47. char *result;
  48. size_t result_len;
  49. };
  50. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  51. {
  52. struct instance *o = vo;
  53. o->i = i;
  54. // read arguments
  55. NCDValRef glue_arg;
  56. NCDValRef pieces_arg;
  57. if (!NCDVal_ListRead(params->args, 2, &glue_arg, &pieces_arg)) {
  58. ModuleLog(i, BLOG_ERROR, "wrong arity");
  59. goto fail0;
  60. }
  61. if (!NCDVal_IsString(glue_arg) || !NCDVal_IsList(pieces_arg)) {
  62. ModuleLog(i, BLOG_ERROR, "wrong type");
  63. goto fail0;
  64. }
  65. // init result string
  66. ExpString str;
  67. if (!ExpString_Init(&str)) {
  68. ModuleLog(i, BLOG_ERROR, "ExpString_Init failed");
  69. goto fail0;
  70. }
  71. size_t count = NCDVal_ListCount(pieces_arg);
  72. for (size_t j = 0; j < count; j++) {
  73. NCDValRef piece = NCDVal_ListGet(pieces_arg, j);
  74. // check piece type
  75. if (!NCDVal_IsString(piece)) {
  76. ModuleLog(i, BLOG_ERROR, "wrong piece type");
  77. goto fail1;
  78. }
  79. // append glue
  80. if (j > 0) {
  81. if (!ExpString_AppendBinary(&str, (const uint8_t *)NCDVal_StringValue(glue_arg), NCDVal_StringLength(glue_arg))) {
  82. ModuleLog(i, BLOG_ERROR, "ExpString_AppendBinary failed");
  83. goto fail1;
  84. }
  85. }
  86. // append piece
  87. if (!ExpString_AppendBinary(&str, (const uint8_t *)NCDVal_StringValue(piece), NCDVal_StringLength(piece))) {
  88. ModuleLog(i, BLOG_ERROR, "ExpString_AppendBinary failed");
  89. goto fail1;
  90. }
  91. }
  92. // store result
  93. o->result = ExpString_Get(&str);
  94. o->result_len = ExpString_Length(&str);
  95. // signal up
  96. NCDModuleInst_Backend_Up(i);
  97. return;
  98. fail1:
  99. ExpString_Free(&str);
  100. fail0:
  101. NCDModuleInst_Backend_SetError(i);
  102. NCDModuleInst_Backend_Dead(i);
  103. }
  104. static void func_die (void *vo)
  105. {
  106. struct instance *o = vo;
  107. // free result
  108. free(o->result);
  109. NCDModuleInst_Backend_Dead(o->i);
  110. }
  111. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  112. {
  113. struct instance *o = vo;
  114. if (name == NCD_STRING_EMPTY) {
  115. *out = NCDVal_NewStringBin(mem, (uint8_t *)o->result, o->result_len);
  116. if (NCDVal_IsInvalid(*out)) {
  117. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewStringBin failed");
  118. }
  119. return 1;
  120. }
  121. return 0;
  122. }
  123. static struct NCDModule modules[] = {
  124. {
  125. .type = "implode",
  126. .func_new2 = func_new,
  127. .func_die = func_die,
  128. .func_getvar2 = func_getvar2,
  129. .alloc_size = sizeof(struct instance)
  130. }, {
  131. .type = NULL
  132. }
  133. };
  134. const struct NCDModuleGroup ncdmodule_implode = {
  135. .modules = modules
  136. };