3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-10 19:27:06 +00:00
z3/test/for_each_file.cpp
Leonardo de Moura 68269c43a6 other components
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
2012-10-02 11:48:48 -07:00

80 lines
1.5 KiB
C++

/*++
Copyright (c) 2006 Microsoft Corporation
Module Name:
foreach_file.cpp
Abstract:
Traverse files in a directory that match a given suffix.
Apply a method to each of the files.
Author:
Nikolaj Bjorner (nbjorner) 2006-11-3.
Revision History:
--*/
#ifdef _WINDOWS
#include <string>
#include <windows.h>
#include <strsafe.h>
#include "for_each_file.h"
bool for_each_file(for_each_file_proc& proc, const char* base, const char* suffix)
{
std::string pattern(base);
pattern += "\\";
pattern += suffix;
char buffer[MAX_PATH];
WIN32_FIND_DATAA data;
HANDLE h = FindFirstFileA(pattern.c_str(),&data);
while (h != INVALID_HANDLE_VALUE) {
StringCchPrintfA(buffer, ARRAYSIZE(buffer), "%s\\%s", base, data.cFileName);
if (!proc(buffer)) {
return false;
}
if (!FindNextFileA(h,&data)) {
break;
}
}
//
// Now recurse through sub-directories.
//
pattern = base;
pattern += "\\*";
h = FindFirstFileA(pattern.c_str(),&data);
while (h != INVALID_HANDLE_VALUE) {
if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
data.cFileName[0] != '.'
){
std::string subdir(base);
subdir += "\\";
subdir += data.cFileName;
if (!for_each_file(proc, subdir.c_str(), suffix)) {
return false;
}
}
if (!FindNextFileA(h,&data)) {
break;
}
}
return true;
};
#endif