index.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 (NCDModuleInst *i, size_t value)
  58. {
  59. // allocate instance
  60. struct instance *o = malloc(sizeof(*o));
  61. if (!o) {
  62. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  63. goto fail0;
  64. }
  65. o->i = i;
  66. NCDModuleInst_Backend_SetUser(i, o);
  67. // set value
  68. o->value = value;
  69. // signal up
  70. NCDModuleInst_Backend_Up(o->i);
  71. return;
  72. fail0:
  73. NCDModuleInst_Backend_SetError(i);
  74. NCDModuleInst_Backend_Dead(i);
  75. }
  76. static void func_new_from_value (NCDModuleInst *i)
  77. {
  78. // read arguments
  79. NCDValRef arg_value;
  80. if (!NCDVal_ListRead(i->args, 1, &arg_value)) {
  81. ModuleLog(i, BLOG_ERROR, "wrong arity");
  82. goto fail0;
  83. }
  84. if (!NCDVal_IsStringNoNulls(arg_value)) {
  85. ModuleLog(i, BLOG_ERROR, "wrong type");
  86. goto fail0;
  87. }
  88. // parse value
  89. uintmax_t value;
  90. if (!parse_unsigned_integer(NCDVal_StringValue(arg_value), &value)) {
  91. ModuleLog(i, BLOG_ERROR, "wrong value");
  92. goto fail0;
  93. }
  94. // check overflow
  95. if (value > SIZE_MAX) {
  96. ModuleLog(i, BLOG_ERROR, "value too large");
  97. goto fail0;
  98. }
  99. func_new_templ(i, value);
  100. return;
  101. fail0:
  102. NCDModuleInst_Backend_SetError(i);
  103. NCDModuleInst_Backend_Dead(i);
  104. }
  105. static void func_new_from_index (NCDModuleInst *i)
  106. {
  107. struct instance *index = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  108. // check overflow
  109. if (index->value == SIZE_MAX) {
  110. ModuleLog(i, BLOG_ERROR, "overflow");
  111. goto fail0;
  112. }
  113. func_new_templ(i, index->value + 1);
  114. return;
  115. fail0:
  116. NCDModuleInst_Backend_SetError(i);
  117. NCDModuleInst_Backend_Dead(i);
  118. }
  119. static void func_die (void *vo)
  120. {
  121. struct instance *o = vo;
  122. NCDModuleInst *i = o->i;
  123. // free instance
  124. free(o);
  125. NCDModuleInst_Backend_Dead(i);
  126. }
  127. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  128. {
  129. struct instance *o = vo;
  130. if (!strcmp(name, "")) {
  131. char str[64];
  132. snprintf(str, sizeof(str), "%zu", o->value);
  133. *out = NCDVal_NewString(mem, str);
  134. if (NCDVal_IsInvalid(*out)) {
  135. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  136. }
  137. return 1;
  138. }
  139. return 0;
  140. }
  141. static const struct NCDModule modules[] = {
  142. {
  143. .type = "index",
  144. .func_new = func_new_from_value,
  145. .func_die = func_die,
  146. .func_getvar = func_getvar
  147. }, {
  148. .type = "index::next",
  149. .base_type = "index",
  150. .func_new = func_new_from_index,
  151. .func_die = func_die,
  152. .func_getvar = func_getvar
  153. }, {
  154. .type = NULL
  155. }
  156. };
  157. const struct NCDModuleGroup ncdmodule_index = {
  158. .modules = modules
  159. };