1 | #include <iostream> |
---|
2 | #include <fstream> |
---|
3 | #include <cstring> |
---|
4 | |
---|
5 | #include <lemon/smart_graph.h> |
---|
6 | #include <lemon/dimacs.h> |
---|
7 | #include <lemon/graph_writer.h> |
---|
8 | |
---|
9 | using namespace std; |
---|
10 | using namespace lemon; |
---|
11 | |
---|
12 | const char* versionString = |
---|
13 | "dim_to_lgf - part of lemon library\n"; |
---|
14 | |
---|
15 | const char* helpString = |
---|
16 | "Dimacs to LGF converter\n" |
---|
17 | "Usage: dim_to_lgf [OPTIONS]\n" |
---|
18 | "\n" |
---|
19 | "Examples:\n" |
---|
20 | " dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n" |
---|
21 | "\n" |
---|
22 | "Options:\n" |
---|
23 | " -i FILE, --input FILE use FILE as input instead of standard input\n" |
---|
24 | " -o FILE, --output FILE use FILE as output instead of standard output\n" |
---|
25 | " -t TYPE, --type TYPE set up the type of the graph\n" |
---|
26 | " Possible types:\n" |
---|
27 | " mincostflow\n" |
---|
28 | " maxflow (default)\n" |
---|
29 | " shortestpath\n" |
---|
30 | " capacitated\n" |
---|
31 | " plain\n" |
---|
32 | " -v, --version shows the version of the converter\n" |
---|
33 | " -h, --help shows the help of the converter\n"; |
---|
34 | |
---|
35 | |
---|
36 | int main(int argc, const char *argv[]) { |
---|
37 | typedef SmartGraph Graph; |
---|
38 | |
---|
39 | typedef Graph::Edge Edge; |
---|
40 | typedef Graph::Node Node; |
---|
41 | typedef Graph::EdgeIt EdgeIt; |
---|
42 | typedef Graph::NodeIt NodeIt; |
---|
43 | typedef Graph::EdgeMap<string> StringMap; |
---|
44 | |
---|
45 | std::string inputName; |
---|
46 | std::string outputName; |
---|
47 | std::string typeName; |
---|
48 | |
---|
49 | bool help = false; |
---|
50 | bool version = false; |
---|
51 | |
---|
52 | for (int arg = 1; arg < argc; ++arg) { |
---|
53 | if (strcmp(argv[arg], "--type") == 0 || |
---|
54 | strcmp(argv[arg], "-t") == 0) { |
---|
55 | if (!typeName.empty()) { |
---|
56 | cerr << "Multiple type description" << endl; |
---|
57 | return -1; |
---|
58 | } |
---|
59 | if (arg + 1 == argc) { |
---|
60 | cerr << "Parameter without value" << endl; |
---|
61 | return -1; |
---|
62 | } |
---|
63 | typeName = argv[++arg]; |
---|
64 | } |
---|
65 | else if (strcmp(argv[arg], "--input") == 0 || |
---|
66 | strcmp(argv[arg], "-i") == 0) { |
---|
67 | if (!inputName.empty()) { |
---|
68 | cerr << "Multiple input description" << endl; |
---|
69 | return -1; |
---|
70 | } |
---|
71 | if (arg + 1 == argc) { |
---|
72 | cerr << "Parameter without value" << endl; |
---|
73 | return -1; |
---|
74 | } |
---|
75 | inputName = argv[++arg]; |
---|
76 | } |
---|
77 | else if (strcmp(argv[arg], "--output") == 0 || |
---|
78 | strcmp(argv[arg], "-o") == 0) { |
---|
79 | if (!outputName.empty()) { |
---|
80 | cerr << "Multiple input description" << endl; |
---|
81 | return -1; |
---|
82 | } |
---|
83 | if (arg + 1 == argc) { |
---|
84 | cerr << "Parameter without value" << endl; |
---|
85 | return -1; |
---|
86 | } |
---|
87 | outputName = argv[++arg]; |
---|
88 | } else if (strcmp(argv[arg], "--help") == 0 || |
---|
89 | strcmp(argv[arg], "-h") == 0) { |
---|
90 | help = true; |
---|
91 | } else if (strcmp(argv[arg], "--version") == 0 || |
---|
92 | strcmp(argv[arg], "-v") == 0) { |
---|
93 | version = true; |
---|
94 | } else { |
---|
95 | cerr << "Invalid option: " << argv[arg] << endl; |
---|
96 | return -1; |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | if (version) { |
---|
101 | cout << versionString; |
---|
102 | } |
---|
103 | if (help) { |
---|
104 | cout << helpString; |
---|
105 | } |
---|
106 | if (help || version) { |
---|
107 | return 0; |
---|
108 | } |
---|
109 | |
---|
110 | ifstream input; |
---|
111 | if (!inputName.empty()) { |
---|
112 | input.open(inputName.c_str()); |
---|
113 | if (!input) { |
---|
114 | cerr << "File open error" << endl; |
---|
115 | return -1; |
---|
116 | } |
---|
117 | } |
---|
118 | istream& is = (inputName.empty() ? cin : input); |
---|
119 | |
---|
120 | ofstream output; |
---|
121 | if (!outputName.empty()) { |
---|
122 | output.open(outputName.c_str()); |
---|
123 | if (!output) { |
---|
124 | cerr << "File open error" << endl; |
---|
125 | return -1; |
---|
126 | } |
---|
127 | } |
---|
128 | ostream& os = (outputName.empty() ? cout : output); |
---|
129 | |
---|
130 | if (typeName.empty()) { |
---|
131 | typeName = "maxflow"; |
---|
132 | } |
---|
133 | |
---|
134 | if (typeName == "mincostflow") { |
---|
135 | Graph graph; |
---|
136 | Node s, t; |
---|
137 | StringMap cost(graph), capacity(graph); |
---|
138 | readDimacs(is, graph, capacity, s, t, cost); |
---|
139 | writeGraph(os, graph, capacity, s, t, cost); |
---|
140 | } else if (typeName == "maxflow") { |
---|
141 | Graph graph; |
---|
142 | Node s, t; |
---|
143 | StringMap capacity(graph); |
---|
144 | readDimacs(is, graph, capacity, s, t); |
---|
145 | writeGraph(os, graph, capacity, s, t); |
---|
146 | } else if (typeName == "shortestpath") { |
---|
147 | Graph graph; |
---|
148 | Node s; |
---|
149 | StringMap capacity(graph); |
---|
150 | readDimacs(is, graph, capacity, s); |
---|
151 | writeGraph(os, graph, capacity, s); |
---|
152 | } else if (typeName == "capacitated") { |
---|
153 | Graph graph; |
---|
154 | StringMap capacity(graph); |
---|
155 | readDimacs(is, graph, capacity); |
---|
156 | writeGraph(os, graph, capacity); |
---|
157 | } else if (typeName == "plain") { |
---|
158 | Graph graph; |
---|
159 | readDimacs(is, graph); |
---|
160 | writeGraph(os, graph); |
---|
161 | } else { |
---|
162 | cerr << "Invalid type error" << endl; |
---|
163 | return -1; |
---|
164 | } |
---|
165 | return 0; |
---|
166 | } |
---|