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) {
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
00093
00094 char errbuffer[256];
00095 char fullerr[1024];
00096 memset(errbuffer, 0, sizeof(errbuffer));
00097 strerror_r(errno, errbuffer, sizeof(errbuffer));
00098
00099 snprintf(fullerr, sizeof(fullerr), "Could not create directory: '%s' err: %d:'%s'", tocreate.str().c_str(), errno, errbuffer);
00100 fullerr[sizeof(fullerr)-1] = '\0';
00101 throw DmException(errno, fullerr);
00102 }
00103 }
00104 }
00105 }
00106
00107 inline std::string bool_to_str(bool b) {
00108 if(b) return "true";
00109 else return "false";
00110 }
00111
00112 inline bool str_to_bool(const std::string &str) {
00113 bool value = false;
00114
00115 if(str == "false" || str == "0" || str == "no") {
00116 value = false;
00117 } else if(str == "true" || str == "1" || str == "yes") {
00118 value = true;
00119 }
00120 return value;
00121 }
00122
00123 inline std::string pfn_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(pos+1, rfn.size());
00128 }
00129
00130 inline std::string server_from_rfio_syntax(const std::string &rfn) {
00131 size_t pos = rfn.find(":");
00132 if(pos == std::string::npos)
00133 return rfn;
00134 return rfn.substr(0, pos);
00135 }
00136
00137 inline std::string unescape_forward_slashes(const std::string &str) {
00138 std::ostringstream ss;
00139 for(size_t i = 0; i < str.size(); i++) {
00140 if(i != str.size()-1 && str[i] == '\\' && str[i+1] == '/') {
00141 ss << "/";
00142 i++;
00143 }
00144 else {
00145 ss << str[i];
00146 }
00147 }
00148 return ss.str();
00149 }
00150
00151 }
00152
00153
00154
00155
00156 #endif