Skip to content

Instantly share code, notes, and snippets.

@justinvanwinkle
Created July 14, 2014 04:55
Show Gist options
  • Save justinvanwinkle/f6e7b0baf87dbf7dc1ff to your computer and use it in GitHub Desktop.
Save justinvanwinkle/f6e7b0baf87dbf7dc1ff to your computer and use it in GitHub Desktop.
inline vector<string> glob(string pattern, bool only_files = false) {
unique_ptr<glob_t, decltype(&globfree)> glob_buffer(new glob_t(), globfree);
glob(pattern.c_str(), GLOB_TILDE, NULL, glob_buffer.get());
strings fns;
for (size_t i = 0; i < glob_buffer->gl_pathc; ++i) {
fns.push_back(string(glob_buffer->gl_pathv[i]));
}
if (only_files) {
strings file_fns;
for (auto &fn : fns) {
if (not is_dir(fn))
file_fns.push_back(fn);
}
return file_fns;
}
return fns;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment