NCDValGenerator.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * @file NCDValGenerator.c
  3. * @author Ambroz Bizjak <[email protected]>
  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 "NCDValGenerator.h"
  36. #include <generated/blog_channel_NCDValGenerator.h>
  37. static int generate_val (NCDValRef value, ExpString *out_str)
  38. {
  39. ASSERT(!NCDVal_IsInvalid(value))
  40. switch (NCDVal_Type(value)) {
  41. case NCDVAL_STRING: {
  42. size_t len = NCDVal_StringLength(value);
  43. if (!ExpString_AppendChar(out_str, '"')) {
  44. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  45. goto fail;
  46. }
  47. size_t pos = 0;
  48. while (pos < len) {
  49. const char *chunk_data;
  50. size_t chunk_len;
  51. NCDVal_StringGetPtr(value, pos, len - pos, &chunk_data, &chunk_len);
  52. for (size_t i = 0; i < chunk_len; i++) {
  53. if (chunk_data[i] == '\0') {
  54. char buf[5];
  55. snprintf(buf, sizeof(buf), "\\x%02"PRIx8, (uint8_t)chunk_data[i]);
  56. if (!ExpString_Append(out_str, buf)) {
  57. BLog(BLOG_ERROR, "ExpString_Append failed");
  58. goto fail;
  59. }
  60. continue;
  61. }
  62. if (chunk_data[i] == '"' || chunk_data[i] == '\\') {
  63. if (!ExpString_AppendChar(out_str, '\\')) {
  64. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  65. goto fail;
  66. }
  67. }
  68. if (!ExpString_AppendChar(out_str, chunk_data[i])) {
  69. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  70. goto fail;
  71. }
  72. }
  73. pos += chunk_len;
  74. }
  75. if (!ExpString_AppendChar(out_str, '"')) {
  76. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  77. goto fail;
  78. }
  79. } break;
  80. case NCDVAL_LIST: {
  81. size_t count = NCDVal_ListCount(value);
  82. if (!ExpString_AppendChar(out_str, '{')) {
  83. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  84. goto fail;
  85. }
  86. for (size_t i = 0; i < count; i++) {
  87. if (i > 0) {
  88. if (!ExpString_Append(out_str, ", ")) {
  89. BLog(BLOG_ERROR, "ExpString_Append failed");
  90. goto fail;
  91. }
  92. }
  93. if (!generate_val(NCDVal_ListGet(value, i), out_str)) {
  94. goto fail;
  95. }
  96. }
  97. if (!ExpString_AppendChar(out_str, '}')) {
  98. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  99. goto fail;
  100. }
  101. } break;
  102. case NCDVAL_MAP: {
  103. if (!ExpString_AppendChar(out_str, '[')) {
  104. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  105. goto fail;
  106. }
  107. int is_first = 1;
  108. for (NCDValMapElem e = NCDVal_MapOrderedFirst(value); !NCDVal_MapElemInvalid(e); e = NCDVal_MapOrderedNext(value, e)) {
  109. NCDValRef ekey = NCDVal_MapElemKey(value, e);
  110. NCDValRef eval = NCDVal_MapElemVal(value, e);
  111. if (!is_first) {
  112. if (!ExpString_Append(out_str, ", ")) {
  113. BLog(BLOG_ERROR, "ExpString_Append failed");
  114. goto fail;
  115. }
  116. }
  117. if (!generate_val(ekey, out_str)) {
  118. goto fail;
  119. }
  120. if (!ExpString_AppendChar(out_str, ':')) {
  121. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  122. goto fail;
  123. }
  124. if (!generate_val(eval, out_str)) {
  125. goto fail;
  126. }
  127. is_first = 0;
  128. }
  129. if (!ExpString_AppendChar(out_str, ']')) {
  130. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  131. goto fail;
  132. }
  133. } break;
  134. default: ASSERT(0);
  135. }
  136. return 1;
  137. fail:
  138. return 0;
  139. }
  140. char * NCDValGenerator_Generate (NCDValRef value)
  141. {
  142. ASSERT(!NCDVal_IsInvalid(value))
  143. ExpString str;
  144. if (!ExpString_Init(&str)) {
  145. BLog(BLOG_ERROR, "ExpString_Init failed");
  146. goto fail0;
  147. }
  148. if (!generate_val(value, &str)) {
  149. goto fail1;
  150. }
  151. return ExpString_Get(&str);
  152. fail1:
  153. ExpString_Free(&str);
  154. fail0:
  155. return NULL;
  156. }
  157. int NCDValGenerator_AppendGenerate (NCDValRef value, ExpString *str)
  158. {
  159. ASSERT(!NCDVal_IsInvalid(value))
  160. ASSERT(str)
  161. return generate_val(value, str);
  162. }