в общем вот программа, которая ищет файл на жестком диске методом "вглубь" и "вширь" (обход графа)
и выводит каким методом быстрее поиск идет
помогите переписать на Делфи
а то я не спец.. одни ошибки выходят..
заранее спасибо
Код:
// VitDir.cpp
#include "stdafx.h"
#include <windows.h>
#include <string>
#include <vector>
#include <deque>
#include <iostream>
#include <locale>
using namespace std;
enum SearchType {STYPE_DEPTH, STYPE_WIDTH};
int DirsFound;
bool ListFiles(wstring path, wstring mask, vector<wstring>& files, int stype = STYPE_DEPTH)
{
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA ffd;
wstring spec;
deque<wstring> directories;
directories.push_back(path);
files.clear();
DirsFound = 0;
while (!directories.empty()) {
path = ( stype==STYPE_DEPTH ? directories.back() : directories.front() );
spec = path + L"\\" + mask;
if ( stype==STYPE_DEPTH )
directories.pop_back();
else
directories.pop_front();
hFind = FindFirstFile(spec.c_str(), &ffd);
if (hFind == INVALID_HANDLE_VALUE) {
return false;
}
do {
// wcout << L"-" << ffd.cFileName << L"\n";
if (wcscmp(ffd.cFileName, L".") != 0 &&
wcscmp(ffd.cFileName, L"..") != 0) {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
directories.push_back(path + L"\\" + ffd.cFileName);
DirsFound++;
}
else {
files.push_back(path + L"\\" + ffd.cFileName);
}
}
} while (FindNextFile(hFind, &ffd) != 0);
if (GetLastError() != ERROR_NO_MORE_FILES) {
FindClose(hFind);
return false;
}
FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
}
return true;
}
int CONST_listFiles = 0;
int CONST_repeats = 10;
int wmain(int argc, WCHAR* argv[])
{
vector<wstring> files;
wstring PathToSearch = (argc < 2 ? L"C:\\" : argv[1] );
// if not set then wcout will go in BAD state after trying to print some RUS str
wcout.imbue(locale("Russian_Russia.866"));
wcout << L"Starting first test search..." << endl;
/* you can ask why we do search before measuring timings? :) guess!
if (ListFiles(PathToSearch, L"*", files,STYPE_DEPTH))
{
if (CONST_listFiles)
for (vector<wstring>::iterator it = files.begin(); it != files.end(); ++it)
wcout << it->c_str() << endl;
}
else
{
wcout << L"Something went wrong!" << endl;
return 1; // something went wrong
} */
wcout << L"Total files found: " << files.size() << endl;
wcout << L"Total dirs found: " << DirsFound << endl;
DWORD stage0 = GetTickCount();
wcout << L"Running 'depth' searches..." << endl;
for (int i = 0; i < CONST_repeats; i++)
ListFiles(PathToSearch, L"*", files,STYPE_DEPTH);
DWORD stage1 = GetTickCount();
wcout << L"Running 'width' searches..." << endl;
for (int i = 0; i < CONST_repeats; i++)
ListFiles(PathToSearch, L"*", files,STYPE_WIDTH);
DWORD stage2 = GetTickCount();
wcout << L"Statistics: (searches of each type were repeated " << CONST_repeats << L" times)" << endl;
wcout << L" - Depth searches (ms):" << stage1-stage0 << endl;
wcout << L" - Width searches (ms):" << stage2-stage1 << endl;
wcout << endl << L"Press any key to continue..." ;
wcin.get();
return 0;
}