ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/applications/atom2mdin/atom2mdin.cpp
Revision: 2462
Committed: Mon Nov 21 22:59:13 2005 UTC (18 years, 7 months ago) by gezelter
File size: 10530 byte(s)
Log Message:
compilation issue

File Contents

# Content
1 /**********************************************************************
2 main.cpp - Main conversion program, command-line handling.
3
4 Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
5 Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison
6 Some portions Copyright (C) 2004-2005 by Chris Morley
7
8 This file is part of the Open Babel project.
9 For more information, see <http://openbabel.sourceforge.net/>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation version 2 of the License.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19 ***********************************************************************/
20
21 #include "config.h"
22 #if HAVE_IOSTREAM
23 #include <iostream>
24 #elif HAVE_IOSTREAM_H
25 #include <iostream.h>
26 #endif
27 #if HAVE_FSTREAM
28 #include <fstream>
29 #elif HAVE_FSTREAM_H
30 #include <fstream.h>
31 #endif
32 #if HAVE_SSTREAM
33 #include <sstream>
34 #elif
35 #include <sstream.h>
36 #endif
37
38 #include <string>
39 #include <map>
40 #if HAVE_CONIO_H
41 #include <conio.h>
42 #endif
43
44 #if !HAVE_STRNCASECMP
45 extern "C" int strncasecmp(const char *s1, const char *s2, size_t n);
46 #endif
47
48 #include "openbabel/obconversion.hpp"
49 #include "brains/Register.hpp"
50
51 using namespace std;
52 using namespace OpenBabel;
53 using namespace oopse;
54 void DoOption(const char* p, OBConversion& Conv, OBConversion::Option_type typ,
55 int& arg, int argc, char *argv[]);
56 void usage();
57 void help();
58
59 // There isn't a great way to do this -- we need to save argv[0] for usage()
60 static char *program_name;
61
62 int main(int argc,char *argv[])
63 {
64 registerOBFormats();
65 OBConversion Conv(&cin, &cout); //default input and output are console
66
67 // string GenOptions;
68 OBFormat* pInFormat = NULL;
69 OBFormat* pOutFormat = NULL;
70 vector<string> FileList, OutputFileList;
71 string OutputFileName;
72 // obMessageLevel filterLevel = obWarning; // 2 out of 5
73
74 // Parse commandline
75 bool gotInType = false, gotOutType = false;
76 bool UseSavedOptions = false;
77
78 char *oext;
79 char *iext;
80 string inputExt;
81 string outputExt;
82
83 //Save name of program without its path (and .exe)
84 string pn(argv[0]);
85 unsigned int pos;
86 #ifdef _WIN32
87 pos = pn.find(".exe");
88 if(pos!=string::npos)
89 argv[0][pos]='\0';
90 #endif
91 pos = pn.find_last_of("/\\");
92 if(pos==string::npos)
93 program_name=argv[0];
94 else
95 program_name=argv[0]+pos+1;
96
97 const char* p;
98 int arg;
99 for (arg = 1; arg < argc; arg++)
100 {
101 if (argv[arg])
102 {
103 if (argv[arg][0] == '-')
104 {
105 switch (argv[arg][1])
106 {
107
108 case 'V':
109 {
110 cout << "Open Babel " << BABEL_VERSION << " -- "
111 << __DATE__ << " -- " << __TIME__ << endl;
112 exit(0);
113 }
114
115 case 'i':
116 gotInType = true;
117 iext = argv[arg] + 2;
118 if(!*iext)
119 iext = argv[++arg]; //space left after -i: use next argument
120
121 if (strncasecmp(iext, "MIME", 4) == 0)
122 {
123 // get the MIME type from the next argument
124 iext = argv[++arg];
125 pInFormat = Conv.FormatFromMIME(iext);
126 }
127 else
128 {
129 //The ID provided by the OBFormat class is used as the identifying file extension
130 pInFormat = Conv.FindFormat(iext);
131 }
132 if(pInFormat==NULL)
133 {
134 cerr << program_name << ": cannot read input format!" << endl;
135 usage();
136 }
137 inputExt = iext;
138 break;
139
140 case 'o':
141 gotOutType = true;
142 oext = argv[arg] + 2;
143 if(!*oext)
144 oext = argv[++arg]; //space left after -i: use next argument
145
146 if (strncasecmp(oext, "MIME", 4) == 0)
147 {
148 // get the MIME type from the next argument
149 oext = argv[++arg];
150 pOutFormat = Conv.FormatFromMIME(oext);
151 }
152 else
153 pOutFormat = Conv.FindFormat(oext);
154
155 if(pOutFormat==NULL)
156 {
157 cerr << program_name << ": cannot write output format!" << endl;
158 usage();
159 }
160 outputExt = oext;
161 break;
162
163 case '?':
164 case 'H':
165 if(isalnum(argv[arg][2]))
166 {
167 if(strncasecmp(argv[arg]+2,"all",3))
168 {
169 OBFormat* pFormat = Conv.FindFormat(argv[arg]+2);
170 if(pFormat)
171 {
172 cout << argv[arg]+2 << " " << pFormat->Description() << endl;
173 if(strlen(pFormat->SpecificationURL()))
174 cout << "Specification at: " << pFormat->SpecificationURL() << endl;
175 }
176 else
177 cout << "Format type: " << argv[arg]+2 << " was not recognized" <<endl;
178 }
179 else
180 {
181 Formatpos pos;
182 OBFormat* pFormat;
183 const char* str=NULL;
184 while(OBConversion::GetNextFormat(pos,str,pFormat))
185 {
186 if((pFormat->Flags() & NOTWRITABLE) && (pFormat->Flags() & NOTREADABLE))
187 continue;
188 cout << str << endl;
189 const char* p = strchr(pFormat->Description(),'\n');
190 cout << p+1; //second line of description
191 if(strlen(pFormat->SpecificationURL()))
192 cout << "Specification at: " << pFormat->SpecificationURL();
193 cout << endl << endl;
194 }
195 }
196 }
197 else
198 help();
199 exit(0);
200
201
202 default: //single character general option
203 p = argv[arg]+1;
204 DoOption(p,Conv,OBConversion::GENOPTIONS,arg,argc,argv);
205 break;
206 }
207 }
208 else
209 {
210 //filenames
211 if(!gotOutType)
212 FileList.push_back(argv[arg]);
213 else
214 OutputFileName = argv[arg];
215 }
216 }
217 }
218
219 //user didn't specify input and output format in commandline option
220 //try to parse it from program name (pdb2mdin means input format is pdb, output format is mdin)
221 string formatName(program_name);
222 pos = formatName.find_first_of("2");
223 if(pos!=string::npos) {
224 if (!gotInType)
225 {
226 string tmpExt = formatName.substr(0, pos);
227 pInFormat = Conv.FindFormat(tmpExt.c_str());
228 if(pInFormat==NULL)
229 {
230 cerr << program_name << ": cannot read input format!" << endl;
231 usage();
232 } else
233 {
234 gotInType = true;
235 inputExt = tmpExt;
236 }
237 }
238
239 if (!gotOutType)
240 {
241 string tmpExt = formatName.substr(pos+1, string::npos);
242 pOutFormat = Conv.FindFormat(tmpExt.c_str());
243 if(pOutFormat==NULL)
244 {
245 cerr << program_name << ": cannot write output format!" << endl;
246 usage();
247 }else {
248 gotOutType = true;
249 outputExt = tmpExt;
250 }
251 }
252 }
253
254 if(FileList.empty())
255 {
256 cerr << "No input file or format spec!" <<endl;
257 usage();
258 }
259
260 if (OutputFileName.empty())
261 {
262 pos = FileList.front().rfind(".");
263 if(pos==string::npos)
264 OutputFileName = FileList.front()+ "." + outputExt;
265 else
266 OutputFileName = FileList.front().substr(0, pos) + "." + outputExt;
267 }
268
269 Conv.SetInAndOutFormats(pInFormat,pOutFormat);
270
271
272 // send info message to clog -- don't mess up cerr or cout for user programs
273 int count = Conv.FullConvert(FileList, OutputFileName, OutputFileList);
274 if ( count == 1 )
275 clog << count << " molecule converted" << endl;
276 else
277 clog << count << " molecules converted" << endl;
278
279 if(OutputFileList.size()>1)
280 {
281 clog << OutputFileList.size() << " files output. The first is " << OutputFileList[0] <<endl;
282 }
283
284 #ifdef _DEBUG
285 //CM keep window open
286 cout << "Press any key to finish" <<endl;
287 getch();
288 #endif
289
290 return 0;
291 }
292
293 void DoOption(const char* p, OBConversion& Conv, OBConversion::Option_type typ, int& arg, int argc, char *argv[])
294 {
295 while(p && *p) //can have multiple single char options
296 {
297 char ch[2]="?";
298 *ch = *p++;
299 const char* txt=NULL;
300 //Get the option text if needed
301 int nParams = Conv.GetOptionParams(ch, typ);
302 if(nParams)
303 {
304 if(*p)
305 {
306 txt = p; //use text immediately following the option letter
307 p=NULL; //no more single char options
308 }
309 else if(arg<argc-1)
310 {
311 txt = argv[++arg]; //use text from next arg
312 if(*txt=='-')
313 {
314 //...unless it is another option
315 cerr << "Option -" << ch << " takes a parameter" << endl;
316 exit(0);
317 }
318 }
319 }
320 Conv.AddOption(ch, typ, txt);
321 }
322 }
323
324 void usage()
325 {
326 cout << "Open Babel " << BABEL_VERSION << " -- " << __DATE__ << " -- "
327 << __TIME__ << endl;
328 cout << "Usage: " << program_name
329 << " [-i<input-type>] <name> [-o<output-type>] <name>" << endl;
330 cout << "Try -H option for more information." << endl;
331 /*
332 #ifdef _DEBUG
333 //CM keep window open
334 cout << "Press any key to finish" <<endl;
335 getch();
336 #endif
337 */
338 exit (0);
339 }
340
341 void help()
342 {
343 cout << "Open Babel converts chemical structures from one file format to another"<< endl << endl;
344 cout << "Usage: " << program_name << " <input spec> <output spec> [Options]" << endl << endl;
345 cout << "Each spec can be a file whose extension decides the format." << endl;
346 cout << "Optionally the format can be specified by preceding the file by" << endl;
347 cout << "-i<format-type> e.g. -ipdb, for input and -o<format-type> for output" << endl << endl;
348 cout << "See below for available format-types, which are the same as the " << endl;
349 cout << "file extensions and are case independent." << endl;
350 cout << "If no input or output file is given stdin or stdout are used instead." << endl << endl;
351 cout << "More than one input file can be specified and their names can contain" <<endl;
352 cout << "wildcard chars (* and ?).The molecules are aggregated in the output file.\n" << endl;
353 cout << OBConversion::Description(); // Conversion options
354 cout << " -H Outputs this help text" << endl;
355 cout << " -Hxxx (xxx is file format ID e.g. -Hpdb) gives format info" <<endl;
356 cout << " -Hall Outputs details of all formats" <<endl;
357 cout << " -V Outputs version number" <<endl;
358
359
360 OBFormat* pDefault = OBConversion::GetDefaultFormat();
361 if(pDefault)
362 cout << pDefault->TargetClassDescription();// some more options probably for OBMol
363
364 OBFormat* pAPI= OBConversion::FindFormat("obapi");
365 if(pAPI)
366 cout << pAPI->Description();
367
368 cout << "The following file formats are recognized:" << endl;
369 Formatpos pos;
370 OBFormat* pFormat;
371 const char* str=NULL;
372 while(OBConversion::GetNextFormat(pos,str,pFormat))
373 {
374 if((pFormat->Flags() & NOTWRITABLE) && (pFormat->Flags() & NOTREADABLE))
375 continue;
376 cout << " " << str << endl;
377 }
378 cout << "\nSee further specific info and options using -H<format-type>, e.g. -Hpdb" << endl;
379 }
380

Properties

Name Value
svn:executable *