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 1351 : State &get_state( ) { 11 1351 : if ( shutting_down ) 12 0 : throw std::runtime_error("casatools::get_state( ) called after shutdown initiated"); 13 1351 : if ( casacore::AppStateSource::fetch( ).initialized( ) == false ) 14 3 : casacore::AppStateSource::initialize( new State ); 15 1351 : return dynamic_cast<State&>(casacore::AppStateSource::fetch( )); 16 : } 17 : 18 3 : void State::shutdown( ) { 19 3 : shutting_down = true; 20 3 : } 21 : 22 967 : 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 967 : 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 967 : if ( stat( subdir.c_str( ), &statbuf ) == 0 ) { 43 105 : if ( subdir[0] == '/' ) return subdir; 44 : char buf[PATH_MAX]; 45 4 : if ( getcwd(buf,sizeof(buf)) ) { 46 8 : 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 862 : return casacore::AppState::resolve( subdir ); 54 : } 55 : 56 : }