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