yamlwrapper.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * This file is a part of rAthena++.
  3. * Copyright(C) 2017 rAthena Development Team
  4. * https://rathena.org - https://github.com/rathena
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * Author(s)
  20. * Jittapan Pluemsumran <secret@rathena.org>
  21. */
  22. #include "yamlwrapper.h"
  23. #include <cstring>
  24. #include "malloc.h"
  25. #include "showmsg.h"
  26. extern "C" {
  27. yamlwrapper::yamlwrapper(YAML::Node node) {
  28. this->root = node;
  29. }
  30. yamliterator::yamliterator(YAML::Node sequence_) {
  31. this->sequence = sequence_;
  32. this->index = 0;
  33. }
  34. yamliterator* yamlwrapper::iterator() {
  35. return new yamliterator(this->root);
  36. }
  37. yamlwrapper* yaml_load_file(const char* file_name) {
  38. YAML::Node node;
  39. try {
  40. node = YAML::LoadFile(file_name);
  41. if (!node.IsDefined() || node.IsNull())
  42. return NULL;
  43. } catch (YAML::ParserException &e) {
  44. ShowError("YAML Exception Caught: %s\n", e.what());
  45. return NULL;
  46. } catch (YAML::BadFile) {
  47. return NULL;
  48. }
  49. return new yamlwrapper(node);
  50. }
  51. extern "C++" YAML::Node yaml_get_node(const YAML::Node& node,const std::string& key) {
  52. if (key.empty())
  53. return node;
  54. size_t pos = key.find('.');
  55. if (pos == std::string::npos)
  56. return node[key];
  57. else
  58. return yaml_get_node(node[key.substr(0, pos)], key.substr(pos+1));
  59. }
  60. void yaml_destroy_wrapper(yamlwrapper* wrapper) {
  61. delete wrapper;
  62. }
  63. char* yaml_get_c_string(yamlwrapper* wrapper, const char* key) {
  64. std::string cpp_str = yaml_get_node(wrapper->root, std::string(key)).as<std::string>();
  65. const char* c_str = cpp_str.c_str();
  66. size_t str_size = std::strlen(c_str) + 1;
  67. char* buf = (char*)aCalloc(1, str_size);
  68. strcpy(buf, c_str);
  69. return buf;
  70. }
  71. extern "C++" {
  72. template<typename T>
  73. T yaml_get_value(yamlwrapper* wrapper, const char* key) {
  74. if (wrapper == nullptr || key == nullptr)
  75. return {};
  76. try {
  77. return yaml_get_node(wrapper->root, std::string(key)).as<T>();
  78. }
  79. catch (const std::exception& e) {
  80. ShowError("Error during YAML node value resolving in node %s.\n", e.what());
  81. return {};
  82. }
  83. }
  84. }
  85. int yaml_get_int(yamlwrapper* wrapper, const char* key) {
  86. return yaml_get_value<int>(wrapper, key);
  87. }
  88. int16 yaml_get_int16(yamlwrapper* wrapper, const char* key) {
  89. return yaml_get_value<int16>(wrapper, key);
  90. }
  91. int32 yaml_get_int32(yamlwrapper* wrapper, const char* key) {
  92. return yaml_get_value<int32>(wrapper, key);
  93. }
  94. int64 yaml_get_int64(yamlwrapper* wrapper, const char* key) {
  95. return yaml_get_value<int64>(wrapper, key);
  96. }
  97. bool yaml_get_boolean(yamlwrapper* wrapper, const char* key) {
  98. return yaml_get_value<bool>(wrapper, key);
  99. }
  100. char* yaml_as_c_string(yamlwrapper* wrapper) {
  101. std::string cpp_str = wrapper->root.as<std::string>();
  102. const char* c_str = cpp_str.c_str();
  103. size_t str_size = std::strlen(c_str) + 1;
  104. char* buf = (char*)aCalloc(1, str_size);
  105. strcpy(buf, c_str);
  106. return buf;
  107. }
  108. extern "C++" {
  109. template<typename T>
  110. T yaml_as_value(yamlwrapper* wrapper) {
  111. if (wrapper == nullptr)
  112. return {};
  113. try {
  114. return wrapper->root.as<T>();
  115. }
  116. catch (const std::exception& e) {
  117. ShowError("Error during YAML node value resolving in node %s.\n", e.what());
  118. return {};
  119. }
  120. }
  121. }
  122. int yaml_as_int(yamlwrapper* wrapper) {
  123. return yaml_as_value<int>(wrapper);
  124. }
  125. int16 yaml_as_int16(yamlwrapper* wrapper) {
  126. return yaml_as_value<int16>(wrapper);
  127. }
  128. int32 yaml_as_int32(yamlwrapper* wrapper) {
  129. return yaml_as_value<int32>(wrapper);
  130. }
  131. int64 yaml_as_int64(yamlwrapper* wrapper) {
  132. return yaml_as_value<int64>(wrapper);
  133. }
  134. bool yaml_as_boolean(yamlwrapper* wrapper) {
  135. return yaml_as_value<bool>(wrapper);
  136. }
  137. bool yaml_node_is_defined(yamlwrapper* wrapper, const char* key) {
  138. if (wrapper == nullptr || key == nullptr)
  139. return false;
  140. return yaml_get_node(wrapper->root, std::string(key)).IsDefined();
  141. }
  142. yamlwrapper* yaml_get_subnode(yamlwrapper* wrapper, const char* key) {
  143. return new yamlwrapper(yaml_get_node(wrapper->root, std::string(key)));
  144. }
  145. yamliterator* yaml_get_iterator(yamlwrapper* wrapper) {
  146. return new yamliterator(wrapper->root);
  147. }
  148. bool yaml_iterator_is_valid(yamliterator* it) {
  149. return it->sequence.IsSequence();
  150. }
  151. yamlwrapper* yaml_iterator_first(yamliterator* it) {
  152. it->index++;
  153. return new yamlwrapper(it->sequence[0]);
  154. }
  155. yamlwrapper* yaml_iterator_next(yamliterator* it) {
  156. return new yamlwrapper(it->sequence[it->index++]);
  157. }
  158. bool yaml_iterator_has_next(yamliterator* it) {
  159. return it->index <= it->sequence.size();
  160. }
  161. void yaml_iterator_destroy(yamliterator* it) {
  162. delete it;
  163. }
  164. } /* extern "C" */