test mussa class
[mussa.git] / mussa.cxx
1 //  This file is part of the Mussa source distribution.
2 //  http://mussa.caltech.edu/
3 //  Contact author: Tristan  De Buysscher, tristan@caltech.edu
4
5 // This program and all associated source code files are Copyright (C) 2005
6 // the California Institute of Technology, Pasadena, CA, 91125 USA.  It is
7 // under the GNU Public License; please see the included LICENSE.txt
8 // file for more information, or contact Tristan directly.
9
10
11 #include "gui/ConnWindow.hh"
12 #include <iostream>
13 using namespace std;
14
15 char
16 parse_args(int argc, char **argv, string *a_file_path, int *window, 
17            int *threshold, char *ana_mode, float *ent_thres);
18
19
20 int main(int argc, char **argv) 
21 {
22   Mussa an_analysis;
23   string a_file_path;
24   char * picked_file;
25   int window, threshold;
26   float ent_thres;
27   char run_mode, ana_mode;
28   int x_max=1000;
29   int y_max=500;
30   string err_msg;
31
32
33   err_msg = "";
34
35   // yeah, its horrible to be passing these by reference, but I just don't see
36   // any other way without making parse_args part of MussaClass, which just
37   // seems wrong (args are after all, an io type thing, and the gui only mode
38   // will have it own way as well...
39   run_mode = parse_args(argc, argv, &a_file_path, &window, &threshold,
40                         &ana_mode, &ent_thres);
41
42   cout << "mussa: run mode = " << run_mode;
43   cout << "\tfile_path = "<< a_file_path << endl;
44   cout << "mussa: ent_thres = " << ent_thres << endl;
45
46   // if no error from parse args (run_mode = 'e', ie error), run in proper mode
47   if (run_mode != 'e')
48   {
49     if ((run_mode == 'f') || (run_mode == 'n'))
50     {
51       err_msg = an_analysis.load_mupa_file(a_file_path);
52       err_msg = an_analysis.analyze(window, threshold, ana_mode, ent_thres);
53       //an_overlord.do_analysis();
54     }
55
56     if (run_mode == 'v')
57       err_msg = an_analysis.load(a_file_path);
58       //an_overlord.get_analysis();
59
60     // no longer needed, but still semi-useful reality check...
61     if (run_mode == 'g')
62     {
63       cout << "GTV - All Gui, All the Time\n";
64     }
65
66     if (err_msg == "")
67     {
68       if  ((run_mode == 'f') || (run_mode == 'v') || (run_mode == 'g'))
69       {
70         //an_overlord.spawnConnView(1000,500);
71         ConnWindow *a_conn_win = new ConnWindow(x_max, y_max, "Mussa");
72
73         // we have an analysis already if in these 2 modes
74         if  ((run_mode == 'f') || (run_mode == 'v'))
75           a_conn_win->add_ana(&an_analysis);
76
77         Fl::run();
78       }
79     }
80     else
81       cout << err_msg << endl;
82   }
83 }
84
85 struct ParseError
86 {
87   string err;
88   ParseError(const string& msg) : err(msg) { } ;
89 };
90
91 // minimal arg reading function, not very robust to errors
92 char
93 parse_args(int argc, char **argv, string *a_file_path, int *window, 
94            int *threshold, char *ana_mode, float *ent_thres)
95 {
96   int i, else_i;
97   string an_arg;
98   char run_mode;
99
100   // initialize these to 0 as flag if they are not changed
101   *window = 0;
102   *threshold = 0;
103   *ent_thres = 0.0;
104   run_mode = 'e'; //error default if no run mode set implicitly or explicitly
105   *ana_mode = 't'; // default to transitivie analyses mode
106
107
108   // no args means gui only mode
109   if (argc == 1)
110     run_mode = 'g';
111   // 1 arg means its a mupa file and run in full mode
112   /*
113   else if (argc == 2)
114   {
115   }
116   */
117   else
118   {
119     else_i = 0;
120     i = 1;
121     while (i < argc)
122     {
123       an_arg = * ++argv;
124       i++;
125
126       // see what alternate mode the user wants to run in
127       if (an_arg == "-m")
128       {
129         an_arg = * ++argv;
130         i++;
131         if ( (an_arg == "v") || (an_arg == "n") ) //only 2 valid modes so far
132           run_mode = an_arg[0];
133         else
134         {
135           cout << "Error: \'" << an_arg;
136           cout << "\' is not a valid analysis mode for -a argument" << endl; 
137           throw ParseError("not valid -a argument");
138         }
139         *a_file_path = * ++argv;
140         i++;
141       }
142       // alternate analyses modes
143       else if (an_arg == "-a")
144       {
145         an_arg = * ++argv;
146         i++;
147         // t = transitive, r = radial, e = entropy
148         if ( (an_arg == "t") || (an_arg == "r") || (an_arg == "e") )
149           *ana_mode = an_arg[0];
150         else
151         {
152           cout << "Error: \'" << an_arg;
153           cout << "\' is not a valid run mode for -m argument" << endl; 
154           throw ParseError("bad argument -m");
155         }
156
157       }
158       else if (an_arg == "-w") // alternate window size arg
159       {
160         *window = atoi(* ++argv);
161         i++;
162       }
163       else if (an_arg == "-t") // alternate threshold arg
164       {
165         *threshold = atoi(* ++argv);
166         i++;
167       }
168       else if (an_arg == "-et") // alternate entropy threshold arg
169       {
170         *ent_thres = atof(* ++argv);
171         i++;
172       }
173       else
174       {
175         if (else_i == 0)
176         {
177           *a_file_path = an_arg;
178           run_mode = 'f';
179           else_i++;
180         }
181         else
182         {
183           //cout << "Error, unknown arg: \'" << an_arg << "\'" << endl;
184           cout << "Error, too many filenames: \'" << an_arg << "\'" << endl;
185           run_mode == 'e';
186         }
187       }
188     }
189   }
190   return run_mode;
191 }
192
193
194 /*
195       cout << "fee\n";
196       cout << "fie\n";
197       cout << "foe\n";
198       cout << "fum\n";
199 */