?? sadirread.cpp
字號(hào):
/*********************************************************************
SADirRead
Copyright (C) 2002 Smaller Animals Software, Inc.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
http://www.smalleranimals.com
smallest@smalleranimals.com
**********************************************************************/
// SADirRead.cpp: implementation of the CSADirRead class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "Dir.h"
#include "SADirRead.h"
//#include <io.h>
//#include <errno.h>
//#include <sys/types.h>
//#include <sys/stat.h>
#include <algorithm>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
// some helper functions we'll use here
bool asc_alpha_dir_sort(const CSADirRead::CSADirEntry &a, const CSADirRead::CSADirEntry &b);
bool asc_date_file_sort(const CSADirRead::CSAFileEntry &a, const CSADirRead::CSAFileEntry &b);
bool asc_size_file_sort(const CSADirRead::CSAFileEntry &a, const CSADirRead::CSAFileEntry &b);
bool asc_alpha_file_sort(const CSADirRead::CSAFileEntry &a, const CSADirRead::CSAFileEntry &b);
bool dsc_date_file_sort(const CSADirRead::CSAFileEntry &a, const CSADirRead::CSAFileEntry &b);
bool dsc_size_file_sort(const CSADirRead::CSAFileEntry &a, const CSADirRead::CSAFileEntry &b);
bool dsc_alpha_file_sort(const CSADirRead::CSAFileEntry &a, const CSADirRead::CSAFileEntry &b);
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSADirRead::CSADirRead()
{
}
//////////////////////////////////////////////////////////////////////
CSADirRead::~CSADirRead()
{
}
//////////////////////////////////////////////////////////////////////
bool CSADirRead::GetDirs(const TCHAR * pDirPath, bool bRecurse)
{
m_sSourceDir = pDirPath;
FormatPath(m_sSourceDir);
// find tree starting from pDirPath
if (bRecurse)
{
GetSubDirs(m_dirs, m_sSourceDir);
}
// add the main directory.
m_dirs.push_back(CSADirEntry(m_sSourceDir));
return true;
}
//////////////////////////////////////////////////////////////////////
bool CSADirRead::GetSubDirs(vector<CSADirRead::CSADirEntry> &dir_array, const CString &path)
{
CString newPath;
CString searchString;
searchString = path;
searchString+= "\\*.*";
try
{
#ifndef USE_WIN32_FINDFILE
struct _finddata_t c_file;
long fhandle;
if ((fhandle=_findfirst( searchString, &c_file ))!=-1)
{
// we only care about subdirs
if ((c_file.attrib & _A_SUBDIR)==_A_SUBDIR)
{
// add c_file.name to the string array
// we'll handle parents on our own
if ((strcmp(c_file.name, ".")!=0) && (strcmp(c_file.name, "..")!=0))
{
newPath = path;
newPath+= "\\";
newPath+= c_file.name;
GetSubDirs(dir_array, newPath);
dir_array.push_back(newPath);
}
}
// find the rest of them
while(_findnext( fhandle, &c_file ) == 0 )
{
if ((c_file.attrib & _A_SUBDIR)==_A_SUBDIR)
{
// we'll handle parents on our own
if ((strcmp(c_file.name, ".")!=0) && (strcmp(c_file.name, "..")!=0))
{
newPath = path;
newPath+= "\\";
newPath+= c_file.name;
GetSubDirs(dir_array, newPath);
dir_array.push_back(newPath);
}
}
}
_findclose(fhandle);
}
#else
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
if ((hFind = FindFirstFile(searchString, &FindFileData))!=INVALID_HANDLE_VALUE)
{
// we only care about files, not subdirs
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY)
{
// we'll handle parents on our own
if ((wcscmp(FindFileData.cFileName, _T("."))!=0) && (wcscmp(FindFileData.cFileName, _T(".."))!=0))
{
newPath = path;
newPath+= _T("\\");
newPath+=FindFileData.cFileName;
GetSubDirs(dir_array, newPath);
dir_array.push_back(newPath);
}
}
// find the rest of them
while(FindNextFile( hFind, &FindFileData ))
{
// we only care about files, not subdirs
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY)
{
// we'll handle parents on our own
if ((wcscmp(FindFileData.cFileName, _T("."))!=0) && (wcscmp(FindFileData.cFileName, _T(".."))!=0))
{
newPath = path;
newPath+= _T("\\");
newPath+=FindFileData.cFileName;
GetSubDirs(dir_array, newPath);
dir_array.push_back(newPath);
}
}
}
}
FindClose(hFind);
#endif
}
catch (...)
{
return false;
}
return true;
}
//////////////////////////////////////////////////////////////////////
bool CSADirRead::ClearFiles()
{
m_files.clear();
return true;
}
//////////////////////////////////////////////////////////////////////
bool CSADirRead::ClearDirs()
{
m_dirs.clear();
return true;
}
//////////////////////////////////////////////////////////////////////
bool CSADirRead::GetFiles(const TCHAR *pFilemask, bool bIncludeFilesInFileList, bool bIncludeFoldersInFileList)
{
// get the files in each of our directories
for (vector<CSADirEntry>::iterator it = m_dirs.begin();it!=m_dirs.end(); it++)
{
CString curDir = (*it).m_sName;
// sanity check
if (curDir.IsEmpty())
{
continue;
}
if (!FindFiles(curDir, pFilemask, bIncludeFilesInFileList, bIncludeFoldersInFileList))
{
return false;
}
}
return true;
}
//////////////////////////////////////////////////////////////////////
bool CSADirRead::SortFiles(int iSortStyle, bool bReverse)
{
switch (iSortStyle)
{
default:
break;
case eSortAlpha:
std::sort(m_files.begin(), m_files.end(), (bReverse ? dsc_alpha_file_sort : asc_alpha_file_sort));
break;
case eSortWriteDate:
std::sort(m_files.begin(), m_files.end(), (bReverse ? dsc_date_file_sort : asc_date_file_sort));
break;
case eSortSize:
std::sort(m_files.begin(), m_files.end(), (bReverse ? dsc_size_file_sort : asc_size_file_sort));
break;
}
return true;
}
//////////////////////////////////////////////////////////////////////
// chop off trailing "\"
void CSADirRead::FormatPath(CString &path)
{
CString inPath = path;
inPath.TrimRight();
CString tmp;
int iLastSlashPos = inPath.ReverseFind('\\');
if (iLastSlashPos == -1)
{
iLastSlashPos = inPath.ReverseFind('/');
}
if (iLastSlashPos!=-1)
{
if (iLastSlashPos==inPath.GetLength()-1)
{
path = inPath.Left(iLastSlashPos);
FormatPath(path); // in case the incoming path is "C:\temp\\\..."
}
}
}
//////////////////////////////////////////////////////////////////////
UINT CSADirRead::FindFiles(const CString & dir, const CString & filter, bool bIncludeFilesInFileList, bool bIncludeFoldersInFileList)
{
// make sure the path ends in a single "\"
CString baseName = dir;
FormatPath(baseName);
baseName+='\\';
CString fullPath = baseName;
fullPath += filter;
CString fileName;
// find first file in current directory
#ifndef USE_WIN32_FINDFILE
struct _finddata_t c_file;
long fhandle;
try
{
if ((fhandle=_findfirst( fullPath, &c_file ))!=-1)
{
bool bIsFolder = (c_file.attrib & _A_SUBDIR)==_A_SUBDIR;
bool bAddThisOne = (bIsFolder && bIncludeFoldersInFileList) || (!bIsFolder && bIncludeFilesInFileList);
// skip . and ..
if (bIsFolder && (strcmp(c_file.name, ".")==0) || (strcmp(c_file.name, "..")==0))
{
bAddThisOne = false;
}
if (bAddThisOne)
{
fileName = baseName;
fileName += c_file.name;
CSAFileEntry t;
t.bIsFolder = bIsFolder;
t.attrib = c_file.attrib;
t.m_sName = fileName;
t.time_write = c_file.time_write;
t.time_create = c_file.time_create;
t.size = c_file.size;
m_files.push_back(t);
}
// find the rest of them
while(_findnext( fhandle, &c_file ) == 0 )
{
bool bIsFolder = (c_file.attrib & _A_SUBDIR)==_A_SUBDIR;
bool bAddThisOne = (bIsFolder && bIncludeFoldersInFileList) || (!bIsFolder && bIncludeFilesInFileList);
// skip . and ..
if (bIsFolder && (strcmp(c_file.name, ".")==0) || (strcmp(c_file.name, "..")==0))
{
bAddThisOne = false;
}
if (bAddThisOne)
{
fileName=baseName;
fileName += c_file.name;
CSAFileEntry t;
t.bIsFolder = bIsFolder;
t.attrib = c_file.attrib;
t.m_sName = fileName;
t.time_write = c_file.time_write;
t.time_create = c_file.time_create;
t.size = c_file.size;
m_files.push_back(t);
}
}
_findclose(fhandle);
}
}
catch (...)
{
return false;
}
#else
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
try
{
if ((hFind = FindFirstFile(fullPath, &FindFileData))!=INVALID_HANDLE_VALUE)
{
bool bIsFolder = (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY;
bool bAddThisOne = (bIsFolder && bIncludeFoldersInFileList) || ((!bIsFolder) && bIncludeFilesInFileList);
// skip . and ..
if (bIsFolder && (wcscmp(FindFileData.cFileName, _T("."))==0) || (wcscmp(FindFileData.cFileName, _T(".."))==0))
{
bAddThisOne = false;
}
if (bAddThisOne)
{
fileName = baseName;
fileName += FindFileData.cFileName;
CSAFileEntry t;
t.m_sName = fileName;
t.bIsFolder = bIsFolder;
t.attrib = FindFileData.dwFileAttributes;
ASSERT(sizeof(FindFileData.ftLastWriteTime)== sizeof(_FILETIME));
memcpy(&t.time_write, &FindFileData.ftLastWriteTime, sizeof(_FILETIME));
ASSERT(sizeof(FindFileData.ftLastWriteTime)== sizeof(_FILETIME));
memcpy(&t.time_create, &FindFileData.ftCreationTime, sizeof(_FILETIME));
t.size = ((unsigned __int64)FindFileData.nFileSizeHigh * ((unsigned __int64)MAXDWORD+1)) + (unsigned __int64)FindFileData.nFileSizeLow;
m_files.push_back(t);
}
// find the rest of them
while (FindNextFile(hFind, &FindFileData))
{
bool bIsFolder = (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY;
bool bAddThisOne = (bIsFolder && bIncludeFoldersInFileList) || ((!bIsFolder) && bIncludeFilesInFileList);
// skip . and ..
if (bIsFolder && (wcscmp(FindFileData.cFileName, _T("."))==0) || (wcscmp(FindFileData.cFileName, _T(".."))==0))
{
bAddThisOne = false;
}
if (bAddThisOne)
{
fileName = baseName;
fileName += FindFileData.cFileName;
CSAFileEntry t;
t.m_sName = fileName;
t.bIsFolder = bIsFolder;
t.attrib = FindFileData.dwFileAttributes;
ASSERT(sizeof(FindFileData.ftLastWriteTime)== sizeof(_FILETIME));
memcpy(&t.time_write, &FindFileData.ftLastWriteTime, sizeof(_FILETIME));
ASSERT(sizeof(FindFileData.ftLastWriteTime)== sizeof(_FILETIME));
memcpy(&t.time_create, &FindFileData.ftCreationTime, sizeof(_FILETIME));
t.size = ((unsigned __int64)FindFileData.nFileSizeHigh * ((unsigned __int64)MAXDWORD+1)) + (unsigned __int64)FindFileData.nFileSizeLow;
m_files.push_back(t);
}
}
FindClose(hFind);
}
}
catch (...)
{
return false;
}
#endif
return true;
}
//////////////////////////////////////////////////////////////////////
bool asc_alpha_dir_sort(const CSADirRead::CSADirEntry &a, const CSADirRead::CSADirEntry &b)
{
return (wcsicmp(a.m_sName, b.m_sName) < 0);
}
//////////////////////////////////////////////////////////////////////
bool asc_alpha_file_sort(const CSADirRead::CSAFileEntry &a, const CSADirRead::CSAFileEntry &b)
{
return (wcsicmp(a.m_sName, b.m_sName) < 0);
}
//////////////////////////////////////////////////////////////////////
bool asc_date_file_sort(const CSADirRead::CSAFileEntry &a, const CSADirRead::CSAFileEntry &b)
{
#ifndef USE_WIN32_FINDFILE
return (difftime(a.time_write, b.time_write) < 0);
#else
return (a.time_write < b.time_write);
#endif
}
//////////////////////////////////////////////////////////////////////
bool asc_size_file_sort(const CSADirRead::CSAFileEntry &a, const CSADirRead::CSAFileEntry &b)
{
return (a.size < b.size);
}
//////////////////////////////////////////////////////////////////////
bool dsc_alpha_file_sort(const CSADirRead::CSAFileEntry &a, const CSADirRead::CSAFileEntry &b)
{
return (!asc_alpha_file_sort(a,b));
}
//////////////////////////////////////////////////////////////////////
bool dsc_date_file_sort(const CSADirRead::CSAFileEntry &a, const CSADirRead::CSAFileEntry &b)
{
return (!asc_date_file_sort(a,b));
}
//////////////////////////////////////////////////////////////////////
bool dsc_size_file_sort(const CSADirRead::CSAFileEntry &a, const CSADirRead::CSAFileEntry &b)
{
return (!asc_size_file_sort(a,b));
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -