index.c 4.9 KB

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