OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
heavyai_glob.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include <boost/filesystem.hpp>
20 
21 #include "Shared/clean_windows.h"
22 
23 #include <string>
24 #include <vector>
25 
26 namespace fs = boost::filesystem;
27 
28 namespace {
29 
30 bool has_wildcard(const std::string& name) {
31  if (name.find('*') != std::string::npos) {
32  return true;
33  }
34  if (name.find('?') != std::string::npos) {
35  return true;
36  }
37  return false;
38 }
39 
40 void glob(const fs::path& base, const fs::path& pattern, std::vector<std::string>& out) {
41  if (pattern.empty()) {
42  out.push_back(base.string());
43  return;
44  }
45 
46  auto it = pattern.begin();
47  auto next_part = *(it++);
48  fs::path next_pattern;
49  for (; it != pattern.end(); ++it) {
50  next_pattern /= *it;
51  }
52 
53  if (has_wildcard(next_part.string())) {
54  WIN32_FIND_DATA file_data;
55  auto search = base / next_part;
56 #ifdef _UNICODE
57  auto handle = FindFirstFile(search.wstring().data(), &file_data);
58 #else
59  auto handle = FindFirstFile(search.string().data(), &file_data);
60 #endif
61  if (handle != INVALID_HANDLE_VALUE) {
62  do {
63  fs::path found_part(file_data.cFileName);
64  if (!found_part.filename_is_dot() && !found_part.filename_is_dot_dot()) {
65  glob(base / found_part, next_pattern, out);
66  }
67  } while (FindNextFile(handle, &file_data) != 0);
68  FindClose(handle);
69  }
70  } else {
71  glob(base / next_part, next_pattern, out);
72  }
73 }
74 
75 } // namespace
76 
77 namespace heavyai {
78 
79 std::vector<std::string> glob(const std::string& pattern) {
80  std::vector<std::string> results;
81  fs::path pattern_path(pattern);
82  if (!pattern_path.empty()) {
83  ::glob(pattern_path.root_path(), pattern_path.relative_path(), results);
84  }
85  return results;
86 }
87 
88 } // namespace heavyai
bool has_wildcard(const std::string &name)
std::vector< std::string > glob(const std::string &pattern)
string name
Definition: setup.in.py:72