Changeset 368 in 3DVCSoftware for branches/HTM-DEV-0.1-dev/source/Lib/TAppCommon


Ignore:
Timestamp:
3 May 2013, 17:16:12 (12 years ago)
Author:
tech
Message:

Fixed erroneously removed parts.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HTM-DEV-0.1-dev/source/Lib/TAppCommon/program_options_lite.h

    r367 r368  
    3737#include <map>
    3838
     39#if H_MV
     40#include <vector>
     41#include <errno.h>
     42#include <cstring>
     43
     44#ifdef WIN32
     45#define strdup _strdup
     46#endif
     47#endif
    3948//! \ingroup TAppCommon
    4049//! \{
     
    136145    }
    137146
     147#if H_MV   
     148    template<>
     149    inline void
     150      Option<char*>::parse(const std::string& arg)
     151    {
     152      opt_storage = arg.empty() ? NULL : strdup(arg.c_str()) ;
     153    }
     154
     155    template<>
     156    inline void
     157      Option< std::vector<char*> >::parse(const std::string& arg)
     158    {
     159      opt_storage.clear();
     160
     161      char* pcStart = (char*) arg.data();     
     162      char* pcEnd = strtok (pcStart," ");
     163
     164      while (pcEnd != NULL)
     165      {
     166        size_t uiStringLength = pcEnd - pcStart;
     167        char* pcNewStr = (char*) malloc( uiStringLength + 1 );
     168        strncpy( pcNewStr, pcStart, uiStringLength);
     169        pcNewStr[uiStringLength] = '\0';
     170        pcStart = pcEnd+1;
     171        pcEnd = strtok (NULL, " ,.-");
     172        opt_storage.push_back( pcNewStr );
     173      }     
     174    }
     175
     176
     177    template<>   
     178    inline void
     179      Option< std::vector<double> >::parse(const std::string& arg)
     180    {
     181      char* pcNextStart = (char*) arg.data();
     182      char* pcEnd = pcNextStart + arg.length();
     183
     184      char* pcOldStart = 0;
     185
     186      size_t iIdx = 0;
     187
     188      while (pcNextStart < pcEnd)
     189      {
     190        errno = 0;
     191
     192        if ( iIdx < opt_storage.size() )
     193        {
     194          opt_storage[iIdx] = strtod(pcNextStart, &pcNextStart);
     195        }
     196        else
     197        {
     198          opt_storage.push_back( strtod(pcNextStart, &pcNextStart)) ;
     199        }
     200        iIdx++;
     201
     202        if ( errno == ERANGE || (pcNextStart == pcOldStart) )
     203        {
     204          std::cerr << "Error Parsing Doubles: `" << arg << "'" << std::endl;
     205          exit(EXIT_FAILURE);   
     206        };   
     207        while( (pcNextStart < pcEnd) && ( *pcNextStart == ' ' || *pcNextStart == '\t' || *pcNextStart == '\r' ) ) pcNextStart++; 
     208        pcOldStart = pcNextStart;
     209
     210      }
     211    }
     212
     213    template<>
     214    inline void
     215      Option< std::vector<int> >::parse(const std::string& arg)
     216    {
     217      opt_storage.clear();
     218
     219
     220      char* pcNextStart = (char*) arg.data();
     221      char* pcEnd = pcNextStart + arg.length();
     222
     223      char* pcOldStart = 0;
     224
     225      size_t iIdx = 0;
     226
     227
     228      while (pcNextStart < pcEnd)
     229      {
     230
     231        if ( iIdx < opt_storage.size() )
     232        {
     233          opt_storage[iIdx] = (int) strtol(pcNextStart, &pcNextStart,10);
     234        }
     235        else
     236        {
     237          opt_storage.push_back( (int) strtol(pcNextStart, &pcNextStart,10)) ;
     238        }
     239        iIdx++;
     240        if ( errno == ERANGE || (pcNextStart == pcOldStart) )
     241        {
     242          std::cerr << "Error Parsing Integers: `" << arg << "'" << std::endl;
     243          exit(EXIT_FAILURE);
     244        };   
     245        while( (pcNextStart < pcEnd) && ( *pcNextStart == ' ' || *pcNextStart == '\t' || *pcNextStart == '\r' ) ) pcNextStart++; 
     246        pcOldStart = pcNextStart;
     247      }
     248    }
     249
     250
     251    template<>
     252    inline void
     253      Option< std::vector<bool> >::parse(const std::string& arg)
     254    {
     255      char* pcNextStart = (char*) arg.data();
     256      char* pcEnd = pcNextStart + arg.length();
     257
     258      char* pcOldStart = 0;
     259
     260      size_t iIdx = 0;
     261
     262      while (pcNextStart < pcEnd)
     263      {
     264        if ( iIdx < opt_storage.size() )
     265        {
     266          opt_storage[iIdx] = (strtol(pcNextStart, &pcNextStart,10) != 0);
     267        }
     268        else
     269        {
     270          opt_storage.push_back(strtol(pcNextStart, &pcNextStart,10) != 0) ;
     271        }
     272        iIdx++;
     273
     274        if ( errno == ERANGE || (pcNextStart == pcOldStart) )
     275        {
     276          std::cerr << "Error Parsing Bools: `" << arg << "'" << std::endl;
     277          exit(EXIT_FAILURE);
     278        };   
     279        while( (pcNextStart < pcEnd) && ( *pcNextStart == ' ' || *pcNextStart == '\t' || *pcNextStart == '\r' ) ) pcNextStart++; 
     280        pcOldStart = pcNextStart;
     281      }
     282    }
     283#endif
    138284    /** Option class for argument handling using a user provided function */
    139285    struct OptionFunc : public OptionBase
     
    210356      }
    211357     
     358#if H_MV
     359      template<typename T>
     360      OptionSpecific&
     361        operator()(const std::string& name, std::vector<T>& storage, T default_val, unsigned uiMaxNum, const std::string& desc = "" )
     362      {
     363        std::string cNameBuffer;
     364        std::string cDescriptionBuffer;
     365
     366        cNameBuffer       .resize( name.size() + 10 );
     367        cDescriptionBuffer.resize( desc.size() + 10 );
     368
     369        storage.resize(uiMaxNum);
     370        for ( unsigned int uiK = 0; uiK < uiMaxNum; uiK++ )
     371        {
     372          // isn't there are sprintf function for string??
     373          sprintf((char*) cNameBuffer.c_str()       ,name.c_str(),uiK,uiK);
     374          sprintf((char*) cDescriptionBuffer.c_str(),desc.c_str(),uiK,uiK);
     375
     376          parent.addOption(new Option<T>( cNameBuffer, (storage[uiK]), default_val, cDescriptionBuffer ));
     377        }
     378
     379        return *this;
     380      }
     381#endif
    212382      /**
    213383       * Add option described by name to the parent Options list,
Note: See TracChangeset for help on using the changeset viewer.