天天看點

使用ACE周遊目錄下的檔案



//使用ACE周遊目錄下的檔案(跨平台)

#ifdef WIN32
#define S_ISREG(B) ((B)&_S_IFREG)
#define S_ISDIR(B) ((B)&_S_IFDIR)
#endif
// 使用ACE周遊目錄下的檔案(跨平台) 參數為目錄全路徑
int GetDirFileList(std::string strDirPath)
{
	int nRet = 0;
	std::string strTemp;
	std::string strAbsFilePath;
	ACE_stat stFileInfo;
	ACE_DIR* pBaseDir = ACE_OS::opendir(strDirPath.c_str());
	if(NULL == pBaseDir)
	{
		return 0;
	}
	else
	{
		struct ACE_DIRENT* pDir = ACE_OS::readdir(pBaseDir);
		while(NULL != pDir)
		{
			// skip . and .. directory
			strTemp = pDir->d_name;
			if((strTemp == ".") || (strTemp == ".."))
			{
				pDir = ACE_OS::readdir(pBaseDir);
				continue;
			}
			strAbsFilePath = strDirPath + "/" + strTemp;

			memset(&stFileInfo, 0, sizeof(ACE_stat));
			nRet = ACE_OS::stat(strAbsFilePath.c_str(), &stFileInfo);
			if (nRet != 0)
			{
				ACE_DEBUG((LM_ERROR, ACE_TEXT("[%N:%l]: stat error[%s].\n"), 
					strAbsFilePath.c_str()));
			}
			else
			{
				// when is dir, recursive
				if(S_ISDIR(stFileInfo.st_mode))
				{
					ACE_DEBUG((LM_DEBUG, ACE_TEXT("[%N:%l]: dir:%s\n"), 
						strAbsFilePath.c_str()));
					GetDirFileList(strAbsFilePath);
				}
				else // when is file, print
				{
					ACE_DEBUG((LM_DEBUG, ACE_TEXT("[%N:%l]: file:%s\n"), 
						strAbsFilePath.c_str()));
				}
			}
			pDir = ACE_OS::readdir(pBaseDir);
		}
	}
	ACE_OS::closedir(pBaseDir);
	return 0;
}
           

繼續閱讀