]> cygwin.com Git - cygwin-apps/setup.git/blob - find.cc
Merged across diffs between setup_crypto_branch_branchpoint and
[cygwin-apps/setup.git] / find.cc
1 /*
2 * Copyright (c) 2002, Robert Collins.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by DJ Delorie <dj@cygnus.com>
13 * Rewritten by Robert Collins <rbtcollins@hotmail.com>
14 *
15 */
16
17 /* The purpose of this file is to doa recursive find on a given
18 directory, calling a given function for each file found. */
19
20 #if 0
21 static const char *cvsid =
22 "\n%%% $Id$\n";
23 #endif
24
25 #include "find.h"
26
27 #include "FindVisitor.h"
28 #include <stdexcept>
29
30 using namespace std;
31
32 Find::Find(const std::string& starting_dir)
33 : h(INVALID_HANDLE_VALUE)
34 {
35 _start_dir = starting_dir;
36 int l = _start_dir.size ();
37
38 /* Ensure that _start_dir has a trailing slash if it doesn't already. */
39 if (l < 1 || (starting_dir[l - 1] != '/' && starting_dir[l - 1] != '\\'))
40 _start_dir += '/';
41 }
42
43 Find::~Find()
44 {
45 if (h != INVALID_HANDLE_VALUE && h)
46 FindClose (h);
47 }
48
49 void
50 Find::accept (FindVisitor &aVisitor)
51 {
52 WIN32_FIND_DATA wfd;
53 if (_start_dir.size() > MAX_PATH)
54 throw new length_error ("starting dir longer than MAX_PATH");
55
56 h = FindFirstFile ((_start_dir + "*").c_str(), &wfd);
57
58 if (h == INVALID_HANDLE_VALUE)
59 return;
60
61 do
62 {
63 if (strcmp (wfd.cFileName, ".") == 0
64 || strcmp (wfd.cFileName, "..") == 0)
65 continue;
66
67 /* TODO: make a non-win32 file and dir info class and have that as the
68 * visited node
69 */
70 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
71 aVisitor.visitDirectory (_start_dir, &wfd);
72 else
73 aVisitor.visitFile (_start_dir, &wfd);
74 }
75 while (FindNextFile (h, &wfd));
76 }
This page took 0.03752 seconds and 5 git commands to generate.