dim_to_lgf
authordeba
Wed, 14 Mar 2007 18:01:04 +0000
changeset 2410fe46b61da4e3
parent 2409 fe0a8fe16271
child 2411 5820c6cec654
dim_to_lgf
- use the argparser class
arg_parser improvments
- usage of assert in cc
- const char** argv
error
- handling the string parameter
lemon/arg_parser.cc
lemon/arg_parser.h
lemon/error.h
tools/dim_to_lgf.cc
tools/lgf-gen.cc
     1.1 --- a/lemon/arg_parser.cc	Tue Mar 13 16:32:35 2007 +0000
     1.2 +++ b/lemon/arg_parser.cc	Wed Mar 14 18:01:04 2007 +0000
     1.3 @@ -26,8 +26,8 @@
     1.4      exit(1);
     1.5    }
     1.6  
     1.7 -  ArgParser::ArgParser(int argc, char **argv) :_argc(argc), _argv(argv),
     1.8 -					       _command_name(argv[0]) {
     1.9 +  ArgParser::ArgParser(int argc, const char **argv) :_argc(argc), _argv(argv),
    1.10 +                                                     _command_name(argv[0]) {
    1.11      funcOption("-help","Print a short help message",_showHelp,this);
    1.12      synonym("help","-help");
    1.13      synonym("h","-help");
    1.14 @@ -134,8 +134,8 @@
    1.15    }
    1.16  
    1.17    ArgParser &ArgParser::refOption(const std::string &name,
    1.18 -			       const std::string &help,
    1.19 -			       double &ref, bool obl)
    1.20 +                                  const std::string &help,
    1.21 +                                  double &ref, bool obl)
    1.22    {
    1.23      ParData p;
    1.24      p.double_p=&ref;
    1.25 @@ -148,8 +148,8 @@
    1.26    }
    1.27  
    1.28    ArgParser &ArgParser::refOption(const std::string &name,
    1.29 -			       const std::string &help,
    1.30 -			       bool &ref, bool obl)
    1.31 +                                  const std::string &help,
    1.32 +                                  bool &ref, bool obl)
    1.33    {
    1.34      ParData p;
    1.35      p.bool_p=&ref;
    1.36 @@ -197,13 +197,12 @@
    1.37  				    const std::string &opt)
    1.38    {
    1.39      Opts::iterator i = _opts.find(opt);
    1.40 -    if(i==_opts.end()) throw LogicError();
    1.41 -    else if(i->second.ingroup) throw LogicError();
    1.42 -    else {
    1.43 -      GroupData &g=_groups[group];
    1.44 -      g.opts.push_back(opt);
    1.45 -      i->second.ingroup=true;
    1.46 -    }
    1.47 +    LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'");
    1.48 +    LEMON_ASSERT(!(i->second.ingroup), 
    1.49 +                 "Option already in option group: '"+opt+"'");
    1.50 +    GroupData &g=_groups[group];
    1.51 +    g.opts.push_back(opt);
    1.52 +    i->second.ingroup=true;
    1.53      return *this;
    1.54    }
    1.55  
    1.56 @@ -219,16 +218,14 @@
    1.57    {
    1.58      Opts::iterator o = _opts.find(opt);
    1.59      Opts::iterator s = _opts.find(syn);
    1.60 -    if(o==_opts.end()||s!=_opts.end())
    1.61 -      throw LogicError();
    1.62 -    else {
    1.63 -      ParData p;
    1.64 -      p.help=opt;
    1.65 -      p.mandatory=false;
    1.66 -      p.syn=true;
    1.67 -      _opts[syn]=p;
    1.68 -      o->second.has_syn=true;
    1.69 -    }
    1.70 +    LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'");
    1.71 +    LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'");
    1.72 +    ParData p;
    1.73 +    p.help=opt;
    1.74 +    p.mandatory=false;
    1.75 +    p.syn=true;
    1.76 +    _opts[syn]=p;
    1.77 +    o->second.has_syn=true;
    1.78      return *this;
    1.79    }
    1.80  
     2.1 --- a/lemon/arg_parser.h	Tue Mar 13 16:32:35 2007 +0000
     2.2 +++ b/lemon/arg_parser.h	Wed Mar 14 18:01:04 2007 +0000
     2.3 @@ -47,7 +47,7 @@
     2.4    protected:
     2.5      
     2.6      int _argc;
     2.7 -    char **_argv;
     2.8 +    const char **_argv;
     2.9      
    2.10      enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
    2.11      
    2.12 @@ -107,7 +107,7 @@
    2.13    public:
    2.14  
    2.15      ///\e
    2.16 -    ArgParser(int argc, char **argv);
    2.17 +    ArgParser(int argc, const char **argv);
    2.18  
    2.19      ~ArgParser();
    2.20  
     3.1 --- a/lemon/error.h	Tue Mar 13 16:32:35 2007 +0000
     3.2 +++ b/lemon/error.h	Wed Mar 14 18:01:04 2007 +0000
     3.3 @@ -475,9 +475,11 @@
     3.4  
     3.5  
     3.6    template <typename Exception>
     3.7 -  inline void assert_fail(const char *file, int line, const char *func,
     3.8 -		   Exception exception, const char *assertion = 0,
     3.9 -		   bool do_abort=true)
    3.10 +  inline void assert_fail(const char *file, int line, 
    3.11 +                          const char *func,
    3.12 +                          Exception exception, 
    3.13 +                          const char *assertion = 0,
    3.14 +                          bool do_abort=true)
    3.15    {
    3.16      using namespace std;
    3.17      cerr << file << ":" << line << ": ";
    3.18 @@ -510,6 +512,16 @@
    3.19        abort();
    3.20    }
    3.21  
    3.22 +  template <>
    3.23 +  inline void assert_fail<std::string>(const char *file, int line, 
    3.24 +                                       const char *func,
    3.25 +                                       std::string message, 
    3.26 +                                       const char *assertion,
    3.27 +                                       bool do_abort)
    3.28 +  {
    3.29 +    assert_fail(file, line, func, message.c_str(), assertion, do_abort);
    3.30 +  }
    3.31 +
    3.32    template <typename Exception>
    3.33    inline void assert_fail_failure(const char *file, int line, const char *func,
    3.34  			   Exception exception, 
    3.35 @@ -521,14 +533,24 @@
    3.36  
    3.37    template <>
    3.38    inline void assert_fail_failure<const char *>(const char *file, int line, 
    3.39 -					 const char *func,
    3.40 -					 const char *message, 
    3.41 -					 const char *assertion,
    3.42 -					 bool)
    3.43 +                                                const char *func,
    3.44 +                                                const char *message, 
    3.45 +                                                const char *assertion,
    3.46 +                                                bool)
    3.47    {
    3.48      throw AssertionFailedError(file, line, func, message, assertion);
    3.49    }
    3.50  
    3.51 +  template <>
    3.52 +  inline void assert_fail_failure<std::string>(const char *file, int line, 
    3.53 +                                               const char *func,
    3.54 +                                               std::string message, 
    3.55 +                                               const char *assertion,
    3.56 +                                               bool)
    3.57 +  {
    3.58 +    assert_fail_failure(file, line, func, message.c_str(), assertion, true);
    3.59 +  }
    3.60 +
    3.61    template <typename Exception> 
    3.62    inline void assert_fail_exception(const char *file, int line, const char *func,
    3.63  			     Exception exception, 
    3.64 @@ -547,6 +569,16 @@
    3.65      throw AssertionFailedError(file, line, func, message, assertion);
    3.66    }
    3.67  
    3.68 +  template <>
    3.69 +  inline void assert_fail_exception<std::string>(const char *file, int line, 
    3.70 +                                                 const char *func,
    3.71 +                                                 std::string message, 
    3.72 +                                                 const char *assertion,
    3.73 +                                                 bool)
    3.74 +  {
    3.75 +    assert_fail_exception(file, line, func, message.c_str(), assertion, true);    
    3.76 +  }
    3.77 +
    3.78  /// @}
    3.79  
    3.80  }
     4.1 --- a/tools/dim_to_lgf.cc	Tue Mar 13 16:32:35 2007 +0000
     4.2 +++ b/tools/dim_to_lgf.cc	Wed Mar 14 18:01:04 2007 +0000
     4.3 @@ -18,7 +18,7 @@
     4.4  
     4.5  ///\ingroup demos
     4.6  ///\file
     4.7 -///\brief DIMACS to LGF converter (demo).
     4.8 +///\brief DIMACS to LGF converter.
     4.9  ///
    4.10  /// This program converts various DIMACS formats to the LEMON Graph Format
    4.11  /// (LGF).
    4.12 @@ -33,32 +33,11 @@
    4.13  #include <lemon/dimacs.h>
    4.14  #include <lemon/graph_writer.h>
    4.15  
    4.16 +#include <lemon/arg_parser.h>
    4.17 +
    4.18  using namespace std;
    4.19  using namespace lemon;
    4.20  
    4.21 -const char* versionString =
    4.22 -"dim_to_lgf - part of lemon library\n";
    4.23 -
    4.24 -const char* helpString =
    4.25 -"DIMACS to LGF converter\n"
    4.26 -"Usage: dim_to_lgf [OPTIONS]\n"
    4.27 -"\n"
    4.28 -"Examples:\n"
    4.29 -"  dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n"
    4.30 -"\n"
    4.31 -"Options:\n"
    4.32 -"  -i FILE, --input FILE    use FILE as input instead of standard input\n"
    4.33 -"  -o FILE, --output FILE   use FILE as output instead of standard output\n"
    4.34 -"  -t TYPE, --type TYPE     set up the type of the graph\n"
    4.35 -"                             Possible types:\n"
    4.36 -"                               mincostflow\n"
    4.37 -"                               maxflow (default)\n"
    4.38 -"                               shortestpath\n"
    4.39 -"                               capacitated\n"
    4.40 -"                               plain\n"
    4.41 -"  -v, --version            shows the version of the converter\n"
    4.42 -"  -h, --help               shows the help of the converter\n";
    4.43 -
    4.44  
    4.45  int main(int argc, const char *argv[]) {
    4.46    typedef SmartGraph Graph;
    4.47 @@ -73,66 +52,46 @@
    4.48    std::string outputName;
    4.49    std::string typeName;
    4.50  
    4.51 -  bool help = false;
    4.52 -  bool version = false;
    4.53 +  bool mincostflow;
    4.54 +  bool maxflow;
    4.55 +  bool shortestpath;
    4.56 +  bool capacitated;
    4.57 +  bool plain;
    4.58  
    4.59 -  for (int arg = 1; arg < argc; ++arg) {
    4.60 -    if (strcmp(argv[arg], "--type") == 0 || 
    4.61 -	strcmp(argv[arg], "-t") == 0) {
    4.62 -      if (!typeName.empty()) {
    4.63 -	cerr << "Multiple type description" << endl;
    4.64 -	return -1;
    4.65 -      }
    4.66 -      if (arg + 1 == argc) {
    4.67 -	cerr << "Parameter without value" << endl;
    4.68 -	return -1;
    4.69 -      }
    4.70 -      typeName = argv[++arg];
    4.71 -    }
    4.72 -    else if (strcmp(argv[arg], "--input") == 0 || 
    4.73 -	     strcmp(argv[arg], "-i") == 0) {
    4.74 -      if (!inputName.empty()) {
    4.75 -	cerr << "Multiple input description" << endl;
    4.76 -	return -1;
    4.77 -      }
    4.78 -      if (arg + 1 == argc) {
    4.79 -	cerr << "Parameter without value" << endl;
    4.80 -	return -1;
    4.81 -      }
    4.82 -      inputName = argv[++arg];
    4.83 -    }
    4.84 -    else if (strcmp(argv[arg], "--output") == 0 || 
    4.85 -	     strcmp(argv[arg], "-o") == 0) {
    4.86 -      if (!outputName.empty()) {
    4.87 -	cerr << "Multiple input description" << endl;
    4.88 -	return -1;
    4.89 -      }
    4.90 -      if (arg + 1 == argc) {
    4.91 -	cerr << "Parameter without value" << endl;
    4.92 -	return -1;
    4.93 -      }
    4.94 -      outputName = argv[++arg];
    4.95 -    } else if (strcmp(argv[arg], "--help") == 0 ||
    4.96 -	       strcmp(argv[arg], "-h") == 0) {
    4.97 -      help = true;
    4.98 -    } else if (strcmp(argv[arg], "--version") == 0 ||
    4.99 -	       strcmp(argv[arg], "-v") == 0) {
   4.100 -      version = true;
   4.101 -    } else {
   4.102 -      cerr << "Invalid option: " << argv[arg] << endl;
   4.103 -      return -1;
   4.104 -    }
   4.105 -  }
   4.106 +  bool version;
   4.107  
   4.108 -  if (version) {
   4.109 -    cout << versionString;
   4.110 -  }
   4.111 -  if (help) {
   4.112 -    cout << helpString;
   4.113 -  }
   4.114 -  if (help || version) {
   4.115 -    return 0;
   4.116 -  }
   4.117 +  ArgParser ap(argc, argv);
   4.118 +  ap.refOption("-input", 
   4.119 +               "use FILE as input instead of standard input", 
   4.120 +               inputName).synonym("i", "-input")
   4.121 +    .refOption("-output", 
   4.122 +               "use FILE as output instead of standard output", 
   4.123 +               outputName).synonym("o", "-output")
   4.124 +    .refOption("-mincostflow", 
   4.125 +               "set the type of the graph to \"mincostflow\" graph", 
   4.126 +               mincostflow)
   4.127 +    .optionGroup("type", "-mincostflow").synonym("mcf", "-mincostflow")
   4.128 +    .refOption("-maxflow", 
   4.129 +               "set the type of the graph to \"maxflow\" graph", 
   4.130 +               maxflow)
   4.131 +    .optionGroup("type", "-maxflow").synonym("mf", "-maxflow")
   4.132 +    .refOption("-shortestpath", 
   4.133 +               "set the type of the graph to \"shortestpath\" graph", 
   4.134 +               shortestpath)
   4.135 +    .optionGroup("type", "-shortestpath").synonym("sp", "-shortestpath")
   4.136 +    .refOption("-capacitated", 
   4.137 +               "set the type of the graph to \"capacitated\" graph", 
   4.138 +               capacitated)
   4.139 +    .optionGroup("type", "-capacitated").synonym("cap", "-capacitated")
   4.140 +    .refOption("-plain", 
   4.141 +               "set the type of the graph to \"plain\" graph", 
   4.142 +               plain)
   4.143 +    .optionGroup("type", "-plain").synonym("pl", "-plain")
   4.144 +    .onlyOneGroup("type")
   4.145 +    .mandatoryGroup("type")
   4.146 +    .refOption("-version", "show version information", version)
   4.147 +    .synonym("v", "-version")
   4.148 +    .run();
   4.149  
   4.150    ifstream input;
   4.151    if (!inputName.empty()) {
   4.152 @@ -154,11 +113,7 @@
   4.153    }
   4.154    ostream& os = (outputName.empty() ? cout : output);
   4.155  
   4.156 -  if (typeName.empty()) {
   4.157 -    typeName = "maxflow";
   4.158 -  }
   4.159 -
   4.160 -  if (typeName == "mincostflow") {
   4.161 +  if (mincostflow) {
   4.162      Graph graph;
   4.163      Node s, t;
   4.164      DoubleMap cost(graph), capacity(graph);
   4.165 @@ -169,7 +124,7 @@
   4.166        writeNode("target", t).
   4.167        writeEdgeMap("cost", cost).
   4.168        run();
   4.169 -  } else if (typeName == "maxflow") {
   4.170 +  } else if (maxflow) {
   4.171      Graph graph;
   4.172      Node s, t;
   4.173      DoubleMap capacity(graph);
   4.174 @@ -179,7 +134,7 @@
   4.175        writeNode("source", s).
   4.176        writeNode("target", t).
   4.177        run();
   4.178 -  } else if (typeName == "shortestpath") {
   4.179 +  } else if (shortestpath) {
   4.180      Graph graph;
   4.181      Node s;
   4.182      DoubleMap capacity(graph);
   4.183 @@ -188,14 +143,14 @@
   4.184        writeEdgeMap("capacity", capacity).
   4.185        writeNode("source", s).
   4.186        run();
   4.187 -  } else if (typeName == "capacitated") {
   4.188 +  } else if (capacitated) {
   4.189      Graph graph;
   4.190      DoubleMap capacity(graph);
   4.191      readDimacs(is, graph, capacity);
   4.192      GraphWriter<Graph>(os, graph).
   4.193        writeEdgeMap("capacity", capacity).
   4.194        run();
   4.195 -  } else if (typeName == "plain") {
   4.196 +  } else if (plain) {
   4.197      Graph graph;
   4.198      readDimacs(is, graph);
   4.199      GraphWriter<Graph>(os, graph).run();
     5.1 --- a/tools/lgf-gen.cc	Tue Mar 13 16:32:35 2007 +0000
     5.2 +++ b/tools/lgf-gen.cc	Wed Mar 14 18:01:04 2007 +0000
     5.3 @@ -279,7 +279,7 @@
     5.4  
     5.5  
     5.6  
     5.7 -int main(int argc,char **argv) 
     5.8 +int main(int argc,const char **argv) 
     5.9  {
    5.10    ArgParser ap(argc,argv);
    5.11