Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #pragma once
0002 
0003 /**
0004 SGLM_Parse : minimal command string parsing into key,val,opt strings
0005 -----------------------------------------------------------------------
0006 
0007 Old opticks used boost-program-options for liveline parsing, thats a no-no
0008 as dont need to be all that general, and dont need so much flexibility
0009 and dont have too many keys. Can simply use a big if to route config 
0010 to the right place::
0011 
0012     --near 777 --far 7777 --tmin 0.1 --tmax 100 --eye 0,10,0.1 --look 0,0,0
0013 
0014 Also can require tokens to always be preceded by "--" "--token value" 
0015 or "--switch --another_switch"
0016 
0017 1. split the cmd into elements delimited by spaces
0018 2. iterate thru the elements in pairs collecting 
0019    into key,val,opt vectors 
0020 
0021 **/
0022 
0023 #include "sstr.h"
0024 
0025 struct SGLM_Parse
0026 {
0027     std::vector<std::string> key ; 
0028     std::vector<std::string> val ; 
0029     std::vector<std::string> opt ; 
0030 
0031     static bool IsKey(const char* str); 
0032     SGLM_Parse(const char* cmd);
0033     std::string desc() const ; 
0034 };
0035 
0036 inline bool SGLM_Parse::IsKey(const char* str) // static
0037 {
0038     return str && strlen(str) > 2 && str[0] == '-' && str[1] == '-' ;  
0039 }
0040 
0041 inline SGLM_Parse::SGLM_Parse(const char* cmd)
0042 {    
0043     //std::cout << "SGLM_Parse::SGLM_Parse [" << ( cmd ? cmd : "-" ) << "]" << std::endl; 
0044     std::vector<std::string> elem ; 
0045     sstr::Split(cmd,' ',elem); 
0046     int num_elem = elem.size(); 
0047 
0048     for(int i=0 ; i < std::max(1, num_elem - 1) ; i++)
0049     {
0050         const char* e0 = i   < num_elem ? elem[i].c_str()   : nullptr ; 
0051         const char* e1 = i+1 < num_elem ? elem[i+1].c_str() : nullptr ; 
0052         bool k0 = IsKey(e0); 
0053         bool k1 = IsKey(e1); 
0054 
0055         if( ( k0 && k1 ) || (k0 && e1 == nullptr)  )   // "--red --blue" OR "--green" 
0056         {
0057             opt.push_back(e0+2); 
0058         }
0059         else if(k0 && !k1)  // eg: --eye 0,10,0.1 
0060         {
0061             key.push_back(e0+2); 
0062             val.push_back(e1); 
0063         } 
0064     }
0065 }
0066 
0067 inline std::string SGLM_Parse::desc() const 
0068 {
0069     std::stringstream ss ; 
0070     ss << "SGLM_Parse::desc"
0071        << " key.size " << key.size()
0072        << " val.size " << val.size()
0073        << " opt.size " << opt.size()
0074        << std::endl 
0075        ;
0076     std::string str = ss.str(); 
0077     return str ; 
0078 }
0079 
0080