index.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @file index.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. * index index(string value)
  33. * index index::next()
  34. *
  35. * Description:
  36. * Non-negative integer with range of a size_t.
  37. * The first form creates an index from the given decimal string.
  38. * The second form cretes an index with value one more than an existing
  39. * index.
  40. *
  41. * Variables:
  42. * string (empty) - the index value. Note this may be different from
  43. * than the value given to index() if it was not in normal form.
  44. */
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <ncd/module_common.h>
  48. #include <generated/blog_channel_ncd_index.h>
  49. struct instance {
  50. NCDModuleInst *i;
  51. size_t value;
  52. };
  53. static void func_new_templ (void *vo, NCDModuleInst *i, size_t value)
  54. {
  55. struct instance *o = vo;
  56. o->i = i;
  57. // set value
  58. o->value = value;
  59. // signal up
  60. NCDModuleInst_Backend_Up(o->i);
  61. }
  62. static void func_new_from_value (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  63. {
  64. // read arguments
  65. NCDValRef arg_value;
  66. if (!NCDVal_ListRead(params->args, 1, &arg_value)) {
  67. ModuleLog(i, BLOG_ERROR, "wrong arity");
  68. goto fail0;
  69. }
  70. // parse value
  71. uintmax_t value;
  72. if (!ncd_read_uintmax(arg_value, &value)) {
  73. ModuleLog(i, BLOG_ERROR, "wrong value");
  74. goto fail0;
  75. }
  76. // check overflow
  77. if (value > SIZE_MAX) {
  78. ModuleLog(i, BLOG_ERROR, "value too large");
  79. goto fail0;
  80. }
  81. func_new_templ(vo, i, value);
  82. return;
  83. fail0:
  84. NCDModuleInst_Backend_DeadError(i);
  85. }
  86. static void func_new_from_index (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  87. {
  88. struct instance *index = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  89. // check overflow
  90. if (index->value == SIZE_MAX) {
  91. ModuleLog(i, BLOG_ERROR, "overflow");
  92. goto fail0;
  93. }
  94. func_new_templ(vo, i, index->value + 1);
  95. return;
  96. fail0:
  97. NCDModuleInst_Backend_DeadError(i);
  98. }
  99. static void func_die (void *vo)
  100. {
  101. struct instance *o = vo;
  102. NCDModuleInst_Backend_Dead(o->i);
  103. }
  104. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  105. {
  106. struct instance *o = vo;
  107. if (!strcmp(name, "")) {
  108. *out = ncd_make_uintmax(mem, o->value);
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. static struct NCDModule modules[] = {
  114. {
  115. .type = "index",
  116. .func_new2 = func_new_from_value,
  117. .func_die = func_die,
  118. .func_getvar = func_getvar,
  119. .alloc_size = sizeof(struct instance)
  120. }, {
  121. .type = "index::next",
  122. .base_type = "index",
  123. .func_new2 = func_new_from_index,
  124. .func_die = func_die,
  125. .func_getvar = func_getvar,
  126. .alloc_size = sizeof(struct instance)
  127. }, {
  128. .type = NULL
  129. }
  130. };
  131. const struct NCDModuleGroup ncdmodule_index = {
  132. .modules = modules
  133. };