Add ConfigFile::parse()

This commit is contained in:
Pedro J. Estébanez 2020-02-10 09:19:00 +01:00
parent 47f19cc776
commit e5bd3b707f
3 changed files with 35 additions and 4 deletions

View File

@ -255,6 +255,22 @@ Error ConfigFile::_internal_load(const String &p_path, FileAccess *f) {
VariantParser::StreamFile stream; VariantParser::StreamFile stream;
stream.f = f; 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("<string>", &stream);
}
Error ConfigFile::_parse(const String &p_path, VariantParser::Stream *p_stream) {
String assign; String assign;
Variant value; Variant value;
VariantParser::Tag next_tag; VariantParser::Tag next_tag;
@ -270,13 +286,11 @@ Error ConfigFile::_internal_load(const String &p_path, FileAccess *f) {
next_tag.fields.clear(); next_tag.fields.clear();
next_tag.name = String(); 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) { if (err == ERR_FILE_EOF) {
memdelete(f);
return OK; return OK;
} else if (err != OK) { } else if (err != OK) {
ERR_PRINT("ConfgFile::load - " + p_path + ":" + itos(lines) + " error: " + error_text + "."); ERR_PRINT("ConfgFile - " + p_path + ":" + itos(lines) + " error: " + error_text + ".");
memdelete(f);
return err; return err;
} }
@ -286,6 +300,8 @@ Error ConfigFile::_internal_load(const String &p_path, FileAccess *f) {
section = next_tag.name; section = next_tag.name;
} }
} }
return OK;
} }
void ConfigFile::_bind_methods() { 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("erase_section_key", "section", "key"), &ConfigFile::erase_section_key);
ClassDB::bind_method(D_METHOD("load", "path"), &ConfigFile::load); 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("save", "path"), &ConfigFile::save);
ClassDB::bind_method(D_METHOD("load_encrypted", "path", "key"), &ConfigFile::load_encrypted); ClassDB::bind_method(D_METHOD("load_encrypted", "path", "key"), &ConfigFile::load_encrypted);

View File

@ -34,6 +34,7 @@
#include "core/ordered_hash_map.h" #include "core/ordered_hash_map.h"
#include "core/os/file_access.h" #include "core/os/file_access.h"
#include "core/reference.h" #include "core/reference.h"
#include "core/variant_parser.h"
class ConfigFile : public Reference { class ConfigFile : public Reference {
@ -46,6 +47,8 @@ class ConfigFile : public Reference {
Error _internal_load(const String &p_path, FileAccess *f); Error _internal_load(const String &p_path, FileAccess *f);
Error _internal_save(FileAccess *file); Error _internal_save(FileAccess *file);
Error _parse(const String &p_path, VariantParser::Stream *p_stream);
protected: protected:
static void _bind_methods(); static void _bind_methods();
@ -64,6 +67,7 @@ public:
Error save(const String &p_path); Error save(const String &p_path);
Error load(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<uint8_t> &p_key); Error load_encrypted(const String &p_path, const Vector<uint8_t> &p_key);
Error load_encrypted_pass(const String &p_path, const String &p_pass); Error load_encrypted_pass(const String &p_path, const String &p_pass);

View File

@ -129,6 +129,16 @@
<description> <description>
</description> </description>
</method> </method>
<method name="parse">
<return type="int" enum="Error">
</return>
<argument index="0" name="data" type="String">
</argument>
<description>
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).
</description>
</method>
<method name="save"> <method name="save">
<return type="int" enum="Error"> <return type="int" enum="Error">
</return> </return>