Changeset 1351


Ignore:
Timestamp:
Apr 3, 2011, 8:30:06 PM (12 years ago)
Author:
Peter Johansson
Message:

update to latest libyat. closes #471

Location:
trunk/yat
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/CommandLine.cc

    r1336 r1351  
    1 // $Id: CommandLine.cc 2384 2010-12-22 14:03:36Z peter $
     1// $Id: CommandLine.cc 2458 2011-04-03 15:14:46Z peter $
    22
    33/*
     
    115115      arguments.push_back(argv[i]);
    116116    std::vector<std::string>::iterator arg(arguments.begin());   
     117    std::vector<std::string>::iterator end(arguments.end());   
    117118    stringstream ss(*arg++);
    118119    // keeping string after last /
    119120    while (getline(ss, program_name_,'/')) {}
    120121
     122    bool ok=true;
     123    std::string error_message;
     124    for (; arg!=end; ++arg) {
     125      try {
     126        parse(arg, end);
     127      }
     128      catch (cmd_error& e) {
     129        if (ok) { // we only store first error
     130          ok = false;
     131          error_message = e.what();
     132        }
     133      }
     134    }
     135    // validate all options
    121136    try {
    122       for (; arg!=arguments.end(); ++arg) {
    123         if (is_long_option(*arg)) {
    124           std::string key(arg->substr(2));
    125           std::stringstream ss2(key);
    126           getline(ss2, key, '=');
    127           std::string value;
    128           getline(ss2, value, '\0');
    129           if (!value.empty()){
    130             *arg = value;
    131             *(--arg) = std::string("--")+key;
    132           }         
    133           else
    134             *arg = key;
    135           std::map<std::string, Option*>::const_iterator
    136             iter(long_options_.find(key));
    137           if (iter!=long_options_.end())
    138             iter->second->parse(arg, arguments.end());
    139           else if (key.size()>3 && key.substr(0,3)=="no-") {
    140             iter = long_options_.find(key.substr(3));
    141             if (iter!=long_options_.end())
    142               iter->second->parse(arg, arguments.end());
    143           }           
    144           else if (iter==long_options_.end()) {
    145             std::stringstream ss3;
    146             ss3 << "unrecognized option `" << key << "'\n" << try_help();
    147             throw cmd_error(ss3.str());
    148           }
    149         }
    150         else if (is_short_option(*arg)) {
    151           size_t size=arg->size();
    152           for (size_t i=1; i<size; ++i){
    153             std::map<char, Option*>::const_iterator
    154               iter(short_options_.find((*arg)[i]));
    155             if (iter==short_options_.end()) {
    156               std::stringstream ss2;
    157               ss2 << ": invalid option -- " << (*arg)[i] << "\n"
    158                   << try_help() << "\n";
    159               throw cmd_error(ss2.str());
    160             }       
    161             else
    162               iter->second->parse(arg, arguments.end());
    163           }
    164         }
    165         else {
    166           free_arg_.push_back(*arg);
    167           if (free_arg_.size()>free_arg_max_) {
    168             std::stringstream ss2;
    169             ss2 << ": invalid option -- " << *arg << "\n"
    170                 << try_help() << "\n";
    171             throw cmd_error(ss2.str());
    172           }
    173         }
    174       }
    175       for_each(options_.begin(),options_.end(),
     137      for_each(options_.begin(), options_.end(),
    176138               std::mem_fun(&Option::validate));
    177139    }
    178     catch (cmd_error& e){
     140    catch (cmd_error& e) {
     141      if (ok) { // we only store first error
     142        ok = false;
     143        error_message = e.what();
     144      }
     145    }
     146   
     147    if (!ok) {
     148      std::stringstream ss;
     149      ss << program_name_ << ": " << error_message;
     150      throw cmd_error(ss.str());
     151    }
     152  }
     153 
     154
     155  void CommandLine::parse_long(std::vector<std::string>::iterator& arg,
     156                               std::vector<std::string>::iterator& last)
     157  {
     158    std::string key(arg->substr(2));
     159    std::stringstream ss2(key);
     160    getline(ss2, key, '=');
     161    std::string value;
     162    getline(ss2, value, '\0');
     163    if (!value.empty()){
     164      *arg = value;
     165      *(--arg) = std::string("--")+key;
     166    }         
     167    else
     168      *arg = key;
     169    std::map<std::string, Option*>::const_iterator
     170      iter(long_options_.find(key));
     171    if (iter!=long_options_.end())
     172      iter->second->parse(arg, last);
     173    else if (key.size()>3 && key.substr(0,3)=="no-") {
     174      iter = long_options_.find(key.substr(3));
     175      if (iter!=long_options_.end())
     176        iter->second->parse(arg, last);
     177    }           
     178    else if (iter==long_options_.end()) {
     179      std::stringstream ss3;
     180      ss3 << "unrecognized option `" << key << "'\n" << try_help();
     181      throw cmd_error(ss3.str());
     182    }
     183  }
     184
     185  void CommandLine::parse_short(std::vector<std::string>::iterator& arg,
     186                                std::vector<std::string>::iterator& last)
     187  {
     188    size_t size=arg->size();
     189    for (size_t i=1; i<size; ++i){
     190      std::map<char, Option*>::const_iterator
     191        iter(short_options_.find((*arg)[i]));
     192      if (iter==short_options_.end()) {
     193        std::stringstream ss2;
     194        ss2 << ": invalid option -- " << (*arg)[i] << "\n"
     195            << try_help() << "\n";
     196        throw cmd_error(ss2.str());
     197      }       
     198      else
     199        iter->second->parse(arg, last);
     200    }
     201  }
     202
     203
     204  void CommandLine::parse_free_arg(std::vector<std::string>::iterator& arg,
     205                                   std::vector<std::string>::iterator& last)
     206  {
     207    free_arg_.push_back(*arg);
     208    if (free_arg_.size()>free_arg_max_) {
    179209      std::stringstream ss2;
    180       ss2 << program_name_ << ": " << e.what();
     210      ss2 << ": invalid option -- " << *arg << "\n"
     211          << try_help() << "\n";
    181212      throw cmd_error(ss2.str());
    182213    }
    183      
    184   }
    185 
     214  }
     215
     216 
     217  void CommandLine::parse(std::vector<std::string>::iterator& first,
     218                          std::vector<std::string>::iterator& last)
     219  {
     220    if (is_long_option(*first))
     221      parse_long(first, last);
     222    else if (is_short_option(*first))
     223      parse_short(first, last);
     224    else
     225      parse_free_arg(first, last);
     226
     227  }
     228 
    186229
    187230  bool CommandLine::parsed(void) const
  • trunk/yat/CommandLine.h

    r1336 r1351  
    22#define _theplu_yat_utility_commandline_
    33
    4 //$Id: CommandLine.h 2384 2010-12-22 14:03:36Z peter $
     4//$Id: CommandLine.h 2458 2011-04-03 15:14:46Z peter $
    55
    66/*
    77  Copyright (C) 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
    8   Copyright (C) 2010 Peter Johansson
     8  Copyright (C) 2010, 2011 Peter Johansson
    99
    1010  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    191191    bool is_long_option(std::string str) const;
    192192    bool is_short_option(std::string str) const;
    193 
     193    void parse(std::vector<std::string>::iterator& first,
     194               std::vector<std::string>::iterator& last);
     195    void parse_long(std::vector<std::string>::iterator& first,
     196                    std::vector<std::string>::iterator& last);
     197    void parse_short(std::vector<std::string>::iterator& first,
     198                     std::vector<std::string>::iterator& last);
     199    void parse_free_arg(std::vector<std::string>::iterator& first,
     200                        std::vector<std::string>::iterator& last);
    194201    std::string description_;
    195202    std::vector<std::string> free_arg_;
     
    202209
    203210    // use cond to make doxygen ignore this privat class
    204     /// \cond
     211    /// \cond yat_ignore_this
    205212    struct OptionCompare
    206213    {
  • trunk/yat/Exception.cc

    r1146 r1351  
    1 // $Id: Exception.cc 2303 2010-07-28 17:47:58Z peter $
     1// $Id: Exception.cc 2459 2011-04-03 15:42:44Z peter $
    22
    33/*
     
    3737
    3838  cmd_error::cmd_error(std::string message)
    39     : std::runtime_error(message)
     39    : runtime_error(message)
    4040  {}
    4141
    4242
    4343  errno_error::errno_error(std::string message)
    44     : std::runtime_error(message + strerror(errno))
     44    : runtime_error(message + strerror(errno))
    4545  {}
    4646
    4747
    4848  IO_error::IO_error(std::string message)
    49     : std::runtime_error("IO_error: " + message)
     49    : runtime_error("IO_error: " + message)
    5050  {}
    5151
  • trunk/yat/Exception.cc.patch

    r1307 r1351  
    1 --- Exception.cc.orig 2010-03-06 20:31:52.000000000 -0500
    2 +++ Exception.cc  2010-03-06 20:32:12.000000000 -0500
     1--- Exception.cc.orig 2011-04-03 14:20:22.000000000 -0400
     2+++ Exception.cc  2011-04-03 14:18:09.000000000 -0400
    33@@ -21,8 +21,6 @@
    44 
     
    88-
    99 #include <cerrno>
     10 #include <cstring>
    1011 #include <stdexcept>
    11  #include <string>
    12 @@ -46,17 +44,6 @@
     12@@ -47,17 +45,6 @@
    1313  {}
    1414 
    1515 
    1616- GSL_error::GSL_error(std::string message)
    17 -   : std::runtime_error("GSL_error: " + message)
     17-   : runtime_error("GSL_error: " + message)
    1818- {}
    1919-
    2020-
    2121- GSL_error::GSL_error(std::string message, int gsl_status)
    22 -   : std::runtime_error("GSL_error: " + message + " " +
     22-   : runtime_error("GSL_error: " + message + " " +
    2323-                        gsl_strerror(gsl_status))
    2424- {}
     
    2626-
    2727  IO_error::IO_error(std::string message)
    28     : std::runtime_error("IO_error: " + message)
     28    : runtime_error("IO_error: " + message)
    2929  {}
  • trunk/yat/Exception.h

    r1193 r1351  
    22#define _theplu_yat_utility_exception_
    33
    4 // $Id: Exception.h 2331 2010-10-02 16:07:14Z peter $
     4// $Id: Exception.h 2459 2011-04-03 15:42:44Z peter $
    55
    66/*
     
    5151     \brief Class used for error reported from Commandline or Option.
    5252   */
    53   class cmd_error : public std::runtime_error
     53  class cmd_error : public runtime_error
    5454  {
    5555  public:
     
    6969     \since New in yat 0.7
    7070   */
    71   class errno_error : public std::runtime_error
     71  class errno_error : public runtime_error
    7272  {
    7373  public:
     
    8585     exceptions.
    8686  */
    87   class GSL_error : public std::runtime_error
     87  class GSL_error : public runtime_error
    8888  {
    8989  public:
     
    107107     exceptions.
    108108  */
    109   class IO_error : public std::runtime_error
     109  class IO_error : public runtime_error
    110110  {
    111111  public:
  • trunk/yat/config_public.h.in

    r1027 r1351  
    22#define _theplu_yat_utility_config_public_
    33
    4 // $Id: config_public.h.in 2140 2009-12-31 22:43:29Z peter $
     4// $Id: config_public.h.in 2431 2011-03-06 20:04:31Z peter $
    55
    66/*
    77  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
    8   Copyright (C) 2009 Peter Johansson
     8  Copyright (C) 2009, 2011 Peter Johansson
    99
    1010  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    3131#undef YAT_HAVE_GCC_DEPRECATED
    3232
     33/// Version of yat in string format
     34#undef YAT_VERSION
     35/// First digit in VERSION triplet
     36#undef YAT_MAJOR_VERSION
     37/// Second digit in VERSION triplet
     38#undef YAT_MINOR_VERSION
     39/// Third digit in VERSION triplet or zero if VERSION is MAJOR.MINOR
     40#undef YAT_PATCH_VERSION
     41/// Set to true when releasing tarball
     42#undef YAT_DEV_BUILD
     43
    3344#endif
  • trunk/yat/split.cc

    r1152 r1351  
    1 // $Id: split.cc 2305 2010-08-02 14:14:32Z peter $
     1// $Id: split.cc 2434 2011-03-08 16:52:23Z peter $
    22
    33/*
  • trunk/yat/split.h

    r1152 r1351  
    22#define _theplu_yat_utility_split_
    33
    4 // $Id: split.h 2305 2010-08-02 14:14:32Z peter $
     4// $Id: split.h 2434 2011-03-08 16:52:23Z peter $
    55
    66/*
  • trunk/yat/utility.h

    r1336 r1351  
    22#define _theplu_yat_utility_utility_
    33
    4 // $Id: utility.h 2386 2010-12-22 20:12:49Z peter $
     4// $Id: utility.h 2451 2011-03-28 23:16:14Z peter $
    55
    66/*
     
    88  Copyright (C) 2006 Jari Häkkinen
    99  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
    10   Copyright (C) 2009, 2010 Peter Johansson
     10  Copyright (C) 2009, 2010, 2011 Peter Johansson
    1111
    1212  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    6262                     OutputIterator result);
    6363
     64  /**
     65     \brief convert T to a string
     66
     67     T is supposed to be a numerical type.
     68
     69     \since new in yat 0.8
     70   */
     71  template<typename T>
     72  std::string convert(T input);
    6473
    6574  /**
     
    259268
    260269  // template implementations
     270  template<typename T>
     271  std::string convert(T input)
     272  {
     273    std::ostringstream ss;
     274    ss << input;
     275    return ss.str();
     276  }
     277
     278
    261279  template<typename T>
    262280  T convert(const std::string& s)
Note: See TracChangeset for help on using the changeset viewer.