aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/lib/io/match.cc
blob: 1563642d0ba6f207956ed8b20cbf8e4107a8f692 (plain)
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
#include "tensorflow/core/lib/io/match.h"
#include <fnmatch.h>
#include "tensorflow/core/lib/io/path.h"
#include "tensorflow/core/public/env.h"

namespace tensorflow {
namespace io {

Status GetMatchingFiles(Env* env, const string& pattern,
                             std::vector<string>* results) {
  results->clear();
  std::vector<string> all_files;
  string dir = Dirname(pattern).ToString();
  if (dir.empty()) dir = ".";
  string basename_pattern = Basename(pattern).ToString();
  Status s = env->GetChildren(dir, &all_files);
  if (!s.ok()) {
    return s;
  }
  for (const auto& f : all_files) {
    int flags = 0;
    if (fnmatch(basename_pattern.c_str(), Basename(f).ToString().c_str(),
                flags) == 0) {
      results->push_back(JoinPath(dir, f));
    }
  }
  return Status::OK();
}

}  // namespace io
}  // namespace tensorflow