00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef DOMEUTILS_H
00025 #define DOMEUTILS_H
00026
00027 #include <string>
00028 #include <vector>
00029
00030 namespace DomeUtils {
00031
00032 using namespace dmlite;
00033
00034 inline std::string remove_prefix_if_exists(const std::string &str, const std::string &prefix) {
00035 if(prefix.size() > str.size()) return str;
00036
00037 if(std::equal(prefix.begin(), prefix.end(), str.begin())) {
00038 return str.substr(prefix.size(), str.size()-prefix.size());
00039 }
00040
00041 return str;
00042 }
00043
00044 inline std::string trim_trailing_slashes(std::string str) {
00045 while(str.size() > 0 && str[str.size()-1] == '/') {
00046 str.erase(str.size()-1);
00047 }
00048 return str;
00049 }
00050
00051 inline std::string join(const std::string &separator, const std::vector<std::string> &arr) {
00052 if(arr.empty()) return std::string();
00053
00054 std::stringstream ss;
00055 for(size_t i = 0; i < arr.size()-1; i++) {
00056 ss << arr[i];
00057 ss << separator;
00058 }
00059 ss << arr[arr.size()-1];
00060 return ss.str();
00061 }
00062
00063 inline std::vector<std::string> split(std::string data, std::string token) {
00064 std::vector<std::string> output;
00065 size_t pos = std::string::npos;
00066 do {
00067 pos = data.find(token);
00068 output.push_back(data.substr(0, pos));
00069 if(std::string::npos != pos)
00070 data = data.substr(pos + token.size());
00071 } while (std::string::npos != pos);
00072 return output;
00073 }
00074
00075 inline void mkdirp(const std::string& path) throw (DmException) {
00076 std::vector<std::string> parts = split(path, "/");
00077 std::ostringstream tocreate(parts[0]);
00078
00079
00080 for(std::vector<std::string>::iterator it = parts.begin()+1; it+1 != parts.end(); it++) {
00081 tocreate << "/" + *it;
00082
00083 struct stat info;
00084 if(::stat(tocreate.str().c_str(), &info) != 0) {
00085 Log(Logger::Lvl1, Logger::unregistered, Logger::unregisteredname, " Creating directory: " << tocreate.str());
00086
00087 mode_t prev = umask(0);
00088 int ret = ::mkdir(tocreate.str().c_str(), 0770);
00089 umask(prev);
00090
00091 if(ret != 0) {
00092 char errbuffer[128];
00093 strerror_r(errno, errbuffer, sizeof(errbuffer));
00094 throw DmException(errno, "Could not create directory: %s err: %s", tocreate.str().c_str(), errbuffer);
00095 }
00096 }
00097 }
00098 }
00099
00100 inline std::string bool_to_str(bool b) {
00101 if(b) return "true";
00102 else return "false";
00103 }
00104
00105 inline bool str_to_bool(const std::string &str) {
00106 bool value = false;
00107
00108 if(str == "false" || str == "0" || str == "no") {
00109 value = false;
00110 } else if(str == "true" || str == "1" || str == "yes") {
00111 value = true;
00112 }
00113 return value;
00114 }
00115
00116 inline std::string pfn_from_rfio_syntax(const std::string &rfn) {
00117 size_t pos = rfn.find(":");
00118 if(pos == std::string::npos)
00119 return rfn;
00120 return rfn.substr(pos+1, rfn.size());
00121 }
00122
00123 inline std::string server_from_rfio_syntax(const std::string &rfn) {
00124 size_t pos = rfn.find(":");
00125 if(pos == std::string::npos)
00126 return rfn;
00127 return rfn.substr(0, pos);
00128 }
00129
00130 inline std::string unescape_forward_slashes(const std::string &str) {
00131 std::ostringstream ss;
00132 for(size_t i = 0; i < str.size(); i++) {
00133 if(i != str.size()-1 && str[i] == '\\' && str[i+1] == '/') {
00134 ss << "/";
00135 i++;
00136 }
00137 else {
00138 ss << str[i];
00139 }
00140 }
00141 return ss.str();
00142 }
00143
00144 }
00145
00146
00147 #endif