valuemetic.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * @file valuemetic.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. * Comparison functions for values.
  32. *
  33. * Synopsis:
  34. * val_lesser(v1, v2)
  35. * val_greater(v1, v2)
  36. * val_lesser_equal(v1, v2)
  37. * val_greater_equal(v1, v2)
  38. * val_equal(v1, v2)
  39. * val_different(v1, v2)
  40. *
  41. * Variables:
  42. * (empty) - "true" or "false", reflecting the value of the relation in question
  43. *
  44. * Description:
  45. * These statements perform comparisons of values. Order of values is defined by the
  46. * following rules:
  47. * 1. Values of different types have the following order: strings, lists, maps.
  48. * 2. String values are ordered lexicographically, with respect to the numeric values
  49. * of their bytes.
  50. * 3. List values are ordered lexicographically, where the order of the elements is
  51. * defined by recursive application of these rules.
  52. * 4. Map values are ordered lexicographically, as if a map was a list of (key, value)
  53. * pairs ordered by key, where the order of both keys and values is defined by
  54. * recursive application of these rules.
  55. */
  56. #include <stdlib.h>
  57. #include <string.h>
  58. #include <ncd/NCDModule.h>
  59. #include <generated/blog_channel_ncd_valuemetic.h>
  60. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  61. struct instance {
  62. NCDModuleInst *i;
  63. int result;
  64. };
  65. typedef int (*compute_func) (NCDValRef v1, NCDValRef v2);
  66. static int compute_lesser (NCDValRef v1, NCDValRef v2)
  67. {
  68. return NCDVal_Compare(v1, v2) < 0;
  69. }
  70. static int compute_greater (NCDValRef v1, NCDValRef v2)
  71. {
  72. return NCDVal_Compare(v1, v2) > 0;
  73. }
  74. static int compute_lesser_equal (NCDValRef v1, NCDValRef v2)
  75. {
  76. return NCDVal_Compare(v1, v2) <= 0;
  77. }
  78. static int compute_greater_equal (NCDValRef v1, NCDValRef v2)
  79. {
  80. return NCDVal_Compare(v1, v2) >= 0;
  81. }
  82. static int compute_equal (NCDValRef v1, NCDValRef v2)
  83. {
  84. return NCDVal_Compare(v1, v2) == 0;
  85. }
  86. static int compute_different (NCDValRef v1, NCDValRef v2)
  87. {
  88. return NCDVal_Compare(v1, v2) != 0;
  89. }
  90. static void new_templ (void *vo, NCDModuleInst *i, compute_func cfunc)
  91. {
  92. struct instance *o = vo;
  93. o->i = i;
  94. NCDValRef v1_arg;
  95. NCDValRef v2_arg;
  96. if (!NCDVal_ListRead(i->args, 2, &v1_arg, &v2_arg)) {
  97. ModuleLog(i, BLOG_ERROR, "wrong arity");
  98. goto fail0;
  99. }
  100. o->result = cfunc(v1_arg, v2_arg);
  101. NCDModuleInst_Backend_Up(i);
  102. return;
  103. fail0:
  104. NCDModuleInst_Backend_SetError(i);
  105. NCDModuleInst_Backend_Dead(i);
  106. }
  107. static void func_die (void *vo)
  108. {
  109. struct instance *o = vo;
  110. NCDModuleInst_Backend_Dead(o->i);
  111. }
  112. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  113. {
  114. struct instance *o = vo;
  115. if (!strcmp(name, "")) {
  116. const char *str = o->result ? "true" : "false";
  117. *out = NCDVal_NewString(mem, str);
  118. if (NCDVal_IsInvalid(*out)) {
  119. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  120. }
  121. return 1;
  122. }
  123. return 0;
  124. }
  125. static void func_new_lesser (void *vo, NCDModuleInst *i)
  126. {
  127. new_templ(vo, i, compute_lesser);
  128. }
  129. static void func_new_greater (void *vo, NCDModuleInst *i)
  130. {
  131. new_templ(vo, i, compute_greater);
  132. }
  133. static void func_new_lesser_equal (void *vo, NCDModuleInst *i)
  134. {
  135. new_templ(vo, i, compute_lesser_equal);
  136. }
  137. static void func_new_greater_equal (void *vo, NCDModuleInst *i)
  138. {
  139. new_templ(vo, i, compute_greater_equal);
  140. }
  141. static void func_new_equal (void *vo, NCDModuleInst *i)
  142. {
  143. new_templ(vo, i, compute_equal);
  144. }
  145. static void func_new_different (void *vo, NCDModuleInst *i)
  146. {
  147. new_templ(vo, i, compute_different);
  148. }
  149. static const struct NCDModule modules[] = {
  150. {
  151. .type = "val_lesser",
  152. .func_new2 = func_new_lesser,
  153. .func_die = func_die,
  154. .func_getvar = func_getvar,
  155. .alloc_size = sizeof(struct instance)
  156. }, {
  157. .type = "val_greater",
  158. .func_new2 = func_new_greater,
  159. .func_die = func_die,
  160. .func_getvar = func_getvar,
  161. .alloc_size = sizeof(struct instance)
  162. }, {
  163. .type = "val_lesser_equal",
  164. .func_new2 = func_new_lesser_equal,
  165. .func_die = func_die,
  166. .func_getvar = func_getvar,
  167. .alloc_size = sizeof(struct instance)
  168. }, {
  169. .type = "val_greater_equal",
  170. .func_new2 = func_new_greater_equal,
  171. .func_die = func_die,
  172. .func_getvar = func_getvar,
  173. .alloc_size = sizeof(struct instance)
  174. }, {
  175. .type = "val_equal",
  176. .func_new2 = func_new_equal,
  177. .func_die = func_die,
  178. .func_getvar = func_getvar,
  179. .alloc_size = sizeof(struct instance)
  180. }, {
  181. .type = "val_different",
  182. .func_new2 = func_new_different,
  183. .func_die = func_die,
  184. .func_getvar = func_getvar,
  185. .alloc_size = sizeof(struct instance)
  186. }, {
  187. .type = NULL
  188. }
  189. };
  190. const struct NCDModuleGroup ncdmodule_valuemetic = {
  191. .modules = modules
  192. };