[Lemon-commits] deba: r3242 - in lemon/trunk: lemon tools
Lemon SVN
svn at lemon.cs.elte.hu
Wed Mar 14 19:01:05 CET 2007
Author: deba
Date: Wed Mar 14 19:01:04 2007
New Revision: 3242
Modified:
lemon/trunk/lemon/arg_parser.cc
lemon/trunk/lemon/arg_parser.h
lemon/trunk/lemon/error.h
lemon/trunk/tools/dim_to_lgf.cc
lemon/trunk/tools/lgf-gen.cc
Log:
dim_to_lgf
- use the argparser class
arg_parser improvments
- usage of assert in cc
- const char** argv
error
- handling the string parameter
Modified: lemon/trunk/lemon/arg_parser.cc
==============================================================================
--- lemon/trunk/lemon/arg_parser.cc (original)
+++ lemon/trunk/lemon/arg_parser.cc Wed Mar 14 19:01:04 2007
@@ -26,8 +26,8 @@
exit(1);
}
- ArgParser::ArgParser(int argc, char **argv) :_argc(argc), _argv(argv),
- _command_name(argv[0]) {
+ ArgParser::ArgParser(int argc, const char **argv) :_argc(argc), _argv(argv),
+ _command_name(argv[0]) {
funcOption("-help","Print a short help message",_showHelp,this);
synonym("help","-help");
synonym("h","-help");
@@ -134,8 +134,8 @@
}
ArgParser &ArgParser::refOption(const std::string &name,
- const std::string &help,
- double &ref, bool obl)
+ const std::string &help,
+ double &ref, bool obl)
{
ParData p;
p.double_p=&ref;
@@ -148,8 +148,8 @@
}
ArgParser &ArgParser::refOption(const std::string &name,
- const std::string &help,
- bool &ref, bool obl)
+ const std::string &help,
+ bool &ref, bool obl)
{
ParData p;
p.bool_p=&ref;
@@ -197,13 +197,12 @@
const std::string &opt)
{
Opts::iterator i = _opts.find(opt);
- if(i==_opts.end()) throw LogicError();
- else if(i->second.ingroup) throw LogicError();
- else {
- GroupData &g=_groups[group];
- g.opts.push_back(opt);
- i->second.ingroup=true;
- }
+ LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'");
+ LEMON_ASSERT(!(i->second.ingroup),
+ "Option already in option group: '"+opt+"'");
+ GroupData &g=_groups[group];
+ g.opts.push_back(opt);
+ i->second.ingroup=true;
return *this;
}
@@ -219,16 +218,14 @@
{
Opts::iterator o = _opts.find(opt);
Opts::iterator s = _opts.find(syn);
- if(o==_opts.end()||s!=_opts.end())
- throw LogicError();
- else {
- ParData p;
- p.help=opt;
- p.mandatory=false;
- p.syn=true;
- _opts[syn]=p;
- o->second.has_syn=true;
- }
+ LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'");
+ LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'");
+ ParData p;
+ p.help=opt;
+ p.mandatory=false;
+ p.syn=true;
+ _opts[syn]=p;
+ o->second.has_syn=true;
return *this;
}
Modified: lemon/trunk/lemon/arg_parser.h
==============================================================================
--- lemon/trunk/lemon/arg_parser.h (original)
+++ lemon/trunk/lemon/arg_parser.h Wed Mar 14 19:01:04 2007
@@ -47,7 +47,7 @@
protected:
int _argc;
- char **_argv;
+ const char **_argv;
enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
@@ -107,7 +107,7 @@
public:
///\e
- ArgParser(int argc, char **argv);
+ ArgParser(int argc, const char **argv);
~ArgParser();
Modified: lemon/trunk/lemon/error.h
==============================================================================
--- lemon/trunk/lemon/error.h (original)
+++ lemon/trunk/lemon/error.h Wed Mar 14 19:01:04 2007
@@ -475,9 +475,11 @@
template <typename Exception>
- inline void assert_fail(const char *file, int line, const char *func,
- Exception exception, const char *assertion = 0,
- bool do_abort=true)
+ inline void assert_fail(const char *file, int line,
+ const char *func,
+ Exception exception,
+ const char *assertion = 0,
+ bool do_abort=true)
{
using namespace std;
cerr << file << ":" << line << ": ";
@@ -510,6 +512,16 @@
abort();
}
+ template <>
+ inline void assert_fail<std::string>(const char *file, int line,
+ const char *func,
+ std::string message,
+ const char *assertion,
+ bool do_abort)
+ {
+ assert_fail(file, line, func, message.c_str(), assertion, do_abort);
+ }
+
template <typename Exception>
inline void assert_fail_failure(const char *file, int line, const char *func,
Exception exception,
@@ -521,14 +533,24 @@
template <>
inline void assert_fail_failure<const char *>(const char *file, int line,
- const char *func,
- const char *message,
- const char *assertion,
- bool)
+ const char *func,
+ const char *message,
+ const char *assertion,
+ bool)
{
throw AssertionFailedError(file, line, func, message, assertion);
}
+ template <>
+ inline void assert_fail_failure<std::string>(const char *file, int line,
+ const char *func,
+ std::string message,
+ const char *assertion,
+ bool)
+ {
+ assert_fail_failure(file, line, func, message.c_str(), assertion, true);
+ }
+
template <typename Exception>
inline void assert_fail_exception(const char *file, int line, const char *func,
Exception exception,
@@ -547,6 +569,16 @@
throw AssertionFailedError(file, line, func, message, assertion);
}
+ template <>
+ inline void assert_fail_exception<std::string>(const char *file, int line,
+ const char *func,
+ std::string message,
+ const char *assertion,
+ bool)
+ {
+ assert_fail_exception(file, line, func, message.c_str(), assertion, true);
+ }
+
/// @}
}
Modified: lemon/trunk/tools/dim_to_lgf.cc
==============================================================================
--- lemon/trunk/tools/dim_to_lgf.cc (original)
+++ lemon/trunk/tools/dim_to_lgf.cc Wed Mar 14 19:01:04 2007
@@ -18,7 +18,7 @@
///\ingroup demos
///\file
-///\brief DIMACS to LGF converter (demo).
+///\brief DIMACS to LGF converter.
///
/// This program converts various DIMACS formats to the LEMON Graph Format
/// (LGF).
@@ -33,32 +33,11 @@
#include <lemon/dimacs.h>
#include <lemon/graph_writer.h>
+#include <lemon/arg_parser.h>
+
using namespace std;
using namespace lemon;
-const char* versionString =
-"dim_to_lgf - part of lemon library\n";
-
-const char* helpString =
-"DIMACS to LGF converter\n"
-"Usage: dim_to_lgf [OPTIONS]\n"
-"\n"
-"Examples:\n"
-" dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n"
-"\n"
-"Options:\n"
-" -i FILE, --input FILE use FILE as input instead of standard input\n"
-" -o FILE, --output FILE use FILE as output instead of standard output\n"
-" -t TYPE, --type TYPE set up the type of the graph\n"
-" Possible types:\n"
-" mincostflow\n"
-" maxflow (default)\n"
-" shortestpath\n"
-" capacitated\n"
-" plain\n"
-" -v, --version shows the version of the converter\n"
-" -h, --help shows the help of the converter\n";
-
int main(int argc, const char *argv[]) {
typedef SmartGraph Graph;
@@ -73,66 +52,46 @@
std::string outputName;
std::string typeName;
- bool help = false;
- bool version = false;
-
- for (int arg = 1; arg < argc; ++arg) {
- if (strcmp(argv[arg], "--type") == 0 ||
- strcmp(argv[arg], "-t") == 0) {
- if (!typeName.empty()) {
- cerr << "Multiple type description" << endl;
- return -1;
- }
- if (arg + 1 == argc) {
- cerr << "Parameter without value" << endl;
- return -1;
- }
- typeName = argv[++arg];
- }
- else if (strcmp(argv[arg], "--input") == 0 ||
- strcmp(argv[arg], "-i") == 0) {
- if (!inputName.empty()) {
- cerr << "Multiple input description" << endl;
- return -1;
- }
- if (arg + 1 == argc) {
- cerr << "Parameter without value" << endl;
- return -1;
- }
- inputName = argv[++arg];
- }
- else if (strcmp(argv[arg], "--output") == 0 ||
- strcmp(argv[arg], "-o") == 0) {
- if (!outputName.empty()) {
- cerr << "Multiple input description" << endl;
- return -1;
- }
- if (arg + 1 == argc) {
- cerr << "Parameter without value" << endl;
- return -1;
- }
- outputName = argv[++arg];
- } else if (strcmp(argv[arg], "--help") == 0 ||
- strcmp(argv[arg], "-h") == 0) {
- help = true;
- } else if (strcmp(argv[arg], "--version") == 0 ||
- strcmp(argv[arg], "-v") == 0) {
- version = true;
- } else {
- cerr << "Invalid option: " << argv[arg] << endl;
- return -1;
- }
- }
-
- if (version) {
- cout << versionString;
- }
- if (help) {
- cout << helpString;
- }
- if (help || version) {
- return 0;
- }
+ bool mincostflow;
+ bool maxflow;
+ bool shortestpath;
+ bool capacitated;
+ bool plain;
+
+ bool version;
+
+ ArgParser ap(argc, argv);
+ ap.refOption("-input",
+ "use FILE as input instead of standard input",
+ inputName).synonym("i", "-input")
+ .refOption("-output",
+ "use FILE as output instead of standard output",
+ outputName).synonym("o", "-output")
+ .refOption("-mincostflow",
+ "set the type of the graph to \"mincostflow\" graph",
+ mincostflow)
+ .optionGroup("type", "-mincostflow").synonym("mcf", "-mincostflow")
+ .refOption("-maxflow",
+ "set the type of the graph to \"maxflow\" graph",
+ maxflow)
+ .optionGroup("type", "-maxflow").synonym("mf", "-maxflow")
+ .refOption("-shortestpath",
+ "set the type of the graph to \"shortestpath\" graph",
+ shortestpath)
+ .optionGroup("type", "-shortestpath").synonym("sp", "-shortestpath")
+ .refOption("-capacitated",
+ "set the type of the graph to \"capacitated\" graph",
+ capacitated)
+ .optionGroup("type", "-capacitated").synonym("cap", "-capacitated")
+ .refOption("-plain",
+ "set the type of the graph to \"plain\" graph",
+ plain)
+ .optionGroup("type", "-plain").synonym("pl", "-plain")
+ .onlyOneGroup("type")
+ .mandatoryGroup("type")
+ .refOption("-version", "show version information", version)
+ .synonym("v", "-version")
+ .run();
ifstream input;
if (!inputName.empty()) {
@@ -154,11 +113,7 @@
}
ostream& os = (outputName.empty() ? cout : output);
- if (typeName.empty()) {
- typeName = "maxflow";
- }
-
- if (typeName == "mincostflow") {
+ if (mincostflow) {
Graph graph;
Node s, t;
DoubleMap cost(graph), capacity(graph);
@@ -169,7 +124,7 @@
writeNode("target", t).
writeEdgeMap("cost", cost).
run();
- } else if (typeName == "maxflow") {
+ } else if (maxflow) {
Graph graph;
Node s, t;
DoubleMap capacity(graph);
@@ -179,7 +134,7 @@
writeNode("source", s).
writeNode("target", t).
run();
- } else if (typeName == "shortestpath") {
+ } else if (shortestpath) {
Graph graph;
Node s;
DoubleMap capacity(graph);
@@ -188,14 +143,14 @@
writeEdgeMap("capacity", capacity).
writeNode("source", s).
run();
- } else if (typeName == "capacitated") {
+ } else if (capacitated) {
Graph graph;
DoubleMap capacity(graph);
readDimacs(is, graph, capacity);
GraphWriter<Graph>(os, graph).
writeEdgeMap("capacity", capacity).
run();
- } else if (typeName == "plain") {
+ } else if (plain) {
Graph graph;
readDimacs(is, graph);
GraphWriter<Graph>(os, graph).run();
Modified: lemon/trunk/tools/lgf-gen.cc
==============================================================================
--- lemon/trunk/tools/lgf-gen.cc (original)
+++ lemon/trunk/tools/lgf-gen.cc Wed Mar 14 19:01:04 2007
@@ -279,7 +279,7 @@
-int main(int argc,char **argv)
+int main(int argc,const char **argv)
{
ArgParser ap(argc,argv);
More information about the Lemon-commits
mailing list