2
0

config_file.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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-2016 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. StringArray ConfigFile::_get_sections() const {
  34. List<String> s;
  35. get_sections(&s);
  36. StringArray 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. StringArray ConfigFile::_get_section_keys(const String& p_section) const{
  45. List<String> s;
  46. get_section_keys(p_section,&s);
  47. StringArray 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. Error ConfigFile::save(const String& p_path){
  96. Error err;
  97. FileAccess *file = FileAccess::open(p_path,FileAccess::WRITE,&err);
  98. if (err) {
  99. if (file)
  100. memdelete(file);
  101. return err;
  102. }
  103. for(Map< String, Map<String, Variant> >::Element *E=values.front();E;E=E->next()) {
  104. if (E!=values.front())
  105. file->store_string("\n");
  106. file->store_string("["+E->key()+"]\n\n");
  107. for(Map<String, Variant>::Element *F=E->get().front();F;F=F->next()) {
  108. String vstr;
  109. VariantWriter::write_to_string(F->get(),vstr);
  110. file->store_string(F->key()+"="+vstr+"\n");
  111. }
  112. }
  113. memdelete(file);
  114. return OK;
  115. }
  116. Error ConfigFile::load(const String& p_path) {
  117. Error err;
  118. FileAccess *f= FileAccess::open(p_path,FileAccess::READ,&err);
  119. if (!f)
  120. return ERR_CANT_OPEN;
  121. VariantParser::StreamFile stream;
  122. stream.f=f;
  123. String assign;
  124. Variant value;
  125. VariantParser::Tag next_tag;
  126. int lines=0;
  127. String error_text;
  128. String section;
  129. while(true) {
  130. assign=Variant();
  131. next_tag.fields.clear();
  132. next_tag.name=String();
  133. err = VariantParser::parse_tag_assign_eof(&stream,lines,error_text,next_tag,assign,value,NULL,true);
  134. if (err==ERR_FILE_EOF) {
  135. memdelete(f);
  136. return OK;
  137. }
  138. else if (err!=OK) {
  139. ERR_PRINTS("ConfgFile::load - "+p_path+":"+itos(lines)+" error: "+error_text);
  140. memdelete(f);
  141. return err;
  142. }
  143. if (assign!=String()) {
  144. set_value(section,assign,value);
  145. } else if (next_tag.name!=String()) {
  146. section=next_tag.name;
  147. }
  148. }
  149. memdelete(f);
  150. return OK;
  151. }
  152. void ConfigFile::_bind_methods(){
  153. ObjectTypeDB::bind_method(_MD("set_value","section","key","value"),&ConfigFile::set_value);
  154. ObjectTypeDB::bind_method(_MD("get_value","section","key","default"),&ConfigFile::get_value,DEFVAL(Variant()));
  155. ObjectTypeDB::bind_method(_MD("has_section","section"),&ConfigFile::has_section);
  156. ObjectTypeDB::bind_method(_MD("has_section_key","section","key"),&ConfigFile::has_section_key);
  157. ObjectTypeDB::bind_method(_MD("get_sections"),&ConfigFile::_get_sections);
  158. ObjectTypeDB::bind_method(_MD("get_section_keys","section"),&ConfigFile::_get_section_keys);
  159. ObjectTypeDB::bind_method(_MD("load:Error","path"),&ConfigFile::load);
  160. ObjectTypeDB::bind_method(_MD("save:Error","path"),&ConfigFile::save);
  161. }
  162. ConfigFile::ConfigFile()
  163. {
  164. }