glue.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. Copyright (c) 2023 Bruce A Henderson
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "toml.hpp"
  20. #include "brl.mod/blitz.mod/blitz.h"
  21. extern "C" {
  22. struct TomlDate
  23. {
  24. uint16_t year;
  25. uint8_t month;
  26. uint8_t day;
  27. };
  28. struct TomlTime
  29. {
  30. uint8_t hour;
  31. uint8_t minute;
  32. uint8_t second;
  33. uint32_t nanosecond;
  34. };
  35. struct TomlTimeOffset
  36. {
  37. int16_t minutes;
  38. };
  39. struct TomlOptionalOffset
  40. {
  41. uint8_t hasValue;
  42. struct TomlTimeOffset offset;
  43. };
  44. struct TomlDateTime
  45. {
  46. struct TomlDate date;
  47. struct TomlTime time;
  48. struct TomlOptionalOffset offset;
  49. };
  50. struct TomlSourcePosition
  51. {
  52. uint32_t line;
  53. uint32_t column;
  54. };
  55. BBObject * text_toml_TTomlTable__create();
  56. void text_toml_TTomlTable__Add(BBObject * table, BBString * key, BBObject * value);
  57. BBObject * text_toml_TTomlArray__create(int size);
  58. void text_toml_TTomlArray__Set(BBObject * arr, int index, BBObject * value);
  59. BBObject * text_toml_TTomlString__create(BBString * value);
  60. BBObject * text_toml_TTomlInteger__create(BBLONG value);
  61. BBObject * text_toml_TTomlFloatingPoint__create(BBDOUBLE value);
  62. BBObject * text_toml_TTomlBoolean__create(BBINT value);
  63. BBObject * text_toml_TTomlDate__create(struct TomlDate value);
  64. BBObject * text_toml_TTomlTime__create(struct TomlTime value);
  65. BBObject * text_toml_TTomlDateTime__create(struct TomlDateTime value);
  66. BBObject * text_toml_common_TTomlSourceRegion__create(struct TomlSourcePosition begin, struct TomlSourcePosition end, BBString * path);
  67. BBObject * text_toml_TTomlParseError__create(BBString * message, BBObject * region);
  68. BBObject * bmx_toml_parse_string(BBString * s);
  69. BBObject * bmx_toml_build_node(toml::node * node);
  70. BBObject * bmx_toml_build_table(toml::table & table);
  71. BBObject * bmx_toml_build_array(toml::array & array);
  72. BBObject * bmx_toml_build_string(toml::value<std::string> & value);
  73. BBObject * bmx_toml_build_integer(toml::value<int64_t> & value);
  74. BBObject * bmx_toml_build_floating_point(toml::value<double> & value);
  75. BBObject * bmx_toml_build_boolean(toml::value<bool> & value);
  76. BBObject * bmx_toml_build_date(toml::value<toml::date> & value);
  77. BBObject * bmx_toml_build_time(toml::value<toml::time> & value);
  78. BBObject * bmx_toml_build_date_time(toml::value<toml::date_time> & value);
  79. }
  80. BBObject * bmx_toml_build_node(toml::node * node) {
  81. switch (node->type()) {
  82. case toml::node_type::table: {
  83. return bmx_toml_build_table(*node->as_table());
  84. }
  85. case toml::node_type::array: {
  86. return bmx_toml_build_array(*node->as_array());
  87. }
  88. case toml::node_type::string: {
  89. return bmx_toml_build_string(*node->as_string());
  90. }
  91. case toml::node_type::integer: {
  92. return bmx_toml_build_integer(*node->as_integer());
  93. }
  94. case toml::node_type::floating_point: {
  95. return bmx_toml_build_floating_point(*node->as_floating_point());
  96. }
  97. case toml::node_type::boolean: {
  98. return bmx_toml_build_boolean(*node->as_boolean());
  99. }
  100. case toml::node_type::date: {
  101. return bmx_toml_build_date(*node->as_date());
  102. }
  103. case toml::node_type::time: {
  104. return bmx_toml_build_time(*node->as_time());
  105. }
  106. case toml::node_type::date_time: {
  107. return bmx_toml_build_date_time(*node->as_date_time());
  108. }
  109. }
  110. }
  111. BBObject * bmx_toml_build_table(toml::table & table) {
  112. BBObject * bmxTable = text_toml_TTomlTable__create();
  113. for (auto && [key, value] : table) {
  114. auto k = std::string(key);
  115. auto node = bmx_toml_build_node(&value);
  116. auto s = bbStringFromUTF8Bytes((unsigned char*)k.c_str(), k.size());
  117. text_toml_TTomlTable__Add(bmxTable, s, node);
  118. }
  119. return bmxTable;
  120. }
  121. BBObject * bmx_toml_build_array(toml::array & array) {
  122. size_t size = array.size();
  123. BBObject * bmxArray = text_toml_TTomlArray__create(size);
  124. for (size_t i = 0; i < size; i++) {
  125. auto node = bmx_toml_build_node(array.get(i));
  126. text_toml_TTomlArray__Set(bmxArray, i, node);
  127. }
  128. return bmxArray;
  129. }
  130. BBObject * bmx_toml_build_string(toml::value<std::string> & value) {
  131. BBString * s = bbStringFromUTF8Bytes((unsigned char*)value->c_str(), value->size());
  132. return text_toml_TTomlString__create(s);
  133. }
  134. BBObject * bmx_toml_build_integer(toml::value<int64_t> & value) {
  135. return text_toml_TTomlInteger__create(*value);
  136. }
  137. BBObject * bmx_toml_build_floating_point(toml::value<double> & value) {
  138. return text_toml_TTomlFloatingPoint__create(*value);
  139. }
  140. BBObject * bmx_toml_build_boolean(toml::value<bool> & value) {
  141. return text_toml_TTomlBoolean__create(*value);
  142. }
  143. BBObject * bmx_toml_build_date(toml::value<toml::date> & value) {
  144. struct TomlDate d = {value->year, value->month, value->day};
  145. return text_toml_TTomlDate__create(d);
  146. }
  147. BBObject * bmx_toml_build_time(toml::value<toml::time> & value) {
  148. struct TomlTime t = {value->hour, value->minute, value->second, value->nanosecond};
  149. return text_toml_TTomlTime__create(t);
  150. }
  151. BBObject * bmx_toml_build_date_time(toml::value<toml::date_time> & value) {
  152. struct TomlDateTime dt = {.date = {value->date.year, value->date.month, value->date.day},
  153. .time = {value->time.hour, value->time.minute, value->time.second, value->time.nanosecond},
  154. .offset = {value->offset.has_value(), {value->offset.has_value() ? value->offset.value().minutes : (int16_t)0}}};
  155. return text_toml_TTomlDateTime__create(dt);
  156. }
  157. ///////////////////////////////////////////////////////////
  158. BBObject * bmx_toml_parse_string(BBString * doc) {
  159. try {
  160. char * d = (char*)bbStringToUTF8String(doc);
  161. std::string cdoc(d);
  162. bbMemFree(d);
  163. auto res = toml::parse(cdoc);
  164. BBObject * table = bmx_toml_build_table(res);
  165. return table;
  166. } catch (toml::parse_error & e) {
  167. auto source = e.source();
  168. BBString * path;
  169. if (source.path) {
  170. path = bbStringFromUTF8Bytes((unsigned char*)source.path->c_str(), source.path->size());
  171. }
  172. else {
  173. path = &bbEmptyString;
  174. }
  175. BBObject * region = text_toml_common_TTomlSourceRegion__create({ source.begin.line, source.begin.column }, { source.end.line, source.end.column }, path);
  176. unsigned char * message = (unsigned char*)e.what();
  177. BBObject * ex = text_toml_TTomlParseError__create(bbStringFromUTF8String(message), region);
  178. bbExThrow(ex);
  179. }
  180. return &bbNullObject;
  181. }