diff options
author | reduz <reduzio@gmail.com> | 2014-06-11 01:19:39 -0300 |
---|---|---|
committer | reduz <reduzio@gmail.com> | 2014-06-11 01:19:39 -0300 |
commit | bb0dd1c5f9c1219eb5d4880f5521943f66b8d03f (patch) | |
tree | d7bd13bad144c41f2e8bb448d5d233b46402738b | |
parent | 2bf34875debd1b60809d22cf61a300b56cdfe084 (diff) | |
parent | e6c1689b69e9e0ec530902b550c9e1e2d1cd0aae (diff) |
Merge pull request #459 from marynate/PR-diracccess-dir-exists
Add DirAccess:dir_exist api
-rw-r--r-- | core/bind/core_bind.cpp | 5 | ||||
-rw-r--r-- | core/bind/core_bind.h | 1 | ||||
-rw-r--r-- | core/io/file_access_pack.cpp | 5 | ||||
-rw-r--r-- | core/io/file_access_pack.h | 1 | ||||
-rw-r--r-- | core/os/dir_access.h | 2 | ||||
-rw-r--r-- | drivers/unix/dir_access_unix.cpp | 20 | ||||
-rw-r--r-- | drivers/unix/dir_access_unix.h | 4 | ||||
-rw-r--r-- | drivers/windows/dir_access_windows.cpp | 33 | ||||
-rw-r--r-- | drivers/windows/dir_access_windows.h | 1 | ||||
-rw-r--r-- | platform/android/dir_access_jandroid.cpp | 26 | ||||
-rw-r--r-- | platform/android/dir_access_jandroid.h | 1 | ||||
-rw-r--r-- | platform/flash/dir_access_flash.cpp | 20 | ||||
-rw-r--r-- | 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 960cdbac20..73b8c01ee2 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 bb68bbaad8..02fe3e8874 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 6a28fa9dae..e2cb300ebc 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 07ce8cbaf8..a4c750bf3c 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 58a925465a..d8672218bd 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 fff86c5a7e..452d791d96 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 7ede4fc31c..119cb5c3f4 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 30f6ff6d8c..aacd02ca24 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 926497c583..36530ba9b3 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 0c8a5785f8..f32e16e7d8 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 b6e3fe393f..958ea34891 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 a0f1679243..4b8992f9d6 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 09e0b844ce..1378a6e56f 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(); |