C/C++ code/**************************************************************
** 这个函数用于输出某个文件的绝对路径名。本函数会发生重入。
** pparent : 上一级路径名,如"D:\\TestDir\\dir-A"
** pself : 当前文件夹名,如"dir-B"
** pfileflt : 过滤文件类型,如"*.mp3;*.mp4;*.obj"
** fileopposite : 是否只查找pfileflt以外的所有文件
** pfldflt : 过滤文件夹,如"folderA;folderB;"
** fldopposite : 是否只查找pfldflt以外的所有文件夹
** pout : 输出文件,文件句柄
** ref : 参考类型,即是否比给定的时间新或旧
*/
static void GetFileFullPathName(char *pparent, char *pself,
char *pfileflt, int fileopposite,
char *pfldflt, int fldopposite,
FILE *pout,
FILE_COMPARE_TYPE ref)
{
FINDDATA_T finddata;
long findhandle = INVALID_HANDLE;
char *tobefind = NULL;
int leng = 0;
leng = strlen(pparent) + 3;
if(pself != NULL)
leng += strlen(pself);
/*这里使用malloc申请内存,而且使用free释放内存,需要注意的是,这个函数时递归调用的,
**系统执行时可能会不断的申请内存,如果申请的内存过小,会导致运行时出错:
**debug error,DAMAGE: after normal block (#55) at 0x.......
**适当增大malloc的内存,可以解决这个问题。
**当然这个问题也可能是由于使用strcat这类的函数导致了数组越界,内存溢出而引发的。
*/
leng = leng > FILE_NAME_STRING_LENGTH ? leng : FILE_NAME_STRING_LENGTH;
tobefind = (char*)malloc(leng);
if(tobefind != NULL)
{
memset((void*)tobefind, 0, leng);
if(pself == NULL)
sprintf(tobefind, "%s\\*.*", pparent);
else
sprintf(tobefind, "%s\\%s\\*.*", pparent, pself);
findhandle = _findfirst(tobefind, &finddata);
if(findhandle != INVALID_HANDLE)
{
while(1)
{
if((finddata.attrib & _A_SUBDIR) && (finddata.name[0] != '.'))
{
memset((void*)tobefind, 0, leng);
if(pself == NULL)
sprintf(tobefind, "%s", pparent);
else
sprintf(tobefind, "%s\\%s", pparent, pself);
if(fldopposite ^ IsCurrentFolderNameFit(pfldflt, finddata.name))
{
//这里开始递归调用
GetFileFullPathName(tobefind,
finddata.name[0]=='\0'?NULL:finddata.name,
pfileflt,
fileopposite,
pfldflt,
fldopposite,
pout,
ref);
}
}
else if(!(finddata.attrib & _A_SUBDIR))
{
if(fileopposite ^ IsCurrentFileExtendNameFit(pfileflt, finddata.name))
{
if(pself == NULL)
{
//是否记录当前文件的绝对路径
if(pout != NULL && IsRecordFilePathFullName(finddata.time_write, file_time_ref, ref))
{
fprintf(pout, "%s\\%s\r\n", pparent, finddata.name);
}
}
else
{
//是否记录当前文件的绝对路径
if(pout != NULL && IsRecordFilePathFullName(finddata.time_write, file_time_ref, ref))
{
fprintf(pout, "%s\\%s\\%s\r\n", pparent, pself, finddata.name);
}
}
}
}
if(_findnext(findhandle, &finddata) == INVALID_HANDLE)
break;
}
_findclose(findhandle);
}
free(tobefind);
}
}