NCDInterpValue.c 7.3 KB

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