ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/applications/atom2md/atom2md.cpp
(Generate patch)

Comparing trunk/src/applications/atom2md/atom2md.cpp (file contents):
Revision 1021 by gezelter, Wed Aug 2 19:40:39 2006 UTC vs.
Revision 1442 by gezelter, Mon May 10 17:28:26 2010 UTC

# Line 1 | Line 1
1   /**********************************************************************
2 < atom2md.cpp - OpenBabel-based conversion program to OOPSE MD file,
2 > atom2md.cpp - OpenBabel-based conversion program to OpenMD file,
3                command-line handling.
4  
5   Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
6 < Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison
7 < Some portions Copyright (C) 2004-2005 by Chris Morley
6 > Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
7 > Some portions Copyright (C) 2004-2006 by Chris Morley
8 > Some portions Copyright (C) 2008-2009 by J. Daniel Gezelter
9  
10 < This file is part of the OOPSE and Open Babel projects.
11 < For more information, see <http://oopse.org> and <http://openbabel.sourceforge.net/>
10 > This file is part of both the OpenMD and Open Babel projects.
11 > For more information, see <http://openmd.net> and <http://openbabel.sourceforge.net/>
12  
13   This program is free software; you can redistribute it and/or modify
14   it under the terms of the GNU General Public License as published by
# Line 20 | Line 21 | GNU General Public License for more details.
21   ***********************************************************************/
22  
23   #include "config.h"
24 < #if HAVE_IOSTREAM
25 <        #include <iostream>
26 < #elif HAVE_IOSTREAM_H
27 <        #include <iostream.h>
24 >
25 > // used to set import/export for Cygwin DLLs
26 > #ifdef WIN32
27 > #define USING_OBDLL
28   #endif
28 #if HAVE_FSTREAM
29        #include <fstream>
30 #elif HAVE_FSTREAM_H
31        #include <fstream.h>
32 #endif
33 #if HAVE_SSTREAM
34        #include <sstream>
35 #elif
36        #include <sstream.h>
37 #endif
29  
30 + #include <openbabel/babelconfig.h>
31 +
32 + #include <iostream>
33 + #include <fstream>
34 + #include <sstream>
35 +
36   #include <string>
37   #include <map>
38   #if HAVE_CONIO_H
# Line 46 | Line 43 | extern "C" int strncasecmp(const char *s1, const char
43   extern "C" int strncasecmp(const char *s1, const char *s2, size_t n);
44   #endif
45  
46 < #include "openbabel/obconversion.hpp"
47 < #include "brains/Register.hpp"
46 > #include <openbabel/obconversion.h>
47 > #include <openbabel/plugin.h>
48 > #include <cstdlib>
49 > #include <cstring>
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[]);
53 >
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  
# Line 62 | Line 61 | int main(int argc,char *argv[])
61  
62   int main(int argc,char *argv[])
63   {
65  registerOBFormats();  
64    OBConversion Conv(&cin, &cout); //default input and output are console
65 <  
68 <  //  string GenOptions;
65 >
66    OBFormat* pInFormat = NULL;
67    OBFormat* pOutFormat = NULL;
68    vector<string> FileList, OutputFileList;
69    string OutputFileName;
70 <  //  obMessageLevel filterLevel = obWarning; // 2 out of 5
74 <  
70 >
71    // Parse commandline
72    bool gotInType = false, gotOutType = false;
73 <  bool UseSavedOptions = false;
74 <  
75 <  char *oext;
76 <  char *iext;
73 >  bool SplitOrBatch=false;
74 >
75 >  char *oext = NULL;
76 >  char *iext = NULL;
77 >
78 >  // for use with command name to type conversion
79    string inputExt;
80    string outputExt;
81 <  
81 >
82    //Save name of program without its path (and .exe)
83    string pn(argv[0]);
84 <  unsigned int pos;
84 >  string::size_type pos;
85   #ifdef _WIN32
86    pos = pn.find(".exe");
87    if(pos!=string::npos)
# Line 94 | Line 92 | int main(int argc,char *argv[])
92      program_name=argv[0];
93    else
94      program_name=argv[0]+pos+1;
95 <  
95 >
96    const char* p;
97    int arg;
98 <  for (arg = 1; arg < argc; arg++)
98 >  for (arg = 1; arg < argc; ++arg)
99      {
100        if (argv[arg])
101          {
# Line 105 | Line 103 | int main(int argc,char *argv[])
103              {
104                switch (argv[arg][1])
105                  {
106 <                  
106 >
107                  case 'V':
108                    {
109 <                    cout << "atom2md: part of OOPSE " <<
110 <                      OOPSE_VERSION_MAJOR << "." << OOPSE_VERSION_MINOR << "." <<
111 <                      OOPSE_VERSION_TINY << " and Open Babel " << BABEL_VERSION << " -- "
109 >                    cout << program_name << ": part of OpenMD " <<
110 >                      OPENMD_VERSION_MAJOR << "." << OPENMD_VERSION_MINOR <<
111 >                      "." << OPENMD_VERSION_TINY <<
112 >                      " and Open Babel " << BABEL_VERSION << " -- "
113                           << __DATE__ << " -- " << __TIME__ << endl;
114                      exit(0);
115                    }
116 <                  
116 >
117                  case 'i':
118                    gotInType = true;
119                    iext = argv[arg] + 2;
120                    if(!*iext)
121                      iext = argv[++arg]; //space left after -i: use next argument
122 <                  
122 >
123                    if (strncasecmp(iext, "MIME", 4) == 0)
124                      {
125                        // get the MIME type from the next argument
# Line 137 | Line 136 | int main(int argc,char *argv[])
136                        cerr << program_name << ": cannot read input format!" << endl;
137                        usage();
138                      }
140                  inputExt = iext;
139                    break;
140                    
141                  case 'o':
# Line 145 | Line 143 | int main(int argc,char *argv[])
143                    oext = argv[arg] + 2;
144                    if(!*oext)
145                      oext = argv[++arg]; //space left after -i: use next argument
146 <                  
146 >                                        
147                    if (strncasecmp(oext, "MIME", 4) == 0)
148                      {
149                        // get the MIME type from the next argument
# Line 154 | Line 152 | int main(int argc,char *argv[])
152                      }
153                    else
154                      pOutFormat = Conv.FindFormat(oext);
155 <                  
155 >
156                    if(pOutFormat==NULL)
157                      {
158                        cerr << program_name << ": cannot write output format!" << endl;
159                        usage();
160                      }
161 <                  outputExt = oext;
162 <                  break;                                
163 <                  
161 >                  break;
162 >
163 >                /*case 'F':
164 >                  if(!Conv.SetOutFormat("fpt"))
165 >                    cout << "FingerprintFormat needs to be loaded" << endl;
166 >                  else
167 >                    {
168 >                      Conv.AddOption("F",OBConversion::OUTOPTIONS);
169 >                      Conv.Write(NULL);
170 >                    }
171 >                  return 0;
172 >                  */
173 >                case 'L': //display a list of plugin type or classes
174 >                  {
175 >                    const char* param=NULL;
176 >                    if(argc>arg+1)
177 >                      param = argv[arg+2];
178 >                    OBPlugin::List(argv[arg+1], param);
179 >                    return 0;
180 >                  }
181                  case '?':
182                  case 'H':
183 <                  if(isalnum(argv[arg][2]))
183 >                  if(isalnum(argv[arg][2]) || arg==argc-2)
184                      {
185                        if(strncasecmp(argv[arg]+2,"all",3))
186                          {
187 <                          OBFormat* pFormat = Conv.FindFormat(argv[arg]+2);
187 >                          const char* pID= (arg==argc-2) ? argv[arg+1] : argv[arg]+2;
188 >                          OBFormat* pFormat = Conv.FindFormat(pID);
189                            if(pFormat)
190                              {
191 <                              cout << argv[arg]+2 << "  " << pFormat->Description() << endl;
191 >                              cout << pID << "  " << pFormat->Description() << endl;
192 >                              if(pFormat->Flags() & NOTWRITABLE)
193 >                                cout << " This format is Read-only" << endl;
194 >                              if(pFormat->Flags() & NOTREADABLE)
195 >                                cout << " This format is Write-only" << endl;
196 >
197                                if(strlen(pFormat->SpecificationURL()))
198                                  cout << "Specification at: " << pFormat->SpecificationURL() << endl;
199                              }
200                            else
201 <                            cout << "Format type: " << argv[arg]+2 << " was not recognized" <<endl;
201 >                            cout << "Format type: " << pID << " was not recognized" <<endl;
202                          }
203                        else
204                          {
205 <                          Formatpos pos;
185 <                          OBFormat* pFormat;
186 <                          const char* str=NULL;
187 <                          while(OBConversion::GetNextFormat(pos,str,pFormat))
188 <                            {
189 <                              if((pFormat->Flags() & NOTWRITABLE) && (pFormat->Flags() & NOTREADABLE))
190 <                                continue;
191 <                              cout << str << endl;
192 <                              const char* p = strchr(pFormat->Description(),'\n');
193 <                              cout << p+1; //second line of description
194 <                              if(strlen(pFormat->SpecificationURL()))
195 <                                cout << "Specification at: " << pFormat->SpecificationURL();
196 <                              cout << endl << endl;
197 <                            }
205 >                          OBPlugin::List("formats","verbose");
206                          }
207                      }
208                    else
209                      help();
210 <                  exit(0);                                      
211 <                  
212 <                  
210 >                  return 0;
211 >                                        
212 >                case '-': //long option --name text
213 >                  {
214 >                    //Do nothing if name is empty
215 >                    //Option's text is the next arg provided it doesn't start with -
216 >                    char* nam = argv[arg]+2;
217 >                    if(*nam != '\0')
218 >                      {
219 >                        string txt;
220 >                        int i;
221 >                        for(i=0; i<Conv.GetOptionParams(nam, OBConversion::GENOPTIONS)
222 >                              && arg<argc-1 && argv[arg+1];++i) //removed  && *argv[arg+1]!='-'
223 >                          {
224 >                            if(!txt.empty()) txt+=' ';
225 >                            txt += argv[++arg];
226 >                          }
227 >                        if(*nam=='-')
228 >                          {
229 >                            // Is a API directive, e.g.---errorlevel
230 >                            //Send to the pseudoformat "obapi" (without any leading -)
231 >                            OBConversion apiConv;
232 >                            OBFormat* pAPI= OBConversion::FindFormat("obapi");
233 >                            if(pAPI)
234 >                              {
235 >                                apiConv.SetOutFormat(pAPI);
236 >                                apiConv.AddOption(nam+1, OBConversion::GENOPTIONS, txt.c_str());
237 >                                apiConv.Write(NULL, &std::cout);
238 >                              }
239 >                          }
240 >                        else
241 >                          // Is a long option name, e.g --addtotitle
242 >                          Conv.AddOption(nam,OBConversion::GENOPTIONS,txt.c_str());
243 >                      }
244 >                  }
245 >                  break;
246 >                                        
247 >                case 'm': //multiple output files
248 >                  SplitOrBatch=true;
249 >                  break;
250 >                                        
251 >                case 'a': //single character input option
252 >                  p = argv[arg]+2;
253 >                  DoOption(p,Conv,OBConversion::INOPTIONS,arg,argc,argv);
254 >                  break;
255 >
256 >                case 'x': //single character output option
257 >                  p = argv[arg]+2;
258 >                  DoOption(p,Conv,OBConversion::OUTOPTIONS,arg,argc,argv);
259 >                  break;
260 >                                        
261                  default: //single character general option
262                    p = argv[arg]+1;
263                    DoOption(p,Conv,OBConversion::GENOPTIONS,arg,argc,argv);
# Line 219 | Line 275 | int main(int argc,char *argv[])
275          }
276      }
277    
278 <  //user didn't specify input and output format in commandline option
279 <  //try to parse it from program name (pdb2mdin means input format is pdb, output format is mdin)
278 >  // user didn't specify input and output format in commandline option
279 >  // try to parse it from program name (pdb2md means input format is pdb,
280 >  // output format is md)
281 >
282    string formatName(program_name);
283    pos = formatName.find_first_of("2");
284    if(pos!=string::npos) {
# Line 238 | Line 296 | int main(int argc,char *argv[])
296              inputExt = tmpExt;
297            }
298        }
299 <    
299 >
300      if (!gotOutType)
301        {
302          string tmpExt = formatName.substr(pos+1, string::npos);
# Line 252 | Line 310 | int main(int argc,char *argv[])
310            outputExt = tmpExt;
311          }
312        }
313 <  }
313 >  }
314 >
315 >  if(!gotOutType) //the last file is the output
316 >    {
317 >      if(FileList.empty())
318 >        {
319 >          cerr << "No output file or format spec!" << endl;
320 >          usage();
321 >        }
322 >      OutputFileName = FileList.back();
323 >      FileList.pop_back();
324 >    }
325 >
326 > #ifdef _WIN32
327 >  //Expand wildcards in input filenames and add to FileList
328 >  vector<string> tempFileList(FileList);
329 >  FileList.clear();
330 >  vector<string>::iterator itr;
331 >  for(itr=tempFileList.begin();itr!=tempFileList.end();++itr)
332 >    DLHandler::findFiles (FileList, *itr);
333 > #endif
334    
335 <  if(FileList.empty())
335 >  if (!gotInType)
336      {
337 <      cerr << "No input file or format spec!" <<endl;
338 <      usage();
337 >      if(FileList.empty())
338 >        {
339 >          cerr << "No input file or format spec!" <<endl;
340 >          usage();
341 >        }
342      }
343 +
344 +  if (!gotOutType)
345 +    {
346 +      pOutFormat = Conv.FormatFromExt(OutputFileName.c_str());
347 +      if(pOutFormat==NULL)
348 +        {
349 +          cerr << program_name << ": cannot write output format!" << endl;
350 +          usage();
351 +        }
352 +    }
353    
354 <  if (OutputFileName.empty())
354 >    if(!Conv.SetInFormat(pInFormat))
355      {
356 <      pos = FileList.front().rfind(".");
357 <      if(pos==string::npos)
358 <        OutputFileName = FileList.front()+ "." + outputExt;
356 >      cerr << "Invalid input format" << endl;
357 >      usage();
358 >    }
359 >    if(!Conv.SetOutFormat(pOutFormat))
360 >    {
361 >      cerr << "Invalid output format" << endl;
362 >      usage();
363 >    }
364 >
365 >  if(SplitOrBatch)
366 >    {
367 >      //Put * into output file name before extension (or ext.gz)
368 >      if(OutputFileName.empty())
369 >        {
370 >          OutputFileName = "*.";
371 >          if (oext != NULL)
372 >            OutputFileName += oext;
373 >        }
374        else
375 <        OutputFileName = FileList.front().substr(0, pos) + "." + outputExt;            
375 >        {
376 >          string::size_type pos = OutputFileName.rfind(".gz");
377 >          if(pos==string::npos)
378 >            pos = OutputFileName.rfind('.');
379 >          else
380 >            pos = OutputFileName.rfind('.',pos-1);
381 >          if(pos==string::npos)
382 >            OutputFileName += '*';
383 >          else
384 >            OutputFileName.insert(pos,"*");
385 >        }
386      }
387 <  
272 <  Conv.SetInAndOutFormats(pInFormat,pOutFormat);
273 <  
274 <  
275 <  // send info message to clog -- don't mess up cerr or cout for user programs
387 >
388    int count = Conv.FullConvert(FileList, OutputFileName, OutputFileList);
389 <  if ( count == 1 )
390 <    clog << count << " molecule converted" << endl;
391 <  else
280 <    clog << count << " molecules converted" << endl;
281 <  
389 >
390 >  Conv.ReportNumberConverted(count);
391 >
392    if(OutputFileList.size()>1)
393      {
394        clog << OutputFileList.size() << " files output. The first is " << OutputFileList[0] <<endl;
395      }
396 <  
396 >
397 >  std::string messageSummary = obErrorLog.GetMessageSummary();
398 >  if (messageSummary.size())
399 >    {
400 >      clog << messageSummary << endl;
401 >    }
402 >
403   #ifdef _DEBUG
404    //CM keep window open
405    cout << "Press any key to finish" <<endl;
# Line 293 | Line 409 | int main(int argc,char *argv[])
409    return 0;
410   }
411  
412 < void DoOption(const char* p, OBConversion& Conv, OBConversion::Option_type typ, int& arg, int argc, char *argv[])
412 > void DoOption(const char* p, OBConversion& Conv,
413 >              OBConversion::Option_type typ, int& arg, int argc, char *argv[])
414   {
415    while(p && *p) //can have multiple single char options
416 +  {
417 +    char ch[2]="?";
418 +    *ch = *p++;
419 +    const char* txt=NULL;                              
420 +    //Get the option text if needed
421 +    int nParams = Conv.GetOptionParams(ch, typ);
422 +    if(nParams)
423      {
424 <      char ch[2]="?";
425 <      *ch = *p++;
426 <      const char* txt=NULL;                            
427 <      //Get the option text if needed
428 <      int nParams = Conv.GetOptionParams(ch, typ);
429 <      if(nParams)
424 >      if(*p)
425 >      {
426 >        txt = p; //use text immediately following the option letter
427 >        p=NULL; //no more single char options
428 >      }
429 >      else if(arg<argc-1)
430 >      {
431 >        txt = argv[++arg]; //use text from next arg
432 >        if(*txt=='-')
433          {
434 <          if(*p)
435 <            {
436 <              txt = p; //use text immediately following the option letter
310 <              p=NULL; //no more single char options
311 <            }
312 <          else if(arg<argc-1)
313 <            {
314 <              txt = argv[++arg]; //use text from next arg
315 <              if(*txt=='-')
316 <                {
317 <                  //...unless it is another option
318 <                  cerr << "Option -" << ch << " takes a parameter" << endl;
319 <                  exit(0);
320 <                }
321 <            }
434 >          //...unless it is another option
435 >          cerr << "Option -" << ch << " takes a parameter" << endl;
436 >          exit(0);
437          }
438 <      Conv.AddOption(ch, typ, txt);
438 >      }
439      }
440 +    Conv.AddOption(ch, typ, txt);
441 +  }
442   }
443  
444   void usage()
445   {
446 <  cout << "atom2md: part of OOPSE " <<
447 <    OOPSE_VERSION_MAJOR << "." << OOPSE_VERSION_MINOR << "." <<
448 <    OOPSE_VERSION_TINY << " and Open Babel " << BABEL_VERSION << " -- "
446 >  cout << program_name << ": part of OpenMD " <<
447 >    OPENMD_VERSION_MAJOR << "." << OPENMD_VERSION_MINOR << "." <<
448 >    OPENMD_VERSION_TINY << " and OpenBabel " << BABEL_VERSION << " -- "
449         << __DATE__ << " -- " << __TIME__ << endl;
450    cout << "Usage: " << program_name
451         << " [-i<input-type>] <name> [-o<output-type>] <name>" << endl;
452    cout << "Try  -H option for more information." << endl;
453 < /*  
453 >
454   #ifdef _DEBUG
455    //CM keep window open
456    cout << "Press any key to finish" <<endl;
457    getch();
458   #endif
342 */  
459    exit (0);
460   }
461  
462   void help()
463   {
464 <  cout << "Open Babel converts chemical structures from one file format to another"<< endl << endl;
464 >  cout << program_name << " converts chemical structures from one file format to another"<< endl << endl;
465    cout << "Usage: " << program_name << " <input spec> <output spec> [Options]" << endl << endl;
466    cout << "Each spec can be a file whose extension decides the format." << endl;
467    cout << "Optionally the format can be specified by preceding the file by" << endl;
# Line 377 | Line 493 | void help()
493    while(OBConversion::GetNextFormat(pos,str,pFormat))
494      {
495        if((pFormat->Flags() & NOTWRITABLE) && (pFormat->Flags() & NOTREADABLE))
496 <        continue;
496 >        continue;
497        cout << "  " << str << endl;
498      }
499    cout << "\nSee further specific info and options using -H<format-type>, e.g. -Hpdb" << endl;

Comparing trunk/src/applications/atom2md/atom2md.cpp (property svn:keywords):
Revision 1021 by gezelter, Wed Aug 2 19:40:39 2006 UTC vs.
Revision 1442 by gezelter, Mon May 10 17:28:26 2010 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines