Allow running with cache and graphviz only
[dueringa_WikiWalker.git] / src / main.cpp
blob4ab4cbc1d528a47695044d2c6730f9f9da6eba38
1 //! \file main.cpp
3 #include <iostream>
4 #include <fstream>
6 #include "WikiWalker.h"
7 #include "version.h"
8 #include "BoostPoCommandLineParser.h"
9 #include "ToGraphvizWriter.h"
11 using namespace std;
13 int main(int argc, char** argv)
15 BoostPoCommandLineParser cmdp;
17 try {
18 cmdp.parse(argc, argv);
19 } catch(std::exception& e) {
20 cerr << endl << e.what() << endl;
21 cmdp.printHelp();
22 return -1;
25 if (cmdp.hasSet("version")) {
26 std::cout << "WikiWalker, version " << _WW_VERSION << std::endl;
27 return 0;
30 if (cmdp.hasSet("help")) {
31 cmdp.printHelp();
32 return 0;
35 bool isUrlSet = cmdp.hasSet("url");
36 bool isCacheSet = cmdp.hasSet("json-cache");
37 bool isDotSet = cmdp.hasSet("dot-out");
38 bool validRunConfig = isUrlSet || (isDotSet && isCacheSet);
40 if(!validRunConfig) {
41 cerr << "Must either specify at least URL, "
42 << "or dot and cache file." << endl;
43 cmdp.printHelp();
44 return 1;
47 bool read_failed = false;
48 WikiWalker w;
50 if(isCacheSet) {
51 try {
52 std::string cachefile = cmdp.getValue("json-cache");
53 w.readCache(cachefile);
54 } catch(std::exception& e) {
55 std::cout << e.what() << endl;
56 read_failed = true;
60 if(isUrlSet) {
61 try {
62 std::string url = cmdp.getValue("url");
63 w.startWalking(url);
64 } catch(std::exception& e) {
65 cout << "Error " << e.what() << endl;
66 return -1;
70 if(isCacheSet) {
71 if(read_failed) {
72 cout << "Reading from cache failed, won't overwrite" << endl;
73 } else {
74 try {
75 std::string cachefile = cmdp.getValue("json-cache");
76 w.writeCache(cachefile);
77 } catch(std::exception& e) {
78 cout << "Error: " << e.what() << endl;
83 if(isDotSet) {
84 const ArticleCollection& ac = w.getCollection();
85 std::string outfile = cmdp.getValue("dot-out");
86 ToGraphvizWriter tgw;
87 ofstream file(outfile, ios::trunc | ios::out);
89 if(file.fail()) {
90 cerr << "Error opening dot out file for writing" << endl;
91 } else {
92 tgw.output(ac, file);
93 file.flush();
95 if(file.bad() || file.fail()) {
96 cerr << "Error during writing dot out file." << endl;
99 file.close();
103 return 0;