Merge pull request #459 from marynate/PR-diracccess-dir-exists

Add DirAccess:dir_exist api
This commit is contained in:
reduz 2014-06-11 01:19:39 -03:00
commit bb0dd1c5f9
13 changed files with 118 additions and 2 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -67,6 +67,8 @@ public:
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);

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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