MTK系统中搜索文件函数

//此函数搜索当前目录下的视频文件,并返回搜索到的视频个数。

//主要思想是将搜索到的文件后缀名与视频文件后缀名进行比对,如果符合则算作视频文件。

//用了基础API FS_FindNext,FS_FindFirst

S32 srv_search_cur_folder_videos_count(WCHAR* searchPath, U8 filter)
{
	S32 videoSearched = 0;
	FS_HANDLE fileHdl = FS_NO_ERROR;
	FS_HANDLE ret = FS_NO_ERROR;
	FS_DOSDirEntry fileInfo = {0};
	WCHAR fileName[SRV_FMGR_PATH_MAX_LEN+1] = {0};//建议此处用ctr_buffer,由于mtk平台栈空间较小,在栈空间上定义太大的数组会容易导致栈爆掉
	WCHAR filePath[SRV_FMGR_PATH_MAX_LEN+1] = {0};
	WCHAR file_ext[SRV_FMGR_PATH_MAX_FILE_EXT_LEN + 1] = {0};
        MMI_BOOL is_shortname = MMI_FALSE;

	mmi_ucs2cpy((CHAR *)filePath, (const CHAR *)searchPath);
	//mmi_ucs2cpy((CHAR *)filePath, (const CHAR *)searchPath.getBuf());
        mmi_ucs2cat((CHAR *)filePath, (const CHAR *)L"\\*.*");

	ret= FS_FindFirst(
					(const WCHAR*)filePath, 
					0, 
					0, 
					&fileInfo, 
					(WCHAR*)fileName, 
					(SRV_FMGR_PATH_MAX_LEN+1));
					
	if (ret >= 0)
	{
		do
		{
			//如果是目录的话,则不计入文件总数
			if (fileInfo.Attributes & (FS_ATTR_DIR | FS_ATTR_VOLUME | FS_ATTR_HIDDEN | FS_ATTR_SYSTEM))
			{
				goto tag_findNext;
			}

			is_shortname = ((fileInfo.NTReserved & FS_SFN_MATCH) != 0);
			if (is_shortname)
			{
				U8 j = 0;
				for (j = 0; j < sizeof(fileInfo.Extension); j++)
				{
					if (fileInfo.Extension[j] == L' ')
					{
						break;
					}
					file_ext[j] = (UI_character_type)fileInfo.Extension[j];
				}
				file_ext[j] = 0;
			}
			else
			{
				CHAR * ext = mmi_ucs2rchr((const CHAR *)fileName, (WCHAR)L'.');
				if (ext != NULL)
				{
					ext += 2;
					mmi_ucs2ncpy((CHAR *)file_ext, (const CHAR *)ext, SRV_FMGR_PATH_MAX_FILE_EXT_LEN);
					file_ext[SRV_FMGR_PATH_MAX_FILE_EXT_LEN] = L'\0';
				}
				else
				{
					ext = NULL;
					file_ext[0] = 0;
				}
			}
		
			if ((ret >= FS_NO_ERROR)
				&&((!mmi_ucs2cmp((const CHAR*)file_ext, (CHAR*)L"avi"))
				||(!mmi_ucs2cmp((const CHAR*)file_ext, (CHAR*)L"3gp"))
				||(!mmi_ucs2cmp((const CHAR*)file_ext, (CHAR*)L"mp4"))
				||(!mmi_ucs2cmp((const CHAR*)file_ext, (CHAR*)L"rm"))
				||(!mmi_ucs2cmp((const CHAR*)file_ext, (CHAR*)L"rmv")))
				)
			//(SearchFileFormatFilter(file_ext)>=0)
			{
				++videoSearched;
			}

		tag_findNext:
			ret = FS_FindNext(
					ret, 
					&fileInfo, 
					(WCHAR*) fileName, 
					(SRV_FMGR_PATH_MAX_LEN+1));
		}
		while (ret == FS_NO_ERROR);
	}
	FS_FindClose(ret);
	
	return videoSearched;
}

如果要搜索当前目录及下层目录下的视频文件路径,也可以考虑在此基础上扩展。

你可能感兴趣的:(File,ext,filter,Path,character,MTK)