valuemetic.c 5.8 KB

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