Line data Source code
1 : // This file is a template that is processed by shell commands during 2 : // the make process. Be very cautious about changing the parts in the 3 : // first namespace block since the shell commands are fairly simple 4 : // and can be confused by altering even one character in this section. 5 : 6 : #include <casacore/casa/Exceptions/Error.h> 7 : #include <stdcasa/version.h> 8 : #include <cstdlib> 9 : #include <numeric> 10 : #include <cstdlib> 11 : #include <cctype> 12 : #include <string> 13 : #include <algorithm> 14 : using std::getenv; 15 : using std::accumulate; 16 : using std::isdigit; 17 : using std::stoi; 18 : using std::string; 19 : using std::to_string; 20 : using std::min; 21 : using casacore::AipsError; 22 : 23 : namespace casa { 24 : 25 12 : static int environ_version( const char *evar ) { 26 12 : char *cstr = getenv(evar); 27 12 : if ( cstr ) { 28 0 : std::string val(cstr); 29 0 : bool alldigits = accumulate( val.begin(), val.end(), true, 30 0 : [=](bool sum, char c){return sum && isdigit(c);} ); 31 0 : if ( alldigits ) return stoi(val); 32 0 : } 33 12 : return -1; 34 : } 35 : 36 33 : int VersionInfo::major( ) { 37 : static bool initialized = false; 38 : static int version = -1; 39 33 : if ( initialized == false ) { 40 3 : initialized = true; 41 3 : version = environ_version("CASA_VERSION_MAJOR"); 42 : } 43 33 : return version > 0 ? version : 6; 44 : } 45 : 46 33 : int VersionInfo::minor( ) { 47 : static bool initialized = false; 48 : static int version = -1; 49 33 : if ( initialized == false ) { 50 3 : initialized = true; 51 3 : version = environ_version("CASA_VERSION_MINOR"); 52 : } 53 33 : return version > 0 ? version : 7; 54 : } 55 : 56 33 : int VersionInfo::patch( ) { 57 : static bool initialized = false; 58 : static int version = -1; 59 33 : if ( initialized == false ) { 60 3 : initialized = true; 61 3 : version = environ_version("CASA_VERSION_PATCH"); 62 : } 63 33 : return version > 0 ? version : 0; 64 : } 65 : 66 33 : int VersionInfo::feature( ) { 67 : static bool initialized = false; 68 : static int version = -1; 69 33 : if ( initialized == false ) { 70 3 : initialized = true; 71 3 : version = environ_version("CASA_VERSION_FEATURE"); 72 : } 73 33 : return version > 0 ? version : 8; 74 : } 75 : 76 387 : std::string VersionInfo::desc( ) { 77 : static bool initialized = false; 78 387 : static std::string version("CASAtools:v1.0.0"); 79 387 : if ( initialized == false ) { 80 1 : initialized = true; 81 1 : char *desc = getenv("CASA_VERSION_DESC"); 82 1 : if ( desc ) version = desc; 83 : } 84 387 : return version; 85 : } 86 : 87 0 : std::string VersionInfo::variant( ) { 88 : static bool initialized = false; 89 0 : static std::string casavariant(""); 90 0 : if ( initialized == false ) { 91 0 : initialized = true; 92 0 : char *v = getenv("CASA_VARIANT"); 93 0 : if ( v ) casavariant = v; 94 : } 95 0 : return casavariant; 96 : } 97 : 98 : 99 0 : std::string VersionInfo::info( ) { 100 : static bool initialized = false; 101 0 : static std::string version = to_string(major( )) + "." + 102 0 : to_string(minor( )) + "." + 103 0 : to_string(patch( )) + "-" + 104 0 : to_string(feature( )) + 105 0 : (desc( ).size( ) > 0 ? " " + desc( ) : ""); 106 0 : if ( initialized == false ) { 107 0 : initialized = true; 108 0 : char *desc = getenv("CASA_VERSION_INFO"); 109 0 : if ( desc ) version = desc; 110 : } 111 0 : return version; 112 : } 113 : 114 1733 : std::string VersionInfo::str( ) { 115 : static bool initialized = false; 116 4 : static std::string version = to_string(major( )) + "." + 117 8 : to_string(minor( )) + "." + 118 8 : to_string(patch( )) + "-" + 119 1737 : to_string(feature( )); 120 1733 : if ( initialized == false ) { 121 2 : initialized = true; 122 2 : char *desc = getenv("CASA_VERSION_STR"); 123 2 : if ( desc ) version = desc; 124 : } 125 1733 : return version; 126 : } 127 : 128 30 : bool VersionInfo::compare(const string& comparitor, const std::vector<int>& vec) { 129 30 : std::vector<int> current_version { major( ), minor( ), patch( ), feature( ) }; 130 147 : for ( unsigned int i=0; i < vec.size( ); ++i ) 131 117 : if ( vec[i] < 0 ) throw(AipsError("negative values not allowed in version numbers")); 132 : 133 30 : unsigned int limit = min(current_version.size( ),vec.size( )); 134 : //std::cerr<< "Limit: " << limit << std::endl; 135 30 : if ( comparitor == ">" ) { 136 : 137 17 : for ( unsigned int i=0; i < limit; ++i ) { 138 : //std::cerr<< "Test: " << current_version[i] << " " << vec[i] << std::endl; 139 15 : if ( current_version[i] > vec[i] ) return true; 140 15 : else if ( current_version[i] < vec[i] ) return false; 141 : } 142 2 : for ( unsigned int i=limit; i < current_version.size( ); ++i ) { 143 : //std::cerr<< "Test2: i:" << i << " current_version.size()" << current_version.size() << "current_version[i]" << current_version[i]<< std::endl; 144 1 : if ( current_version[i] >= 0 ) return true; 145 : } 146 1 : return false; 147 24 : } else if ( comparitor == "<" ) { 148 15 : for ( unsigned int i=0; i < limit; ++i ) { 149 14 : if ( current_version[i] > vec[i] ) return false; 150 14 : else if ( current_version[i] < vec[i] ) return true; 151 : } 152 1 : return false; 153 19 : } else if ( comparitor == ">=" ) { 154 15 : for ( unsigned int i=0; i < limit; ++i ) { 155 14 : if ( current_version[i] > vec[i] ) return true; 156 14 : else if ( current_version[i] < vec[i] ) return false; 157 : } 158 1 : return true; 159 14 : } else if ( comparitor == "<=" ) { 160 15 : for ( unsigned int i=0; i < limit; ++i ) { 161 14 : if ( current_version[i] > vec[i] ) return false; 162 14 : else if ( current_version[i] < vec[i] ) return true; 163 : } 164 1 : for ( unsigned int i=limit; i < current_version.size( ); ++i ) 165 0 : if ( current_version[i] > 0 ) return false; 166 1 : return true; 167 9 : } else if ( comparitor == "=" || comparitor == "==" ) { 168 15 : for ( unsigned int i=0; i < limit; ++i ) { 169 14 : if ( current_version[i] != vec[i] ) return false; 170 : } 171 1 : for ( unsigned int i=limit; i < current_version.size( ); ++i ) 172 0 : if ( current_version[i] > 0 ) return false; 173 1 : return true; 174 4 : } else if ( comparitor == "!=" ) { 175 4 : return ! compare("=",vec); 176 : } else { 177 0 : throw(AipsError("unknown comparator")); 178 : } 179 : return false; 180 30 : } 181 : 182 : } //# NAMESPACE CASA - END