dmlite 0.6
DomeUtils.h
Go to the documentation of this file.
1/*
2 * Copyright 2015 CERN
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18/** @file DomeUtils.h
19 * @brief Small utilities used throughout dome
20 * @author Georgios Bitzes
21 * @date Feb 2016
22 */
23
24#ifndef DOMEUTILS_H
25#define DOMEUTILS_H
26
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <unistd.h>
30
31#include <string>
32#include <vector>
33
35
36namespace DomeUtils {
37
38using namespace dmlite;
39
40inline std::string remove_prefix_if_exists(const std::string &str, const std::string &prefix) {
41 if(prefix.size() > str.size()) return str;
42
43 if(std::equal(prefix.begin(), prefix.end(), str.begin())) {
44 return str.substr(prefix.size(), str.size()-prefix.size());
45 }
46
47 return str;
48}
49
50inline std::string trim_trailing_slashes(std::string str) {
51 while(str.size() > 0 && str[str.size()-1] == '/') {
52 str.erase(str.size()-1);
53 }
54 return str;
55}
56
57inline std::string join(const std::string &separator, const std::vector<std::string> &arr) {
58 if(arr.empty()) return std::string();
59
60 std::stringstream ss;
61 for(size_t i = 0; i < arr.size()-1; i++) {
62 ss << arr[i];
63 ss << separator;
64 }
65 ss << arr[arr.size()-1];
66 return ss.str();
67}
68
69inline std::vector<std::string> split(std::string data, std::string token) {
70 std::vector<std::string> output;
71 size_t pos = std::string::npos;
72 do {
73 pos = data.find(token);
74 output.push_back(data.substr(0, pos));
75 if(std::string::npos != pos)
76 data = data.substr(pos + token.size());
77 } while (std::string::npos != pos);
78 return output;
79}
80
81inline std::vector<std::string> rsplit(std::string data, std::string token, int max=-1) {
82 std::vector<std::string> output;
83 for (int cnt = 0; ; cnt++) {
84 size_t start = data.rfind(token);
85 if (start == std::string::npos || cnt == max) {
86 output.insert(output.begin(), data.substr(0, data.length()));
87 break;
88 }
89 output.insert(output.begin(), data.substr(start+token.length(), data.length()-start));
90 data = data.substr(0, start);
91 }
92 return output;
93}
94
95inline void mkdirp(const std::string& path) {
96 std::vector<std::string> parts = split(path, "/");
97 std::ostringstream tocreate(parts[0]);
98
99 // rudimentary sanity protection: never try to create the top-level directory
100 for(std::vector<std::string>::iterator it = parts.begin()+1; it+1 != parts.end(); it++) {
101 tocreate << "/" + *it;
102
103 struct stat info;
104 if(::stat(tocreate.str().c_str(), &info) != 0) {
105 Log(Logger::Lvl1, Logger::unregistered, Logger::unregisteredname, " Creating directory: " << tocreate.str());
106
107 mode_t prev = umask(0);
108 int ret = ::mkdir(tocreate.str().c_str(), 0770);
109 umask(prev);
110
111 if(ret != 0) {
112 char errbuffer[256];
113 dpm_strerror_r(errno, errbuffer, sizeof(errbuffer));
114 throw DmException(errno, "Could not create directory: '%s' err: %d:'%s'", tocreate.str().c_str(), errno, errbuffer);
115 }
116 }
117 }
118}
119
120inline std::string bool_to_str(bool b) {
121 if(b) return "true";
122 else return "false";
123}
124
125inline bool str_to_bool(const std::string &str) {
126 bool value = false;
127
128 if(str == "false" || str == "0" || str == "no") {
129 value = false;
130 } else if(str == "true" || str == "1" || str == "yes") {
131 value = true;
132 }
133 return value;
134}
135
136inline std::string pfn_from_rfio_syntax(const std::string &rfn) {
137 size_t pos = rfn.find(":");
138 if(pos == std::string::npos)
139 return rfn;
140 return rfn.substr(pos+1, rfn.size());
141}
142
143inline std::string server_from_rfio_syntax(const std::string &rfn) {
144 size_t pos = rfn.find(":");
145 if(pos == std::string::npos)
146 return rfn;
147 return rfn.substr(0, pos);
148}
149
150inline std::string unescape_forward_slashes(const std::string &str) {
151 std::ostringstream ss;
152 for(size_t i = 0; i < str.size(); i++) {
153 if(i != str.size()-1 && str[i] == '\\' && str[i+1] == '/') {
154 ss << "/";
155 i++;
156 }
157 else {
158 ss << str[i];
159 }
160 }
161 return ss.str();
162}
163
164}
165
166
167
168
169#endif
static bitmask unregistered
Definition: logger.h:83
static char * unregisteredname
Definition: logger.h:84
@ Lvl1
Definition: logger.h:91
Base exception class.
Definition: exceptions.h:17
Exceptions used by the API.
#define Log(lvl, mymask, where, what)
Definition: logger.h:53
#define dpm_strerror_r(errnum, buf, buflen)
Definition: logger.h:32
Definition: DomeUtils.h:36
std::string join(const std::string &separator, const std::vector< std::string > &arr)
Definition: DomeUtils.h:57
std::string server_from_rfio_syntax(const std::string &rfn)
Definition: DomeUtils.h:143
std::string remove_prefix_if_exists(const std::string &str, const std::string &prefix)
Definition: DomeUtils.h:40
std::string unescape_forward_slashes(const std::string &str)
Definition: DomeUtils.h:150
std::vector< std::string > split(std::string data, std::string token)
Definition: DomeUtils.h:69
std::string bool_to_str(bool b)
Definition: DomeUtils.h:120
std::vector< std::string > rsplit(std::string data, std::string token, int max=-1)
Definition: DomeUtils.h:81
std::string trim_trailing_slashes(std::string str)
Definition: DomeUtils.h:50
std::string pfn_from_rfio_syntax(const std::string &rfn)
Definition: DomeUtils.h:136
void mkdirp(const std::string &path)
Definition: DomeUtils.h:95
bool str_to_bool(const std::string &str)
Definition: DomeUtils.h:125
Namespace for the dmlite C++ API.
Definition: authn.h:16