Project
Loading...
Searching...
No Matches
StringUtils.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
13#include <cstdlib>
14#include <filesystem>
15#include <TGrid.h>
16#include <unistd.h>
17
18using namespace o2::utils;
19
20std::vector<std::string> Str::tokenize(const std::string& src, char delim, bool trimToken, bool skipEmpty)
21{
22 std::stringstream ss(src);
23 std::string token;
24 std::vector<std::string> tokens;
25
26 while (std::getline(ss, token, delim)) {
27 if (trimToken) {
28 trim(token);
29 }
30 if (!token.empty() || !skipEmpty) {
31 tokens.push_back(std::move(token));
32 }
33 }
34 return tokens;
35}
36
37// generate random string of given lenght, suitable for file names
38std::string Str::getRandomString(int lenght)
39{
40 int pid = (int)getpid();
41 auto nextAllowed = [pid]() {
42 constexpr char chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
43 constexpr size_t L = sizeof(chars) - 1;
44 int rn = std::rand() | pid;
45 return chars[rn % L];
46 };
47 std::string str(lenght, 0);
48 std::generate_n(str.begin(), lenght, nextAllowed);
49 return str;
50}
51
52bool Str::pathExists(const std::string_view p)
53{
54 return p.compare(0, 8, "alien://") ? std::filesystem::exists(std::string{p}) : true; // we don't validate alien paths
55}
56
57bool Str::pathIsDirectory(const std::string_view p)
58{
59 return std::filesystem::is_directory(std::string{p});
60}
61
62std::string Str::getFullPath(const std::string_view p)
63{
64 return std::filesystem::canonical(std::string{p}).string();
65}
66
67std::string Str::rectifyDirectory(const std::string_view p)
68{
69 std::string dir(p);
70 if (dir.empty() || dir == "none") {
71 dir = "";
72 } else {
73 if (p.compare(0, 8, "alien://") == 0) {
74 if (!gGrid && !TGrid::Connect("alien://")) {
75 throw std::runtime_error(fmt::format("failed to initialize alien for {}", dir));
76 }
77 // for root or raw files do not treat as directory
78 if (dir.back() != '/' && !(endsWith(dir, ".root") || endsWith(dir, ".raw") || endsWith(dir, ".tf"))) {
79 dir += '/';
80 }
81 } else {
82 dir = getFullPath(dir);
83 if (!pathIsDirectory(dir)) {
84 throw std::runtime_error(fmt::format("{} is not an accessible directory", dir));
85 } else {
86 if (dir.back() != '/') {
87 dir += '/';
88 }
89 }
90 }
91 }
92 return dir;
93}
94
95// Create unique non-existing path name starting with prefix. Loose equivalent of boost::filesystem::unique_path()
96// The prefix can be either existing directory or just a string to add in front of the random part
97// in absence of such a function in std::filesystem
98std::string Str::create_unique_path(const std::string_view prefix, int length)
99{
100 std::string path;
101 bool needSlash = pathIsDirectory(prefix) && !prefix.empty() && prefix.back() != '/';
102 do {
103 path = concat_string(prefix, needSlash ? "/" : "", getRandomString(length));
104 } while (pathExists(path));
105
106 return path;
107}
uint16_t pid
Definition RawData.h:2
GLenum src
Definition glcorearb.h:1767
GLuint GLsizei GLsizei * length
Definition glcorearb.h:790
GLsizei const GLchar *const * path
Definition glcorearb.h:3591
static bool pathIsDirectory(const std::string_view p)
static void trim(std::string &s)
Definition StringUtils.h:71
static std::string rectifyDirectory(const std::string_view p)
static bool pathExists(const std::string_view p)
static std::vector< std::string > tokenize(const std::string &src, char delim, bool trimToken=true, bool skipEmpty=true)
static std::string concat_string(Ts const &... ts)
static std::string getFullPath(const std::string_view p)
static std::string create_unique_path(const std::string_view prefix="", int length=16)
static bool endsWith(const std::string &s, const std::string &ending)
static std::string getRandomString(int length)
const std::string str