concat.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * @file concat.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. * concat([string elem ...])
  33. * concatv(list strings)
  34. *
  35. * Description:
  36. * Concatenates zero or more strings. The result is available as the empty
  37. * string variable. For concatv(), the strings are provided as a single
  38. * list argument. For concat(), the strings are provided as arguments
  39. * themselves.
  40. */
  41. #include <string.h>
  42. #include <misc/balloc.h>
  43. #include <misc/offset.h>
  44. #include <misc/BRefTarget.h>
  45. #include <ncd/module_common.h>
  46. #include <generated/blog_channel_ncd_concat.h>
  47. struct result {
  48. BRefTarget ref_target;
  49. size_t length;
  50. char data[];
  51. };
  52. struct instance {
  53. NCDModuleInst *i;
  54. struct result *result;
  55. };
  56. static void result_ref_target_func_release (BRefTarget *ref_target)
  57. {
  58. struct result *result = UPPER_OBJECT(ref_target, struct result, ref_target);
  59. BFree(result);
  60. }
  61. static void new_concat_common (void *vo, NCDModuleInst *i, NCDValRef list)
  62. {
  63. ASSERT(NCDVal_IsList(list))
  64. struct instance *o = vo;
  65. o->i = i;
  66. size_t count = NCDVal_ListCount(list);
  67. bsize_t result_size = bsize_fromsize(sizeof(struct result));
  68. // check arguments and compute result size
  69. for (size_t j = 0; j < count; j++) {
  70. NCDValRef arg = NCDVal_ListGet(list, j);
  71. if (!NCDVal_IsString(arg)) {
  72. ModuleLog(i, BLOG_ERROR, "wrong type");
  73. goto fail0;
  74. }
  75. result_size = bsize_add(result_size, bsize_fromsize(NCDVal_StringLength(arg)));
  76. }
  77. // allocate result
  78. o->result = BAllocSize(result_size);
  79. if (!o->result) {
  80. ModuleLog(i, BLOG_ERROR, "BAllocSize failed");
  81. goto fail0;
  82. }
  83. // init ref target
  84. BRefTarget_Init(&o->result->ref_target, result_ref_target_func_release);
  85. // copy data to result
  86. o->result->length = 0;
  87. for (size_t j = 0; j < count; j++) {
  88. NCDValRef arg = NCDVal_ListGet(list, j);
  89. MemRef mr = NCDVal_StringMemRef(arg);
  90. MemRef_CopyOut(mr, o->result->data + o->result->length);
  91. o->result->length += mr.len;
  92. }
  93. // signal up
  94. NCDModuleInst_Backend_Up(o->i);
  95. return;
  96. fail0:
  97. NCDModuleInst_Backend_DeadError(i);
  98. }
  99. static void func_new_concat (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  100. {
  101. new_concat_common(vo, i, params->args);
  102. }
  103. static void func_new_concatv (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  104. {
  105. NCDValRef list_arg;
  106. if (!NCDVal_ListRead(params->args, 1, &list_arg)) {
  107. ModuleLog(i, BLOG_ERROR, "wrong arity");
  108. goto fail0;
  109. }
  110. if (!NCDVal_IsList(list_arg)) {
  111. ModuleLog(i, BLOG_ERROR, "wrong type");
  112. goto fail0;
  113. }
  114. new_concat_common(vo, i, list_arg);
  115. return;
  116. fail0:
  117. NCDModuleInst_Backend_DeadError(i);
  118. }
  119. static void func_die (void *vo)
  120. {
  121. struct instance *o = vo;
  122. // release result reference
  123. BRefTarget_Deref(&o->result->ref_target);
  124. NCDModuleInst_Backend_Dead(o->i);
  125. }
  126. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  127. {
  128. struct instance *o = vo;
  129. if (name == NCD_STRING_EMPTY) {
  130. *out = NCDVal_NewExternalString(mem, o->result->data, o->result->length, &o->result->ref_target);
  131. return 1;
  132. }
  133. return 0;
  134. }
  135. static struct NCDModule modules[] = {
  136. {
  137. .type = "concat",
  138. .func_new2 = func_new_concat,
  139. .func_die = func_die,
  140. .func_getvar2 = func_getvar2,
  141. .alloc_size = sizeof(struct instance)
  142. }, {
  143. .type = "concatv",
  144. .func_new2 = func_new_concatv,
  145. .func_die = func_die,
  146. .func_getvar2 = func_getvar2,
  147. .alloc_size = sizeof(struct instance)
  148. }, {
  149. .type = NULL
  150. }
  151. };
  152. const struct NCDModuleGroup ncdmodule_concat = {
  153. .modules = modules
  154. };