Added 2 new helper methods to FileAccess.

This commit is contained in:
Relintai 2024-01-20 13:15:20 +01:00
parent a4b364ea3f
commit 562b005314
4 changed files with 64 additions and 0 deletions

View File

@ -1152,6 +1152,35 @@ String FileAccess::get_file_as_string(const String &p_path, Error *r_error) {
return ret;
}
void FileAccess::write_file(const String &p_path, const String &data, Error *r_error) {
FileAccess *f = FileAccess::create_and_open(p_path, WRITE, r_error);
if (!f) {
if (r_error) { // if error requested, do not throw error
return;
}
ERR_FAIL_MSG("Can't open file from path '" + String(p_path) + "'.");
}
f->store_string(data);
f->close();
memdelete(f);
}
void FileAccess::write_file_buffer(const String &p_path, const Vector<uint8_t> &data, Error *r_error) {
FileAccess *f = FileAccess::create_and_open(p_path, WRITE, r_error);
if (!f) {
if (r_error) { // if error requested, do not throw error
return;
}
ERR_FAIL_MSG("Can't open file from path '" + String(p_path) + "'.");
}
f->store_buffer_vec(data);
f->close();
memdelete(f);
}
/*
FileAccess::FileAccess() {
endian_swap = false;

View File

@ -141,6 +141,9 @@ public:
static Vector<uint8_t> get_file_as_array(const String &p_path, Error *r_error = nullptr);
static String get_file_as_string(const String &p_path, Error *r_error = nullptr);
static void write_file(const String &p_path, const String &data, Error *r_error = nullptr);
static void write_file_buffer(const String &p_path, const Vector<uint8_t> &data, Error *r_error = nullptr);
FileAccess();
virtual ~FileAccess();

View File

@ -1152,6 +1152,35 @@ String FileAccess::get_file_as_string(const String &p_path, Error *r_error) {
return ret;
}
void FileAccess::write_file(const String &p_path, const String &data, Error *r_error) {
FileAccess *f = FileAccess::create_and_open(p_path, WRITE, r_error);
if (!f) {
if (r_error) { // if error requested, do not throw error
return;
}
ERR_FAIL_MSG("Can't open file from path '" + String(p_path) + "'.");
}
f->store_string(data);
f->close();
memdelete(f);
}
void FileAccess::write_file_buffer(const String &p_path, const Vector<uint8_t> &data, Error *r_error) {
FileAccess *f = FileAccess::create_and_open(p_path, WRITE, r_error);
if (!f) {
if (r_error) { // if error requested, do not throw error
return;
}
ERR_FAIL_MSG("Can't open file from path '" + String(p_path) + "'.");
}
f->store_buffer_vec(data);
f->close();
memdelete(f);
}
/*
FileAccess::FileAccess() {
endian_swap = false;

View File

@ -141,6 +141,9 @@ public:
static Vector<uint8_t> get_file_as_array(const String &p_path, Error *r_error = nullptr);
static String get_file_as_string(const String &p_path, Error *r_error = nullptr);
static void write_file(const String &p_path, const String &data, Error *r_error = nullptr);
static void write_file_buffer(const String &p_path, const Vector<uint8_t> &data, Error *r_error = nullptr);
FileAccess();
virtual ~FileAccess();