NCDValueGenerator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**
  2. * @file NCDValueGenerator.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 <string.h>
  31. #include <inttypes.h>
  32. #include <misc/debug.h>
  33. #include <misc/expstring.h>
  34. #include <base/BLog.h>
  35. #include <ncd/NCDValueGenerator.h>
  36. #include <generated/blog_channel_NCDValueGenerator.h>
  37. static int generate_value (NCDValue *value, ExpString *out_str)
  38. {
  39. switch (NCDValue_Type(value)) {
  40. case NCDVALUE_STRING: {
  41. const char *str = NCDValue_StringValue(value);
  42. size_t len = NCDValue_StringLength(value);
  43. if (!ExpString_AppendChar(out_str, '"')) {
  44. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  45. goto fail;
  46. }
  47. for (size_t i = 0; i < len; i++) {
  48. if (str[i] == '\0') {
  49. char buf[5];
  50. snprintf(buf, sizeof(buf), "\\x%02"PRIx8, (uint8_t)str[i]);
  51. if (!ExpString_Append(out_str, buf)) {
  52. BLog(BLOG_ERROR, "ExpString_Append failed");
  53. goto fail;
  54. }
  55. continue;
  56. }
  57. if (str[i] == '"' || str[i] == '\\') {
  58. if (!ExpString_AppendChar(out_str, '\\')) {
  59. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  60. goto fail;
  61. }
  62. }
  63. if (!ExpString_AppendChar(out_str, str[i])) {
  64. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  65. goto fail;
  66. }
  67. }
  68. if (!ExpString_AppendChar(out_str, '"')) {
  69. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  70. goto fail;
  71. }
  72. } break;
  73. case NCDVALUE_LIST: {
  74. if (!ExpString_AppendChar(out_str, '{')) {
  75. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  76. goto fail;
  77. }
  78. int is_first = 1;
  79. for (NCDValue *e = NCDValue_ListFirst(value); e; e = NCDValue_ListNext(value, e)) {
  80. if (!is_first) {
  81. if (!ExpString_Append(out_str, ", ")) {
  82. BLog(BLOG_ERROR, "ExpString_Append failed");
  83. goto fail;
  84. }
  85. }
  86. if (!generate_value(e, out_str)) {
  87. goto fail;
  88. }
  89. is_first = 0;
  90. }
  91. if (!ExpString_AppendChar(out_str, '}')) {
  92. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  93. goto fail;
  94. }
  95. } break;
  96. case NCDVALUE_MAP: {
  97. if (!ExpString_AppendChar(out_str, '[')) {
  98. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  99. goto fail;
  100. }
  101. int is_first = 1;
  102. for (NCDValue *ekey = NCDValue_MapFirstKey(value); ekey; ekey = NCDValue_MapNextKey(value, ekey)) {
  103. NCDValue *eval = NCDValue_MapKeyValue(value, ekey);
  104. if (!is_first) {
  105. if (!ExpString_Append(out_str, ", ")) {
  106. BLog(BLOG_ERROR, "ExpString_Append failed");
  107. goto fail;
  108. }
  109. }
  110. if (!generate_value(ekey, out_str)) {
  111. goto fail;
  112. }
  113. if (!ExpString_AppendChar(out_str, ':')) {
  114. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  115. goto fail;
  116. }
  117. if (!generate_value(eval, out_str)) {
  118. goto fail;
  119. }
  120. is_first = 0;
  121. }
  122. if (!ExpString_AppendChar(out_str, ']')) {
  123. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  124. goto fail;
  125. }
  126. } break;
  127. case NCDVALUE_VAR: {
  128. if (!ExpString_Append(out_str, NCDValue_VarName(value))) {
  129. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  130. goto fail;
  131. }
  132. } break;
  133. default: ASSERT(0);
  134. }
  135. return 1;
  136. fail:
  137. return 0;
  138. }
  139. static int generate_val (NCDValRef value, ExpString *out_str)
  140. {
  141. ASSERT(!NCDVal_IsInvalid(value))
  142. switch (NCDVal_Type(value)) {
  143. case NCDVAL_STRING: {
  144. const char *str = NCDVal_StringValue(value);
  145. size_t len = NCDVal_StringLength(value);
  146. if (!ExpString_AppendChar(out_str, '"')) {
  147. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  148. goto fail;
  149. }
  150. for (size_t i = 0; i < len; i++) {
  151. if (str[i] == '\0') {
  152. char buf[5];
  153. snprintf(buf, sizeof(buf), "\\x%02"PRIx8, (uint8_t)str[i]);
  154. if (!ExpString_Append(out_str, buf)) {
  155. BLog(BLOG_ERROR, "ExpString_Append failed");
  156. goto fail;
  157. }
  158. continue;
  159. }
  160. if (str[i] == '"' || str[i] == '\\') {
  161. if (!ExpString_AppendChar(out_str, '\\')) {
  162. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  163. goto fail;
  164. }
  165. }
  166. if (!ExpString_AppendChar(out_str, str[i])) {
  167. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  168. goto fail;
  169. }
  170. }
  171. if (!ExpString_AppendChar(out_str, '"')) {
  172. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  173. goto fail;
  174. }
  175. } break;
  176. case NCDVAL_LIST: {
  177. size_t count = NCDVal_ListCount(value);
  178. if (!ExpString_AppendChar(out_str, '{')) {
  179. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  180. goto fail;
  181. }
  182. for (size_t i = 0; i < count; i++) {
  183. if (i > 0) {
  184. if (!ExpString_Append(out_str, ", ")) {
  185. BLog(BLOG_ERROR, "ExpString_Append failed");
  186. goto fail;
  187. }
  188. }
  189. if (!generate_val(NCDVal_ListGet(value, i), out_str)) {
  190. goto fail;
  191. }
  192. }
  193. if (!ExpString_AppendChar(out_str, '}')) {
  194. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  195. goto fail;
  196. }
  197. } break;
  198. case NCDVAL_MAP: {
  199. if (!ExpString_AppendChar(out_str, '[')) {
  200. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  201. goto fail;
  202. }
  203. int is_first = 1;
  204. for (NCDValMapElem e = NCDVal_MapOrderedFirst(value); !NCDVal_MapElemInvalid(e); e = NCDVal_MapOrderedNext(value, e)) {
  205. NCDValRef ekey = NCDVal_MapElemKey(value, e);
  206. NCDValRef eval = NCDVal_MapElemVal(value, e);
  207. if (!is_first) {
  208. if (!ExpString_Append(out_str, ", ")) {
  209. BLog(BLOG_ERROR, "ExpString_Append failed");
  210. goto fail;
  211. }
  212. }
  213. if (!generate_val(ekey, out_str)) {
  214. goto fail;
  215. }
  216. if (!ExpString_AppendChar(out_str, ':')) {
  217. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  218. goto fail;
  219. }
  220. if (!generate_val(eval, out_str)) {
  221. goto fail;
  222. }
  223. is_first = 0;
  224. }
  225. if (!ExpString_AppendChar(out_str, ']')) {
  226. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  227. goto fail;
  228. }
  229. } break;
  230. default: ASSERT(0);
  231. }
  232. return 1;
  233. fail:
  234. return 0;
  235. }
  236. char * NCDValueGenerator_Generate (NCDValue *value)
  237. {
  238. NCDValue_Type(value);
  239. ExpString str;
  240. if (!ExpString_Init(&str)) {
  241. BLog(BLOG_ERROR, "ExpString_Init failed");
  242. goto fail0;
  243. }
  244. if (!generate_value(value, &str)) {
  245. goto fail1;
  246. }
  247. return ExpString_Get(&str);
  248. fail1:
  249. ExpString_Free(&str);
  250. fail0:
  251. return NULL;
  252. }
  253. int NCDValueGenerator_AppendGenerate (NCDValue *value, ExpString *str)
  254. {
  255. NCDValue_Type(value);
  256. ASSERT(str)
  257. return generate_value(value, str);
  258. }
  259. char * NCDValGenerator_Generate (NCDValRef value)
  260. {
  261. ASSERT(!NCDVal_IsInvalid(value))
  262. ExpString str;
  263. if (!ExpString_Init(&str)) {
  264. BLog(BLOG_ERROR, "ExpString_Init failed");
  265. goto fail0;
  266. }
  267. if (!generate_val(value, &str)) {
  268. goto fail1;
  269. }
  270. return ExpString_Get(&str);
  271. fail1:
  272. ExpString_Free(&str);
  273. fail0:
  274. return NULL;
  275. }
  276. int NCDValGenerator_AppendGenerate (NCDValRef value, ExpString *str)
  277. {
  278. ASSERT(!NCDVal_IsInvalid(value))
  279. ASSERT(str)
  280. return generate_val(value, str);
  281. }