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