1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <sys/stat.h>
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#define WHITESPACE_CHARS " \t\n\r"
#undef streq
#ifdef _UNICODE
#define streq(x,y) (wcscmp(x,y) == 0)
#else
#define streq(x,y) (strcmp(x,y)==0)
#endif
void win_path_to_unix_ath(char *path)
{
char *c;
for (c = path; *c; c++)
if (*c == '\\')
*c = '/';
}
File::File()
{
}
File::~File()
{
}
File::Pointer File::New()
{
return Pointer(new File());
}
void File::ScanDir(std::string dir, std::vector<std::string> &filenames)
{
win_path_to_unix_ath((char*)dir.c_str());
if (dir[dir.size() - 1] != '/')
dir += ("/");
std::wstring windir(dir.begin(), dir.end()), file;
WIN32_FIND_DATA find;
HANDLE h;
DWORD attr;
bool add;
h = FindFirstFile((windir + _T("*")).c_str(), &find);
if (h != INVALID_HANDLE_VALUE)
{
do
{
if (!streq(find.cFileName, _T(".")) && !streq(find.cFileName, _T("..")))
{
file = windir + find.cFileName;
attr = GetFileAttributes(file.c_str());
if (attr & FILE_ATTRIBUTE_DIRECTORY)
{
std::vector<std::string> temp;
ScanDir(std::string(file.begin(), file.end()), filenames);
}
else
{
filenames.push_back(std::string(file.begin(), file.end()));
}
}
} while (FindNextFile(h, &find));
FindClose(h);
}
}
#include <iostream>
int main()
{
bool flag = false;
for (int i = 0; i < 1000; i++)
{
if (flag)
{
std::shared_ptr<File> file = File::New();
std::vector<std::string> files;
file->ScanDir("D:\\datu_data_test", files);//a local path
}
}
std::cout << "success" << std::endl;
std::cin.get();
}
|