LCOV - code coverage report
Current view: top level - build/stdcasa - version.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 88 103 85.4 %
Date: 2024-11-06 17:42:47 Functions: 9 11 81.8 %

          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        2632 :     int VersionInfo::major( ) {
      37             :         static bool initialized = false;
      38             :         static int version = -1;
      39        2632 :         if ( initialized == false ) {
      40           3 :             initialized = true;
      41           3 :             version = environ_version("CASA_VERSION_MAJOR");
      42             :         }
      43        2632 :         return version > 0 ? version : 6;
      44             :     }
      45             : 
      46        2632 :     int VersionInfo::minor( ) {
      47             :         static bool initialized = false;
      48             :         static int version = -1;
      49        2632 :         if ( initialized == false ) {
      50           3 :             initialized = true;
      51           3 :             version = environ_version("CASA_VERSION_MINOR");
      52             :         }
      53        2632 :         return version > 0 ? version : 7;
      54             :     }
      55             : 
      56        2632 :     int VersionInfo::patch( ) {
      57             :         static bool initialized = false;
      58             :         static int version = -1;
      59        2632 :         if ( initialized == false ) {
      60           3 :             initialized = true;
      61           3 :             version = environ_version("CASA_VERSION_PATCH");
      62             :         }
      63        2632 :         return version > 0 ? version : 0;
      64             :     }
      65             : 
      66        2632 :     int VersionInfo::feature( ) {
      67             :         static bool initialized = false;
      68             :         static int version = -1;
      69        2632 :         if ( initialized == false ) {
      70           3 :             initialized = true;
      71           3 :             version = environ_version("CASA_VERSION_FEATURE");
      72             :         }
      73        2632 :         return version > 0 ? version : 8;
      74             :     }
      75             : 
      76        3094 :     std::string VersionInfo::desc( ) {
      77             :         static bool initialized = false;
      78        3094 :         static std::string version("CASAtools:v1.0.0");
      79        3094 :         if ( initialized == false ) {
      80           1 :             initialized = true;
      81           1 :             char *desc = getenv("CASA_VERSION_DESC");
      82           1 :             if ( desc ) version = desc;
      83             :         }
      84        3094 :         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         981 :     std::string VersionInfo::info( ) {
     100             :         static bool initialized = false;
     101           2 :         static std::string version = to_string(major( )) + "." +
     102           4 :                                      to_string(minor( )) + "." +
     103           4 :                                      to_string(patch( )) + "-" +
     104           2 :                                      to_string(feature( )) +
     105         983 :                                      (desc( ).size( ) > 0 ? " " + desc( ) : "");
     106         981 :         if ( initialized == false ) {
     107           1 :             initialized = true;
     108           1 :             char *desc = getenv("CASA_VERSION_INFO");
     109           1 :             if ( desc ) version = desc;
     110             :         }
     111         981 :         return version;
     112             :     }
     113             : 
     114       12509 :     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       12513 :                                      to_string(feature( ));
     120       12509 :         if ( initialized == false ) {
     121           2 :             initialized = true;
     122           2 :             char *desc = getenv("CASA_VERSION_STR");
     123           2 :             if ( desc ) version = desc;
     124             :         }
     125       12509 :         return version;
     126             :     }
     127             : 
     128        2628 :     bool VersionInfo::compare(const  string& comparitor,  const std::vector<int>& vec) {
     129        2628 :         std::vector<int> current_version { major( ), minor( ), patch( ), feature( ) };
     130       10539 :         for ( unsigned int i=0; i < vec.size( ); ++i )
     131        7911 :             if ( vec[i] < 0 ) throw(AipsError("negative values not allowed in version numbers"));
     132             : 
     133        2628 :         unsigned int limit = min(current_version.size( ),vec.size( ));
     134             :         //std::cerr<< "Limit: " << limit << std::endl;
     135        2628 :         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        2622 :         } else if ( comparitor == "<" ) {
     148         763 :             for ( unsigned int i=0; i < limit; ++i ) {
     149         762 :                 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        1869 :         } else if ( comparitor == ">=" ) {
     154        1865 :             for ( unsigned int i=0; i < limit; ++i ) {
     155        1864 :                 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        2628 :     }
     181             : 
     182             : } //# NAMESPACE CASA - END

Generated by: LCOV version 1.16