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) throw (DmException) {
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  char errbuffer[128];
93  strerror_r(errno, errbuffer, sizeof(errbuffer));
94  throw DmException(errno, "Could not create directory: %s err: %s", tocreate.str().c_str(), errbuffer);
95  }
96  }
97  }
98 }
99 
100 inline std::string bool_to_str(bool b) {
101  if(b) return "true";
102  else return "false";
103 }
104 
105 inline bool str_to_bool(const std::string &str) {
106  bool value = false;
107 
108  if(str == "false" || str == "0" || str == "no") {
109  value = false;
110  } else if(str == "true" || str == "1" || str == "yes") {
111  value = true;
112  }
113  return value;
114 }
115 
116 inline std::string pfn_from_rfio_syntax(const std::string &rfn) {
117  size_t pos = rfn.find(":");
118  if(pos == std::string::npos)
119  return rfn;
120  return rfn.substr(pos+1, rfn.size());
121 }
122 
123 inline std::string server_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(0, pos);
128 }
129 
130 inline std::string unescape_forward_slashes(const std::string &str) {
131  std::ostringstream ss;
132  for(size_t i = 0; i < str.size(); i++) {
133  if(i != str.size()-1 && str[i] == '\\' && str[i+1] == '/') {
134  ss << "/";
135  i++;
136  }
137  else {
138  ss << str[i];
139  }
140  }
141  return ss.str();
142 }
143 
144 }
145 
146 
147 #endif
std::string bool_to_str(bool b)
Definition: DomeUtils.h:100
static bitmask unregistered
Definition: logger.h:45
std::string pfn_from_rfio_syntax(const std::string &rfn)
Definition: DomeUtils.h:116
std::string join(const std::string &separator, const std::vector< std::string > &arr)
Definition: DomeUtils.h:51
#define Log(lvl, mymask, where, what)
Definition: logger.h:15
Base exception class.
Definition: exceptions.h:17
std::string remove_prefix_if_exists(const std::string &str, const std::string &prefix)
Definition: DomeUtils.h:34
std::string server_from_rfio_syntax(const std::string &rfn)
Definition: DomeUtils.h:123
std::string unescape_forward_slashes(const std::string &str)
Definition: DomeUtils.h:130
std::vector< std::string > split(std::string data, std::string token)
Definition: DomeUtils.h:63
bool str_to_bool(const std::string &str)
Definition: DomeUtils.h:105
std::string trim_trailing_slashes(std::string str)
Definition: DomeUtils.h:44
Definition: logger.h:53
void mkdirp(const std::string &path)
Definition: DomeUtils.h:75
static char * unregisteredname
Definition: logger.h:46