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/oberror.h> 00036 #include <openbabel/format.h> 00037 #include <openbabel/lineend.h> 00038 00039 // These macros are used in DLL builds. If they have not 00040 // been set in babelconfig.h, define them as nothing. 00041 #ifndef OBCONV 00042 #define OBCONV 00043 #endif 00044 #ifndef OBDLL 00045 #define OBDLL 00046 #endif 00047 00048 //using namespace std; 00049 namespace OpenBabel { 00050 00051 // Needed to preserve deprecated API 00052 typedef OBPlugin::PluginIterator Formatpos; 00053 00054 OBERROR extern OBMessageHandler obErrorLog; 00055 00056 //************************************************* 00058 // Class introduction in obconversion.cpp 00059 class OBCONV OBConversion 00060 { 00062 public: 00064 00065 OBConversion(std::istream* is=NULL, std::ostream* os=NULL); 00067 OBConversion(const OBConversion& o); 00068 virtual ~OBConversion(); 00070 00071 00072 00073 static int RegisterFormat(const char* ID, OBFormat* pFormat, const char* MIME = NULL); 00075 static OBFormat* FindFormat(const char* ID); 00078 static OBFormat* FindFormat(const std::string ID); 00080 static OBFormat* FormatFromExt(const char* filename); 00083 static OBFormat* FormatFromExt(const std::string filename); 00085 static OBFormat* FormatFromMIME(const char* MIME); 00086 00088 static bool GetNextFormat(Formatpos& itr, const char*& str,OBFormat*& pFormat); 00090 00092 00093 static const char* Description(); //generic conversion options 00095 00097 00098 std::istream* GetInStream() const {return pInStream;}; 00099 std::ostream* GetOutStream() const {return pOutStream;}; 00100 void SetInStream(std::istream* pIn) 00101 { 00102 if (pInStream && NeedToFreeInStream) { 00103 delete pInStream; NeedToFreeInStream = false; 00104 } 00105 pInStream=pIn; 00106 CheckedForGzip = false; // haven't tried to gzip decode this stream 00107 }; 00108 void SetOutStream(std::ostream* pOut) 00109 { 00110 if (pOutStream && NeedToFreeOutStream) { 00111 delete pOutStream; NeedToFreeOutStream = false; 00112 } 00113 pOutStream=pOut; 00114 }; 00116 bool SetInAndOutFormats(const char* inID, const char* outID); 00117 bool SetInAndOutFormats(OBFormat* pIn, OBFormat* pOut); 00119 bool SetInFormat(const char* inID); 00120 bool SetInFormat(OBFormat* pIn); 00122 bool SetOutFormat(const char* outID); 00123 bool SetOutFormat(OBFormat* pOut); 00124 00125 OBFormat* GetInFormat() const{return pInFormat;}; 00126 OBFormat* GetOutFormat() const{return pOutFormat;}; 00127 std::string GetInFilename() const{return InFilename;}; 00128 std::string GetOutFilename() const{return OutFilename;}; 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 bool OpenAndSetFormat(bool SetFormat, std::ifstream* is, std::stringstream* ss=NULL); 00349 00350 std::string InFilename, OutFilename; //OutFileName added v2.4.0 00351 std::istream* pInStream; 00352 std::ostream* pOutStream; 00353 static OBFormat* pDefaultFormat; 00354 OBFormat* pInFormat; 00355 OBFormat* pOutFormat; 00356 00357 std::map<std::string,std::string> OptionsArray[3]; 00358 00359 int Index; 00360 unsigned int StartNumber; 00361 unsigned int EndNumber; 00362 int Count; 00363 bool m_IsFirstInput; 00364 bool m_IsLast; 00365 bool MoreFilesToCome; 00366 bool OneObjectOnly; 00367 bool ReadyToInput; 00368 bool CheckedForGzip; 00369 bool NeedToFreeInStream; 00370 bool NeedToFreeOutStream; 00371 typedef FilteringInputStreambuf< LineEndingExtractor > LErdbuf; 00372 LErdbuf* pLineEndBuf; 00373 00374 OBBase* pOb1; 00375 std::streampos wInpos; 00376 std::streampos rInpos; 00377 size_t wInlen; 00378 size_t rInlen; 00379 00380 OBConversion* pAuxConv; 00381 00382 std::vector<std::string> SupportedInputFormat; 00383 std::vector<std::string> SupportedOutputFormat; 00384 00385 }; 00386 00387 } //namespace OpenBabel 00388 #endif //OB_CONV_H 00389 00392 00393