Back to home page

EIC code displayed by LXR

 
 

    


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

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 #pragma once
0021 #include <cstring>
0022 #include <string>
0023 #include <sstream>
0024 #include <iostream>
0025 #include <vector>
0026 
0027 /**
0028 opticks/sysrap/S_get_option.hh
0029 ================================
0030 
0031 Really minimal command line option parsing in this simple header::
0032 
0033     #include "S_get_option.hh"
0034 
0035     int main(int argc, char **argv)
0036     {
0037       const char* size_default = "1200,800" ; 
0038 
0039       int stack = get_option<int>(argc, argv, "--stack", "3000" );   
0040       int width = get_option<int>(argc, argv, "--size,0", size_default ) ;   
0041       int height = get_option<int>(argc, argv, "--size,1", size_default ) ;   
0042 
0043       std::cout << " stack " << stack << std::endl ; 
0044       std::cout << " width [" << width << "]" << std::endl ; 
0045       std::cout << " height [" << height << "]" << std::endl ; 
0046 
0047       return 0
0048     }
0049 
0050 **/
0051 
0052 
0053 const char* get_option_(int argc, char** argv, const char* option, const char* fallback)
0054 {
0055     const char* val = NULL ; 
0056     for(int i=1 ; i < argc - 1 ; i++ ) 
0057     {   
0058         const char* a0 = argv[i] ; 
0059         const char* a1 = argv[i+1] ;
0060         if(a0 && strcmp(a0, option) == 0) val = a1 ;   
0061     }
0062     return val ? val : fallback ; 
0063 }
0064 
0065 template <typename T>
0066 T lexical_cast(const char* par)
0067 {
0068     T var;
0069     std::istringstream iss;
0070     iss.str(par);
0071     iss >> var;
0072     return var;
0073 }
0074 
0075 std::string get_field( const std::string& par, int field=-1, char delim=',')
0076 {
0077     if( field < 0 ) return par ; 
0078     std::vector<std::string> fields ; 
0079     std::stringstream ss ; 
0080     int l = strlen(par.c_str()); 
0081     for(int i=0 ; i < l ; i++)
0082     {
0083          char c = par[i];  
0084          if( c != delim )
0085          {
0086              ss << c ; 
0087          }
0088 
0089          if( c == delim || i == l-1 )
0090          {
0091              std::string s = ss.str();  
0092              fields.push_back( s ); 
0093              ss.str("");
0094              ss.clear();
0095          }   
0096     }
0097     return field < int(fields.size()) ? fields[field] : "" ;  
0098 }
0099 
0100 template <typename T> T get_option( int argc, char** argv, const char* option, const char* fallback  )
0101 {
0102     std::string opt = option ; 
0103     char delim = ',' ; 
0104     
0105     size_t pos = opt.find(delim) ;  
0106     int field(-1) ; 
0107 
0108     //std::cout << " pos " << pos << std::endl ; 
0109     if( pos != std::string::npos ) 
0110     {
0111         std::string s_field = opt.substr(pos+1) ; 
0112         opt = opt.substr(0, pos) ; 
0113         field = lexical_cast<int>( s_field.c_str() ); 
0114 
0115         //std::cout << " opt " << opt << " field " << field << std::endl ; 
0116     } 
0117 
0118     std::string par = get_option_( argc, argv, opt.c_str() , fallback ); 
0119     if( field > -1 ) par = get_field( par, field, delim ); 
0120     return lexical_cast<T>(par.c_str()) ; 
0121 }
0122 
0123