-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathStringUtils.cxx
More file actions
91 lines (80 loc) · 2.66 KB
/
StringUtils.cxx
File metadata and controls
91 lines (80 loc) · 2.66 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
// 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>
using namespace o2::utils;
std::vector<std::string> Str::tokenize(const std::string& src, char delim, bool trimToken)
{
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()) {
tokens.push_back(std::move(token));
}
}
return tokens;
}
// generate random string of given lenght, suitable for file names
std::string Str::getRandomString(int lenght)
{
auto nextAllowed = []() {
constexpr char chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
constexpr size_t L = sizeof(chars) - 1;
return chars[std::rand() % L];
};
std::string str(lenght, 0);
std::generate_n(str.begin(), lenght, nextAllowed);
return str;
}
bool Str::pathExists(const std::string_view p)
{
return std::filesystem::exists(std::string{p});
}
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();
}
std::string Str::rectifyDirectory(const std::string_view p)
{
std::string dir(p);
if (dir.empty() || dir == "none") {
dir = "";
} else {
dir = getFullPath(dir);
if (!pathIsDirectory(dir)) {
throw std::runtime_error(fmt::format("{:s} is not an accessible directory", dir));
} else {
dir += '/';
}
}
return dir;
}
// 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;
}