BioInfWeb - TreeGraph / Version 1 / Show File - tgf-main.cc

TreeGraph subversion repository

sventon subversion web client - http://www.sventon.org
[show recent changes]
 
  Help
Rev: HEAD (2) - https://secure.bioinfweb.info/Code/svn/TreeGraph / trunk / main / tgf-main.cc
Show File - tgf-main.cc  [show properties]
spinner
/*
 tgf-main.cc, part of
 treegraph
 Tree formatting program
 Generates vector graphics (SVG,EPS) from .tgf-tree description files.
10   Copyright (c) 2003-04 by Joern Mueller
11 
12 
13   This program is free software; you can redistribute it and/or
14   modify it under the terms of the GNU General Public License
15   as published by the Free Software Foundation; either version 2
16   of the License, or (at your option) any later version.
17 
18   This program is distributed in the hope that it will be useful,
19   but WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21   GNU General Public License for more details.
22 
23   You should have received a copy of the GNU General Public License
24   along with this program (GPL.html); if not, write to the Free Software
25   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26   (also: http://www.gnu.org)
27  */
28 
29 
30 
31 
32 
33 
34 
35 
36  #include "tgf-main.h"
37  #include "tgf-tree.h"
38  #include "NEXUS2tgf.h"
39  #include "tgf-render.h"
40  #include <cstdlib>
41 
42 
43  #define LEERZEILE "\r \n"
44 
45 
46 
47  using namespace std;
48 
49  void getstring_d(string& a, const string& info, const string& def="");
50  void splitname(const string& s, string& r, string& t);
51 
52  #if USE_COMMANDLINE_PARAMS
53  int main(int nargs, char *argv[])
54  #else
55  int main()
56  #endif
57  {
58 
59 
60   bool interactive=false;
61 
62   string nam;
63   tree *baum=0;
64   char ch='v';
65 
66  #if USE_COMMANDLINE_PARAMS
67   if(nargs<=1){
68  #endif
69    interactive=true;
70    cout << "TreeGraph " TGFVERSION ", Copyright (C) 2003 by Joern Mueller, Kai Mueller.\n";
71    cout <<"TreeGraph comes with NO WARRANTY; see GPL.html for details.\n\n";
72    getstring_d(nam,"file name");
73  #if USE_COMMANDLINE_PARAMS
74   } else {
75    for(int i=1; i<nargs; i++){
76     if(argv[i][0]=='-'){
77      if(strchr("pvest",argv[i][1]))
78       ch=argv[i][1];
79     } else {
80      nam=argv[i];
81      break;
82     }
83    }
84   }
85  #endif
86 
87 
88   bool transl=false;
89   string namroot,namtail;
90   splitname(nam,namroot,namtail);
91   if(ch=='t' || namtail!=".tgf"){ //translate from a non-TGF file
92    transl=true;
93    interactive=true;
94    ifstream eing1(nam.c_str());
95    if(!eing1){
96     cout << "file '" << nam << "' not found.\n";
97     return 2;
98    }
99    nam=namroot+".tgf";
100    ifstream tmp(nam.c_str());
101    if(tmp){
102     string res;
103     getstring_d(res,"File "+nam+" already exists. Overwrite? (Y/n) ","n");
104     if(res[0]!='Y')
105      return 1;
106     tmp.close();
107    }
108    ofstream aus(nam.c_str());
109     try {
110 
111            translateNEXUS(eing1,aus);
112           }
113           catch(...){
114     interactive=true;
115     cout << "conversion error" << "\n";
116           }
117    eing1.close();
118    aus.close();
119    cout << "\nTGF file written to "<< nam << " and loaded\n\n";
120   }
121 
122   ifstream eing(nam.c_str());
123   if(!eing){
124    cout << "file '" << nam << "' not found.\n";
125    return 2;
126   }
127   baum=new tree();
128   baum->read(eing);
129   eing.close();
130 
131 
132   if(interactive)
133    cout << "\nenter command or '?'\n\n";
134 
135   do {
136    if(interactive){
137     cout << endl << namroot << (baum->dirty()?"[*]> ":"> ");
138     flush(cout);
139     string str1="?";
140     getline(cin,str1,'\n');
141     ch=str1[0];
142    }
143    if(ch=='q'){
144     cout << "quit.\n";
145     interactive=false;
146    } else if(ch=='p'){
147     string tos=namroot+".eps";
148     if(interactive)
149      getstring_d(tos,"PostScript file name",namroot+".eps");
150     ofstream ausg(tos.c_str());
151     if(!ausg){
152      cout << "error.\n";
153     } else {
154      ps_ostream psaus(ausg);
155      cout << "writing to '"<<tos<< "' ...";
156      flush(cout);
157      baum->render(&psaus);
158      cout << LEERZEILE;
159     }
160    } else if(ch=='v'){
161     string tos=namroot+".svg";
162     if(interactive)
163      getstring_d(tos,"SVG (Scalable Vector Graphics) file name",namroot+".svg");
164     ofstream ausg(tos.c_str());
165     if(!ausg){
166      cout << "error.\n";
167     } else {
168      svg_ostream svgaus(ausg);
169      cout << "writing to '"<<tos<< "' ...";
170      flush(cout);
171      baum->render(&svgaus);
172      cout << LEERZEILE;
173     }
174    } else if(ch=='e'){
175     cout << "Edit mode.\n";
176     interactive=true; //Editieren ist interaktiv
177     baum->edit();
178    } else if(ch=='F'){
179     baum->show_dsc();
180    } else if(ch=='s'){
181     string tos;
182     getstring_d(tos,"Save as ",namroot+".tgf");
183     ofstream ausg(tos.c_str());
184     if(!ausg){
185      cout << "Couldn't create output file.\n";
186     } else {
187      cout << "Saving as '" << tos << "' ...";
188      flush(cout);
189      baum->write(ausg);
190      cout << LEERZEILE;
191     }
192    } else if(ch=='l'){
193     getstring_d(nam,"TGF-file",namroot+".tgf");
194     ifstream eing2(nam.c_str());
195     if(!eing2){
196      cout << "file not found.\n";
197     } else {
198      if(baum) delete baum;
199      baum =new tree();
200      baum->read(eing2);
201      splitname(nam,namroot,namtail);
202     }
203    } else if(ch=='t') {
204         string nam2;
205     cout << "name of file to translate: \n";
206         cin >> nam2;
207         ifstream eing(nam2.c_str());
208         if(!eing){
209             cout << "file '" << nam << "' not found.\n";
210            }
211         splitname(nam2,namroot,namtail);
212         nam2=namroot+".tgf";
213         ofstream aus(nam2.c_str());
214         translateNEXUS(eing,aus);
215         eing.close();
216         aus.close();
217         cout << "\nTGF file written to "<< nam2 <<endl;
218    } else {
219     cout << "available commands:\n\n";
220     cout << "l loads tree from TGF file\n";
221     cout << "t translates to TGF file\n";
222     cout << "s saves current tree as TGF file\n";
223     cout << "p writes current tree into EPS file\n";
224     cout << "v writes current tree into SVG file\n";
225     cout << "e edit mode\n";
226     cout << "F show settings\n";
227     cout << "q quits program\n";
228    }
229   } while(interactive);
230   return 0;
231  }
232 
233 
234  void getstring_d(string& a, const string& info, const string& def)
235  {
236    cout << info;
237    if(def.size()>0){
238      cout << " ["<<def<<"]";
239    }
240    cout << ": ";
241    flush(cout);
242    getline(cin,a,'\n');
243    if(a.size()==0) a=def;
244  }
245 
246 
247  void splitname(const string& s, string& r, string& t)
248  {
249   string::size_type p=s.rfind('.');
250   r=s.substr(0,p);
251   t=(p!=string::npos)?s.substr(p):"";
252 
253  }


feed icon

sventon 2.5.1

Valid XHTML 1.0 Strict   CSS ist valide!
bioinfweb RSS feed bioinfweb on twitter bioinfweb on GitHub
bioinfweb - Biology & Informatics Website