📄 filelist.h
字号:
#pragma once
#include "tlink.h"
#include <io.h>
struct Cfilename
{
char path_buffer[_MAX_PATH];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
Cfilename(char *path)
{
// _fullpath(path_buffer,path,_MAX_PATH);
strcpy(path_buffer,path);
_splitpath(path_buffer,drive,dir,fname,ext);
}
};
class CFileList;
class CListNode:public CLink<CListNode> // the link is his sons(sub_dir or files)
{
protected:
CListNode *parent;
bool is_file;
bool Subdir;
unsigned DirCount;
Cfilename filename;
_finddata_t c_file;
unsigned long handle;
bool FindFirst(bool is_file,char *name="*",char *ext=".*")
{
char buff[_MAX_PATH];
strcpy(buff,filename.drive);
strcat(buff,filename.dir);
strcat(buff,name);
strcat(buff,ext);
int result=0;
for (handle=_findfirst(buff,&c_file);(handle!=-1)&&(result==0);result=_findnext(handle,&c_file))
if ((is_file&&(c_file.attrib&_A_SUBDIR)==0)||(!is_file&&(c_file.attrib&_A_SUBDIR)))
return true;
return false; // the data in c_file is invalid
}
bool FindNext(bool is_file)
{
assert(handle!=-1);
int result=0;
for (result=_findnext(handle,&c_file);result==0;result=_findnext(handle,&c_file))
if ((is_file&&(c_file.attrib&_A_SUBDIR)==0)||(!is_file&&(c_file.attrib&_A_SUBDIR)))
return true;
return false; // the data in c_file is invalid
}
void GetPath(char *buffer)
{
CListNode *p=parent;
while(p)
{
strcat(buffer,"\\");
strcat(buffer,p->filename.fname);
strcat(buffer,p->filename.ext);
p=p->parent;
}
}
public:
void Init()
{
Head();
CListNode *p;
for (p=GetData();p;Next(),p=GetData())
p->Init();
Head();
}
CListNode *CreateNewNode(char *name,bool isfile,bool subdir)
{
char buff[_MAX_PATH];
strcpy(buff,filename.drive);
strcat(buff,filename.dir);
strcat(buff,name);
if (!isfile)
{
strcat(buff,"\\");
strcat(buff,filename.fname);
strcat(buff,filename.ext);
}
CListNode *p=new CListNode(buff,isfile,subdir,this);
return p;
}
CListNode(char *name,bool isfile,bool include_sub,CListNode *p=NULL):CLink<CListNode>(true,false),filename(name)
{
//strncpy(fname,filename,_MAX_FNAME);
is_file=isfile;
parent=p;
DirCount=0;
if (!is_file) // if is a directory then we search the contents in it
{
if (FindFirst(true,filename.fname,filename.ext)) // add files
{
CListNode *p=CreateNewNode(c_file.name,true,include_sub);
Append(p);
while(FindNext(true))
{
p=CreateNewNode(c_file.name,true,include_sub);
Append(p);
}
}
if (include_sub&&FindFirst(false)) // add dir
{
if (strcmp(c_file.name,".")&&strcmp(c_file.name,".."))
{
strcat(c_file.name,"\\");
CListNode *p=CreateNewNode(c_file.name,false,include_sub);
Append(p);
DirCount++;
}
while(FindNext(false))
{
if (strcmp(c_file.name,".")&&strcmp(c_file.name,".."))
{
p=CreateNewNode(c_file.name,false,include_sub);
Append(p);
DirCount++;
}
}
}
}
}
int operator==(CListNode &node2)
{
return strcmp(filename.path_buffer,node2.filename.path_buffer)==0;
}
friend class CFileList;
};
class CFileList:public CListNode
{
CListNode *CurrentDir;
// char currentfile[_MAX_PATH];
public:
CFileList(char *filename,bool IncludeSubDirectory):
CListNode(filename,false,IncludeSubDirectory)
{
Init();
CurrentDir=this;
}
char *FirstFile()
{
CurrentDir=this;
return FindFile();
}
char *FindFile()
{
while(1)
{
CListNode *p=CurrentDir->GetData();
if (p)
{
if (p->is_file) // if is a file then return the name
{
return p->filename.path_buffer;
}
else
{
CurrentDir=p; // come in to the dir "CD dir"
CurrentDir->Head(); // get first file/dir
}
}
else // the dir's content is all checked
{
p=CurrentDir->parent; // CD..
if (p==NULL) // no more parent ( it is root )
return NULL;
CurrentDir=p;
CurrentDir->Next();
}
}
}
char *NextFile()
{
CurrentDir->Next();
return FindFile();
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -