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.org/>
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);
00079       static OBFormat*  FindFormat(const std::string ID);
00081       static OBFormat*  FormatFromExt(const char* filename);
00084       static OBFormat*  FormatFromExt(const std::string filename);
00086       static OBFormat*        FormatFromMIME(const char* MIME);
00087 
00089       static bool               GetNextFormat(Formatpos& itr, const char*& str,OBFormat*& pFormat);
00091 
00093 
00094       static const char* Description(); //generic conversion options
00096 
00098 
00099       std::istream* GetInStream() const {return pInStream;};
00100       std::ostream* GetOutStream() const {return pOutStream;};
00101       void          SetInStream(std::istream* pIn)
00102         {
00103           if (pInStream && NeedToFreeInStream) {
00104             delete pInStream; NeedToFreeInStream = false;
00105           }
00106           pInStream=pIn;
00107           CheckedForGzip = false; // haven't tried to gzip decode this stream
00108         };
00109       void          SetOutStream(std::ostream* pOut)
00110         {
00111           if (pOutStream && NeedToFreeOutStream) {
00112             delete pOutStream; NeedToFreeOutStream = false;
00113           }
00114           pOutStream=pOut;
00115         };
00117       bool        SetInAndOutFormats(const char* inID, const char* outID);
00118       bool        SetInAndOutFormats(OBFormat* pIn, OBFormat* pOut);
00120       bool            SetInFormat(const char* inID);
00121       bool            SetInFormat(OBFormat* pIn);
00123       bool            SetOutFormat(const char* outID);
00124       bool            SetOutFormat(OBFormat* pOut);
00125 
00126       OBFormat*   GetInFormat() const{return pInFormat;};
00127       OBFormat*   GetOutFormat() const{return pOutFormat;};
00128       std::string GetInFilename() const{return InFilename;};
00129 
00131       std::streampos GetInPos()const{return wInpos;};
00132 
00134       size_t GetInLen()const{return wInlen;};
00135 
00137       const char* GetTitle() const;
00138 
00140       OBConversion* GetAuxConv() const {return pAuxConv;};
00141       void          SetAuxConv(OBConversion* pConv) {pAuxConv=pConv;};
00143 
00180 
00181       enum Option_type { INOPTIONS, OUTOPTIONS, GENOPTIONS, ALL };
00182 
00184       const char* IsOption(const char* opt,Option_type opttyp=OUTOPTIONS);
00185 
00187       const std::map<std::string,std::string>* GetOptions(Option_type opttyp)
00188         { return &OptionsArray[opttyp];};
00189 
00191       void AddOption(const char* opt, Option_type opttyp=OUTOPTIONS, const char* txt=NULL);
00192 
00193       bool RemoveOption(const char* opt, Option_type optype);
00194 
00196       void SetOptions(const char* options, Option_type opttyp);
00197 
00199       static void RegisterOptionParam(std::string name, OBFormat* pFormat,
00200                                       int numberParams=0, Option_type typ=OUTOPTIONS);
00201 
00203       static int GetOptionParams(std::string name, Option_type typ);
00205 
00207       void CopyOptions(OBConversion* pSourceConv, Option_type typ=ALL);
00208 
00210 
00211       // @brief Set and return the list of supported input format
00212       std::vector<std::string> GetSupportedInputFormat();
00213       // @brief Set and return the list of supported output format
00214       std::vector<std::string> GetSupportedOutputFormat();
00216 
00218 
00219 
00220       int         Convert(std::istream* is, std::ostream* os);
00221 
00223       int         Convert();
00224 
00227       int                                       FullConvert(std::vector<std::string>& FileList,
00228                               std::string& OutputFileName, std::vector<std::string>& OutputFileList);
00230 
00232 
00233       int     AddChemObject(OBBase* pOb);
00234       OBBase*  GetChemObject(); 
00235       bool     IsLast();
00236       bool     IsFirstInput();
00237       void     SetFirstInput(bool b=true);
00238       int      GetOutputIndex() const ;
00239       void     SetOutputIndex(int indx);
00240       void     SetMoreFilesToCome();
00241       void     SetOneObjectOnly(bool b=true);
00242       void     SetLast(bool b){SetOneObjectOnly(b);}
00243       bool     IsLastFile(){ return !MoreFilesToCome;}
00244 
00245 
00246       int      GetCount()const { return Count; }
00248 
00249 
00250 
00251       static OBFormat* GetDefaultFormat(){return OBFormat::FindType(NULL);};
00252 
00254 
00257       bool                              Write(OBBase* pOb, std::ostream* pout=NULL);
00258 
00260 
00266       std::string                     WriteString(OBBase* pOb, bool trimWhitespace = false);
00267 
00269 
00274       bool                            WriteFile(OBBase* pOb, std::string filePath);
00275 
00280       void CloseOutFile();
00281 
00283 
00287       bool      Read(OBBase* pOb, std::istream* pin=NULL);
00288 
00292 //      OBBase* ReadObject(std::istream* pin=NULL);
00293 
00295 
00299       bool      ReadString(OBBase* pOb, std::string input);
00300 
00302 
00309       bool      ReadFile(OBBase* pOb, std::string filePath);
00310 
00316       bool OpenInAndOutFiles(std::string infilepath, std::string outfilepath);
00317 
00322       void ReportNumberConverted(int count, OBFormat* pFormat=NULL);
00323 
00327       int NumInputObjects();
00328 
00329 
00330 protected:
00332       static std::string BatchFileName(std::string& BaseName, std::string& InFile);
00334       static std::string IncrementedFileName(std::string& BaseName, const int Count);
00336       static bool CheckForUnintendedBatch(const std::string& infile, const std::string& outfile);
00338       void InstallStreamFilter();
00339 
00341 
00342     protected:
00343       bool             SetStartAndEnd();
00344 //      static FMapType& FormatsMap();///<contains ID and pointer to all OBFormat classes
00345 //      static FMapType& FormatsMIMEMap();///<contains MIME and pointer to all OBFormat classes
00346       typedef std::map<std::string,int> OPAMapType;
00347       static OPAMapType& OptionParamArray(Option_type typ);
00348       static int       LoadFormatFiles();
00349       bool             OpenAndSetFormat(bool SetFormat, std::ifstream* is, std::stringstream* ss=NULL);
00350 
00351       std::string         InFilename;
00352       std::istream*     pInStream;
00353       std::ostream*     pOutStream;
00354       static OBFormat*  pDefaultFormat;
00355       OBFormat*           pInFormat;
00356       OBFormat*   pOutFormat;
00357 
00358       std::map<std::string,std::string> OptionsArray[3];
00359 
00360       int                 Index;
00361       unsigned int        StartNumber;
00362       unsigned int        EndNumber;
00363       int                 Count;
00364       bool                      m_IsFirstInput;
00365       bool                m_IsLast;
00366       bool                MoreFilesToCome;
00367       bool                OneObjectOnly;
00368       bool                ReadyToInput;
00369       bool      CheckedForGzip;      
00370       bool      NeedToFreeInStream;
00371       bool      NeedToFreeOutStream;
00372       typedef   FilteringInputStreambuf< LineEndingExtractor > LErdbuf;
00373       LErdbuf*  pLineEndBuf;
00374 
00375       static int FormatFilesLoaded;
00376       OBBase*             pOb1;
00377       std::streampos wInpos; 
00378       std::streampos rInpos; 
00379       size_t wInlen; 
00380       size_t rInlen; 
00381 
00382       OBConversion* pAuxConv;
00383 
00384       std::vector<std::string> SupportedInputFormat; 
00385       std::vector<std::string> SupportedOutputFormat; 
00386 
00387     };
00388 
00389 } //namespace OpenBabel
00390 #endif //OB_CONV_H
00391 
00394 
00395 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines