Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:42

0001 /*
0002  * Copyright (c) 2019 Opticks Team. All Rights Reserved.
0003  *
0004  * This file is part of Opticks
0005  * (see https://bitbucket.org/simoncblyth/opticks).
0006  *
0007  * Licensed under the Apache License, Version 2.0 (the "License"); 
0008  * you may not use this file except in compliance with the License.  
0009  * You may obtain a copy of the License at
0010  *
0011  *   http://www.apache.org/licenses/LICENSE-2.0
0012  *
0013  * Unless required by applicable law or agreed to in writing, software 
0014  * distributed under the License is distributed on an "AS IS" BASIS, 
0015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0016  * See the License for the specific language governing permissions and 
0017  * limitations under the License.
0018  */
0019 
0020 #include <sstream>
0021 #include <iomanip>
0022 #include <cassert>
0023 #include <cstring>
0024 #include <csignal>
0025 #include <vector>
0026 
0027 #include "SAr.hh"
0028 #include "SSys.hh"
0029 #include "SDigest.hh"
0030 #include "SStr.hh"
0031 #include "SPath.hh"
0032 #include "SOpticksKey.hh"
0033 
0034 #include "SLOG.hh"
0035 
0036 const plog::Severity SOpticksKey::LEVEL = SLOG::EnvLevel("SOpticksKey", "DEBUG") ; 
0037 
0038 SOpticksKey* SOpticksKey::fKey = NULL ; 
0039 
0040 const char* SOpticksKey::G4LIVE = "g4live" ; 
0041 const char* SOpticksKey::IDSTEM = "g4ok" ; 
0042 const char* SOpticksKey::IDFILE = "g4ok.gltf" ; 
0043 const char* SOpticksKey::IDSUBD = "g4ok_gltf" ; 
0044 int         SOpticksKey::LAYOUT = 1 ; 
0045 const char* SOpticksKey::LAYOUT_ = "1" ; 
0046 
0047 
0048 bool SOpticksKey::IsSet()  // static
0049 {
0050     return fKey != NULL ; 
0051 }
0052 
0053 
0054 const char* SOpticksKey::Key()
0055 {
0056     return SSys::getenvvar("OPTICKS_KEY");  
0057 }
0058 
0059 SOpticksKey* SOpticksKey::GetKey()
0060 {
0061     // invoked by SOpticksResource::SOpticksResource at Opticks instanciation
0062     return fKey ; 
0063 }
0064 
0065 const char* SOpticksKey::StemName( const char* ext, const char* sep )
0066 {
0067     return SStr::Concat(IDSTEM, sep, ext );
0068 }
0069 
0070 
0071 
0072 /**
0073 SOpticksKey::SetKey
0074 ---------------------
0075 
0076 Argument spec NULL is the normal case, signalling an 
0077 attempt to get the spec from envvar OPTICKS_KEY.
0078 
0079 
0080 **/
0081 
0082 bool SOpticksKey::SetKey(const char* spec)
0083 {
0084     if(SOpticksKey::IsSet())
0085     {
0086         LOG(LEVEL) << "key is already set, ignoring update with spec " << spec ;
0087         return true ;  
0088     }
0089 
0090     bool live = spec != NULL ;  ; 
0091 
0092     if(spec == NULL)
0093     {
0094         spec = Key(); 
0095         LOG(LEVEL) << "from OPTICKS_KEY envvar " << spec ; 
0096     } 
0097 
0098     LOG(LEVEL) << " spec " << spec ; 
0099 
0100     fKey = spec ? new SOpticksKey(spec) : NULL  ; 
0101 
0102     if(fKey) 
0103     {
0104          LOG(LEVEL) << std::endl << fKey->desc() ; 
0105          fKey->setLive(live); 
0106     }
0107 
0108     return true ; 
0109 }
0110 
0111 void SOpticksKey::Desc()
0112 {
0113     LOG_IF(info, !fKey) << std::endl << fKey->desc() ; 
0114 }
0115 
0116 
0117 
0118 bool SOpticksKey::IsLive() // static
0119 {
0120     return fKey ? fKey->isLive() : false ; 
0121 }
0122 void SOpticksKey::setLive(bool live)
0123 {
0124     m_live = live ; 
0125 }
0126 bool SOpticksKey::isLive() const 
0127 {
0128     return m_live ; 
0129 }
0130 
0131    
0132 
0133 
0134 bool SOpticksKey::isKeySource() const  // current executable is geocache creator 
0135 {
0136     return m_current_exename && m_exename && strcmp(m_current_exename, m_exename) == 0 ; 
0137 }
0138 
0139 SOpticksKey::SOpticksKey(const char* spec)
0140     :
0141     m_spec( spec ? strdup(spec) : NULL ),
0142     m_exename( NULL ),
0143     m_class( NULL ),
0144     m_volname( NULL ),
0145     m_digest( NULL ),
0146     m_idname( NULL ),
0147     m_idfile( StemName("gltf", ".") ),
0148     m_idgdml( StemName("gdml", ".") ),
0149     m_idsubd( IDSUBD ),
0150     m_layout( LAYOUT ),
0151     m_current_exename( SAr::Instance ? SAr::Instance->exename() : "OpticksEmbedded" ), 
0152     m_live(false)
0153 {
0154     std::vector<std::string> elem ; 
0155     SStr::Split(spec, '.', elem ); 
0156 
0157     bool four = elem.size() == 4  ;
0158     LOG_IF(fatal, !four) << " expecting 4 element spec delimited by dot [" << spec << "]" ;  
0159     assert( four ); 
0160     
0161     m_exename = strdup(elem[0].c_str()); 
0162     m_class = strdup(elem[1].c_str()); 
0163     m_volname   = strdup(elem[2].c_str()); 
0164     m_digest = strdup(elem[3].c_str()); 
0165 
0166     assert( SDigest::IsDigest(m_digest) ); 
0167 
0168     std::stringstream ss ; 
0169     ss 
0170         << m_exename 
0171         << "_"
0172         << m_volname 
0173         << "_"
0174         << G4LIVE 
0175         ;
0176 
0177     std::string idname = ss.str();
0178 
0179     m_idname = strdup(idname.c_str()); 
0180 }
0181 
0182 const char* SOpticksKey::getSpec() const 
0183 {
0184     return m_spec ;  
0185 }
0186 
0187 std::string SOpticksKey::export_() const 
0188 {
0189     std::stringstream ss ; 
0190     ss   
0191         << "# SOpticksKey::export_ " 
0192         << "\n" 
0193         << "export OPTICKS_KEY=" << m_spec 
0194         << "\n" 
0195         ;    
0196     return ss.str();
0197 }
0198 const char* SOpticksKey::getExename() const 
0199 {
0200     return m_exename ;  
0201 }
0202 const char* SOpticksKey::getClass() const 
0203 {
0204     return m_class ;  
0205 }
0206 const char* SOpticksKey::getVolname() const 
0207 {
0208     return m_volname ;  
0209 }
0210 const char* SOpticksKey::getDigest() const 
0211 {
0212     return m_digest ;  
0213 }
0214 const char* SOpticksKey::getIdname() const 
0215 {
0216     return m_idname ;  
0217 }
0218 const char* SOpticksKey::getIdfile() const 
0219 {
0220     return m_idfile ;  
0221 }
0222 const char* SOpticksKey::getIdGDML() const 
0223 {
0224     return m_idgdml ;  
0225 }
0226 const char* SOpticksKey::getIdsubd() const 
0227 {
0228     return m_idsubd ;  
0229 }
0230 int SOpticksKey::getLayout() const 
0231 {
0232     return m_layout ;  
0233 }
0234 
0235 
0236 std::string SOpticksKey::desc() const 
0237 {
0238     std::stringstream ss ; 
0239     ss 
0240         << std::setw(25) << " SOpticksKey " << " : " << ( isKeySource() ? "KEYSOURCE" : " " ) << std::endl 
0241         << std::setw(25) << " spec (OPTICKS_KEY) " << " : " << m_spec    << std::endl 
0242         << std::setw(25) << " exename " << " : " << m_exename << std::endl 
0243         << std::setw(25) << " current_exename " << " : " << m_current_exename << std::endl 
0244         << std::setw(25) << " class "   << " : " << m_class   << std::endl 
0245         << std::setw(25) << " volname " << " : " << m_volname << std::endl 
0246         << std::setw(25) << " digest "  << " : " << m_digest  << std::endl 
0247         << std::setw(25) << " idname "  << " : " << m_idname  << std::endl 
0248         << std::setw(25) << " idfile "  << " : " << m_idfile  << std::endl 
0249         << std::setw(25) << " idgdml "  << " : " << m_idgdml  << std::endl 
0250         << std::setw(25) << " layout "  << " : " << m_layout  << std::endl 
0251         ;
0252     return ss.str(); 
0253 }
0254 
0255 
0256 const char* SOpticksKey::getIdPath(const char* base) const
0257 {
0258     int create_dirs = 0 ;  // 0:noop
0259     return SPath::Resolve(base, m_idname, m_idsubd, m_digest, LAYOUT_, create_dirs ) ; 
0260 }
0261 
0262 
0263 
0264 
0265