From e6c1689b69e9e0ec530902b550c9e1e2d1cd0aae Mon Sep 17 00:00:00 2001 From: marynate Date: Sun, 25 May 2014 11:34:51 +0800 Subject: [PATCH] Add DirAccess:dir_exist api --- core/bind/core_bind.cpp | 5 ++++ core/bind/core_bind.h | 1 + core/io/file_access_pack.cpp | 5 ++++ core/io/file_access_pack.h | 1 + core/os/dir_access.h | 2 +- drivers/unix/dir_access_unix.cpp | 20 ++++++++++++++ drivers/unix/dir_access_unix.h | 4 ++- drivers/windows/dir_access_windows.cpp | 33 ++++++++++++++++++++++++ drivers/windows/dir_access_windows.h | 1 + platform/android/dir_access_jandroid.cpp | 26 +++++++++++++++++++ platform/android/dir_access_jandroid.h | 1 + platform/flash/dir_access_flash.cpp | 20 ++++++++++++++ platform/flash/dir_access_flash.h | 1 + 13 files changed, 118 insertions(+), 2 deletions(-) diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 960cdbac202..73b8c01ee22 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -1347,6 +1347,10 @@ bool _Directory::file_exists(String p_file){ return d->file_exists(p_file); } +bool _Directory::dir_exists(String p_dir) { + ERR_FAIL_COND_V(!d,false); + return d->dir_exists(p_dir); +} int _Directory::get_space_left(){ @@ -1386,6 +1390,7 @@ void _Directory::_bind_methods() { ObjectTypeDB::bind_method(_MD("make_dir:Error","name"),&_Directory::make_dir); ObjectTypeDB::bind_method(_MD("make_dir_recursive:Error","name"),&_Directory::make_dir_recursive); ObjectTypeDB::bind_method(_MD("file_exists","name"),&_Directory::file_exists); + ObjectTypeDB::bind_method(_MD("dir_exists","name"),&_Directory::dir_exists); // ObjectTypeDB::bind_method(_MD("get_modified_time","file"),&_Directory::get_modified_time); ObjectTypeDB::bind_method(_MD("get_space_left"),&_Directory::get_space_left); ObjectTypeDB::bind_method(_MD("copy:Error","from","to"),&_Directory::copy); diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index bb68bbaad8a..02fe3e88745 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -350,6 +350,7 @@ public: Error make_dir_recursive(String p_dir); bool file_exists(String p_file); + bool dir_exists(String p_dir); int get_space_left(); diff --git a/core/io/file_access_pack.cpp b/core/io/file_access_pack.cpp index 6a28fa9dae3..e2cb300ebc8 100644 --- a/core/io/file_access_pack.cpp +++ b/core/io/file_access_pack.cpp @@ -443,6 +443,11 @@ bool DirAccessPack::file_exists(String p_file){ return current->files.has(p_file); } +bool DirAccessPack::dir_exists(String p_dir) { + + return current->subdirs.has(p_dir); +} + Error DirAccessPack::make_dir(String p_dir){ return ERR_UNAVAILABLE; diff --git a/core/io/file_access_pack.h b/core/io/file_access_pack.h index 07ce8cbaf81..a4c750bf3c7 100644 --- a/core/io/file_access_pack.h +++ b/core/io/file_access_pack.h @@ -190,6 +190,7 @@ public: virtual bool file_exists(String p_file); + virtual bool dir_exists(String p_dir); virtual Error make_dir(String p_dir); diff --git a/core/os/dir_access.h b/core/os/dir_access.h index 58a925465ad..d8672218bdc 100644 --- a/core/os/dir_access.h +++ b/core/os/dir_access.h @@ -91,7 +91,7 @@ public: virtual Error erase_contents_recursive(); //super dangerous, use with care! virtual bool file_exists(String p_file)=0; - + virtual bool dir_exists(String p_dir)=0; virtual size_t get_space_left()=0; diff --git a/drivers/unix/dir_access_unix.cpp b/drivers/unix/dir_access_unix.cpp index fff86c5a7ed..452d791d96b 100644 --- a/drivers/unix/dir_access_unix.cpp +++ b/drivers/unix/dir_access_unix.cpp @@ -81,6 +81,26 @@ bool DirAccessUnix::file_exists(String p_file) { } +bool DirAccessUnix::dir_exists(String p_dir) { + + GLOBAL_LOCK_FUNCTION + + + if (p_dir.is_rel_path()) + p_dir=current_dir+"/"+p_dir; + else + p_dir=fix_path(p_dir); + + struct stat flags; + bool success = (stat(p_dir.utf8().get_data(),&flags)==0); + + if (success && S_ISDIR(flags.st_mode)) + return true; + + return false; + +} + uint64_t DirAccessUnix::get_modified_time(String p_file) { if (p_file.is_rel_path()) diff --git a/drivers/unix/dir_access_unix.h b/drivers/unix/dir_access_unix.h index 7ede4fc31c6..119cb5c3f45 100644 --- a/drivers/unix/dir_access_unix.h +++ b/drivers/unix/dir_access_unix.h @@ -66,7 +66,9 @@ public: virtual String get_current_dir(); ///< return current dir location virtual Error make_dir(String p_dir); - virtual bool file_exists(String p_file); + virtual bool file_exists(String p_file); + virtual bool dir_exists(String p_dir); + virtual uint64_t get_modified_time(String p_file); diff --git a/drivers/windows/dir_access_windows.cpp b/drivers/windows/dir_access_windows.cpp index 30f6ff6d8cb..aacd02ca247 100644 --- a/drivers/windows/dir_access_windows.cpp +++ b/drivers/windows/dir_access_windows.cpp @@ -303,6 +303,39 @@ bool DirAccessWindows::file_exists(String p_file) { return false; } +bool DirAccessWindows::dir_exists(String p_dir) { + + GLOBAL_LOCK_FUNCTION + + if (!p_dir.is_abs_path()) + p_dir=get_current_dir()+"/"+p_dir; + p_dir=fix_path(p_dir); + + p_dir.replace("/","\\"); + + if (unicode) { + + DWORD fileAttr; + + fileAttr = GetFileAttributesW(p_dir.c_str()); + if (0xFFFFFFFF == fileAttr) + return false; + + return (fileAttr&FILE_ATTRIBUTE_DIRECTORY); + + } else { + DWORD fileAttr; + + fileAttr = GetFileAttributesA(p_dir.ascii().get_data()); + if (0xFFFFFFFF == fileAttr) + return false; + return (fileAttr&FILE_ATTRIBUTE_DIRECTORY); + + } + + return false; +} + Error DirAccessWindows::rename(String p_path,String p_new_path) { p_path=fix_path(p_path); diff --git a/drivers/windows/dir_access_windows.h b/drivers/windows/dir_access_windows.h index 926497c5836..36530ba9b39 100644 --- a/drivers/windows/dir_access_windows.h +++ b/drivers/windows/dir_access_windows.h @@ -74,6 +74,7 @@ public: virtual bool file_exists(String p_file); + virtual bool dir_exists(String p_dir); virtual Error make_dir(String p_dir); diff --git a/platform/android/dir_access_jandroid.cpp b/platform/android/dir_access_jandroid.cpp index 0c8a5785f88..f32e16e7d8f 100644 --- a/platform/android/dir_access_jandroid.cpp +++ b/platform/android/dir_access_jandroid.cpp @@ -179,6 +179,32 @@ bool DirAccessJAndroid::file_exists(String p_file){ return exists; } +bool DirAccessJAndroid::dir_exists(String p_dir) { + + JNIEnv *env = ThreadAndroid::get_env(); + + String sd; + if (current_dir=="") + sd=p_dir; + else + sd=current_dir+"/"+p_dir; + + String path=fix_path(sd).simplify_path(); + if (path.begins_with("/")) + path=path.substr(1,path.length()); + else if (path.begins_with("res://")) + path=path.substr(6,path.length()); + + jstring js = env->NewStringUTF(path.utf8().get_data()); + int res = env->CallIntMethod(io,_dir_open,js); + if (res<=0) + return false; + + env->CallVoidMethod(io,_dir_close,res); + env->DeleteLocalRef(js); + + return true; +} Error DirAccessJAndroid::make_dir(String p_dir){ diff --git a/platform/android/dir_access_jandroid.h b/platform/android/dir_access_jandroid.h index b6e3fe393f4..958ea34891e 100644 --- a/platform/android/dir_access_jandroid.h +++ b/platform/android/dir_access_jandroid.h @@ -70,6 +70,7 @@ public: virtual bool file_exists(String p_file); + virtual bool dir_exists(String p_dir); virtual Error make_dir(String p_dir); diff --git a/platform/flash/dir_access_flash.cpp b/platform/flash/dir_access_flash.cpp index a0f16792438..4b8992f9d66 100644 --- a/platform/flash/dir_access_flash.cpp +++ b/platform/flash/dir_access_flash.cpp @@ -156,6 +156,26 @@ bool DirAccessFlash::file_exists(String p_file) { return success; }; +bool DirAccessFlash::dir_exists(String p_dir) { + + GLOBAL_LOCK_FUNCTION + + + if (p_dir.is_rel_path()) + p_dir=current_dir+"/"+p_dir; + else + p_dir=fix_path(p_dir); + + struct stat flags; + bool success = (stat(p_dir.utf8().get_data(),&flags)==0); + + if (success && S_ISDIR(flags.st_mode)) { + return true; + } + + return false; +}; + size_t DirAccessFlash::get_space_left() { return 0; diff --git a/platform/flash/dir_access_flash.h b/platform/flash/dir_access_flash.h index 09e0b844cee..1378a6e56f2 100644 --- a/platform/flash/dir_access_flash.h +++ b/platform/flash/dir_access_flash.h @@ -45,6 +45,7 @@ public: Error make_dir(String p_dir); bool file_exists(String p_file); + bool dir_exists(String p_dir); size_t get_space_left();