LCOV - code coverage report
Current view: top level - build/stdcasa - version.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 70 103 68.0 %
Date: 2024-10-04 16:51:10 Functions: 7 11 63.6 %

          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           8 :     static int environ_version( const char *evar ) {
      26           8 :         char *cstr = getenv(evar);
      27           8 :         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           8 :         return -1;
      34             :     }
      35             : 
      36          32 :     int VersionInfo::major( ) {
      37             :         static bool initialized = false;
      38             :         static int version = -1;
      39          32 :         if ( initialized == false ) {
      40           2 :             initialized = true;
      41           2 :             version = environ_version("CASA_VERSION_MAJOR");
      42             :         }
      43          32 :         return version > 0 ? version : 6;
      44             :     }
      45             : 
      46          32 :     int VersionInfo::minor( ) {
      47             :         static bool initialized = false;
      48             :         static int version = -1;
      49          32 :         if ( initialized == false ) {
      50           2 :             initialized = true;
      51           2 :             version = environ_version("CASA_VERSION_MINOR");
      52             :         }
      53          32 :         return version > 0 ? version : 6;
      54             :     }
      55             : 
      56          32 :     int VersionInfo::patch( ) {
      57             :         static bool initialized = false;
      58             :         static int version = -1;
      59          32 :         if ( initialized == false ) {
      60           2 :             initialized = true;
      61           2 :             version = environ_version("CASA_VERSION_PATCH");
      62             :         }
      63          32 :         return version > 0 ? version : 4;
      64             :     }
      65             : 
      66          32 :     int VersionInfo::feature( ) {
      67             :         static bool initialized = false;
      68             :         static int version = -1;
      69          32 :         if ( initialized == false ) {
      70           2 :             initialized = true;
      71           2 :             version = environ_version("CASA_VERSION_FEATURE");
      72             :         }
      73          32 :         return version > 0 ? version : 27;
      74             :     }
      75             : 
      76           0 :     std::string VersionInfo::desc( ) {
      77             :         static bool initialized = false;
      78           0 :         static std::string version("CASAtools:v1.0.0");
      79           0 :         if ( initialized == false ) {
      80           0 :             initialized = true;
      81           0 :             char *desc = getenv("CASA_VERSION_DESC");
      82           0 :             if ( desc ) version = desc;
      83             :         }
      84           0 :         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         424 :     std::string VersionInfo::str( ) {
     115             :         static bool initialized = false;
     116           2 :         static std::string version = to_string(major( )) + "." +
     117           4 :                                      to_string(minor( )) + "." +
     118           4 :                                      to_string(patch( )) + "-" +
     119         426 :                                      to_string(feature( ));
     120         424 :         if ( initialized == false ) {
     121           1 :             initialized = true;
     122           1 :             char *desc = getenv("CASA_VERSION_STR");
     123           1 :             if ( desc ) version = desc;
     124             :         }
     125         424 :         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

Generated by: LCOV version 1.16