config_file.cpp 6.5 KB

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