📄 file.cpp
字号:
CDir::CDir()
{
this->bOpened = false;
this->bNext = false;
#ifdef WIN32
dp = INVALID_HANDLE_VALUE;
#else
dp = NULL;
#endif
}
CDir::~CDir()
{
Close();
}
bool CDir::OpenDirectory(const char* pathname)
{
this->strDirectory = pathname;
if ( strDirectory[strDirectory.length()-1] != '\\' &&
strDirectory[strDirectory.length()-1] != '/' )
{
#ifdef WIN32
strDirectory += "\\";
#else
strDirectory += "/";
#endif
}
#ifdef WIN32
dp = FindFirstFile((strDirectory + "*").c_str(),&this->fp);
if ( dp == INVALID_HANDLE_VALUE )
return false;
#else
dp = opendir(strDirectory.c_str());
if ( dp == NULL )
return false;
#endif
this->bOpened = true;
return true;
}
bool CDir::HasNextFile()
{
if ( !bOpened )
return false;
this->strFileName = "";
#ifdef WIN32
this->bNext = FindNextFile(dp,&fp);
if ( bNext )
strFileName = fp.cFileName;
#else
struct dirent *dirp;
dirp = readdir(dp);
if ( dirp == NULL )
bNext = false;
else
{
bNext =true;
strFileName = dirp->d_name;
char filename[500];
sprintf(filename,"%s%s",strDirectory.c_str(),strFileName.c_str());
stat(filename,&fp);
}
#endif
return bNext;
}
string CDir::NextFile()
{
return this->strFileName;
}
string CDir::GetDirectory()
{
return this->strDirectory;
}
bool CDir::isDirectory()
{
#ifdef WIN32
return ( fp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY );
#else
return ( S_ISDIR(fp.st_mode) );
#endif
}
ulong64 CDir::GetFileSize()
{
ulong64 filesize;
filesize.HighFileSize = 0;
filesize.LowFileSize = 0;
#ifdef WIN32
filesize.HighFileSize = fp.nFileSizeHigh;
filesize.LowFileSize = fp.nFileSizeLow;
#else
filesize.LowFileSize = fp.st_size & 0xFFFFFFFF;
#ifdef _64BIT_
filesize.HighFileSize = fp.st_size >> 8 ;
#endif
#endif
return filesize;
}
void CDir::Close()
{
#ifdef WIN32
if ( dp != INVALID_HANDLE_VALUE )
FindClose(dp);
dp = INVALID_HANDLE_VALUE;
#else
if ( dp != NULL )
closedir(dp);
dp = NULL;
#endif
bOpened = false;
}
/*
class CFileFilter
?表示任意一个字节
*表示任意长度的字节
*/
CFileFilter::~CFileFilter()
{
}
CFileFilter::CFileFilter(const string& filter)
{
filematchname = filter;
}
bool CFileFilter::isMatch(const string& filename)
{
int oldstate = -1;
int iFilterBeginIndex = 0,iFileNameIndex =0;
char curch=0,prevch=0,firstch = 0;
for ( iFilterBeginIndex = 0 ; iFilterBeginIndex < filematchname.length(); iFilterBeginIndex++)
{
if ( iFileNameIndex >= filename.length() )
return false;
curch = filematchname[iFilterBeginIndex];
switch ( curch )
{
case '?' :
iFileNameIndex++;
break;
case '*' :
oldstate = iFilterBeginIndex;
break;
default :
if ( prevch == '*' || prevch == 0 )
firstch = curch;
if ( curch != filename[iFileNameIndex] )
{
if ( oldstate == -1 )
return false;
else
{
if ( firstch == filename[iFileNameIndex] )
iFileNameIndex --;
iFilterBeginIndex = oldstate;
}
}
iFileNameIndex++;
}
prevch = curch;
}
//出现文件名还有剩余长度的情况
if ( iFileNameIndex != filename.length() )
{
//如果后面是*的匹配则认为完全正确
if ( curch == '*' )
return true;
//如果匹配规则中曾经出现*号则进行反向匹配
else if ( oldstate != -1 )
{
iFileNameIndex = filename.length() - 1;
//从文件名后进行匹配
for ( iFilterBeginIndex--;iFilterBeginIndex >= 0;iFilterBeginIndex--,iFileNameIndex-- )
{
curch = filematchname[iFilterBeginIndex];
switch ( curch )
{
case '?' :
break;
case '*' :
return true;
default :
if ( curch != filename[iFileNameIndex] )
return false;
}
}
return false;
}
//否则认为匹配错误
else
return false;
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -