Skip to content

Commit c8984dd

Browse files
committed
Add FileAccess.store_{buffer,string}_as_file()
1 parent 2dd26a0 commit c8984dd

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

core/io/file_access.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,19 @@ String FileAccess::get_file_as_string(const String &p_path, Error *r_error) {
874874
return ret;
875875
}
876876

877+
Error FileAccess::store_buffer_as_file(const Vector<uint8_t> &p_buffer, const String &p_path) {
878+
Error err;
879+
Ref<FileAccess> f = FileAccess::open(p_path, WRITE, &err);
880+
if (f.is_null()) {
881+
ERR_FAIL_V_MSG(err, vformat("Can't open file from path '%s'.", p_path));
882+
}
883+
return f->store_buffer(p_buffer) ? OK : ERR_FILE_CANT_WRITE;
884+
}
885+
886+
Error FileAccess::store_string_as_file(const String &p_string, const String &p_path) {
887+
return store_buffer_as_file(p_string.to_utf8_buffer(), p_path);
888+
}
889+
877890
String FileAccess::get_md5(const String &p_file) {
878891
Ref<FileAccess> f = FileAccess::open(p_file, READ);
879892
if (f.is_null()) {
@@ -965,6 +978,8 @@ void FileAccess::_bind_methods() {
965978

966979
ClassDB::bind_static_method("FileAccess", D_METHOD("get_file_as_bytes", "path"), &FileAccess::_get_file_as_bytes);
967980
ClassDB::bind_static_method("FileAccess", D_METHOD("get_file_as_string", "path"), &FileAccess::_get_file_as_string);
981+
ClassDB::bind_static_method("FileAccess", D_METHOD("store_buffer_as_file", "buffer", "path"), &FileAccess::store_buffer_as_file);
982+
ClassDB::bind_static_method("FileAccess", D_METHOD("store_string_as_file", "string", "path"), &FileAccess::store_string_as_file);
968983

969984
ClassDB::bind_method(D_METHOD("resize", "length"), &FileAccess::resize);
970985
ClassDB::bind_method(D_METHOD("flush"), &FileAccess::flush);

core/io/file_access.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ class FileAccess : public RefCounted {
268268
static PackedByteArray _get_file_as_bytes(const String &p_path) { return get_file_as_bytes(p_path, &last_file_open_error); }
269269
static String _get_file_as_string(const String &p_path) { return get_file_as_string(p_path, &last_file_open_error); }
270270

271+
static Error store_buffer_as_file(const Vector<uint8_t> &p_buffer, const String &p_path);
272+
static Error store_string_as_file(const String &p_string, const String &p_path);
273+
271274
template <typename T>
272275
static void make_default(AccessType p_access) {
273276
create_func[p_access] = _create_builtin<T>;

doc/classes/FileAccess.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,14 @@
489489
[b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
490490
</description>
491491
</method>
492+
<method name="store_buffer_as_file" qualifiers="static">
493+
<return type="int" enum="Error" />
494+
<param index="0" name="buffer" type="PackedByteArray" />
495+
<param index="1" name="path" type="String" />
496+
<description>
497+
Stores the given array of bytes as the whole content of the file at [param path]. Returns [constant OK] on success.
498+
</description>
499+
</method>
492500
<method name="store_csv_line">
493501
<return type="bool" />
494502
<param index="0" name="values" type="PackedStringArray" />
@@ -557,6 +565,14 @@
557565
[b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
558566
</description>
559567
</method>
568+
<method name="store_string_as_file" qualifiers="static">
569+
<return type="int" enum="Error" />
570+
<param index="0" name="string" type="String" />
571+
<param index="1" name="path" type="String" />
572+
<description>
573+
Stores [param string] as the whole content of the file at [param path], encoding the text as UTF-8. Returns [constant OK] on success.
574+
</description>
575+
</method>
560576
<method name="store_var">
561577
<return type="bool" />
562578
<param index="0" name="value" type="Variant" />

0 commit comments

Comments
 (0)