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 <string>
28 #include <vector>
29 
30 namespace DomeUtils {
31 
32 using namespace dmlite;
33 
34 inline std::string remove_prefix_if_exists(const std::string &str, const std::string &prefix) {
35  if(prefix.size() > str.size()) return str;
36 
37  if(std::equal(prefix.begin(), prefix.end(), str.begin())) {
38  return str.substr(prefix.size(), str.size()-prefix.size());
39  }
40 
41  return str;
42 }
43 
44 inline std::string trim_trailing_slashes(std::string str) {
45  while(str.size() > 0 && str[str.size()-1] == '/') {
46  str.erase(str.size()-1);
47  }
48  return str;
49 }
50 
51 inline std::string join(const std::string &separator, const std::vector<std::string> &arr) {
52  if(arr.empty()) return std::string();
53 
54  std::stringstream ss;
55  for(size_t i = 0; i < arr.size()-1; i++) {
56  ss << arr[i];
57  ss << separator;
58  }
59  ss << arr[arr.size()-1];
60  return ss.str();
61 }
62 
63 inline std::vector<std::string> split(std::string data, std::string token) {
64  std::vector<std::string> output;
65  size_t pos = std::string::npos;
66  do {
67  pos = data.find(token);
68  output.push_back(data.substr(0, pos));
69  if(std::string::npos != pos)
70  data = data.substr(pos + token.size());
71  } while (std::string::npos != pos);
72  return output;
73 }
74 
75 inline void mkdirp(const std::string& path) {
76  std::vector<std::string> parts = split(path, "/");
77  std::ostringstream tocreate(parts[0]);
78 
79  // rudimentary sanity protection: never try to create the top-level directory
80  for(std::vector<std::string>::iterator it = parts.begin()+1; it+1 != parts.end(); it++) {
81  tocreate << "/" + *it;
82 
83  struct stat info;
84  if(::stat(tocreate.str().c_str(), &info) != 0) {
85  Log(Logger::Lvl1, Logger::unregistered, Logger::unregisteredname, " Creating directory: " << tocreate.str());
86 
87  mode_t prev = umask(0);
88  int ret = ::mkdir(tocreate.str().c_str(), 0770);
89  umask(prev);
90 
91  if(ret != 0) {
92  // FF: I do not understand why strerror_r in certain cases produces
93  // garbage. This code is paranoid due to that
94  char errbuffer[256];
95  char fullerr[1024];
96  memset(errbuffer, 0, sizeof(errbuffer));
97  strerror_r(errno, errbuffer, sizeof(errbuffer));
98 
99  snprintf(fullerr, sizeof(fullerr), "Could not create directory: '%s' err: %d:'%s'", tocreate.str().c_str(), errno, errbuffer);
100  fullerr[sizeof(fullerr)-1] = '\0';
101  throw DmException(errno, fullerr);
102  }
103  }
104  }
105 }
106 
107 inline std::string bool_to_str(bool b) {
108  if(b) return "true";
109  else return "false";
110 }
111 
112 inline bool str_to_bool(const std::string &str) {
113  bool value = false;
114 
115  if(str == "false" || str == "0" || str == "no") {
116  value = false;
117  } else if(str == "true" || str == "1" || str == "yes") {
118  value = true;
119  }
120  return value;
121 }
122 
123 inline std::string pfn_from_rfio_syntax(const std::string &rfn) {
124  size_t pos = rfn.find(":");
125  if(pos == std::string::npos)
126  return rfn;
127  return rfn.substr(pos+1, rfn.size());
128 }
129 
130 inline std::string server_from_rfio_syntax(const std::string &rfn) {
131  size_t pos = rfn.find(":");
132  if(pos == std::string::npos)
133  return rfn;
134  return rfn.substr(0, pos);
135 }
136 
137 inline std::string unescape_forward_slashes(const std::string &str) {
138  std::ostringstream ss;
139  for(size_t i = 0; i < str.size(); i++) {
140  if(i != str.size()-1 && str[i] == '\\' && str[i+1] == '/') {
141  ss << "/";
142  i++;
143  }
144  else {
145  ss << str[i];
146  }
147  }
148  return ss.str();
149 }
150 
151 }
152 
153 
154 
155 
156 #endif
Logger::unregistered
static bitmask unregistered
Definition: logger.h:45
DomeUtils::unescape_forward_slashes
std::string unescape_forward_slashes(const std::string &str)
Definition: DomeUtils.h:137
Logger::Lvl1
Definition: logger.h:53
DomeUtils::split
std::vector< std::string > split(std::string data, std::string token)
Definition: DomeUtils.h:63
DomeUtils::mkdirp
void mkdirp(const std::string &path)
Definition: DomeUtils.h:75
Logger::unregisteredname
static char * unregisteredname
Definition: logger.h:46
DomeUtils::bool_to_str
std::string bool_to_str(bool b)
Definition: DomeUtils.h:107
DomeUtils::server_from_rfio_syntax
std::string server_from_rfio_syntax(const std::string &rfn)
Definition: DomeUtils.h:130
DomeUtils::trim_trailing_slashes
std::string trim_trailing_slashes(std::string str)
Definition: DomeUtils.h:44
DomeUtils::pfn_from_rfio_syntax
std::string pfn_from_rfio_syntax(const std::string &rfn)
Definition: DomeUtils.h:123
dmlite::DmException
Base exception class.
Definition: exceptions.h:17
DomeUtils::join
std::string join(const std::string &separator, const std::vector< std::string > &arr)
Definition: DomeUtils.h:51
Log
#define Log(lvl, mymask, where, what)
Definition: logger.h:15
DomeUtils
Definition: DomeUtils.h:30
DomeUtils::str_to_bool
bool str_to_bool(const std::string &str)
Definition: DomeUtils.h:112
dmlite
Namespace for the dmlite C++ API.
Definition: authn.h:15
DomeUtils::remove_prefix_if_exists
std::string remove_prefix_if_exists(const std::string &str, const std::string &prefix)
Definition: DomeUtils.h:34