Open Babel  3.0
obconversion.h
Go to the documentation of this file.
1 /**********************************************************************
2 obconversion.h - Handle file conversions. Declaration of OBFormat, OBConversion
3 
4 Copyright (C) 2004-2009 by Chris Morley
5 
6 This file is part of the Open Babel project.
7 For more information, see <http://openbabel.org/>
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation version 2 of the License.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 ***********************************************************************/
18 
19 #ifndef OB_CONV_H
20 #define OB_CONV_H
21 
22 #include <openbabel/babelconfig.h>
23 
24 #include <iostream>
25 #include <fstream>
26 #include <sstream>
27 #include <cassert>
28 
29 #include <string>
30 #include <vector>
31 #include <map>
32 #ifdef HAVE_STRINGS_H
33 #include <strings.h>
34 #endif
35 
36 #include <openbabel/oberror.h>
37 #include <openbabel/format.h>
38 #include <openbabel/lineend.h>
39 
40 // These macros are used in DLL builds. If they have not
41 // been set in babelconfig.h, define them as nothing.
42 #ifndef OBCONV
43  #define OBCONV
44 #endif
45 #ifndef OBDLL
46  #define OBDLL
47 #endif
48 
49 namespace OpenBabel {
50 
51  // Needed to preserve deprecated API
53 
54  OBERROR extern OBMessageHandler obErrorLog;
55 
56  //*************************************************
58  // Class introduction in obconversion.cpp
59  class OBCONV OBConversion
60  {
62  public:
64 
65  OBConversion(std::istream* is=NULL, std::ostream* os=NULL);
66  OBConversion(std::string inFilename, std::string outFilename="");
68  OBConversion(const OBConversion& o);
70  OBConversion& operator=(const OBConversion& rhs);
71 
72  virtual ~OBConversion();
74 
76  static int RegisterFormat(const char* ID, OBFormat* pFormat, const char* MIME = NULL);
79  static OBFormat* FindFormat(const char* ID);
82  static OBFormat* FindFormat(const std::string ID);
84  static OBFormat* FormatFromExt(const char* filename);
85  static OBFormat* FormatFromExt(const char* filename, bool& isgzip);
88  static OBFormat* FormatFromExt(const std::string filename);
89  static OBFormat* FormatFromExt(const std::string filename, bool& isgzip);
91  static OBFormat* FormatFromMIME(const char* MIME);
92 
94 #ifndef SWIG
95  static bool GetNextFormat(Formatpos& itr, const char*& str,OBFormat*& pFormat);
96 #endif
97 
98 
100 
101  static const char* Description(); //generic conversion options
103 
106 
107  std::istream* GetInStream() const {return pInput;};
108  std::ostream* GetOutStream() const {return pOutput;};
109 
112  // if false, then will be treated as gzipped stream only if z/zin is set.
113  void SetInStream(std::istream* pIn, bool takeOwnership=false);
114  void SetOutStream(std::ostream* pOut, bool takeOwnership=false);
115 
117  bool SetInAndOutFormats(const char* inID, const char* outID, bool ingzip=false, bool outgzip=false);
118  bool SetInAndOutFormats(OBFormat* pIn, OBFormat* pOut, bool ingzip=false, bool outgzip=false);
120  bool SetInFormat(const char* inID, bool isgzip=false);
121  bool SetInFormat(OBFormat* pIn, bool isgzip=false);
123  bool SetOutFormat(const char* outID, bool isgzip=false);
124  bool SetOutFormat(OBFormat* pOut, bool isgzip=false);
125 
126  OBFormat* GetInFormat() const{return pInFormat;};
127  OBFormat* GetOutFormat() const{return pOutFormat;};
128  bool GetInGzipped() const{return inFormatGzip;};
129  bool GetOutGzipped() const{return outFormatGzip;};
130  std::string GetInFilename() const{return InFilename;};
131  std::string GetOutFilename() const{return OutFilename;};
132 
134  std::streampos GetInPos()const{return wInpos;};
135 
137  size_t GetInLen()const{return wInlen;};
138 
140  const char* GetTitle() const;
141 
143  OBConversion* GetAuxConv() const {return pAuxConv;};
144  void SetAuxConv(OBConversion* pConv) {pAuxConv=pConv;};
146 
183  enum Option_type { INOPTIONS, OUTOPTIONS, GENOPTIONS, ALL };
185 
187  const char* IsOption(const char* opt,Option_type opttyp=OUTOPTIONS);
188 
190  const std::map<std::string,std::string>* GetOptions(Option_type opttyp)
191  { return &OptionsArray[opttyp];};
192 
194  void AddOption(const char* opt, Option_type opttyp=OUTOPTIONS, const char* txt=NULL);
195 
196  bool RemoveOption(const char* opt, Option_type optype);
197 
199  void SetOptions(const char* options, Option_type opttyp);
200 
202  static void RegisterOptionParam(std::string name, OBFormat* pFormat,
203  int numberParams=0, Option_type typ=OUTOPTIONS);
204 
206  static int GetOptionParams(std::string name, Option_type typ);
208 
210  void CopyOptions(OBConversion* pSourceConv, Option_type typ=ALL);
211 
213 
214  // @brief Set and return the list of supported input format
215  std::vector<std::string> GetSupportedInputFormat();
216  // @brief Set and return the list of supported output format
217  std::vector<std::string> GetSupportedOutputFormat();
219 
221 
222  int Convert(std::istream* is, std::ostream* os);
224 
226  int Convert();
227 
230  int FullConvert(std::vector<std::string>& FileList,
231  std::string& OutputFileName, std::vector<std::string>& OutputFileList);
233 
235 
236  int AddChemObject(OBBase* pOb);
237  OBBase* GetChemObject();
238  bool IsLast();
239  bool IsFirstInput();
240  void SetFirstInput(bool b=true);
241  int GetOutputIndex() const ;
242  void SetOutputIndex(int indx);
243  void SetMoreFilesToCome();
244  void SetOneObjectOnly(bool b=true);
245  void SetLast(bool b){SetOneObjectOnly(b);}
246  bool IsLastFile(){ return !MoreFilesToCome;}
247  int GetCount()const { return Count; }
251 
253  static OBFormat* GetDefaultFormat(){return OBFormat::FindType(NULL);};
255 
257 
260  bool Write(OBBase* pOb, std::ostream* pout=NULL);
261 
263 
269  std::string WriteString(OBBase* pOb, bool trimWhitespace = false);
270 
272 
277  bool WriteFile(OBBase* pOb, std::string filePath);
278 
283  void CloseOutFile();
284 
286 
290  bool Read(OBBase* pOb, std::istream* pin=NULL);
291 
295 // OBBase* ReadObject(std::istream* pin=NULL);
296 
298 
303  bool ReadString(OBBase* pOb, std::string input);
304 
306 
313  bool ReadFile(OBBase* pOb, std::string filePath);
314 
322  bool OpenInAndOutFiles(std::string infilepath, std::string outfilepath);
323 
328  void ReportNumberConverted(int count, OBFormat* pFormat=NULL);
329 
333  int NumInputObjects();
334 
335 
336 protected:
338  static std::string BatchFileName(std::string& BaseName, std::string& InFile);
340  static std::string IncrementedFileName(std::string& BaseName, const int Count);
342  static bool CheckForUnintendedBatch(const std::string& infile, const std::string& outfile);
343 
344  void ClearInStreams();
346 
347  protected:
348 
349  //helper class for saving stream state
350  struct StreamState
351  {
352  std::ios *pStream; //active stream
353  std::vector<std::ios *> ownedStreams; //streams we own the memory to
354 
355  StreamState(): pStream(NULL) {}
357  {
358  assert(ownedStreams.size() == 0); //should be popped
359  }
360 
361  void pushInput(OBConversion& conv);
362  void popInput(OBConversion& conv);
363 
364  void pushOutput(OBConversion& conv);
365  void popOutput(OBConversion& conv);
366 
367  bool isSet() const { return pStream != NULL; }
368  };
369 
370  bool SetStartAndEnd();
371 // static FMapType& FormatsMap();///<contains ID and pointer to all OBFormat classes
372 // static FMapType& FormatsMIMEMap();///<contains MIME and pointer to all OBFormat classes
373  typedef std::map<std::string,int> OPAMapType;
374  static OPAMapType& OptionParamArray(Option_type typ);
375  bool OpenAndSetFormat(bool SetFormat, std::ifstream* is, std::stringstream* ss=NULL);
376 
377  std::string InFilename, OutFilename; //OutFileName added v2.4.0
378 
380 
381  std::istream *pInput; //input stream, may be filtered
382  std::vector<std::istream *> ownedInStreams; //streams we own the memory to
383 
384  std::ostream *pOutput; //output stream, may have filters applied
385  std::vector<std::ostream *> ownedOutStreams; //streams we own the memory to
386 
387 
391 
392  std::map<std::string,std::string> OptionsArray[3];
393 
394  int Index;
395  unsigned int StartNumber;
396  unsigned int EndNumber;
397  int Count;
399  bool m_IsLast;
404 
405  //unlike the z and zin options, these are not sticky - setting formats will reset them
408 
410  std::streampos wInpos;
411  std::streampos rInpos;
412  size_t wInlen;
413  size_t rInlen;
414 
416 
417  std::vector<std::string> SupportedInputFormat;
418  std::vector<std::string> SupportedOutputFormat;
419 
420  };
421 
422 } //namespace OpenBabel
423 #endif //OB_CONV_H
424 
427 
428 
bool ReadyToInput
Definition: obconversion.h:402
Translate line endings automatically (UNIX, Classic Mac, DOS)
StreamState()
Definition: obconversion.h:355
const std::map< std::string, std::string > * GetOptions(Option_type opttyp)
Access the map with option name as key and any associated text as value.
Definition: obconversion.h:190
std::ostream * pOutput
Definition: obconversion.h:384
void SetAuxConv(OBConversion *pConv)
Definition: obconversion.h:144
unsigned int EndNumber
Definition: obconversion.h:396
unsigned int StartNumber
Definition: obconversion.h:395
std::string GetOutFilename() const
Definition: obconversion.h:131
OBMessageHandler obErrorLog
Global OBMessageHandler error handler.
Definition: oberror.h:187
bool inFormatGzip
skip molecules using -f and -l
Definition: obconversion.h:406
std::vector< std::ios * > ownedStreams
Definition: obconversion.h:353
std::streampos wInpos
position in the input stream of the object being written
Definition: obconversion.h:410
OBFormat * GetOutFormat() const
Definition: obconversion.h:127
bool isSet() const
Definition: obconversion.h:367
Class to convert from one format to another.
Definition: obconversion.h:59
Handle error messages, warnings, debugging information and the like.
Definition: oberror.h:106
bool MoreFilesToCome
Definition: obconversion.h:400
bool m_IsFirstInput
Definition: obconversion.h:398
Definition: obconversion.h:350
std::streampos rInpos
position in the input stream of the object being read
Definition: obconversion.h:411
std::vector< std::ostream * > ownedOutStreams
Definition: obconversion.h:385
static OBFormat * pDefaultFormat
Definition: obconversion.h:388
OBConversion * pAuxConv
Way to extend OBConversion.
Definition: obconversion.h:415
std::ios * pStream
Definition: obconversion.h:352
std::vector< std::string > SupportedOutputFormat
list of supported output format
Definition: obconversion.h:418
int Count
Definition: obconversion.h:397
bool SkippedMolecules
Definition: obconversion.h:403
bool IsLastFile()
True if no more files to be read.
Definition: obconversion.h:246
void SetLast(bool b)
Synonym for SetOneObjectOnly()
Definition: obconversion.h:245
OBBase * pOb1
Definition: obconversion.h:409
PluginMapType::const_iterator PluginIterator
Definition: plugin.h:60
OBFormat * pInFormat
Definition: obconversion.h:389
std::map< std::string, int > OPAMapType
Definition: obconversion.h:373
std::string GetInFilename() const
Definition: obconversion.h:130
OBFormat * pOutFormat
Definition: obconversion.h:390
OBFormat * GetInFormat() const
Definition: obconversion.h:126
size_t rInlen
length in the input stream of the object being read
Definition: obconversion.h:413
size_t GetInLen() const
Get the length in the input stream of the object being read.
Definition: obconversion.h:137
std::istream * pInput
Definition: obconversion.h:381
Option_type
Three types of options set on the the command line by -a? , -x? , or -?
Definition: obconversion.h:184
bool outFormatGzip
Definition: obconversion.h:407
OBPlugin::PluginIterator Formatpos
Definition: obconversion.h:52
std::string OutFilename
Definition: obconversion.h:377
bool GetOutGzipped() const
Definition: obconversion.h:129
OBConversion * GetAuxConv() const
Extension method: deleted in ~OBConversion()
Definition: obconversion.h:143
bool OneObjectOnly
Definition: obconversion.h:401
std::streampos GetInPos() const
Get the position in the input stream of the object being read.
Definition: obconversion.h:134
FilteringInputStream< LineEndingExtractor > LEInStream
Definition: obconversion.h:379
Handle error messages, warnings, notices, etc.
size_t wInlen
length in the input stream of the object being written
Definition: obconversion.h:412
int Index
Definition: obconversion.h:394
~StreamState()
Definition: obconversion.h:356
std::ostream * GetOutStream() const
Definition: obconversion.h:108
Base class for file formats.
Definition: format.h:44
bool m_IsLast
Definition: obconversion.h:399
Base Class.
Definition: base.h:239
std::vector< std::istream * > ownedInStreams
Definition: obconversion.h:382
bool GetInGzipped() const
Definition: obconversion.h:128
Declarations for OBFormat.
A stream interface for FilteringInputStreambuf.
Definition: lineend.h:195
std::istream * GetInStream() const
Definition: obconversion.h:107
Global namespace for all Open Babel code.
Definition: alias.h:22
std::vector< std::string > SupportedInputFormat
list of supported input format
Definition: obconversion.h:417