NCDValueGenerator.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <misc/debug.h>
  32. #include <misc/expstring.h>
  33. #include <base/BLog.h>
  34. #include <ncd/NCDValueGenerator.h>
  35. #include <generated/blog_channel_NCDValueGenerator.h>
  36. static int generate_value (NCDValue *value, ExpString *out_str)
  37. {
  38. switch (NCDValue_Type(value)) {
  39. case NCDVALUE_STRING: {
  40. const char *str = NCDValue_StringValue(value);
  41. size_t len = strlen(str);
  42. if (!ExpString_AppendChar(out_str, '"')) {
  43. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  44. goto fail;
  45. }
  46. for (size_t i = 0; i < len; i++) {
  47. if (str[i] == '"' || str[i] == '\\') {
  48. if (!ExpString_AppendChar(out_str, '\\')) {
  49. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  50. goto fail;
  51. }
  52. }
  53. if (!ExpString_AppendChar(out_str, str[i])) {
  54. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  55. goto fail;
  56. }
  57. }
  58. if (!ExpString_AppendChar(out_str, '"')) {
  59. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  60. goto fail;
  61. }
  62. } break;
  63. case NCDVALUE_LIST: {
  64. if (!ExpString_AppendChar(out_str, '{')) {
  65. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  66. goto fail;
  67. }
  68. int is_first = 1;
  69. for (NCDValue *e = NCDValue_ListFirst(value); e; e = NCDValue_ListNext(value, e)) {
  70. if (!is_first) {
  71. if (!ExpString_Append(out_str, ", ")) {
  72. BLog(BLOG_ERROR, "ExpString_Append failed");
  73. goto fail;
  74. }
  75. }
  76. if (!generate_value(e, out_str)) {
  77. goto fail;
  78. }
  79. is_first = 0;
  80. }
  81. if (!ExpString_AppendChar(out_str, '}')) {
  82. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  83. goto fail;
  84. }
  85. } break;
  86. case NCDVALUE_MAP: {
  87. if (!ExpString_AppendChar(out_str, '[')) {
  88. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  89. goto fail;
  90. }
  91. int is_first = 1;
  92. for (NCDValue *ekey = NCDValue_MapFirstKey(value); ekey; ekey = NCDValue_MapNextKey(value, ekey)) {
  93. NCDValue *eval = NCDValue_MapKeyValue(value, ekey);
  94. if (!is_first) {
  95. if (!ExpString_Append(out_str, ", ")) {
  96. BLog(BLOG_ERROR, "ExpString_Append failed");
  97. goto fail;
  98. }
  99. }
  100. if (!generate_value(ekey, out_str)) {
  101. goto fail;
  102. }
  103. if (!ExpString_AppendChar(out_str, ':')) {
  104. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  105. goto fail;
  106. }
  107. if (!generate_value(eval, out_str)) {
  108. goto fail;
  109. }
  110. is_first = 0;
  111. }
  112. if (!ExpString_AppendChar(out_str, ']')) {
  113. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  114. goto fail;
  115. }
  116. } break;
  117. default: ASSERT(0);
  118. }
  119. return 1;
  120. fail:
  121. return 0;
  122. }
  123. char * NCDValueGenerator_Generate (NCDValue *value)
  124. {
  125. NCDValue_Type(value);
  126. ExpString str;
  127. if (!ExpString_Init(&str)) {
  128. BLog(BLOG_ERROR, "ExpString_Init failed");
  129. goto fail0;
  130. }
  131. if (!generate_value(value, &str)) {
  132. goto fail1;
  133. }
  134. return ExpString_Get(&str);
  135. fail1:
  136. ExpString_Free(&str);
  137. fail0:
  138. return NULL;
  139. }
  140. int NCDValueGenerator_AppendGenerate (NCDValue *value, ExpString *str)
  141. {
  142. NCDValue_Type(value);
  143. ASSERT(str)
  144. return generate_value(value, str);
  145. }