1 | // $Id: test_wead.cc 58 2004-04-14 07:07:08Z jari $ |
---|
2 | |
---|
3 | #include <iostream> |
---|
4 | #include <fstream> |
---|
5 | #include <string> |
---|
6 | |
---|
7 | #include "FileIO.h" |
---|
8 | #include "Wead.h" |
---|
9 | |
---|
10 | |
---|
11 | |
---|
12 | // class for command line options. |
---|
13 | class Parameter { |
---|
14 | public: |
---|
15 | Parameter(const Parameter&); // not implemented |
---|
16 | Parameter(const int argc,const char *argv[]); |
---|
17 | |
---|
18 | inline const std::string& matrix(void) const { return matrix_file_; } |
---|
19 | inline const std::string& flag(void) const { return flag_file_; } |
---|
20 | inline int neighbours(void) const { return neighbours_; } |
---|
21 | |
---|
22 | private: |
---|
23 | void analyse(void); |
---|
24 | void defaults(void); |
---|
25 | |
---|
26 | std::string flag_file_; |
---|
27 | std::string matrix_file_; |
---|
28 | int neighbours_; |
---|
29 | }; |
---|
30 | |
---|
31 | Parameter::Parameter(const int argc,const char *argv[]) |
---|
32 | { |
---|
33 | using namespace std; |
---|
34 | // check if help requested |
---|
35 | for (int i=1; i<argc; i++) |
---|
36 | if (string(argv[i])==string("-help") || string(argv[i])==string("-h")) { |
---|
37 | cout << "Please write some help information\n"; |
---|
38 | exit(0); // always exit after printing help |
---|
39 | } |
---|
40 | |
---|
41 | defaults(); |
---|
42 | |
---|
43 | for (int i=1; i<argc; i++) { |
---|
44 | bool ok=false; |
---|
45 | string myargv(argv[i]); |
---|
46 | if (myargv==string("-matrix")) |
---|
47 | if ((i+1)<argc) { |
---|
48 | matrix_file_=argv[++i]; |
---|
49 | ok=true; |
---|
50 | } |
---|
51 | if (myargv==string("-flag")) |
---|
52 | if ((i+1)<argc) { |
---|
53 | flag_file_=argv[++i]; |
---|
54 | ok=true; |
---|
55 | } |
---|
56 | if (myargv==string("-neighbours")) |
---|
57 | if ((i+1)<argc) { |
---|
58 | neighbours_=atoi(argv[++i]); |
---|
59 | ok=true; |
---|
60 | } |
---|
61 | |
---|
62 | if (!ok) |
---|
63 | std::cerr << "# Parameter::Parameter Invalid option: " |
---|
64 | << argv[i] << '\n'; |
---|
65 | } |
---|
66 | |
---|
67 | analyse(); |
---|
68 | } |
---|
69 | |
---|
70 | void Parameter::analyse(void) |
---|
71 | { |
---|
72 | using namespace theplu::cpptools; |
---|
73 | bool ok=true; |
---|
74 | if (FileIO().access_rights(matrix(),"r")) { |
---|
75 | std::cerr << "Cannot access matrix file " << matrix() << std::endl; |
---|
76 | ok=false; |
---|
77 | } |
---|
78 | if (FileIO().access_rights(flag(),"r")) { |
---|
79 | std::cerr << "Cannot access flag file " << flag() << std::endl; |
---|
80 | ok=false; |
---|
81 | } |
---|
82 | if (!ok) |
---|
83 | std::exit(-1); |
---|
84 | } |
---|
85 | |
---|
86 | void Parameter::defaults(void) |
---|
87 | { |
---|
88 | flag_file_="data/wead_flag.data"; |
---|
89 | matrix_file_="data/wead_matrix.data"; |
---|
90 | neighbours_=3; |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | int main(const int argc,const char* argv[]) |
---|
96 | { |
---|
97 | Parameter option(argc,argv); |
---|
98 | std::ifstream matrix_stream(option.matrix().c_str()); |
---|
99 | std::ifstream flag_stream(option.flag().c_str()); |
---|
100 | theplu::cpptools::Wead wead(matrix_stream,flag_stream,option.neighbours()); |
---|
101 | wead.estimate(); |
---|
102 | std::cout << wead.matrix() << std::endl; |
---|
103 | return 0; // normal exit |
---|
104 | } |
---|