00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00032 #include <openbabel/dlhandler.h>
00033 #include <openbabel/oberror.h>
00034 #include <openbabel/format.h>
00035 #include <openbabel/lineend.h>
00036
00037
00038
00039 #ifndef OBCONV
00040 #define OBCONV
00041 #endif
00042 #ifndef OBDLL
00043 #define OBDLL
00044 #endif
00045
00046
00047 namespace OpenBabel {
00048
00049
00050 typedef OBPlugin::PluginIterator Formatpos;
00051
00052 OBERROR extern OBMessageHandler obErrorLog;
00053
00054
00056
00057 class OBCONV OBConversion
00058 {
00060 public:
00062
00063 OBConversion(std::istream* is=NULL, std::ostream* os=NULL);
00065 OBConversion(const OBConversion& o);
00066 virtual ~OBConversion();
00068
00069
00070
00071 static int RegisterFormat(const char* ID, OBFormat* pFormat, const char* MIME = NULL);
00073 static OBFormat* FindFormat(const char* ID);
00075 static OBFormat* FormatFromExt(const char* filename);
00077 static OBFormat* FormatFromMIME(const char* MIME);
00078
00080 static bool GetNextFormat(Formatpos& itr, const char*& str,OBFormat*& pFormat);
00082
00084
00085 static const char* Description();
00087
00089
00090 std::istream* GetInStream() const {return pInStream;};
00091 std::ostream* GetOutStream() const {return pOutStream;};
00092 void SetInStream(std::istream* pIn)
00093 {
00094 if (pInStream && NeedToFreeInStream) {
00095 delete pInStream; NeedToFreeInStream = false;
00096 }
00097 pInStream=pIn;
00098 CheckedForGzip = false;
00099 };
00100 void SetOutStream(std::ostream* pOut)
00101 {
00102 if (pOutStream && NeedToFreeOutStream) {
00103 delete pOutStream; NeedToFreeOutStream = false;
00104 }
00105 pOutStream=pOut;
00106 };
00108 bool SetInAndOutFormats(const char* inID, const char* outID);
00109 bool SetInAndOutFormats(OBFormat* pIn, OBFormat* pOut);
00111 bool SetInFormat(const char* inID);
00112 bool SetInFormat(OBFormat* pIn);
00114 bool SetOutFormat(const char* outID);
00115 bool SetOutFormat(OBFormat* pOut);
00116
00117 OBFormat* GetInFormat() const{return pInFormat;};
00118 OBFormat* GetOutFormat() const{return pOutFormat;};
00119 std::string GetInFilename() const{return InFilename;};
00120
00122 std::streampos GetInPos()const{return wInpos;};
00123
00125 size_t GetInLen()const{return wInlen;};
00126
00128 const char* GetTitle() const;
00129
00131 OBConversion* GetAuxConv() const {return pAuxConv;};
00132 void SetAuxConv(OBConversion* pConv) {pAuxConv=pConv;};
00134
00135
00136
00137 enum Option_type { INOPTIONS, OUTOPTIONS, GENOPTIONS, ALL };
00138
00140 const char* IsOption(const char* opt,Option_type opttyp=OUTOPTIONS);
00141
00143 const std::map<std::string,std::string>* GetOptions(Option_type opttyp)
00144 { return &OptionsArray[opttyp];};
00145
00147 void AddOption(const char* opt, Option_type opttyp=OUTOPTIONS, const char* txt=NULL);
00148
00149 bool RemoveOption(const char* opt, Option_type optype);
00150
00152 void SetOptions(const char* options, Option_type opttyp);
00153
00155 static void RegisterOptionParam(std::string name, OBFormat* pFormat,
00156 int numberParams=0, Option_type typ=OUTOPTIONS);
00157
00159 static int GetOptionParams(std::string name, Option_type typ);
00161
00163 void CopyOptions(OBConversion* pSourceConv, Option_type typ=ALL);
00164
00166
00167
00168 std::vector<std::string> GetSupportedInputFormat();
00169
00170 std::vector<std::string> GetSupportedOutputFormat();
00172
00174
00175
00176 int Convert(std::istream* is, std::ostream* os);
00177
00179 int Convert();
00180
00183 int FullConvert(std::vector<std::string>& FileList,
00184 std::string& OutputFileName, std::vector<std::string>& OutputFileList);
00186
00188
00189 int AddChemObject(OBBase* pOb);
00190 OBBase* GetChemObject();
00191 bool IsLast();
00192 bool IsFirstInput();
00193 void SetFirstInput(bool b=true);
00194 int GetOutputIndex() const ;
00195 void SetOutputIndex(int indx);
00196 void SetMoreFilesToCome();
00197 void SetOneObjectOnly(bool b=true);
00198 void SetLast(bool b){SetOneObjectOnly(b);}
00199 bool IsLastFile(){ return !MoreFilesToCome;}
00200
00201
00202
00203
00204 static OBFormat* GetDefaultFormat(){return OBFormat::FindType(NULL);};
00205
00207
00210 bool Write(OBBase* pOb, std::ostream* pout=NULL);
00211
00213
00219 std::string WriteString(OBBase* pOb, bool trimWhitespace = false);
00220
00222
00227 bool WriteFile(OBBase* pOb, std::string filePath);
00228
00233 void CloseOutFile();
00234
00236
00240 bool Read(OBBase* pOb, std::istream* pin=NULL);
00241
00245
00246
00248
00252 bool ReadString(OBBase* pOb, std::string input);
00253
00255
00261 bool ReadFile(OBBase* pOb, std::string filePath);
00262
00268 bool OpenInAndOutFiles(std::string infilepath, std::string outfilepath);
00269
00274 void ReportNumberConverted(int count, OBFormat* pFormat=NULL);
00275
00276
00277 protected:
00279 static std::string BatchFileName(std::string& BaseName, std::string& InFile);
00281 static std::string IncrementedFileName(std::string& BaseName, const int Count);
00283 static bool CheckForUnintendedBatch(const std::string& infile, const std::string& outfile);
00285 void InstallStreamFilter();
00286
00288
00289 protected:
00290 bool SetStartAndEnd();
00291
00292
00293 typedef std::map<std::string,int> OPAMapType;
00294 static OPAMapType& OptionParamArray(Option_type typ);
00295 static int LoadFormatFiles();
00296 bool OpenAndSetFormat(bool SetFormat, std::ifstream* is);
00297
00298 std::string InFilename;
00299 std::istream* pInStream;
00300 std::ostream* pOutStream;
00301 static OBFormat* pDefaultFormat;
00302 OBFormat* pInFormat;
00303 OBFormat* pOutFormat;
00304
00305 std::map<std::string,std::string> OptionsArray[3];
00306
00307 int Index;
00308 unsigned int StartNumber;
00309 unsigned int EndNumber;
00310 int Count;
00311 bool m_IsFirstInput;
00312 bool m_IsLast;
00313 bool MoreFilesToCome;
00314 bool OneObjectOnly;
00315 bool ReadyToInput;
00316 bool CheckedForGzip;
00317 bool NeedToFreeInStream;
00318 bool NeedToFreeOutStream;
00319 typedef FilteringInputStreambuf< LineEndingExtractor > LErdbuf;
00320 LErdbuf* pLineEndBuf;
00321
00322 static int FormatFilesLoaded;
00323 OBBase* pOb1;
00324 std::streampos wInpos;
00325 std::streampos rInpos;
00326 size_t wInlen;
00327 size_t rInlen;
00328
00329 OBConversion* pAuxConv;
00330
00331 std::vector<std::string> SupportedInputFormat;
00332 std::vector<std::string> SupportedOutputFormat;
00333
00334 };
00335
00336 }
00337 #endif //OB_CONV_H
00338
00341
00342