Added File.get_path and File.get_path_absolute functions

This commit is contained in:
Marcelo Fernandez 2018-03-09 11:45:21 -03:00
parent b842369442
commit a4e64c5454
8 changed files with 70 additions and 5 deletions

View File

@ -1535,6 +1535,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) {
@ -1824,6 +1835,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));

View File

@ -415,6 +415,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

View File

@ -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

View File

@ -163,6 +163,20 @@
Returns a [String] saved in Pascal format from the file.
</description>
</method>
<method name="get_path">
<return type="String">
</return>
<description>
Returns the path as a [String] for the current open file.
</description>
</method>
<method name="get_path_absolute">
<return type="String">
</return>
<description>
Returns the absolute path as a [String] for the current open file.
</description>
</method>
<method name="get_position" qualifiers="const">
<return type="int">
</return>

View File

@ -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);

View File

@ -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

View File

@ -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;
@ -154,6 +155,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);

View File

@ -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