Back to index

nordugrid-arc-nox  1.1.0~rc6
rte.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include "rte.h"
00006 #include <stdlib.h>
00007 #include <arc/StringConv.h>
00008 
00009 RTE::RTE(const std::string& re) {
00010 
00011        runtime_environment = re;
00012 
00013        name = runtime_environment;
00014        version = "";
00015 
00016        /* This has some assumptions, but holds for all the sane ones */
00017        std::string::size_type pos = re.find_first_of(" -");
00018        while ((pos != std::string::npos) && !(isdigit(re[pos+1])))
00019               pos = re.find_first_of(" -", pos+1);
00020        if (pos != std::string::npos) {
00021               name = re.substr (0, pos);
00022               version = re.substr(pos+1);
00023        }
00024 }
00025 
00026 
00027 RTE::~RTE() { }
00028 
00029 
00030 std::string RTE::str() const {
00031        return runtime_environment;
00032 }
00033 
00034 
00035 std::string RTE::Name() const {
00036        return name;
00037 }
00038 
00039 
00040 std::string RTE::Version() const {
00041        return version;
00042 }
00043 
00044 
00045 bool RTE::operator==(const RTE& other) const {
00046        if (runtime_environment==other.str())
00047               return true;
00048        else
00049               return false;
00050 }
00051 
00052 
00053 bool RTE::operator!=(const RTE& other) const {
00054        return !(*this == other);
00055 }
00056 
00057 
00058 bool RTE::operator>(const RTE& other) const {
00059 
00060        if (name!=other.Name()) return (name>other.Name());
00061 
00062        // Get version
00063        std::vector<std::string> my_version = SplitVersion(version);
00064        std::vector<std::string> other_version = SplitVersion(other.Version());
00065 
00066        // Ensure common length
00067        int ml = my_version.size();
00068        int ol = other_version.size();
00069        unsigned int max_size = std::max(ml, ol);
00070 
00071        while (my_version.size()<max_size)
00072               my_version.push_back("0");
00073        while (other_version.size()<max_size)
00074               other_version.push_back("0");
00075 
00076        // Start comparing
00077        for (unsigned int i=0; i<max_size; i++) {
00078               if (my_version[i]==other_version[i]) continue;
00079               // first try to compare as integers
00080                 char* my_e;
00081                 char* other_e;
00082                 int my_v = strtol(my_version[i].c_str(),&my_e,0);
00083                 int other_v = strtol(other_version[i].c_str(),&other_e,0);
00084                 if((*my_e == 0) && (*other_e == 0)) return (my_v > other_v);
00085               // otherwise compare as strings
00086               return my_version[i]>other_version[i];
00087        }
00088 
00089        // If we are here the versions are the same.
00090        return false;
00091 }
00092 
00093 
00094 bool RTE::operator<(const RTE& other) const {
00095        if (*this == other) return false;
00096        return !(*this > other);
00097 }
00098 
00099 
00100 bool RTE::operator>=(const RTE& other) const {
00101        return !(*this < other);
00102 }
00103 
00104 
00105 bool RTE::operator<=(const RTE& other) const {
00106        return !(*this > other);
00107 }
00108 
00109 
00110 std::vector<std::string> RTE::SplitVersion(const std::string&
00111 version) const {
00112 
00113        std::vector<std::string> tokens;
00114        if (version.empty()) return tokens;
00115 
00116        std::string::size_type pos = 0;
00117        std::string::size_type last = 0;
00118 
00119        while ((pos = version.find_first_of(".-", last)) != std::string::npos) {
00120               std::string token = version.substr(last, pos-last);
00121               tokens.push_back(token);
00122               last = pos + 1;
00123        }
00124 
00125        std::string lasttoken = version.substr(last, version.size()-last);
00126        tokens.push_back(lasttoken);
00127 
00128        return tokens;
00129 }