index.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <stddef.h>
  48. #include <ncd/NCDModule.h>
  49. #include <ncd/extra/value_utils.h>
  50. #include <generated/blog_channel_ncd_index.h>
  51. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  52. struct instance {
  53. NCDModuleInst *i;
  54. size_t value;
  55. };
  56. static void func_new_templ (void *vo, NCDModuleInst *i, size_t value)
  57. {
  58. struct instance *o = vo;
  59. o->i = i;
  60. // set value
  61. o->value = value;
  62. // signal up
  63. NCDModuleInst_Backend_Up(o->i);
  64. }
  65. static void func_new_from_value (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  66. {
  67. // read arguments
  68. NCDValRef arg_value;
  69. if (!NCDVal_ListRead(params->args, 1, &arg_value)) {
  70. ModuleLog(i, BLOG_ERROR, "wrong arity");
  71. goto fail0;
  72. }
  73. if (!NCDVal_IsString(arg_value)) {
  74. ModuleLog(i, BLOG_ERROR, "wrong type");
  75. goto fail0;
  76. }
  77. // parse value
  78. uintmax_t value;
  79. if (!ncd_read_uintmax(arg_value, &value)) {
  80. ModuleLog(i, BLOG_ERROR, "wrong value");
  81. goto fail0;
  82. }
  83. // check overflow
  84. if (value > SIZE_MAX) {
  85. ModuleLog(i, BLOG_ERROR, "value too large");
  86. goto fail0;
  87. }
  88. func_new_templ(vo, i, value);
  89. return;
  90. fail0:
  91. NCDModuleInst_Backend_DeadError(i);
  92. }
  93. static void func_new_from_index (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  94. {
  95. struct instance *index = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  96. // check overflow
  97. if (index->value == SIZE_MAX) {
  98. ModuleLog(i, BLOG_ERROR, "overflow");
  99. goto fail0;
  100. }
  101. func_new_templ(vo, i, index->value + 1);
  102. return;
  103. fail0:
  104. NCDModuleInst_Backend_DeadError(i);
  105. }
  106. static void func_die (void *vo)
  107. {
  108. struct instance *o = vo;
  109. NCDModuleInst_Backend_Dead(o->i);
  110. }
  111. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  112. {
  113. struct instance *o = vo;
  114. if (!strcmp(name, "")) {
  115. *out = ncd_make_uintmax(mem, o->value);
  116. return 1;
  117. }
  118. return 0;
  119. }
  120. static struct NCDModule modules[] = {
  121. {
  122. .type = "index",
  123. .func_new2 = func_new_from_value,
  124. .func_die = func_die,
  125. .func_getvar = func_getvar,
  126. .alloc_size = sizeof(struct instance)
  127. }, {
  128. .type = "index::next",
  129. .base_type = "index",
  130. .func_new2 = func_new_from_index,
  131. .func_die = func_die,
  132. .func_getvar = func_getvar,
  133. .alloc_size = sizeof(struct instance)
  134. }, {
  135. .type = NULL
  136. }
  137. };
  138. const struct NCDModuleGroup ncdmodule_index = {
  139. .modules = modules
  140. };