Форум по Delphi программированию

Delphi Sources



Вернуться   Форум по Delphi программированию > Все о Delphi > [ "Начинающим" ]
Ник
Пароль
Регистрация <<         Правила форума         >> FAQ Пользователи Календарь Поиск Сообщения за сегодня Все разделы прочитаны

 
 
Опции темы Поиск в этой теме Опции просмотра
  #1  
Старый 24.11.2008, 21:21
omarcheek omarcheek вне форума
Прохожий
 
Регистрация: 24.11.2008
Сообщения: 2
Репутация: 10
По умолчанию помогите переписать на Delphi/Pascal прогу

в общем вот программа, которая ищет файл на жестком диске методом "вглубь" и "вширь" (обход графа)
и выводит каким методом быстрее поиск идет

помогите переписать на Делфи
а то я не спец.. одни ошибки выходят..

заранее спасибо

Код:
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// 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;
}
Ответить с цитированием
 


Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать темы
Вы не можете отвечать на сообщения
Вы не можете прикреплять файлы
Вы не можете редактировать сообщения

BB-коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход


Часовой пояс GMT +3, время: 09:10.


 

Сайт

Форум

FAQ

Соглашения

Прочее

 

Copyright © Форум "Delphi Sources" by BrokenByte Software, 2004-2025