NCDInterpValue.c 6.9 KB

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