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// replace all occurencies of from by to, return count
38int Str::replaceAll(std::string& s, const std::string& from, const std::string& to)
39{
40 int count = 0;
41 size_t pos = 0;
42 while ((pos = s.find(from, pos)) != std::string::npos) {
43 s.replace(pos, from.length(), to);
44 pos += to.length(); // Handles case where 'to' is a substring of 'from'
45 count++;
46 }
47 return count;
48}
49
50// generate random string of given lenght, suitable for file names
51std::string Str::getRandomString(int lenght)
52{
53 int pid = (int)getpid();
54 auto nextAllowed = [pid]() {
55 constexpr char chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
56 constexpr size_t L = sizeof(chars) - 1;
57 int rn = std::rand() | pid;
58 return chars[rn % L];
59 };
60 std::string str(lenght, 0);
61 std::generate_n(str.begin(), lenght, nextAllowed);
62 return str;
63}
64
65bool Str::pathExists(const std::string_view p)
66{
67 return p.compare(0, 8, "alien://") ? std::filesystem::exists(std::string{p}) : true; // we don't validate alien paths
68}
69
70bool Str::pathIsDirectory(const std::string_view p)
71{
72 return std::filesystem::is_directory(std::string{p});
73}
74
75std::string Str::getFullPath(const std::string_view p)
76{
77 return std::filesystem::canonical(std::string{p}).string();
78}
79
80std::string Str::rectifyDirectory(const std::string_view p)
81{
82 std::string dir(p);
83 if (dir.empty() || dir == "none") {
84 dir = "";
85 } else {
86 if (p.compare(0, 8, "alien://") == 0) {
87 if (!gGrid && !TGrid::Connect("alien://")) {
88 throw std::runtime_error(fmt::format("failed to initialize alien for {}", dir));
89 }
90 // for root or raw files do not treat as directory
91 if (dir.back() != '/' && !(endsWith(dir, ".root") || endsWith(dir, ".raw") || endsWith(dir, ".tf"))) {
92 dir += '/';
93 }
94 } else {
95 dir = getFullPath(dir);
96 if (!pathIsDirectory(dir)) {
97 throw std::runtime_error(fmt::format("{} is not an accessible directory", dir));
98 } else {
99 if (dir.back() != '/') {
100 dir += '/';
101 }
102 }
103 }
104 }
105 return dir;
106}
107
108// Create unique non-existing path name starting with prefix. Loose equivalent of boost::filesystem::unique_path()
109// The prefix can be either existing directory or just a string to add in front of the random part
110// in absence of such a function in std::filesystem
111std::string Str::create_unique_path(const std::string_view prefix, int length)
112{
113 std::string path;
114 bool needSlash = pathIsDirectory(prefix) && !prefix.empty() && prefix.back() != '/';
115 do {
116 path = concat_string(prefix, needSlash ? "/" : "", getRandomString(length));
117 } while (pathExists(path));
118
119 return path;
120}
uint16_t pos
Definition RawData.h:3
uint16_t pid
Definition RawData.h:2
GLenum src
Definition glcorearb.h:1767
GLint GLsizei count
Definition glcorearb.h:399
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 int replaceAll(std::string &s, const std::string &from, const std::string &to)
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