Ignore:
Timestamp:
1 Sep 2013, 22:47:26 (12 years ago)
Author:
tech
Message:

Merged DEV-2.0-dev0@604.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/Lib/TAppCommon/program_options_lite.h

    r56 r608  
    44 * granted under this license. 
    55 *
    6  * Copyright (c) 2010-2012, ITU/ISO/IEC
     6 * Copyright (c) 2010-2013, ITU/ISO/IEC
    77 * All rights reserved.
    88 *
     
    3434#include <sstream>
    3535#include <string>
    36 #include <vector>
    3736#include <list>
    3837#include <map>
    39 #include <stdlib.h>
     38#include  "../TLibCommon/TypeDef.h"
     39
     40#if H_MV
     41#include <vector>
    4042#include <errno.h>
    4143#include <cstring>
    42 #include <math.h>
    4344
    4445#ifdef WIN32
    4546#define strdup _strdup
    4647#endif
    47 
     48#endif
    4849//! \ingroup TAppCommon
    4950//! \{
     
    5657    struct Options;
    5758   
     59    struct ParseFailure : public std::exception
     60    {
     61      ParseFailure(std::string arg0, std::string val0) throw()
     62      : arg(arg0), val(val0)
     63      {}
     64
     65      ~ParseFailure() throw() {};
     66
     67      std::string arg;
     68      std::string val;
     69
     70      const char* what() const throw() { return "Option Parse Failure"; }
     71    };
     72
    5873    void doHelp(std::ostream& out, Options& opts, unsigned columns = 80);
    5974    unsigned parseGNU(Options& opts, unsigned argc, const char* argv[]);
     
    7186    struct OptionBase
    7287    {
     88#if H_MV     
     89      OptionBase(const std::string& name, const std::string& desc, bool duplicate = false)
     90        : opt_string(name), opt_desc(desc), opt_duplicate(duplicate)
     91#else
    7392      OptionBase(const std::string& name, const std::string& desc)
    7493      : opt_string(name), opt_desc(desc)
     94#endif
    7595      {};
    7696     
     
    84104      std::string opt_string;
    85105      std::string opt_desc;
     106#if H_MV
     107      bool        opt_duplicate;
     108#endif
    86109    };
    87110   
     
    90113    struct Option : public OptionBase
    91114    {
     115#if H_MV
     116      Option(const std::string& name, T& storage, T default_val, const std::string& desc, bool duplicate = false)
     117        : OptionBase(name, desc, duplicate), opt_storage(storage), opt_default_val(default_val)
     118#else
    92119      Option(const std::string& name, T& storage, T default_val, const std::string& desc)
    93120      : OptionBase(name, desc), opt_storage(storage), opt_default_val(default_val)
     121#endif
    94122      {}
    95123     
     
    111139    {
    112140      std::istringstream arg_ss (arg,std::istringstream::in);
    113       arg_ss >> opt_storage;
     141      arg_ss.exceptions(std::ios::failbit);
     142      try
     143      {
     144        arg_ss >> opt_storage;
     145      }
     146      catch (...)
     147      {
     148        throw ParseFailure(opt_string, arg);
     149      }
    114150    }
    115151   
     
    122158      opt_storage = arg;
    123159    }
    124    
     160
     161#if H_MV   
    125162    template<>
    126163    inline void
     
    258295      }
    259296    }
    260 
     297#endif
    261298    /** Option class for argument handling using a user provided function */
    262299    struct OptionFunc : public OptionBase
     
    333370      }
    334371     
     372#if H_MV
    335373      template<typename T>
    336374      OptionSpecific&
     
    338376      {
    339377        std::string cNameBuffer;
    340         std::string cDescriptionBuffer;
     378        std::string cDescBuffer;
    341379
    342380        cNameBuffer       .resize( name.size() + 10 );
    343         cDescriptionBuffer.resize( desc.size() + 10 );
     381        cDescBuffer.resize( desc.size() + 10 );
    344382
    345383        storage.resize(uiMaxNum);
    346384        for ( unsigned int uiK = 0; uiK < uiMaxNum; uiK++ )
    347385        {
     386          Bool duplicate = (uiK != 0);
    348387          // isn't there are sprintf function for string??
    349388          sprintf((char*) cNameBuffer.c_str()       ,name.c_str(),uiK,uiK);
    350           sprintf((char*) cDescriptionBuffer.c_str(),desc.c_str(),uiK,uiK);
    351 
    352           parent.addOption(new Option<T>( cNameBuffer, (storage[uiK]), default_val, cDescriptionBuffer ));
     389
     390          if ( !duplicate )
     391          {         
     392            sprintf((char*) cDescBuffer.c_str(),desc.c_str(),uiK,uiK);
     393          }
     394
     395          cNameBuffer.resize( std::strlen(cNameBuffer.c_str()) ); 
     396          cDescBuffer.resize( std::strlen(cDescBuffer.c_str()) );
     397         
     398
     399          parent.addOption(new Option<T>( cNameBuffer, (storage[uiK]), default_val, cDescBuffer, duplicate ));
    353400        }
    354401
    355402        return *this;
    356403      }
    357 
     404#endif
    358405      /**
    359406       * Add option described by name to the parent Options list,
Note: See TracChangeset for help on using the changeset viewer.