LCOV - code coverage report
Current view: top level - casatools/Config - State.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 12 17 70.6 %
Date: 2024-10-28 15:53:10 Functions: 3 3 100.0 %

          Line data    Source code
       1             : #include <numeric>
       2             : #include <limits.h>
       3             : #include <unistd.h>
       4             : #include <stdexcept>
       5             : #include <casatools/Config/State.h>
       6             : 
       7             : namespace casatools {
       8             :     static bool shutting_down = false;
       9             : 
      10          21 :     State &get_state( )  {
      11          21 :         if ( shutting_down )
      12           0 :             throw std::runtime_error("casatools::get_state( ) called after shutdown initiated");
      13          21 :         if ( casacore::AppStateSource::fetch( ).initialized( ) == false )
      14           2 :             casacore::AppStateSource::initialize( new State );
      15          21 :         return dynamic_cast<State&>(casacore::AppStateSource::fetch( ));
      16             :     }
      17             : 
      18           2 :     void State::shutdown( ) {
      19           2 :         shutting_down = true;        
      20           2 :     }
      21             : 
      22           2 :     std::string State::resolve(const std::string &subdir) const {
      23             :         // This function overrides the base class casacore::AppState::resolve function to ensure
      24             :         // that when the subdir being resolved is found in the current working directory then a
      25             :         // fully qualified path is still returned. This allows the python level idiom:
      26             :         //
      27             :         //    file = 'casatestdata/some-ms.ms'
      28             :         //    resolved = ctsys.resolve(file)
      29             :         //    if file == resolved:
      30             :         //        # then file not found
      31             :         //        ...
      32             :         //
      33             :         // to still work. With this change, it is clear that the path was resolved because the
      34             :         // result is a fully qualified path.
      35             : 
      36             :         // return immediately if subdir is null string
      37           2 :         if ( subdir.size( ) <= 0 ) return subdir;
      38             : 
      39             :         // if subdir already resolves to a valid path, try to return a
      40             :         // fully qualified path...
      41             :         struct stat statbuf;
      42           2 :         if ( stat( subdir.c_str( ), &statbuf ) == 0 ) {
      43           0 :             if ( subdir[0] == '/' ) return subdir;
      44             :             char buf[PATH_MAX];
      45           0 :             if ( getcwd(buf,sizeof(buf)) ) {
      46           0 :                 return std::string(buf) + "/" + subdir;
      47             :             }
      48           0 :             return subdir;
      49             :         }
      50             : 
      51             :         // otherwise, if data path must be searched leave it
      52             :         // to the base class resolve method...
      53           2 :         return casacore::AppState::resolve( subdir );
      54             :     }
      55             : 
      56             : }

Generated by: LCOV version 1.16