From e5bd3b707f07d1f3eb4d4a8a18ee1736ff1fbe76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Mon, 10 Feb 2020 09:19:00 +0100 Subject: [PATCH] Add ConfigFile::parse() --- core/io/config_file.cpp | 25 +++++++++++++++++++++---- core/io/config_file.h | 4 ++++ doc/classes/ConfigFile.xml | 10 ++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/core/io/config_file.cpp b/core/io/config_file.cpp index 99da5122470..74e85d0cc0a 100644 --- a/core/io/config_file.cpp +++ b/core/io/config_file.cpp @@ -255,6 +255,22 @@ Error ConfigFile::_internal_load(const String &p_path, FileAccess *f) { VariantParser::StreamFile stream; stream.f = f; + Error err = _parse(p_path, &stream); + + memdelete(f); + + return err; +} + +Error ConfigFile::parse(const String &p_data) { + + VariantParser::StreamString stream; + stream.s = p_data; + return _parse("", &stream); +} + +Error ConfigFile::_parse(const String &p_path, VariantParser::Stream *p_stream) { + String assign; Variant value; VariantParser::Tag next_tag; @@ -270,13 +286,11 @@ Error ConfigFile::_internal_load(const String &p_path, FileAccess *f) { next_tag.fields.clear(); next_tag.name = String(); - Error err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true); + Error err = VariantParser::parse_tag_assign_eof(p_stream, lines, error_text, next_tag, assign, value, NULL, true); if (err == ERR_FILE_EOF) { - memdelete(f); return OK; } else if (err != OK) { - ERR_PRINT("ConfgFile::load - " + p_path + ":" + itos(lines) + " error: " + error_text + "."); - memdelete(f); + ERR_PRINT("ConfgFile - " + p_path + ":" + itos(lines) + " error: " + error_text + "."); return err; } @@ -286,6 +300,8 @@ Error ConfigFile::_internal_load(const String &p_path, FileAccess *f) { section = next_tag.name; } } + + return OK; } void ConfigFile::_bind_methods() { @@ -303,6 +319,7 @@ void ConfigFile::_bind_methods() { ClassDB::bind_method(D_METHOD("erase_section_key", "section", "key"), &ConfigFile::erase_section_key); ClassDB::bind_method(D_METHOD("load", "path"), &ConfigFile::load); + ClassDB::bind_method(D_METHOD("parse", "data"), &ConfigFile::parse); ClassDB::bind_method(D_METHOD("save", "path"), &ConfigFile::save); ClassDB::bind_method(D_METHOD("load_encrypted", "path", "key"), &ConfigFile::load_encrypted); diff --git a/core/io/config_file.h b/core/io/config_file.h index 95a581d1566..2d61ef6afe9 100644 --- a/core/io/config_file.h +++ b/core/io/config_file.h @@ -34,6 +34,7 @@ #include "core/ordered_hash_map.h" #include "core/os/file_access.h" #include "core/reference.h" +#include "core/variant_parser.h" class ConfigFile : public Reference { @@ -46,6 +47,8 @@ class ConfigFile : public Reference { Error _internal_load(const String &p_path, FileAccess *f); Error _internal_save(FileAccess *file); + Error _parse(const String &p_path, VariantParser::Stream *p_stream); + protected: static void _bind_methods(); @@ -64,6 +67,7 @@ public: Error save(const String &p_path); Error load(const String &p_path); + Error parse(const String &p_data); Error load_encrypted(const String &p_path, const Vector &p_key); Error load_encrypted_pass(const String &p_path, const String &p_pass); diff --git a/doc/classes/ConfigFile.xml b/doc/classes/ConfigFile.xml index 7eaa815a879..ad36f1f1c38 100644 --- a/doc/classes/ConfigFile.xml +++ b/doc/classes/ConfigFile.xml @@ -129,6 +129,16 @@ + + + + + + + Parses the the passed string as the contents of a config file. The string is parsed and loaded in the ConfigFile object which the method was called on. + Returns one of the [enum Error] code constants ([code]OK[/code] on success). + +