config_file.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*************************************************************************/
  2. /* config_file.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "config_file.h"
  31. #include "core/os/file_access.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/variant_parser.h"
  34. PoolStringArray ConfigFile::_get_sections() const {
  35. List<String> s;
  36. get_sections(&s);
  37. PoolStringArray arr;
  38. arr.resize(s.size());
  39. int idx = 0;
  40. for (const List<String>::Element *E = s.front(); E; E = E->next()) {
  41. arr.set(idx++, E->get());
  42. }
  43. return arr;
  44. }
  45. PoolStringArray ConfigFile::_get_section_keys(const String &p_section) const {
  46. List<String> s;
  47. get_section_keys(p_section, &s);
  48. PoolStringArray arr;
  49. arr.resize(s.size());
  50. int idx = 0;
  51. for (const List<String>::Element *E = s.front(); E; E = E->next()) {
  52. arr.set(idx++, E->get());
  53. }
  54. return arr;
  55. }
  56. void ConfigFile::set_value(const String &p_section, const String &p_key, const Variant &p_value) {
  57. if (p_value.get_type() == Variant::NIL) {
  58. //erase
  59. if (!values.has(p_section))
  60. return; // ?
  61. values[p_section].erase(p_key);
  62. if (values[p_section].empty()) {
  63. values.erase(p_section);
  64. }
  65. } else {
  66. if (!values.has(p_section)) {
  67. values[p_section] = OrderedHashMap<String, Variant>();
  68. }
  69. values[p_section][p_key] = p_value;
  70. }
  71. }
  72. Variant ConfigFile::get_value(const String &p_section, const String &p_key, Variant p_default) const {
  73. if (!values.has(p_section) || !values[p_section].has(p_key)) {
  74. if (p_default.get_type() == Variant::NIL) {
  75. ERR_EXPLAIN("Couldn't find the given section/key and no default was given");
  76. ERR_FAIL_V(p_default);
  77. }
  78. return p_default;
  79. }
  80. return values[p_section][p_key];
  81. }
  82. bool ConfigFile::has_section(const String &p_section) const {
  83. return values.has(p_section);
  84. }
  85. bool ConfigFile::has_section_key(const String &p_section, const String &p_key) const {
  86. if (!values.has(p_section))
  87. return false;
  88. return values[p_section].has(p_key);
  89. }
  90. void ConfigFile::get_sections(List<String> *r_sections) const {
  91. for (OrderedHashMap<String, OrderedHashMap<String, Variant> >::ConstElement E = values.front(); E; E = E.next()) {
  92. r_sections->push_back(E.key());
  93. }
  94. }
  95. void ConfigFile::get_section_keys(const String &p_section, List<String> *r_keys) const {
  96. ERR_FAIL_COND(!values.has(p_section));
  97. for (OrderedHashMap<String, Variant>::ConstElement E = values[p_section].front(); E; E = E.next()) {
  98. r_keys->push_back(E.key());
  99. }
  100. }
  101. void ConfigFile::erase_section(const String &p_section) {
  102. values.erase(p_section);
  103. }
  104. Error ConfigFile::save(const String &p_path) {
  105. Error err;
  106. FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  107. if (err) {
  108. if (file)
  109. memdelete(file);
  110. return err;
  111. }
  112. for (OrderedHashMap<String, OrderedHashMap<String, Variant> >::Element E = values.front(); E; E = E.next()) {
  113. if (E != values.front())
  114. file->store_string("\n");
  115. file->store_string("[" + E.key() + "]\n\n");
  116. for (OrderedHashMap<String, Variant>::Element F = E.get().front(); F; F = F.next()) {
  117. String vstr;
  118. VariantWriter::write_to_string(F.get(), vstr);
  119. file->store_string(F.key() + "=" + vstr + "\n");
  120. }
  121. }
  122. memdelete(file);
  123. return OK;
  124. }
  125. Error ConfigFile::load(const String &p_path) {
  126. Error err;
  127. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  128. if (!f)
  129. return ERR_CANT_OPEN;
  130. VariantParser::StreamFile stream;
  131. stream.f = f;
  132. String assign;
  133. Variant value;
  134. VariantParser::Tag next_tag;
  135. int lines = 0;
  136. String error_text;
  137. String section;
  138. while (true) {
  139. assign = Variant();
  140. next_tag.fields.clear();
  141. next_tag.name = String();
  142. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true);
  143. if (err == ERR_FILE_EOF) {
  144. memdelete(f);
  145. return OK;
  146. } else if (err != OK) {
  147. ERR_PRINTS("ConfgFile::load - " + p_path + ":" + itos(lines) + " error: " + error_text);
  148. memdelete(f);
  149. return err;
  150. }
  151. if (assign != String()) {
  152. set_value(section, assign, value);
  153. } else if (next_tag.name != String()) {
  154. section = next_tag.name;
  155. }
  156. }
  157. memdelete(f);
  158. return OK;
  159. }
  160. void ConfigFile::_bind_methods() {
  161. ClassDB::bind_method(D_METHOD("set_value", "section", "key", "value"), &ConfigFile::set_value);
  162. ClassDB::bind_method(D_METHOD("get_value", "section", "key", "default"), &ConfigFile::get_value, DEFVAL(Variant()));
  163. ClassDB::bind_method(D_METHOD("has_section", "section"), &ConfigFile::has_section);
  164. ClassDB::bind_method(D_METHOD("has_section_key", "section", "key"), &ConfigFile::has_section_key);
  165. ClassDB::bind_method(D_METHOD("get_sections"), &ConfigFile::_get_sections);
  166. ClassDB::bind_method(D_METHOD("get_section_keys", "section"), &ConfigFile::_get_section_keys);
  167. ClassDB::bind_method(D_METHOD("erase_section", "section"), &ConfigFile::erase_section);
  168. ClassDB::bind_method(D_METHOD("load", "path"), &ConfigFile::load);
  169. ClassDB::bind_method(D_METHOD("save", "path"), &ConfigFile::save);
  170. }
  171. ConfigFile::ConfigFile() {
  172. }