Back to index

nordugrid-arc-nox  1.1.0~rc6
StringConv.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include <vector>
00008 #include <ctype.h>
00009 #include <algorithm>
00010 #include "Logger.h"
00011 
00012 namespace Arc {
00013 
00014   Logger stringLogger(Logger::getRootLogger(), "StringConv");
00015 
00016   std::string lower(const std::string& s) {
00017     std::string ret = s;
00018     std::transform(ret.begin(), ret.end(), ret.begin(), (int(*) (int)) std::tolower);
00019     return ret;
00020   }
00021 
00022   std::string upper(const std::string& s) {
00023     std::string ret = s;
00024     std::transform(ret.begin(), ret.end(), ret.begin(), (int(*) (int)) std::toupper);
00025     return ret;
00026   }
00027 
00028   void tokenize(const std::string& str, std::vector<std::string>& tokens,
00029                 const std::string& delimiters = " ") {
00030     // Skip delimiters at beginning.
00031     std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00032     // Find first "non-delimiter".
00033     std::string::size_type pos = str.find_first_of(delimiters, lastPos);
00034 
00035     while (std::string::npos != pos || std::string::npos != lastPos) {
00036       // Found a token, add it to the vector.
00037       tokens.push_back(str.substr(lastPos, pos - lastPos));
00038       // Skip delimiters.  Note the "not_of"
00039       lastPos = str.find_first_not_of(delimiters, pos);
00040       // Find next "non-delimiter"
00041       pos = str.find_first_of(delimiters, lastPos);
00042     }
00043   }
00044 
00045   void tokenize(const std::string& str, std::list<std::string>& tokens,
00046                 const std::string& delimiters = " ") {
00047     // Skip delimiters at beginning.
00048     std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00049     // Find first "non-delimiter".
00050     std::string::size_type pos = str.find_first_of(delimiters, lastPos);
00051 
00052     while (std::string::npos != pos || std::string::npos != lastPos) {
00053       // Found a token, add it to the vector.
00054       tokens.push_back(str.substr(lastPos, pos - lastPos));
00055       // Skip delimiters.  Note the "not_of"
00056       lastPos = str.find_first_not_of(delimiters, pos);
00057       // Find next "non-delimiter"
00058       pos = str.find_first_of(delimiters, lastPos);
00059     }
00060   }
00061 
00062   static const char kBlankChars[] = " \t\n\r";
00063   std::string trim(const std::string& str, const char *sep = NULL) {
00064     if (sep == NULL)
00065       sep = kBlankChars;
00066     std::string::size_type const first = str.find_first_not_of(sep);
00067     return (first == std::string::npos) ? std::string() : str.substr(first, str.find_last_not_of(sep) - first + 1);
00068   }
00069 
00070   std::string strip(const std::string& str) {
00071     std::string retstr = "";
00072 
00073     std::string::size_type pos = str.find_first_of("\n");
00074     std::string::size_type lastPos = 0;
00075 
00076     while (std::string::npos != pos) {
00077       const std::string tmpstr = str.substr(lastPos, pos-lastPos);
00078       if (!trim(tmpstr).empty()) {
00079         if (!retstr.empty())
00080           retstr += "\n";
00081         retstr += tmpstr;
00082       }
00083 
00084       lastPos = pos+1;
00085       pos = str.find_first_of("\n", lastPos+1);
00086     }
00087 
00088     if (!str.substr(lastPos).empty()) {
00089       retstr += "\n"+str.substr(lastPos);
00090     }
00091 
00092     return retstr;
00093   }
00094 
00095 #if HAVE_URI_UNESCAPE_STRING
00096   std::string uri_unescape(const std::string& str) {
00097     return Glib::uri_unescape_string(str);
00098   }
00099 #else
00100 #include <glib.h>
00101   static int unescape_character(const std::string& scanner, int i) {
00102     int first_digit;
00103     int second_digit;
00104 
00105     first_digit = g_ascii_xdigit_value(scanner[i++]);
00106     if (first_digit < 0)
00107       return -1;
00108 
00109     second_digit = g_ascii_xdigit_value(scanner[i++]);
00110     if (second_digit < 0)
00111       return -1;
00112 
00113     return (first_digit << 4) | second_digit;
00114   }
00115 
00116   std::string uri_unescape(const std::string& str) {
00117     std::string out = str;
00118     int character;
00119 
00120     if (str.empty())
00121       return str;
00122     int j = 0;
00123     for (int i = 0; i < str.size(); i++) {
00124       character = str[i];
00125       if (str[i] == '%') {
00126         i++;
00127         if (str.size() - i < 2)
00128           return "";
00129         character = unescape_character(str, i);
00130         i++;     /* The other char will be eaten in the loop header */
00131       }
00132       out[j++] = (char)character;
00133     }
00134     out.resize(j);
00135     return out;
00136   }
00137 #endif
00138 
00139   std::string convert_to_rdn(const std::string& dn) {
00140     std::string ret;
00141     size_t pos1 = std::string::npos;
00142     size_t pos2;
00143     do {
00144       std::string str;
00145       pos2 = dn.find_last_of("/", pos1);
00146       if(pos2 != std::string::npos && pos1 == std::string::npos) {
00147         str = dn.substr(pos2+1);
00148         ret.append(str);
00149         pos1 = pos2-1;
00150       }
00151       else if (pos2 != std::string::npos && pos1 != std::string::npos) {
00152         str = dn.substr(pos2+1, pos1-pos2);
00153         ret.append(str);
00154         pos1 = pos2-1;
00155       }
00156       if(pos2 != (std::string::npos+1)) ret.append(",");
00157     }while(pos2 != std::string::npos && pos2 != (std::string::npos+1));
00158     return ret;
00159   }
00160 } // namespace Arc