concat.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <stddef.h>
  42. #include <string.h>
  43. #include <misc/balloc.h>
  44. #include <misc/offset.h>
  45. #include <ncd/NCDModule.h>
  46. #include <ncd/NCDRefTarget.h>
  47. #include <ncd/static_strings.h>
  48. #include <generated/blog_channel_ncd_concat.h>
  49. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  50. struct result {
  51. NCDRefTarget ref_target;
  52. size_t length;
  53. char data[];
  54. };
  55. struct instance {
  56. NCDModuleInst *i;
  57. struct result *result;
  58. };
  59. static void result_ref_target_func_release (NCDRefTarget *ref_target)
  60. {
  61. struct result *result = UPPER_OBJECT(ref_target, struct result, ref_target);
  62. BFree(result);
  63. }
  64. static void new_concat_common (void *vo, NCDModuleInst *i, NCDValRef list)
  65. {
  66. ASSERT(NCDVal_IsList(list))
  67. struct instance *o = vo;
  68. o->i = i;
  69. size_t count = NCDVal_ListCount(list);
  70. bsize_t result_size = bsize_fromsize(sizeof(struct result));
  71. // check arguments and compute result size
  72. for (size_t j = 0; j < count; j++) {
  73. NCDValRef arg = NCDVal_ListGet(list, j);
  74. if (!NCDVal_IsString(arg)) {
  75. ModuleLog(i, BLOG_ERROR, "wrong type");
  76. goto fail0;
  77. }
  78. result_size = bsize_add(result_size, bsize_fromsize(NCDVal_StringLength(arg)));
  79. }
  80. // allocate result
  81. o->result = BAllocSize(result_size);
  82. if (!o->result) {
  83. ModuleLog(i, BLOG_ERROR, "BAllocSize failed");
  84. goto fail0;
  85. }
  86. // init ref target
  87. NCDRefTarget_Init(&o->result->ref_target, result_ref_target_func_release);
  88. // copy data to result
  89. o->result->length = 0;
  90. for (size_t j = 0; j < count; j++) {
  91. NCDValRef arg = NCDVal_ListGet(list, j);
  92. b_cstring cstr = NCDVal_StringCstring(arg);
  93. b_cstring_copy_to_buf(cstr, 0, cstr.length, o->result->data + o->result->length);
  94. o->result->length += cstr.length;
  95. }
  96. // signal up
  97. NCDModuleInst_Backend_Up(o->i);
  98. return;
  99. fail0:
  100. NCDModuleInst_Backend_DeadError(i);
  101. }
  102. static void func_new_concat (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  103. {
  104. new_concat_common(vo, i, params->args);
  105. }
  106. static void func_new_concatv (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  107. {
  108. NCDValRef list_arg;
  109. if (!NCDVal_ListRead(params->args, 1, &list_arg)) {
  110. ModuleLog(i, BLOG_ERROR, "wrong arity");
  111. goto fail0;
  112. }
  113. if (!NCDVal_IsList(list_arg)) {
  114. ModuleLog(i, BLOG_ERROR, "wrong type");
  115. goto fail0;
  116. }
  117. new_concat_common(vo, i, list_arg);
  118. return;
  119. fail0:
  120. NCDModuleInst_Backend_DeadError(i);
  121. }
  122. static void func_die (void *vo)
  123. {
  124. struct instance *o = vo;
  125. // release result reference
  126. NCDRefTarget_Deref(&o->result->ref_target);
  127. NCDModuleInst_Backend_Dead(o->i);
  128. }
  129. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  130. {
  131. struct instance *o = vo;
  132. if (name == NCD_STRING_EMPTY) {
  133. *out = NCDVal_NewExternalString(mem, o->result->data, o->result->length, &o->result->ref_target);
  134. return 1;
  135. }
  136. return 0;
  137. }
  138. static struct NCDModule modules[] = {
  139. {
  140. .type = "concat",
  141. .func_new2 = func_new_concat,
  142. .func_die = func_die,
  143. .func_getvar2 = func_getvar2,
  144. .alloc_size = sizeof(struct instance),
  145. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  146. }, {
  147. .type = "concatv",
  148. .func_new2 = func_new_concatv,
  149. .func_die = func_die,
  150. .func_getvar2 = func_getvar2,
  151. .alloc_size = sizeof(struct instance),
  152. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  153. }, {
  154. .type = NULL
  155. }
  156. };
  157. const struct NCDModuleGroup ncdmodule_concat = {
  158. .modules = modules
  159. };