Back to home page

EIC code displayed by LXR

 
 

    


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

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 "SLOG.hh"
0021 #include "SArgs.hh"
0022 
0023 
0024 const plog::Severity SArgs::LEVEL = SLOG::EnvLevel("SArgs", "DEBUG" ); 
0025 
0026 void SArgs::add(int argc_, char** argv_)
0027 {
0028     if(argc_ == 0)  // needed for Opticks(0, NULL, argforce)  
0029     {
0030         elem.push_back("SArgsDummyExecutable"); 
0031     }
0032     else
0033     {
0034         for(int i=0 ; i < argc_ ; i++) elem.push_back(argv_[i]) ;
0035     }
0036 }
0037 
0038 bool SArgs::starts_with( const std::string& e, const char* pfx )  // static 
0039 {
0040     return strncmp(e.c_str(), pfx, strlen(pfx)) == 0 ;
0041 }
0042 
0043 void SArgs::addElements(const std::string& line, bool dedupe)
0044 {
0045     // split string on whitespace
0046     std::stringstream ss(line);
0047     typedef std::istream_iterator<std::string> ISI ; 
0048     ISI begin(ss);
0049     ISI end ; 
0050     std::vector<std::string> vs(begin, end);
0051 
0052     for(std::vector<std::string>::const_iterator it=vs.begin() ; it != vs.end() ; it++)
0053     {
0054         std::string e = *it ; 
0055 
0056         /*
0057         Need to only skip when looks like an option, starting with "--"
0058         otherwise will skip repeated argument values too ... 
0059         which causes many tboolean- tests to fail, from skipped values
0060         causing option parse errors.
0061         */
0062 
0063         bool skip = dedupe && starts_with(e, "--") && std::find(elem.begin(), elem.end(), e) != elem.end() ;
0064         if(skip) 
0065         {
0066             printf("SArgs.hh: dedupe skipping %s \n", e.c_str());
0067         }
0068         else 
0069         {
0070             elem.push_back(e);
0071         }
0072     }
0073 } 
0074 
0075 void SArgs::make()
0076 {
0077     argc = elem.size();
0078     argv = new char*[argc];
0079     for(int i=0 ; i < argc ; i++) argv[i] = const_cast<char*>(elem[i].c_str()) ;
0080 } 
0081 
0082 std::string SArgs::desc() const 
0083 {
0084     std::stringstream ss ; 
0085     ss << "SArgs::desc" << std::endl ; 
0086     for(int i=0 ; i < argc ; i++)
0087     {
0088         ss << std::setw(3) << i 
0089            << std::setw(30) << elem[i]
0090            << std::setw(30) << argv[i]
0091            << std::endl ; 
0092     }
0093     std::string s = ss.str(); 
0094     return s ; 
0095 }
0096 void SArgs::dump() const 
0097 {
0098     std::cout << desc(); 
0099 }
0100 
0101 
0102 SArgs::SArgs(int argc_, char** argv_, const char* argforced, const char* opts, bool dedupe )
0103 {
0104     // combine standard arguments with elements split from extra 
0105     std::stringstream ss ; 
0106     ss << ( argforced ? argforced : " " ) ;
0107     ss << " " ;   // need space between commmandline options for proper parsing 
0108     ss << ( opts ? opts : " " ) ;
0109     std::string line = ss.str() ; 
0110 
0111     LOG(LEVEL) << " line [" << line << "]" ; 
0112 
0113     add(argc_, argv_);
0114     addElements(line, dedupe);
0115     make();
0116 
0117     LOG(LEVEL) << desc() ; 
0118 }
0119 
0120 SArgs::SArgs(const char* argv0, const char* argline)
0121 {
0122     // construct standard arguments from argv0 (executable path)
0123     // and argline space delimited string
0124     std::stringstream ss ;  
0125     ss << argv0 << " " << argline ; 
0126     std::string line = ss.str() ; 
0127     addElements(line, false);
0128     make();
0129 }
0130 
0131 bool SArgs::hasArg(const char* arg) const 
0132 {
0133     for(int i=0 ; i < argc ; i++) if(strcmp(argv[i], arg) == 0) return true ; 
0134     return false ;         
0135 }
0136 
0137 std::string SArgs::getArgLine() const 
0138 {
0139     std::stringstream ss ;  
0140     for(int i=0 ; i < argc ; i++) ss << argv[i] << " " ; 
0141     return ss.str();
0142 }
0143 
0144 const char* SArgs::get_arg_after(const char* option, const char* fallback) const
0145 {
0146     for(int i=1 ; i < int(elem.size()) - 1 ; i++ ) 
0147     {
0148         const char* a0 = elem[i].c_str() ; 
0149         const char* a1 = elem[i+1].c_str() ;
0150         if(a0 && strcmp(a0, option) == 0) return a1 ;   
0151     }
0152     return fallback ; 
0153 }
0154 
0155 const char* SArgs::get_first_arg_ending_with(const char* ending, const char* fallback) const
0156 {
0157     for(int i=1 ; i < int(elem.size())  ; i++ ) 
0158     {
0159         const char* arg = elem[i].c_str() ; 
0160         int pos = strlen(arg) - strlen(ending) ;
0161         if( pos > 0 && strncmp(arg + pos, ending, strlen(ending)) == 0 ) return arg ;
0162     }
0163     return fallback ; 
0164 }
0165