Project
Loading...
Searching...
No Matches
FileSystemUtils.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12//
13// Created by Sandro Wenzel on 04.06.21.
14//
15
17#include <filesystem>
18#include <vector>
19#include <regex>
20#include <iostream>
21#include <unistd.h>
22#include <fmt/format.h>
23
24namespace o2::utils
25{
26
27// Return a vector of file names in the current dir matching the search
28// pattern. If searchpattern is empty, all files will be returned. Otherwise
29// searchpattern will be treated/parsed as proper regular expression.
30std::vector<std::string> listFiles(std::string const& dir, std::string const& searchpattern)
31{
32 std::vector<std::string> filenames;
33 std::string rs = searchpattern.empty() ? ".*" : searchpattern;
34 std::regex str_expr(rs);
35
36 for (auto& p : std::filesystem::directory_iterator(dir)) {
37 try {
38 if (!p.is_directory()) {
39 auto fn = p.path().filename().string();
40 if (regex_match(fn, str_expr)) {
41 filenames.push_back(p.path().string());
42 }
43 }
44 } catch (...) {
45 // problem listing some file, just ignore continue
46 // with next one
47 continue;
48 }
49 }
50 return filenames;
51}
52
53std::vector<std::string> listFiles(std::string const& searchpattern)
54{
55 return listFiles("./", searchpattern);
56}
57
58void createDirectoriesIfAbsent(std::string const& path)
59{
60 if (!path.empty() && !std::filesystem::create_directories(path) && !std::filesystem::is_directory(path)) {
61 throw std::runtime_error(fmt::format("Failed to create {} directory", path));
62 }
63}
64// A function to expand string containing shell variables
65// to a string in which these vars have been substituted.
66// Motivation:: filesystem::exists() does not do this by default
67// and I couldn't find information on this. Potentially there is an
68// existing solution.
69std::string expandShellVarsInFileName(std::string const& input)
70{
71 std::regex e(R"(\$\{?[a-zA-Z0-9_]*\}?)");
72 std::regex e3("[a-zA-Z0-9_]+");
73 std::string finalstr;
74 std::sregex_iterator iter;
75 auto words_end = std::sregex_iterator(); // the end iterator (default)
76 auto words_begin = std::sregex_iterator(input.begin(), input.end(), e);
77
78 // check first of all if there is shell variable inside
79 if (words_end == words_begin) {
80 return input;
81 }
82
83 std::string tail;
84 for (auto i = words_begin; i != words_end; ++i) {
85 std::smatch match = *i;
86 // remove ${ and }
87 std::smatch m;
88 std::string s(match.str());
89
90 if (std::regex_search(s, m, e3)) {
91 auto envlookup = getenv(m[0].str().c_str());
92 if (envlookup) {
93 finalstr += match.prefix().str() + std::string(envlookup);
94 } else {
95 // in case of non existance we keep the env part unreplaced
96 finalstr += match.prefix().str() + "${" + m[0].str().c_str() + "}";
97 }
98 tail = match.suffix().str();
99 }
100 }
101 finalstr += tail;
102 return finalstr;
103}
104
105} // namespace o2::utils
int32_t i
bool match(const std::vector< std::string > &queries, const char *pattern)
Definition dcs-ccdb.cxx:229
const GLfloat * m
Definition glcorearb.h:4066
GLsizei const GLchar *const * path
Definition glcorearb.h:3591
std::vector< std::string > listFiles(std::string const &dir, std::string const &searchpattern)
void createDirectoriesIfAbsent(std::string const &path)
std::string expandShellVarsInFileName(std::string const &input)
const std::string str