-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathStringUtils.cxx
More file actions
155 lines (142 loc) · 4.49 KB
/
StringUtils.cxx
File metadata and controls
155 lines (142 loc) · 4.49 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "CommonUtils/StringUtils.h"
#include <cstdlib>
#include <filesystem>
#ifndef GPUCA_STANDALONE
#include <TGrid.h>
#include <fmt/format.h>
#endif
#include <unistd.h>
#include <cstring>
using namespace o2::utils;
std::vector<std::string> Str::tokenize(const std::string& src, char delim, bool trimToken, bool skipEmpty)
{
std::stringstream ss(src);
std::string token;
std::vector<std::string> tokens;
while (std::getline(ss, token, delim)) {
if (trimToken) {
trim(token);
}
if (!token.empty() || !skipEmpty) {
tokens.push_back(std::move(token));
}
}
return tokens;
}
std::vector<std::string> Str::tokenize(const std::string& src, const std::string& delim, bool trimToken, bool skipEmpty)
{
std::string inptStr{src};
char* input = inptStr.data();
auto mystrtok = [&]() -> char* {
input += std::strspn(input, delim.c_str());
if (*input == '\0') {
return nullptr;
}
char* const token = input;
input += std::strcspn(input, delim.c_str());
if (*input != '\0') {
*input++ = '\0';
}
return token;
};
std::vector<std::string> tokens;
while (*input != '\0') {
std::string token = mystrtok();
if (trimToken) {
trim(token);
}
if (!token.empty() || !skipEmpty) {
tokens.push_back(std::move(token));
}
}
return tokens;
}
// replace all occurencies of from by to, return count
int Str::replaceAll(std::string& s, const std::string& from, const std::string& to)
{
int count = 0;
size_t pos = 0;
while ((pos = s.find(from, pos)) != std::string::npos) {
s.replace(pos, from.length(), to);
pos += to.length(); // Handles case where 'to' is a substring of 'from'
count++;
}
return count;
}
// generate random string of given lenght, suitable for file names
std::string Str::getRandomString(int lenght)
{
int pid = (int)getpid();
auto nextAllowed = [pid]() {
constexpr char chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
constexpr size_t L = sizeof(chars) - 1;
int rn = std::rand() | pid;
return chars[rn % L];
};
std::string str(lenght, 0);
std::generate_n(str.begin(), lenght, nextAllowed);
return str;
}
bool Str::pathExists(const std::string_view p)
{
return p.compare(0, 8, "alien://") ? std::filesystem::exists(std::string{p}) : true; // we don't validate alien paths
}
bool Str::pathIsDirectory(const std::string_view p)
{
return std::filesystem::is_directory(std::string{p});
}
std::string Str::getFullPath(const std::string_view p)
{
return std::filesystem::canonical(std::string{p}).string();
}
#ifndef GPUCA_STANDALONE
std::string Str::rectifyDirectory(const std::string_view p)
{
std::string dir(p);
if (dir.empty() || dir == "none") {
dir = "";
} else {
if (p.compare(0, 8, "alien://") == 0) {
if (!gGrid && !TGrid::Connect("alien://")) {
throw std::runtime_error(fmt::format("failed to initialize alien for {}", dir));
}
// for root or raw files do not treat as directory
if (dir.back() != '/' && !(endsWith(dir, ".root") || endsWith(dir, ".raw") || endsWith(dir, ".tf"))) {
dir += '/';
}
} else {
dir = getFullPath(dir);
if (!pathIsDirectory(dir)) {
throw std::runtime_error(fmt::format("{} is not an accessible directory", dir));
} else {
if (dir.back() != '/') {
dir += '/';
}
}
}
}
return dir;
}
#endif
// Create unique non-existing path name starting with prefix. Loose equivalent of boost::filesystem::unique_path()
// The prefix can be either existing directory or just a string to add in front of the random part
// in absence of such a function in std::filesystem
std::string Str::create_unique_path(const std::string_view prefix, int length)
{
std::string path;
bool needSlash = pathIsDirectory(prefix) && !prefix.empty() && prefix.back() != '/';
do {
path = concat_string(prefix, needSlash ? "/" : "", getRandomString(length));
} while (pathExists(path));
return path;
}