1 /*++
2 Copyright (c) 2006 Microsoft Corporation
3 
4 Module Name:
5 
6     foreach_file.cpp
7 
8 Abstract:
9 
10     Traverse files in a directory that match a given suffix.
11     Apply a method to each of the files.
12 
13 Author:
14 
15     Nikolaj Bjorner (nbjorner) 2006-11-3.
16 
17 Revision History:
18 
19 --*/
20 #ifdef _WINDOWS
21 #include <string>
22 #include <windows.h>
23 #include <strsafe.h>
24 #include "util/util.h"
25 #include "test/for_each_file.h"
26 
for_each_file(for_each_file_proc & proc,const char * base,const char * suffix)27 bool for_each_file(for_each_file_proc& proc, const char* base, const char* suffix)
28 {
29     std::string pattern(base);
30     pattern += "\\";
31     pattern += suffix;
32 
33     char buffer[MAX_PATH];
34 
35     WIN32_FIND_DATAA data;
36     HANDLE h = FindFirstFileA(pattern.c_str(),&data);
37 
38     while (h != INVALID_HANDLE_VALUE) {
39 
40         StringCchPrintfA(buffer, Z3_ARRAYSIZE(buffer), "%s\\%s", base, data.cFileName);
41 
42         if (!proc(buffer)) {
43             return false;
44         }
45 
46         if (!FindNextFileA(h,&data)) {
47             break;
48         }
49     }
50 
51     //
52     // Now recurse through sub-directories.
53     //
54 
55     pattern = base;
56     pattern += "\\*";
57     h = FindFirstFileA(pattern.c_str(),&data);
58 
59     while (h != INVALID_HANDLE_VALUE) {
60 
61         if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
62             data.cFileName[0] != '.'
63             ){
64             std::string subdir(base);
65             subdir += "\\";
66             subdir += data.cFileName;
67             if (!for_each_file(proc, subdir.c_str(), suffix)) {
68                 return false;
69             }
70         }
71 
72         if (!FindNextFileA(h,&data)) {
73             break;
74         }
75     }
76 
77     return true;
78 };
79 #endif
80 
81