Cocos2.2.3文件夹的创建与递归删除

前端之家收集整理的这篇文章主要介绍了Cocos2.2.3文件夹的创建与递归删除前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
本文介绍了win32、android下文件夹的创建以及递归删除,下述代码都可直接在Cocos中调用文件:
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h> 
#include <stdio.h>
#include <dirent.h>
#endif

代码:
//Android or win32 创建文件夹
bool DLManager::createDirectory(const char * path)
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
	mode_t processMask = umask(0);
	int ret = mkdir(path,S_IRWXU | S_IRWXG | S_IRWXO);
	umask(processMask);
	if (ret != 0 && (errno != EEXIST))
	{
		CCLog("mkdir Failed...");
		return false;
	}

	return true;
#else
	BOOL ret = CreateDirectoryA(path,NULL);
	if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
	{
		return false;
	}
	return true;
#endif
}

//Android or win32 递归删除文件夹
bool DLManager::deleteDirectory(const char * path)
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
	DeleteDirectory(path);
#else	
	return DeleteDirectoryWin32(path);	
#endif
}

//Android 下判断是否为文件夹————引用自http://blog.csdn.net/honty/article/details/7822923
int DLManager::IsDirectory(const char * filename)  
{  
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)

	struct stat buf;  
	int ret = stat(filename,&buf);  
	if(0 == ret)  
	{  
		if(buf.st_mode & S_IFDIR)  
		{  
			//printf("%s is folder\n",filename);  
			return 0;  
		}  
		else  
		{  
			//printf("%s is file\n",filename);  
			return 1;  
		}  
	}  
	return -1;  

#endif

	return -1;
}  

//Android 下递归删除文件夹————引用自http://blog.csdn.net/honty/article/details/7822923
int DLManager::DeleteDirectory(const char * dirname)  
{  
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)

	char chBuf[256];  
	DIR * dir = NULL;  
	struct dirent *ptr;  
	int ret = 0;  
	dir = opendir(dirname);  
	if(NULL == dir)  
	{  
		return -1;  
	}  
	while((ptr = readdir(dir)) != NULL)  
	{  
		ret = strcmp(ptr->d_name,".");  
		if(0 == ret)  
		{  
			continue;  
		}  
		ret = strcmp(ptr->d_name,"..");  
		if(0 == ret)  
		{  
			continue;  
		}  
		snprintf(chBuf,256,"%s/%s",dirname,ptr->d_name);  
		ret = IsDirectory(chBuf);  
		if(0 == ret)  
		{  
			//printf("%s is dir\n",chBuf);  
			ret = DeleteDirectory(chBuf);  
			if(0 != ret)  
			{  
				return -1;  
			}  
		}  
		else if(1 == ret)  
		{  
			//printf("%s is file\n",chBuf);  
			ret = remove(chBuf);  
			if(0 != ret)  
			{  
				return -1;  
			}  
		}  
	}  
	(void)closedir(dir);  

	ret = remove(dirname);  
	if(0 != ret)  
	{  
		return -1;  
	}  
	return 0;  

#endif

	return -1;
}  

//win32 下判断是否为文件夹————引用自http://blog.csdn.net/jaff20071234/article/details/6559533
bool DLManager::IsDirectoryWin32(const char *pDir)  
{ 
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)

    char szCurPath[500];  
    ZeroMemory(szCurPath,500);  
    sprintf_s(szCurPath,500,"%s//*",pDir);  
    WIN32_FIND_DATAA FindFileData;        
    ZeroMemory(&FindFileData,sizeof(WIN32_FIND_DATAA));  
  
    HANDLE hFile = FindFirstFileA(szCurPath,&FindFileData); /**< find first file by given path. */  
  
    if( hFile == INVALID_HANDLE_VALUE )   
    {  
        FindClose(hFile);  
        return FALSE; /** 如果不能找到第一个文件,那么没有目录 */  
    }else  
    {     
        FindClose(hFile);  
        return TRUE;  
    }  

#endif

	return false;
}  

//win32 下递归删除文件夹————引用自http://blog.csdn.net/jaff20071234/article/details/6559533  
bool DLManager::DeleteDirectoryWin32(const char * DirName)  
{  
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)

//http://blog.csdn.net/jaff20071234/article/details/6559533
//  CFileFind tempFind;     //声明一个CFileFind类变量,以用来搜索  
    char szCurPath[MAX_PATH];       //用于定义搜索格式  
    _snprintf(szCurPath,MAX_PATH,"%s//*.*",DirName); //匹配格式为*.*,即该目录下的所有文件  
    WIN32_FIND_DATAA FindFileData;        
    ZeroMemory(&FindFileData,sizeof(WIN32_FIND_DATAA));  
    HANDLE hFile = FindFirstFileA(szCurPath,&FindFileData);  
    BOOL IsFinded = TRUE;  
    while(IsFinded)  
    {  
        IsFinded = FindNextFileA(hFile,&FindFileData); //递归搜索其他的文件  
        if( strcmp(FindFileData.cFileName,".") && strcmp(FindFileData.cFileName,"..") ) //如果不是"." ".."目录  
        {  
            string strFileName = "";  
            strFileName = strFileName + DirName + "//" + FindFileData.cFileName;  
            string strTemp;  
            strTemp = strFileName;  
            if( IsDirectoryWin32(strFileName.c_str()) ) //如果是目录,则递归地调用  
            {     
                printf("目录为:%s/n",strFileName.c_str());  
                DeleteDirectoryWin32(strTemp.c_str());  
            }  
            else  
            {  
                DeleteFileA(strTemp.c_str());  
            }  
        }  
    }  
    FindClose(hFile);  
  
    BOOL bRet = RemoveDirectoryA(DirName);  
    if( bRet == 0 ) //删除目录  
    {  
        printf("删除%s目录失败!/n",DirName);  
        return FALSE;  
    }  
    return TRUE;  

#endif

	return false;
} 
原文链接:https://www.f2er.com/cocos2dx/340155.html

猜你在找的Cocos2d-x相关文章