obconversion.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 obconversion.h - Handle file conversions. Declaration of OBFormat, OBConversion
00003 
00004 Copyright (C) 2004-2005 by Chris Morley
00005 
00006 This file is part of the Open Babel project.
00007 For more information, see <http://openbabel.sourceforge.net/>
00008 
00009 This program is free software; you can redistribute it and/or modify
00010 it under the terms of the GNU General Public License as published by
00011 the Free Software Foundation version 2 of the License.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 ***********************************************************************/
00018 
00019 #ifndef OB_CONV_H
00020 #define OB_CONV_H
00021 
00022 #include "babelconfig.h"
00023 
00024 #if HAVE_IOSTREAM
00025 #include <iostream>
00026 #elif HAVE_IOSTREAM_H
00027 #include <iostream.h>
00028 #endif
00029 #if HAVE_FSTREAM
00030 #include <fstream>
00031 #elif HAVE_FSTREAM_H
00032 #include <fstream.h>
00033 #endif
00034 
00035 #if HAVE_SSTREAM
00036         #include <sstream>
00037 #elif
00038         #include <sstream.h>
00039 #endif
00040 
00041 #include <string>
00042 #include <vector>
00043 #include <map>
00044 
00045 #include "dlhandler.h"
00046 
00047 // These macros are used in DLL builds. If they have not
00048 // been set in babelconfig.h, define them as nothing.
00049 #ifndef OBCONV
00050         #define OBCONV
00051 #endif
00052 #ifndef OBDLL
00053         #define OBDLL
00054 #endif
00055 
00056 //using namespace std;
00057 namespace OpenBabel {
00058 
00059 
00060 class OBBase;
00061 class OBConversion;
00062 //*************************************************
00063 
00065 
00083 class OBCONV OBFormat
00084 {
00085 public:
00087 
00091       virtual bool ReadMolecule(OBBase* /*pOb*/, OBConversion* /*pConv*/)
00092                 { std::cerr << "Not a valid input format"; return false;}
00093 
00095 
00099       virtual bool ReadChemObject(OBConversion* /*pConv*/)
00100                 { std::cerr << "Not a valid input format"; return false;}
00101 
00103 
00108       virtual bool WriteMolecule(OBBase* /*pOb*/, OBConversion* /*pConv*/)
00109                 { std::cerr << "Not a valid output format"; return false;}
00110 
00112 
00116       virtual bool WriteChemObject(OBConversion* /*pConv*/)
00117                 { std::cerr << "Not a valid output format"; return false;}
00118 
00120 
00124         virtual const char* Description()=0;
00125         
00127 
00129         virtual const char* TargetClassDescription();
00130 
00132 
00135         virtual const std::type_info& GetType();
00136         
00138         virtual const char* SpecificationURL() { return ""; }
00139 
00141         virtual const char* GetMIMEType() { return ""; }
00142 
00144    
00148         virtual unsigned int Flags() { return 0;}; 
00149 
00151 
00153       virtual int SkipObjects(int /*n*/, OBConversion* /*pConv*/)
00154         {
00155                 return 0; //shows not implemented in the format class
00156         };
00157 
00159 
00163         virtual OBFormat* MakeNewInstance()
00164         {
00165                 return NULL; //shows not implemented in the format class
00166         }
00167 
00169         virtual ~OBFormat(){};
00170 };
00171 
00172 //*************************************************
00174         struct CharPtrLess : public std::binary_function<const char*,const char*, bool>
00175         {
00176                 bool operator()(const char* p1,const char* p2) const
00177                 { return strcasecmp(p1,p2)<0; }
00178         };
00179 
00180         typedef std::map<const char*,OBFormat*,CharPtrLess > FMapType;
00181         typedef FMapType::iterator Formatpos;
00182 
00183 //*************************************************
00184 
00186 class OBCONV OBConversion
00187 {
00189 public:
00191 
00192                                                         OBConversion(std::istream* is=NULL, std::ostream* os=NULL);
00194                                                         OBConversion(const OBConversion& o);
00195         virtual     ~OBConversion(); 
00197 
00198 
00199 
00200         static int                              RegisterFormat(const char* ID, OBFormat* pFormat, const char* MIME = NULL);
00202         static OBFormat*        FindFormat(const char* ID);
00204         static OBFormat*        FormatFromExt(const char* filename);
00206         static OBFormat*        FormatFromMIME(const char* MIME);
00207 
00209         static bool             GetNextFormat(Formatpos& itr, const char*& str,OBFormat*& pFormat);
00211                 
00213 
00214         static const char* Description(); //generic conversion options
00216 
00218 
00219         std::istream* GetInStream() const {return pInStream;};
00220         std::ostream* GetOutStream() const {return pOutStream;};
00221         void          SetInStream(std::istream* pIn){pInStream=pIn;};
00222         void          SetOutStream(std::ostream* pOut){pOutStream=pOut;};
00223         bool          SetInAndOutFormats(const char* inID, const char* outID);
00224         bool          SetInAndOutFormats(OBFormat* pIn, OBFormat* pOut);
00225         bool          SetInFormat(const char* inID);
00226         bool          SetInFormat(OBFormat* pIn);
00227         bool          SetOutFormat(const char* outID);
00228         bool          SetOutFormat(OBFormat* pOut);
00229 
00230         OBFormat*   GetInFormat() const{return pInFormat;};
00231         OBFormat*   GetOutFormat() const{return pOutFormat;};
00232         std::string GetInFilename() const{return InFilename;};
00233         
00235         std::streampos GetInPos()const{return wInpos;}; 
00236 
00238         size_t GetInLen()const{return wInlen;}; 
00239 
00241         const char* GetTitle() const;
00242 
00244         OBConversion* GetAuxConv() const {return pAuxConv;};
00245         void          SetAuxConv(OBConversion* pConv) {pAuxConv=pConv;};
00247 
00248 
00249 
00250         enum Option_type { INOPTIONS, OUTOPTIONS, GENOPTIONS };
00251 
00253         const char* IsOption(const char* opt,Option_type opttyp=OUTOPTIONS);
00254         
00256         const std::map<std::string,std::string>* GetOptions(Option_type opttyp)
00257         { return &OptionsArray[opttyp];};
00258 
00260         void AddOption(const char* opt, Option_type opttyp, const char* txt=NULL);
00261         
00262         bool RemoveOption(const char* opt, Option_type optype);
00263 
00265         void SetOptions(const char* options, Option_type opttyp);
00266 
00268         static void RegisterOptionParam(std::string name, OBFormat* pFormat,
00269                                               int numberParams=0, Option_type typ=OUTOPTIONS);
00270 
00272         static int GetOptionParams(std::string name, Option_type typ);
00274 
00276 
00277 
00278         int         Convert(std::istream* is, std::ostream* os);
00279 
00281         int         Convert();
00282 
00285         int                                     FullConvert(std::vector<std::string>& FileList,
00286                                                                                 std::string& OutputFileName, std::vector<std::string>& OutputFileList);
00288 
00290 
00291         int                                     AddChemObject(OBBase* pOb);
00292         OBBase*                 GetChemObject(); 
00293         bool                            IsLast();
00294         bool                            IsFirstInput();
00295         int         GetOutputIndex() const ;
00296         void                            SetOutputIndex(int indx);
00297         void                            SetMoreFilesToCome();
00298         void                            SetOneObjectOnly();
00299 
00300 
00301 
00302 
00303         static OBFormat* GetDefaultFormat(){return pDefaultFormat;};
00304 
00306         
00309         bool                            Write(OBBase* pOb, std::ostream* pout=NULL);
00310 
00312         
00316         std::string                     WriteString(OBBase* pOb);
00317 
00319         
00324         bool                            WriteFile(OBBase* pOb, std::string filePath);
00325 
00327         
00331         bool    Read(OBBase* pOb, std::istream* pin=NULL);
00332 
00334         
00338           bool  ReadString(OBBase* pOb, std::string input);
00339 
00341         
00347           bool  ReadFile(OBBase* pOb, std::string filePath);
00348 
00349 
00351         static std::string BatchFileName(std::string& BaseName, std::string& InFile);
00353         static std::string IncrementedFileName(std::string& BaseName, const int Count);
00355 
00356 protected:
00357         bool             SetStartAndEnd();
00358         static FMapType& FormatsMap();
00359         static FMapType& FormatsMIMEMap();
00360         typedef std::map<std::string,int> OPAMapType;
00361         static OPAMapType& OptionParamArray(Option_type typ);
00362         static int       LoadFormatFiles();
00363         bool             OpenAndSetFormat(bool SetFormat, std::ifstream* is);
00364 
00365         std::string       InFilename;
00366         std::istream*     pInStream;
00367         std::ostream*     pOutStream;
00368         static OBFormat*  pDefaultFormat;
00369         OBFormat*         pInFormat;
00370         OBFormat*         pOutFormat;
00371 
00372         std::map<std::string,std::string> OptionsArray[3];
00373 
00374         int               Index;
00375         unsigned int      StartNumber;
00376         unsigned int      EndNumber;
00377         int               Count;
00378         bool              m_IsLast;
00379         bool              MoreFilesToCome;
00380         bool              OneObjectOnly;
00381         bool              ReadyToInput;
00382         bool              CheckedForGzip; 
00383         static int FormatFilesLoaded;
00384         OBBase*           pOb1;
00385         std::streampos wInpos; 
00386         std::streampos rInpos; 
00387         size_t wInlen; 
00388         size_t rInlen; 
00389         
00390         OBConversion* pAuxConv;
00391 };
00392 
00394 #define NOTREADABLE     0x01
00395 #define READONEONLY     0x02
00396 #define READBINARY      0x04
00397 #define NOTWRITABLE     0x10
00398 #define WRITEONEONLY    0x20
00399 #define WRITEBINARY     0x40
00400 #define DEFAULTFORMAT 0x4000
00401 
00402 } //namespace OpenBabel
00403 #endif //OB_CONV_H
00404 
00407 
00408