NCDInterpValue.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * @file NCDInterpValue.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. #include <stdlib.h>
  30. #include <limits.h>
  31. #include <misc/offset.h>
  32. #include <misc/split_string.h>
  33. #include <base/BLog.h>
  34. #include "NCDInterpValue.h"
  35. #include <generated/blog_channel_ncd.h>
  36. static int NCDInterpValue_InitString (NCDInterpValue *o, const char *string, size_t len);
  37. static int NCDInterpValue_InitVar (NCDInterpValue *o, const char *name);
  38. static void NCDInterpValue_InitList (NCDInterpValue *o);
  39. static int NCDInterpValue_ListAppend (NCDInterpValue *o, NCDInterpValue v);
  40. static void NCDInterpValue_InitMap (NCDInterpValue *o);
  41. static int NCDInterpValue_MapAppend (NCDInterpValue *o, NCDInterpValue key, NCDInterpValue val);
  42. int NCDInterpValue_Init (NCDInterpValue *o, NCDValue *val_ast)
  43. {
  44. switch (NCDValue_Type(val_ast)) {
  45. case NCDVALUE_STRING: {
  46. return NCDInterpValue_InitString(o, NCDValue_StringValue(val_ast), NCDValue_StringLength(val_ast));
  47. } break;
  48. case NCDVALUE_VAR: {
  49. return NCDInterpValue_InitVar(o, NCDValue_VarName(val_ast));
  50. } break;
  51. case NCDVALUE_LIST: {
  52. NCDInterpValue_InitList(o);
  53. for (NCDValue *ve = NCDValue_ListFirst(val_ast); ve; ve = NCDValue_ListNext(val_ast, ve)) {
  54. NCDInterpValue e;
  55. if (!NCDInterpValue_Init(&e, ve)) {
  56. goto fail_list;
  57. }
  58. if (!NCDInterpValue_ListAppend(o, e)) {
  59. NCDInterpValue_Free(&e);
  60. goto fail_list;
  61. }
  62. }
  63. return 1;
  64. fail_list:
  65. NCDInterpValue_Free(o);
  66. return 0;
  67. } break;
  68. case NCDVALUE_MAP: {
  69. NCDInterpValue_InitMap(o);
  70. for (NCDValue *ekey = NCDValue_MapFirstKey(val_ast); ekey; ekey = NCDValue_MapNextKey(val_ast, ekey)) {
  71. NCDValue *eval = NCDValue_MapKeyValue(val_ast, ekey);
  72. NCDInterpValue key;
  73. NCDInterpValue val;
  74. if (!NCDInterpValue_Init(&key, ekey)) {
  75. goto fail_map;
  76. }
  77. if (!NCDInterpValue_Init(&val, eval)) {
  78. NCDInterpValue_Free(&key);
  79. goto fail_map;
  80. }
  81. if (!NCDInterpValue_MapAppend(o, key, val)) {
  82. NCDInterpValue_Free(&key);
  83. NCDInterpValue_Free(&val);
  84. goto fail_map;
  85. }
  86. }
  87. return 1;
  88. fail_map:
  89. NCDInterpValue_Free(o);
  90. return 0;
  91. } break;
  92. default:
  93. ASSERT(0);
  94. return 0;
  95. }
  96. }
  97. void NCDInterpValue_Free (NCDInterpValue *o)
  98. {
  99. switch (o->type) {
  100. case NCDVALUE_STRING: {
  101. free(o->string);
  102. } break;
  103. case NCDVALUE_VAR: {
  104. free_strings(o->variable_names);
  105. } break;
  106. case NCDVALUE_LIST: {
  107. while (!LinkedList1_IsEmpty(&o->list)) {
  108. struct NCDInterpValueListElem *elem = UPPER_OBJECT(LinkedList1_GetFirst(&o->list), struct NCDInterpValueListElem, list_node);
  109. NCDInterpValue_Free(&elem->value);
  110. LinkedList1_Remove(&o->list, &elem->list_node);
  111. free(elem);
  112. }
  113. } break;
  114. case NCDVALUE_MAP: {
  115. while (!LinkedList1_IsEmpty(&o->maplist)) {
  116. struct NCDInterpValueMapElem *elem = UPPER_OBJECT(LinkedList1_GetFirst(&o->maplist), struct NCDInterpValueMapElem, maplist_node);
  117. NCDInterpValue_Free(&elem->key);
  118. NCDInterpValue_Free(&elem->val);
  119. LinkedList1_Remove(&o->maplist, &elem->maplist_node);
  120. free(elem);
  121. }
  122. } break;
  123. default: ASSERT(0);
  124. }
  125. }
  126. int NCDInterpValue_InitString (NCDInterpValue *o, const char *string, size_t len)
  127. {
  128. o->type = NCDVALUE_STRING;
  129. if (!(o->string = malloc(len))) {
  130. BLog(BLOG_ERROR, "malloc failed");
  131. return 0;
  132. }
  133. memcpy(o->string, string, len);
  134. o->string_len = len;
  135. return 1;
  136. }
  137. int NCDInterpValue_InitVar (NCDInterpValue *o, const char *name)
  138. {
  139. ASSERT(name)
  140. o->type = NCDVALUE_VAR;
  141. if (!(o->variable_names = split_string(name, '.'))) {
  142. return 0;
  143. }
  144. return 1;
  145. }
  146. void NCDInterpValue_InitList (NCDInterpValue *o)
  147. {
  148. o->type = NCDVALUE_LIST;
  149. LinkedList1_Init(&o->list);
  150. o->list_count = 0;
  151. }
  152. int NCDInterpValue_ListAppend (NCDInterpValue *o, NCDInterpValue v)
  153. {
  154. ASSERT(o->type == NCDVALUE_LIST)
  155. if (o->list_count == SIZE_MAX) {
  156. BLog(BLOG_ERROR, "size overflow");
  157. return 0;
  158. }
  159. struct NCDInterpValueListElem *elem = malloc(sizeof(*elem));
  160. if (!elem) {
  161. BLog(BLOG_ERROR, "malloc failed");
  162. return 0;
  163. }
  164. LinkedList1_Append(&o->list, &elem->list_node);
  165. elem->value = v;
  166. o->list_count++;
  167. return 1;
  168. }
  169. void NCDInterpValue_InitMap (NCDInterpValue *o)
  170. {
  171. o->type = NCDVALUE_MAP;
  172. LinkedList1_Init(&o->maplist);
  173. o->map_count = 0;
  174. }
  175. int NCDInterpValue_MapAppend (NCDInterpValue *o, NCDInterpValue key, NCDInterpValue val)
  176. {
  177. ASSERT(o->type == NCDVALUE_MAP)
  178. if (o->map_count == SIZE_MAX) {
  179. BLog(BLOG_ERROR, "size overflow");
  180. return 0;
  181. }
  182. struct NCDInterpValueMapElem *elem = malloc(sizeof(*elem));
  183. if (!elem) {
  184. BLog(BLOG_ERROR, "malloc failed");
  185. return 0;
  186. }
  187. LinkedList1_Append(&o->maplist, &elem->maplist_node);
  188. elem->key = key;
  189. elem->val = val;
  190. o->map_count++;
  191. return 1;
  192. }