concat.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <misc/bsize.h>
  42. #include <ncd/extra/NCDRefString.h>
  43. #include <ncd/module_common.h>
  44. #include <generated/blog_channel_ncd_concat.h>
  45. struct instance {
  46. NCDModuleInst *i;
  47. size_t length;
  48. NCDRefString *refstr;
  49. };
  50. static void new_concat_common (void *vo, NCDModuleInst *i, NCDValRef list)
  51. {
  52. ASSERT(NCDVal_IsList(list))
  53. struct instance *o = vo;
  54. o->i = i;
  55. size_t count = NCDVal_ListCount(list);
  56. bsize_t result_size = bsize_fromsize(0);
  57. // check arguments and compute result size
  58. for (size_t j = 0; j < count; j++) {
  59. NCDValRef arg = NCDVal_ListGet(list, j);
  60. if (!NCDVal_IsString(arg)) {
  61. ModuleLog(i, BLOG_ERROR, "wrong type");
  62. goto fail0;
  63. }
  64. result_size = bsize_add(result_size, bsize_fromsize(NCDVal_StringLength(arg)));
  65. }
  66. if (result_size.is_overflow) {
  67. ModuleLog(i, BLOG_ERROR, "size overflow");
  68. goto fail0;
  69. }
  70. // allocate result
  71. char *result_data;
  72. o->refstr = NCDRefString_New(result_size.value, &result_data);
  73. if (!o->refstr) {
  74. ModuleLog(i, BLOG_ERROR, "NCDRefString_New failed");
  75. goto fail0;
  76. }
  77. // copy data to result
  78. o->length = 0;
  79. for (size_t j = 0; j < count; j++) {
  80. NCDValRef arg = NCDVal_ListGet(list, j);
  81. MemRef mr = NCDVal_StringMemRef(arg);
  82. MemRef_CopyOut(mr, result_data + o->length);
  83. o->length += mr.len;
  84. }
  85. // signal up
  86. NCDModuleInst_Backend_Up(o->i);
  87. return;
  88. fail0:
  89. NCDModuleInst_Backend_DeadError(i);
  90. }
  91. static void func_new_concat (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  92. {
  93. new_concat_common(vo, i, params->args);
  94. }
  95. static void func_new_concatv (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  96. {
  97. NCDValRef list_arg;
  98. if (!NCDVal_ListRead(params->args, 1, &list_arg)) {
  99. ModuleLog(i, BLOG_ERROR, "wrong arity");
  100. goto fail0;
  101. }
  102. if (!NCDVal_IsList(list_arg)) {
  103. ModuleLog(i, BLOG_ERROR, "wrong type");
  104. goto fail0;
  105. }
  106. new_concat_common(vo, i, list_arg);
  107. return;
  108. fail0:
  109. NCDModuleInst_Backend_DeadError(i);
  110. }
  111. static void func_die (void *vo)
  112. {
  113. struct instance *o = vo;
  114. // release result reference
  115. BRefTarget_Deref(NCDRefString_RefTarget(o->refstr));
  116. NCDModuleInst_Backend_Dead(o->i);
  117. }
  118. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  119. {
  120. struct instance *o = vo;
  121. if (name == NCD_STRING_EMPTY) {
  122. *out = NCDVal_NewExternalString(mem, NCDRefString_GetBuf(o->refstr), o->length, NCDRefString_RefTarget(o->refstr));
  123. return 1;
  124. }
  125. return 0;
  126. }
  127. static struct NCDModule modules[] = {
  128. {
  129. .type = "concat",
  130. .func_new2 = func_new_concat,
  131. .func_die = func_die,
  132. .func_getvar2 = func_getvar2,
  133. .alloc_size = sizeof(struct instance)
  134. }, {
  135. .type = "concatv",
  136. .func_new2 = func_new_concatv,
  137. .func_die = func_die,
  138. .func_getvar2 = func_getvar2,
  139. .alloc_size = sizeof(struct instance)
  140. }, {
  141. .type = NULL
  142. }
  143. };
  144. const struct NCDModuleGroup ncdmodule_concat = {
  145. .modules = modules
  146. };