Browse Source

Add DirAccess:dir_exist api

marynate 11 years ago
parent
commit
e6c1689b69

+ 5 - 0
core/bind/core_bind.cpp

@@ -1347,6 +1347,10 @@ bool _Directory::file_exists(String p_file){
 	return d->file_exists(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(){
 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:Error","name"),&_Directory::make_dir);
 	ObjectTypeDB::bind_method(_MD("make_dir_recursive:Error","name"),&_Directory::make_dir_recursive);
 	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("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_modified_time","file"),&_Directory::get_modified_time);
 	ObjectTypeDB::bind_method(_MD("get_space_left"),&_Directory::get_space_left);
 	ObjectTypeDB::bind_method(_MD("get_space_left"),&_Directory::get_space_left);
 	ObjectTypeDB::bind_method(_MD("copy:Error","from","to"),&_Directory::copy);
 	ObjectTypeDB::bind_method(_MD("copy:Error","from","to"),&_Directory::copy);

+ 1 - 0
core/bind/core_bind.h

@@ -350,6 +350,7 @@ public:
 	Error make_dir_recursive(String p_dir);
 	Error make_dir_recursive(String p_dir);
 
 
 	bool file_exists(String p_file);
 	bool file_exists(String p_file);
+	bool dir_exists(String p_dir);
 
 
 	int get_space_left();
 	int get_space_left();
 
 

+ 5 - 0
core/io/file_access_pack.cpp

@@ -443,6 +443,11 @@ bool DirAccessPack::file_exists(String p_file){
 	return current->files.has(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){
 Error DirAccessPack::make_dir(String p_dir){
 
 
 	return ERR_UNAVAILABLE;
 	return ERR_UNAVAILABLE;

+ 1 - 0
core/io/file_access_pack.h

@@ -190,6 +190,7 @@ public:
 
 
 
 
 	virtual bool file_exists(String p_file);
 	virtual bool file_exists(String p_file);
+	virtual bool dir_exists(String p_dir);
 
 
 	virtual Error make_dir(String p_dir);
 	virtual Error make_dir(String p_dir);
 
 

+ 1 - 1
core/os/dir_access.h

@@ -91,7 +91,7 @@ public:
 	virtual Error erase_contents_recursive(); //super dangerous, use with care!
 	virtual Error erase_contents_recursive(); //super dangerous, use with care!
 
 
 	virtual bool file_exists(String p_file)=0;
 	virtual bool file_exists(String p_file)=0;
-
+	virtual bool dir_exists(String p_dir)=0;
 
 
 	virtual size_t get_space_left()=0;
 	virtual size_t get_space_left()=0;
 
 

+ 20 - 0
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) {
 uint64_t DirAccessUnix::get_modified_time(String p_file) {
 
 
 	if (p_file.is_rel_path())
 	if (p_file.is_rel_path())

+ 3 - 1
drivers/unix/dir_access_unix.h

@@ -66,7 +66,9 @@ public:
 	virtual String get_current_dir(); ///< return current dir location
 	virtual String get_current_dir(); ///< return current dir location
 	virtual Error make_dir(String p_dir);
 	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);
 	virtual uint64_t get_modified_time(String p_file);
 
 
 
 

+ 33 - 0
drivers/windows/dir_access_windows.cpp

@@ -303,6 +303,39 @@ bool DirAccessWindows::file_exists(String p_file) {
 	return false;
 	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) {
 Error DirAccessWindows::rename(String p_path,String p_new_path) {
 
 
 	p_path=fix_path(p_path);
 	p_path=fix_path(p_path);

+ 1 - 0
drivers/windows/dir_access_windows.h

@@ -74,6 +74,7 @@ public:
 
 
 
 
 	virtual bool file_exists(String p_file);
 	virtual bool file_exists(String p_file);
+	virtual bool dir_exists(String p_dir);
 
 
 	virtual Error make_dir(String p_dir);
 	virtual Error make_dir(String p_dir);
 
 

+ 26 - 0
platform/android/dir_access_jandroid.cpp

@@ -179,6 +179,32 @@ bool DirAccessJAndroid::file_exists(String p_file){
 	return exists;
 	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){
 Error DirAccessJAndroid::make_dir(String p_dir){
 
 

+ 1 - 0
platform/android/dir_access_jandroid.h

@@ -70,6 +70,7 @@ public:
 
 
 
 
 	virtual bool file_exists(String p_file);
 	virtual bool file_exists(String p_file);
+	virtual bool dir_exists(String p_dir);
 
 
 	virtual Error make_dir(String p_dir);
 	virtual Error make_dir(String p_dir);
 
 

+ 20 - 0
platform/flash/dir_access_flash.cpp

@@ -156,6 +156,26 @@ bool DirAccessFlash::file_exists(String p_file) {
 	return success;
 	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() {
 size_t DirAccessFlash::get_space_left() {
 
 
 	return 0;
 	return 0;

+ 1 - 0
platform/flash/dir_access_flash.h

@@ -45,6 +45,7 @@ public:
 	Error make_dir(String p_dir);
 	Error make_dir(String p_dir);
 
 
 	bool file_exists(String p_file);
 	bool file_exists(String p_file);
+	bool dir_exists(String p_dir);
 
 
 	size_t get_space_left();
 	size_t get_space_left();