config_file.cpp 6.1 KB

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