From 272fb05270c4d812d5fb97f2d3290349e694c7f1 Mon Sep 17 00:00:00 2001 From: Marcelo Fernandez Date: Fri, 9 Mar 2018 11:45:21 -0300 Subject: [PATCH] Added File.get_path and File.get_path_absolute functions (cherry picked from commit a4e64c545406be00c00fe93818994977ae378fb9) --- core/bind/core_bind.cpp | 13 +++++++++++++ core/bind/core_bind.h | 3 +++ core/os/file_access.h | 3 +++ doc/classes/File.xml | 14 ++++++++++++++ drivers/unix/file_access_unix.cpp | 11 +++++++++++ drivers/unix/file_access_unix.h | 4 ++++ drivers/windows/file_access_windows.cpp | 22 +++++++++++++++++----- drivers/windows/file_access_windows.h | 5 +++++ 8 files changed, 70 insertions(+), 5 deletions(-) diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index e7db336dc1a..bf57e18db3a 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -1519,6 +1519,17 @@ bool _File::is_open() const { return f != NULL; } +String _File::get_path() const { + + ERR_FAIL_COND_V(!f, ""); + return f->get_path(); +} + +String _File::get_path_absolute() const { + + ERR_FAIL_COND_V(!f, ""); + return f->get_path_absolute(); +} void _File::seek(int64_t p_position) { @@ -1808,6 +1819,8 @@ void _File::_bind_methods() { ClassDB::bind_method(D_METHOD("open", "path", "flags"), &_File::open); ClassDB::bind_method(D_METHOD("close"), &_File::close); + ClassDB::bind_method(D_METHOD("get_path"), &_File::get_path); + ClassDB::bind_method(D_METHOD("get_path_absolute"), &_File::get_path_absolute); ClassDB::bind_method(D_METHOD("is_open"), &_File::is_open); ClassDB::bind_method(D_METHOD("seek", "position"), &_File::seek); ClassDB::bind_method(D_METHOD("seek_end", "position"), &_File::seek_end, DEFVAL(0)); diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index ff4898dfd99..9f46cdfeac3 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -410,6 +410,9 @@ public: void close(); ///< close a file bool is_open() const; ///< true when file is open + String get_path() const; /// returns the path for the current open file + String get_path_absolute() const; /// returns the absolute path for the current open file + void seek(int64_t p_position); ///< seek to a given position void seek_end(int64_t p_position = 0); ///< seek from the end of file int64_t get_position() const; ///< get position in the file diff --git a/core/os/file_access.h b/core/os/file_access.h index 5d10c1a9aa7..c4635fdfbbe 100644 --- a/core/os/file_access.h +++ b/core/os/file_access.h @@ -89,6 +89,9 @@ public: virtual void close() = 0; ///< close a file virtual bool is_open() const = 0; ///< true when file is open + virtual String get_path() const { return ""; } /// returns the path for the current open file + virtual String get_path_absolute() const { return ""; } /// returns the absolute path for the current open file + virtual void seek(size_t p_position) = 0; ///< seek to a given position virtual void seek_end(int64_t p_position = 0) = 0; ///< seek from the end of file virtual size_t get_position() const = 0; ///< get position in the file diff --git a/doc/classes/File.xml b/doc/classes/File.xml index 9a2a51b8d6d..151d173118a 100644 --- a/doc/classes/File.xml +++ b/doc/classes/File.xml @@ -163,6 +163,20 @@ Returns a [String] saved in Pascal format from the file. + + + + + Returns the path as a [String] for the current open file. + + + + + + + Returns the absolute path as a [String] for the current open file. + + diff --git a/drivers/unix/file_access_unix.cpp b/drivers/unix/file_access_unix.cpp index 5b093a58855..a7a3eef935c 100644 --- a/drivers/unix/file_access_unix.cpp +++ b/drivers/unix/file_access_unix.cpp @@ -69,6 +69,7 @@ Error FileAccessUnix::_open(const String &p_path, int p_mode_flags) { fclose(f); f = NULL; + path_src = p_path; path = fix_path(p_path); //printf("opening %ls, %i\n", path.c_str(), Memory::get_static_mem_usage()); @@ -152,6 +153,16 @@ bool FileAccessUnix::is_open() const { return (f != NULL); } +String FileAccessUnix::get_path() const { + + return path_src; +} + +String FileAccessUnix::get_path_absolute() const { + + return path; +} + void FileAccessUnix::seek(size_t p_position) { ERR_FAIL_COND(!f); diff --git a/drivers/unix/file_access_unix.h b/drivers/unix/file_access_unix.h index dbb1c9f3b5c..88bb39fbd10 100644 --- a/drivers/unix/file_access_unix.h +++ b/drivers/unix/file_access_unix.h @@ -51,6 +51,7 @@ class FileAccessUnix : public FileAccess { mutable Error last_error; String save_path; String path; + String path_src; static FileAccess *create_libc(); @@ -61,6 +62,9 @@ public: virtual void close(); ///< close a file virtual bool is_open() const; ///< true when file is open + virtual String get_path() const; /// returns the path for the current open file + virtual String get_path_absolute() const; /// returns the absolute path for the current open file + virtual void seek(size_t p_position); ///< seek to a given position virtual void seek_end(int64_t p_position = 0); ///< seek from the end of file virtual size_t get_position() const; ///< get position in the file diff --git a/drivers/windows/file_access_windows.cpp b/drivers/windows/file_access_windows.cpp index a07be5c4fc7..23c8ea2ec76 100644 --- a/drivers/windows/file_access_windows.cpp +++ b/drivers/windows/file_access_windows.cpp @@ -57,7 +57,8 @@ void FileAccessWindows::check_errors() const { Error FileAccessWindows::_open(const String &p_path, int p_mode_flags) { - String filename = fix_path(p_path); + path_src = p_path; + path = fix_path(p_path); if (f) close(); @@ -78,19 +79,19 @@ Error FileAccessWindows::_open(const String &p_path, int p_mode_flags) { backend supports utf8 encoding */ struct _stat st; - if (_wstat(filename.c_str(), &st) == 0) { + if (_wstat(path.c_str(), &st) == 0) { if (!S_ISREG(st.st_mode)) return ERR_FILE_CANT_OPEN; }; if (is_backup_save_enabled() && p_mode_flags & WRITE && !(p_mode_flags & READ)) { - save_path = filename; - filename = filename + ".tmp"; + save_path = path; + path = path + ".tmp"; //print_line("saving instead to "+path); } - f = _wfopen(filename.c_str(), mode_string); + f = _wfopen(path.c_str(), mode_string); if (f == NULL) { last_error = ERR_FILE_CANT_OPEN; @@ -157,6 +158,17 @@ void FileAccessWindows::close() { ERR_FAIL_COND(rename_error); } } + +String FileAccessWindows::get_path() const { + + return path_src; +} + +String FileAccessWindows::get_path_absolute() const { + + return path; +} + bool FileAccessWindows::is_open() const { return (f != NULL); diff --git a/drivers/windows/file_access_windows.h b/drivers/windows/file_access_windows.h index 26bd08b7afd..0462c1e942a 100644 --- a/drivers/windows/file_access_windows.h +++ b/drivers/windows/file_access_windows.h @@ -46,6 +46,8 @@ class FileAccessWindows : public FileAccess { int flags; void check_errors() const; mutable Error last_error; + String path; + String path_src; String save_path; public: @@ -53,6 +55,9 @@ public: virtual void close(); ///< close a file virtual bool is_open() const; ///< true when file is open + virtual String get_path() const; /// returns the path for the current open file + virtual String get_path_absolute() const; /// returns the absolute path for the current open file + virtual void seek(size_t p_position); ///< seek to a given position virtual void seek_end(int64_t p_position = 0); ///< seek from the end of file virtual size_t get_position() const; ///< get position in the file