deba@1296: #include deba@1296: #include deba@1296: #include deba@1296: deba@1296: #include deba@1296: #include deba@1296: #include deba@1296: deba@1296: using namespace std; deba@1296: using namespace lemon; deba@1296: deba@1296: const char* versionString = deba@1296: "dim_to_lgf - part of lemon library\n"; deba@1296: deba@1296: const char* helpString = deba@1296: "Dimacs to LGF converter\n" deba@1296: "Usage: dim_to_lgf [OPTIONS]\n" deba@1296: "\n" deba@1296: "Examples:\n" deba@1296: " dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n" deba@1296: "\n" deba@1296: "Options:\n" deba@1296: " -i FILE, --input FILE use FILE as input instead of standard input\n" deba@1296: " -o FILE, --output FILE use FILE as output instead of standard output\n" deba@1296: " -t TYPE, --type TYPE set up the type of the graph\n" deba@1296: " Possible types:\n" deba@1296: " mincostflow\n" deba@1296: " maxflow (default)\n" deba@1296: " shortestpath\n" deba@1296: " capacitated\n" deba@1296: " plain\n" deba@1296: " -v, --version shows the version of the converter\n" deba@1296: " -h, --help shows the help of the converter\n"; deba@1296: deba@1296: deba@1296: int main(int argc, const char *argv[]) { deba@1296: typedef SmartGraph Graph; deba@1296: deba@1296: typedef Graph::Edge Edge; deba@1296: typedef Graph::Node Node; deba@1296: typedef Graph::EdgeIt EdgeIt; deba@1296: typedef Graph::NodeIt NodeIt; deba@1296: typedef Graph::EdgeMap StringMap; deba@1296: deba@1296: std::string inputName; deba@1296: std::string outputName; deba@1296: std::string typeName; deba@1296: deba@1296: bool help = false; deba@1296: bool version = false; deba@1296: deba@1296: for (int arg = 1; arg < argc; ++arg) { deba@1296: if (strcmp(argv[arg], "--type") == 0 || deba@1296: strcmp(argv[arg], "-t") == 0) { deba@1296: if (!typeName.empty()) { deba@1296: cerr << "Multiple type description" << endl; deba@1296: return -1; deba@1296: } deba@1296: if (arg + 1 == argc) { deba@1296: cerr << "Parameter without value" << endl; deba@1296: return -1; deba@1296: } deba@1296: typeName = argv[++arg]; deba@1296: } deba@1296: else if (strcmp(argv[arg], "--input") == 0 || deba@1296: strcmp(argv[arg], "-i") == 0) { deba@1296: if (!inputName.empty()) { deba@1296: cerr << "Multiple input description" << endl; deba@1296: return -1; deba@1296: } deba@1296: if (arg + 1 == argc) { deba@1296: cerr << "Parameter without value" << endl; deba@1296: return -1; deba@1296: } deba@1296: inputName = argv[++arg]; deba@1296: } deba@1296: else if (strcmp(argv[arg], "--output") == 0 || deba@1296: strcmp(argv[arg], "-o") == 0) { deba@1296: if (!outputName.empty()) { deba@1296: cerr << "Multiple input description" << endl; deba@1296: return -1; deba@1296: } deba@1296: if (arg + 1 == argc) { deba@1296: cerr << "Parameter without value" << endl; deba@1296: return -1; deba@1296: } deba@1296: outputName = argv[++arg]; deba@1296: } else if (strcmp(argv[arg], "--help") == 0 || deba@1296: strcmp(argv[arg], "-h") == 0) { deba@1296: help = true; deba@1296: } else if (strcmp(argv[arg], "--version") == 0 || deba@1296: strcmp(argv[arg], "-v") == 0) { deba@1296: version = true; deba@1296: } else { deba@1296: cerr << "Invalid option: " << argv[arg] << endl; deba@1296: return -1; deba@1296: } deba@1296: } deba@1296: deba@1296: if (version) { deba@1296: cout << versionString; deba@1296: } deba@1296: if (help) { deba@1296: cout << helpString; deba@1296: } deba@1296: if (help || version) { deba@1296: return 0; deba@1296: } deba@1296: deba@1296: ifstream input; deba@1296: if (!inputName.empty()) { deba@1296: input.open(inputName.c_str()); deba@1296: if (!input) { deba@1296: cerr << "File open error" << endl; deba@1296: return -1; deba@1296: } deba@1296: } deba@1296: istream& is = (inputName.empty() ? cin : input); deba@1296: deba@1296: ofstream output; deba@1296: if (!outputName.empty()) { deba@1296: output.open(outputName.c_str()); deba@1296: if (!output) { deba@1296: cerr << "File open error" << endl; deba@1296: return -1; deba@1296: } deba@1296: } deba@1296: ostream& os = (outputName.empty() ? cout : output); deba@1296: deba@1296: if (typeName.empty()) { deba@1296: typeName = "maxflow"; deba@1296: } deba@1296: deba@1296: if (typeName == "mincostflow") { deba@1296: Graph graph; deba@1296: Node s, t; deba@1296: StringMap cost(graph), capacity(graph); deba@1296: readDimacs(is, graph, capacity, s, t, cost); deba@1296: writeGraph(os, graph, capacity, s, t, cost); deba@1296: } else if (typeName == "maxflow") { deba@1296: Graph graph; deba@1296: Node s, t; deba@1296: StringMap capacity(graph); deba@1296: readDimacs(is, graph, capacity, s, t); deba@1296: writeGraph(os, graph, capacity, s, t); deba@1296: } else if (typeName == "shortestpath") { deba@1296: Graph graph; deba@1296: Node s; deba@1296: StringMap capacity(graph); deba@1296: readDimacs(is, graph, capacity, s); deba@1296: writeGraph(os, graph, capacity, s); deba@1296: } else if (typeName == "capacitated") { deba@1296: Graph graph; deba@1296: StringMap capacity(graph); deba@1296: readDimacs(is, graph, capacity); deba@1296: writeGraph(os, graph, capacity); deba@1296: } else if (typeName == "plain") { deba@1296: Graph graph; deba@1296: readDimacs(is, graph); deba@1296: writeGraph(os, graph); deba@1296: } else { deba@1296: cerr << "Invalid type error" << endl; deba@1296: return -1; deba@1296: } deba@1296: return 0; deba@1296: }