From 562b005314963ff15005c42979106e43b97aa88c Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 20 Jan 2024 13:15:20 +0100 Subject: [PATCH] Added 2 new helper methods to FileAccess. --- sfw/core/file_access.cpp | 29 +++++++++++++++++++++++++++++ sfw/core/file_access.h | 3 +++ sfwl/core/file_access.cpp | 29 +++++++++++++++++++++++++++++ sfwl/core/file_access.h | 3 +++ 4 files changed, 64 insertions(+) diff --git a/sfw/core/file_access.cpp b/sfw/core/file_access.cpp index aedb68e..337794d 100644 --- a/sfw/core/file_access.cpp +++ b/sfw/core/file_access.cpp @@ -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 &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; diff --git a/sfw/core/file_access.h b/sfw/core/file_access.h index 80649fc..014e9c4 100644 --- a/sfw/core/file_access.h +++ b/sfw/core/file_access.h @@ -141,6 +141,9 @@ public: static Vector 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 &data, Error *r_error = nullptr); + FileAccess(); virtual ~FileAccess(); diff --git a/sfwl/core/file_access.cpp b/sfwl/core/file_access.cpp index aedb68e..337794d 100644 --- a/sfwl/core/file_access.cpp +++ b/sfwl/core/file_access.cpp @@ -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 &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; diff --git a/sfwl/core/file_access.h b/sfwl/core/file_access.h index 80649fc..014e9c4 100644 --- a/sfwl/core/file_access.h +++ b/sfwl/core/file_access.h @@ -141,6 +141,9 @@ public: static Vector 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 &data, Error *r_error = nullptr); + FileAccess(); virtual ~FileAccess();