config_file.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*************************************************************************/
  2. /* config_file.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "os/file_access.h"
  32. #include "os/keyboard.h"
  33. #include "variant_parser.h"
  34. StringArray ConfigFile::_get_sections() const {
  35. List<String> s;
  36. get_sections(&s);
  37. StringArray 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. StringArray ConfigFile::_get_section_keys(const String &p_section) const {
  46. List<String> s;
  47. get_section_keys(p_section, &s);
  48. StringArray 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] = Map<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 (const Map<String, Map<String, Variant> >::Element *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 (const Map<String, Variant>::Element *E = values[p_section].front(); E; E = E->next()) {
  98. r_keys->push_back(E->key());
  99. }
  100. }
  101. Error ConfigFile::save(const String &p_path) {
  102. Error err;
  103. FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  104. if (err) {
  105. if (file)
  106. memdelete(file);
  107. return err;
  108. }
  109. for (Map<String, Map<String, Variant> >::Element *E = values.front(); E; E = E->next()) {
  110. if (E != values.front())
  111. file->store_string("\n");
  112. file->store_string("[" + E->key() + "]\n\n");
  113. for (Map<String, Variant>::Element *F = E->get().front(); F; F = F->next()) {
  114. String vstr;
  115. VariantWriter::write_to_string(F->get(), vstr);
  116. file->store_string(F->key() + "=" + vstr + "\n");
  117. }
  118. }
  119. memdelete(file);
  120. return OK;
  121. }
  122. Error ConfigFile::load(const String &p_path) {
  123. Error err;
  124. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  125. if (!f)
  126. return ERR_CANT_OPEN;
  127. VariantParser::StreamFile stream;
  128. stream.f = f;
  129. String assign;
  130. Variant value;
  131. VariantParser::Tag next_tag;
  132. int lines = 0;
  133. String error_text;
  134. String section;
  135. while (true) {
  136. assign = Variant();
  137. next_tag.fields.clear();
  138. next_tag.name = String();
  139. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true);
  140. if (err == ERR_FILE_EOF) {
  141. memdelete(f);
  142. return OK;
  143. } else if (err != OK) {
  144. ERR_PRINTS("ConfgFile::load - " + p_path + ":" + itos(lines) + " error: " + error_text);
  145. memdelete(f);
  146. return err;
  147. }
  148. if (assign != String()) {
  149. set_value(section, assign, value);
  150. } else if (next_tag.name != String()) {
  151. section = next_tag.name;
  152. }
  153. }
  154. memdelete(f);
  155. return OK;
  156. }
  157. void ConfigFile::_bind_methods() {
  158. ObjectTypeDB::bind_method(_MD("set_value", "section", "key", "value"), &ConfigFile::set_value);
  159. ObjectTypeDB::bind_method(_MD("get_value:Variant", "section", "key", "default"), &ConfigFile::get_value, DEFVAL(Variant()));
  160. ObjectTypeDB::bind_method(_MD("has_section", "section"), &ConfigFile::has_section);
  161. ObjectTypeDB::bind_method(_MD("has_section_key", "section", "key"), &ConfigFile::has_section_key);
  162. ObjectTypeDB::bind_method(_MD("get_sections"), &ConfigFile::_get_sections);
  163. ObjectTypeDB::bind_method(_MD("get_section_keys", "section"), &ConfigFile::_get_section_keys);
  164. ObjectTypeDB::bind_method(_MD("load:Error", "path"), &ConfigFile::load);
  165. ObjectTypeDB::bind_method(_MD("save:Error", "path"), &ConfigFile::save);
  166. }
  167. ConfigFile::ConfigFile() {
  168. }