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-2009 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 <openbabel/babelconfig.h>
00023 
00024 #include <iostream>
00025 #include <fstream>
00026 #include <sstream>
00027 
00028 #include <string>
00029 #include <vector>
00030 #include <map>
00031 #ifdef HAVE_STRINGS_H
00032 #include <strings.h>
00033 #endif
00034 
00035 #include <openbabel/dlhandler.h>
00036 #include <openbabel/oberror.h>
00037 #include <openbabel/format.h>
00038 #include <openbabel/lineend.h>
00039 
00040 // These macros are used in DLL builds. If they have not
00041 // been set in babelconfig.h, define them as nothing.
00042 #ifndef OBCONV
00043         #define OBCONV
00044 #endif
00045 #ifndef OBDLL
00046         #define OBDLL
00047 #endif
00048 
00049 //using namespace std;
00050 namespace OpenBabel {
00051 
00052   // Needed to preserve deprecated API
00053   typedef OBPlugin::PluginIterator Formatpos;
00054 
00055   OBERROR extern  OBMessageHandler obErrorLog;
00056 
00057   //*************************************************
00059   // Class introduction in obconversion.cpp
00060   class OBCONV OBConversion
00061     {
00063     public:
00065 
00066       OBConversion(std::istream* is=NULL, std::ostream* os=NULL);
00068       OBConversion(const OBConversion& o);
00069       virtual     ~OBConversion(); 
00071 
00072 
00073 
00074       static int                                RegisterFormat(const char* ID, OBFormat* pFormat, const char* MIME = NULL);
00076       static OBFormat*  FindFormat(const char* ID);
00078       static OBFormat*  FormatFromExt(const char* filename);
00080       static OBFormat*        FormatFromMIME(const char* MIME);
00081 
00083       static bool               GetNextFormat(Formatpos& itr, const char*& str,OBFormat*& pFormat);
00085                 
00087 
00088       static const char* Description(); //generic conversion options
00090 
00092 
00093       std::istream* GetInStream() const {return pInStream;};
00094       std::ostream* GetOutStream() const {return pOutStream;};
00095       void          SetInStream(std::istream* pIn)
00096         { 
00097           if (pInStream && NeedToFreeInStream) {
00098             delete pInStream; NeedToFreeInStream = false;
00099           }
00100           pInStream=pIn;
00101           CheckedForGzip = false; // haven't tried to gzip decode this stream
00102         };
00103       void          SetOutStream(std::ostream* pOut)
00104         {
00105           if (pOutStream && NeedToFreeOutStream) {
00106             delete pOutStream; NeedToFreeOutStream = false;
00107           }
00108           pOutStream=pOut;
00109         };
00111       bool        SetInAndOutFormats(const char* inID, const char* outID);
00112       bool        SetInAndOutFormats(OBFormat* pIn, OBFormat* pOut);
00114       bool            SetInFormat(const char* inID);
00115       bool            SetInFormat(OBFormat* pIn);
00117       bool            SetOutFormat(const char* outID);
00118       bool            SetOutFormat(OBFormat* pOut);
00119 
00120       OBFormat*   GetInFormat() const{return pInFormat;};
00121       OBFormat*   GetOutFormat() const{return pOutFormat;};
00122       std::string GetInFilename() const{return InFilename;};
00123         
00125       std::streampos GetInPos()const{return wInpos;}; 
00126 
00128       size_t GetInLen()const{return wInlen;}; 
00129 
00131       const char* GetTitle() const;
00132 
00134       OBConversion* GetAuxConv() const {return pAuxConv;};
00135       void          SetAuxConv(OBConversion* pConv) {pAuxConv=pConv;};
00137 
00138 
00139 
00140       enum Option_type { INOPTIONS, OUTOPTIONS, GENOPTIONS, ALL };
00141 
00143       const char* IsOption(const char* opt,Option_type opttyp=OUTOPTIONS);
00144         
00146       const std::map<std::string,std::string>* GetOptions(Option_type opttyp)
00147         { return &OptionsArray[opttyp];};
00148 
00150       void AddOption(const char* opt, Option_type opttyp=OUTOPTIONS, const char* txt=NULL);
00151         
00152       bool RemoveOption(const char* opt, Option_type optype);
00153 
00155       void SetOptions(const char* options, Option_type opttyp);
00156 
00158       static void RegisterOptionParam(std::string name, OBFormat* pFormat,
00159                                       int numberParams=0, Option_type typ=OUTOPTIONS);
00160 
00162       static int GetOptionParams(std::string name, Option_type typ);
00164 
00166       void CopyOptions(OBConversion* pSourceConv, Option_type typ=ALL);
00167 
00169 
00170       // @brief Set and return the list of supported input format
00171       std::vector<std::string> GetSupportedInputFormat();
00172       // @brief Set and return the list of supported output format
00173       std::vector<std::string> GetSupportedOutputFormat();
00175 
00177 
00178 
00179       int         Convert(std::istream* is, std::ostream* os);
00180 
00182       int         Convert();
00183 
00186       int                                       FullConvert(std::vector<std::string>& FileList,
00187                               std::string& OutputFileName, std::vector<std::string>& OutputFileList);
00189 
00191 
00192       int     AddChemObject(OBBase* pOb);
00193       OBBase*  GetChemObject(); 
00194       bool     IsLast();
00195       bool     IsFirstInput();
00196       void     SetFirstInput(bool b=true);
00197       int      GetOutputIndex() const ;
00198       void     SetOutputIndex(int indx);
00199       void     SetMoreFilesToCome();
00200       void     SetOneObjectOnly(bool b=true);
00201       void     SetLast(bool b){SetOneObjectOnly(b);}
00202       bool     IsLastFile(){ return !MoreFilesToCome;}
00203 
00204 
00205 
00206 
00207       static OBFormat* GetDefaultFormat(){return OBFormat::FindType(NULL);};
00208 
00210         
00213       bool                              Write(OBBase* pOb, std::ostream* pout=NULL);
00214 
00216         
00222       std::string                     WriteString(OBBase* pOb, bool trimWhitespace = false);
00223 
00225         
00230       bool                            WriteFile(OBBase* pOb, std::string filePath);
00231 
00236       void CloseOutFile();
00237 
00239         
00243       bool      Read(OBBase* pOb, std::istream* pin=NULL);
00244         
00248 //      OBBase* ReadObject(std::istream* pin=NULL);
00249 
00251         
00255       bool      ReadString(OBBase* pOb, std::string input);
00256 
00258         
00264       bool      ReadFile(OBBase* pOb, std::string filePath);
00265 
00271       bool OpenInAndOutFiles(std::string infilepath, std::string outfilepath);
00272 
00277       void ReportNumberConverted(int count, OBFormat* pFormat=NULL);
00278 
00282       int NumInputObjects();
00283 
00284 
00285 protected:
00287       static std::string BatchFileName(std::string& BaseName, std::string& InFile);
00289       static std::string IncrementedFileName(std::string& BaseName, const int Count);
00291       static bool CheckForUnintendedBatch(const std::string& infile, const std::string& outfile);
00293       void InstallStreamFilter();
00294 
00296 
00297     protected:
00298       bool             SetStartAndEnd();
00299 //      static FMapType& FormatsMap();///<contains ID and pointer to all OBFormat classes
00300 //      static FMapType& FormatsMIMEMap();///<contains MIME and pointer to all OBFormat classes
00301       typedef std::map<std::string,int> OPAMapType;
00302       static OPAMapType& OptionParamArray(Option_type typ);
00303       static int       LoadFormatFiles();
00304       bool             OpenAndSetFormat(bool SetFormat, std::ifstream* is);
00305 
00306       std::string         InFilename;
00307       std::istream*     pInStream;
00308       std::ostream*     pOutStream;
00309       static OBFormat*  pDefaultFormat;
00310       OBFormat*           pInFormat;
00311       OBFormat*   pOutFormat;
00312 
00313       std::map<std::string,std::string> OptionsArray[3];
00314 
00315       int                 Index;
00316       unsigned int        StartNumber;
00317       unsigned int        EndNumber;
00318       int                 Count;
00319       bool                      m_IsFirstInput;
00320       bool                m_IsLast;
00321       bool                MoreFilesToCome;
00322       bool                OneObjectOnly;
00323       bool                ReadyToInput;
00324       bool      CheckedForGzip;      
00325       bool      NeedToFreeInStream;
00326       bool      NeedToFreeOutStream;
00327       typedef   FilteringInputStreambuf< LineEndingExtractor > LErdbuf;
00328       LErdbuf*  pLineEndBuf;
00329 
00330       static int FormatFilesLoaded;
00331       OBBase*             pOb1;
00332       std::streampos wInpos; 
00333       std::streampos rInpos; 
00334       size_t wInlen; 
00335       size_t rInlen; 
00336         
00337       OBConversion* pAuxConv;
00338 
00339       std::vector<std::string> SupportedInputFormat; 
00340       std::vector<std::string> SupportedOutputFormat; 
00341 
00342     };
00343 
00344 } //namespace OpenBabel
00345 #endif //OB_CONV_H
00346 
00349 
00350