- 论坛徽章:
- 30
|
本帖最后由 siseniao 于 2012-12-07 13:09 编辑
- typedef int (__stdcall *LISTDIRCALLBACK)(int type,char *fullpath,char *name,void *userdata);
- typedef struct dirs
- {
- char *file;
- struct dirs *father;
- struct dirs *child;
- struct dirs *next;
-
- } dirs_t;
- void DelTree(dirs_t *peer) //销毁目录树函数
- {
- dirs_t *p=peer;
- dirs_t *tmp;
- while(p->father!=NULL||(p->child!=p&&p->child!=NULL))
- {
- if(p->child!=NULL&&p->child!=p)
- {
- p=p->child;
- p->father->child=p->next;
- // continue;
- }
- else if(!(p->child!=NULL&&p->child!=p)&&p->father!=NULL)
- {
- tmp=p;
- if(p->next!=NULL)
- {
- p=p->next;
- p->father->child=p->next;
- }
- else
- {
- p=p->father;
- p->child=NULL;
-
- }
- free(tmp->file);
- free(tmp);
- }
- else
- {
- free(p);
- break;
- }
- }
- }
复制代码- int DoProcFile(LISTDIRCALLBACK CallbackFun,char *path,char *filename,void *userdata,int type)
- {
- char *fullpath;
- if(CallbackFun!=NULL)
- {
- fullpath=(char *)malloc((strlen(path)+strlen(filename)+5)*sizeof(char));
- if(fullpath==NULL)
- {
- return -2;
- }
- strcpy(fullpath,path);
- strcat(fullpath,"\\");
- strcat(fullpath,filename);
- if(CallbackFun(type,fullpath,filename,userdata)==-1)
- return -1;
-
- free(fullpath);
- }
- return 0;
- }
复制代码- int __stdcall DirListAll(char *topdir,LISTDIRCALLBACK CallbackFun,void *userdata) //功能函数
- {
- dirs_t *top,*p,*pfather,*tmp;
- int callret;
- WIN32_FIND_DATA FindFileData;
- HANDLE hFind = INVALID_HANDLE_VALUE;
- int firstflag; //下行标志
-
- char *pathname;
- int pathsize;
- char *ptmp;
- // int num=0;
- if(topdir==NULL)
- return -1;
-
- top=(dirs_t *)malloc(sizeof(dirs_t));
- if(top==NULL)
- return -1;
-
- top->file=topdir;
- top->father=NULL;
- top->next=NULL;
- top->child=top;
- p=top;
- do
- {
- firstflag=0;
- if(p->father!=NULL&&p->child==NULL)//上行判断
- {
- if(p->next!=NULL)
- {
- tmp=p;
- p=p->next;
- p->father->child=p->next;
- free(tmp->file);
- free(tmp);
- }
- else
- {
- tmp=p;
- p=p->father;
- p->child=NULL;
- free(tmp->file);
- free(tmp);
- continue;
- }
- }
-
- tmp=p;
- pathsize=strlen(top->file);
- while(tmp!=top)
- {
- pathsize+=strlen(tmp->file);
- pathsize++;
- tmp=tmp->father;
- }
- pathname=(char *)malloc((pathsize+6)*sizeof(char));
- if(pathname==NULL)
- {
- DelTree(p);
- return -1;
- }
- tmp=p;
-
- strcpy(pathname,top->file);
- ptmp=pathname+pathsize;
- while(tmp!=top)
- {
- ptmp=ptmp-strlen(tmp->file);
- memcpy(ptmp,tmp->file,strlen(tmp->file));
- memcpy(ptmp-1,"\\",1);
- ptmp--;
- tmp=tmp->father;
- }
- pathname[pathsize]='\0';
- strcat(pathname,"\\*.*");
- hFind = FindFirstFile(pathname, &FindFileData);
- if(hFind!=INVALID_HANDLE_VALUE)
- {
- do
- {
- if(strcmp(FindFileData.cFileName,".")!=0&&strcmp(FindFileData.cFileName,"..")!=0)
- {
- if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- {
- if(!firstflag) //下行标志
- {
- p->child=(dirs_t *)malloc(sizeof(dirs_t));
- if(p->child==NULL)
- {
- DelTree(p);
- FindClose(hFind);
- free(pathname);
- return -1;
- }
- pfather=p;
- p=p->child;
- p->father=pfather;
- firstflag=1;
- }
- else
- {
- p->next=(dirs_t *)malloc(sizeof(dirs_t));
- if(p->next==NULL)
- {
- DelTree(p);
- FindClose(hFind);
- free(pathname);
- return -1;
- }
- p=p->next;
- }
- p->father=pfather;
- p->next=NULL;
- p->child=p;
- p->file=(char *)malloc((strlen(FindFileData.cFileName)+1)*sizeof(char));
- if(p->file==NULL)
- {
- DelTree(p);
- FindClose(hFind);
- free(pathname);
- return -1;
- }
- strcpy(p->file,FindFileData.cFileName);
- pathname[pathsize]='\0';
-
- callret=DoProcFile(CallbackFun,pathname,FindFileData.cFileName,userdata,TYPE_DIR);
- if(callret<0)
- {
- DelTree(p);
- free(pathname);
- FindClose(hFind);
- if(callret==-1)
- return 0;
- else
- return -1;
- }
- }
- else
- {
- pathname[pathsize]='\0';
- callret=DoProcFile(CallbackFun,pathname,FindFileData.cFileName,userdata,TYPE_FILE);
- if(callret<0)
- {
- DelTree(p);
- free(pathname);
- FindClose(hFind);
- if(callret==-1)
- return 0;
- else
- return -1;
- }
-
- // num++;
- }
- }
- }
- while(FindNextFile(hFind, &FindFileData));
- }
- FindClose(hFind);
- free(pathname);
- if(firstflag!=0) //进入子目录
- {
- // updownflag=0;
- p=p->father->child;
- p->father->child=p->next;
- }
- else if(firstflag==0&&p->father!=NULL)
- {
- tmp=p;
- if(p->next==NULL) //进入父目录
- {
- p=p->father;
- p->child=NULL;
- }
- else //进入同级目录
- {
- p=p->next;
- p->father->child=p->next;
- }
- free(tmp->file);
- free(tmp);
- }
- else
- {
- break;
- }
- }while(p->father!=NULL||p->child!=NULL);
- free(top);
- //printf("共%d个文件\n",num);
- return 0;
- }
复制代码 |
|