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
00035
00036
00037 #ifndef OBCONV
00038 #define OBCONV
00039 #endif
00040 #ifndef OBDLL
00041 #define OBDLL
00042 #endif
00043
00044
00045 namespace OpenBabel {
00046
00047
00048 class OBBase;
00049 class OBConversion;
00050 OBERROR extern OBMessageHandler obErrorLog;
00051
00052
00053
00055
00056 class OBCONV OBFormat
00057 {
00058 public:
00060
00064 virtual bool ReadMolecule(OBBase* , OBConversion* )
00065 { std::cerr << "Not a valid input format"; return false;}
00066
00068
00072 virtual bool ReadChemObject(OBConversion* )
00073 { std::cerr << "Not a valid input format"; return false;}
00074
00076
00081 virtual bool WriteMolecule(OBBase* , OBConversion* )
00082 { std::cerr << "Not a valid output format"; return false;}
00083
00085
00089 virtual bool WriteChemObject(OBConversion* )
00090 { std::cerr << "Not a valid output format"; return false;}
00091
00093
00097 virtual const char* Description()=0;
00098
00100
00102 virtual const char* TargetClassDescription();
00103
00105
00108 virtual const std::type_info& GetType();
00109
00111 virtual const char* SpecificationURL() { return ""; }
00112
00114 virtual const char* GetMIMEType() { return ""; }
00115
00117
00121 virtual unsigned int Flags() { return 0;};
00122
00124
00126 virtual int SkipObjects(int , OBConversion* )
00127 {
00128 return 0;
00129 };
00130
00132
00136 virtual OBFormat* MakeNewInstance()
00137 {
00138 return NULL;
00139 }
00140
00142 virtual ~OBFormat(){};
00143 };
00144
00145
00148 struct CharPtrLess : public std::binary_function<const char*,const char*, bool>
00149 {
00150 bool operator()(const char* p1,const char* p2) const
00151 { return strcasecmp(p1,p2)<0; }
00152 };
00153
00154 typedef std::map<const char*,OBFormat*,CharPtrLess > FMapType;
00155 typedef FMapType::iterator Formatpos;
00156
00157
00159
00160 class OBCONV OBConversion
00161 {
00163 public:
00165
00166 OBConversion(std::istream* is=NULL, std::ostream* os=NULL);
00168 OBConversion(const OBConversion& o);
00169 virtual ~OBConversion();
00171
00172
00173
00174 static int RegisterFormat(const char* ID, OBFormat* pFormat, const char* MIME = NULL);
00176 static OBFormat* FindFormat(const char* ID);
00178 static OBFormat* FormatFromExt(const char* filename);
00180 static OBFormat* FormatFromMIME(const char* MIME);
00181
00183 static bool GetNextFormat(Formatpos& itr, const char*& str,OBFormat*& pFormat);
00185
00187
00188 static const char* Description();
00190
00192
00193 std::istream* GetInStream() const {return pInStream;};
00194 std::ostream* GetOutStream() const {return pOutStream;};
00195 void SetInStream(std::istream* pIn)
00196 {
00197 if (pInStream && NeedToFreeInStream) {
00198 delete pInStream; NeedToFreeInStream = false;
00199 }
00200 pInStream=pIn;
00201 CheckedForGzip = false;
00202 };
00203 void SetOutStream(std::ostream* pOut)
00204 {
00205 if (pOutStream && NeedToFreeOutStream) {
00206 delete pOutStream; NeedToFreeOutStream = false;
00207 }
00208 pOutStream=pOut;
00209 };
00211 bool SetInAndOutFormats(const char* inID, const char* outID);
00212 bool SetInAndOutFormats(OBFormat* pIn, OBFormat* pOut);
00214 bool SetInFormat(const char* inID);
00215 bool SetInFormat(OBFormat* pIn);
00217 bool SetOutFormat(const char* outID);
00218 bool SetOutFormat(OBFormat* pOut);
00219
00220 OBFormat* GetInFormat() const{return pInFormat;};
00221 OBFormat* GetOutFormat() const{return pOutFormat;};
00222 std::string GetInFilename() const{return InFilename;};
00223
00225 std::streampos GetInPos()const{return wInpos;};
00226
00228 size_t GetInLen()const{return wInlen;};
00229
00231 const char* GetTitle() const;
00232
00234 OBConversion* GetAuxConv() const {return pAuxConv;};
00235 void SetAuxConv(OBConversion* pConv) {pAuxConv=pConv;};
00237
00238
00239
00240 enum Option_type { INOPTIONS, OUTOPTIONS, GENOPTIONS };
00241
00243 const char* IsOption(const char* opt,Option_type opttyp=OUTOPTIONS);
00244
00246 const std::map<std::string,std::string>* GetOptions(Option_type opttyp)
00247 { return &OptionsArray[opttyp];};
00248
00250 void AddOption(const char* opt, Option_type opttyp, const char* txt=NULL);
00251
00252 bool RemoveOption(const char* opt, Option_type optype);
00253
00255 void SetOptions(const char* options, Option_type opttyp);
00256
00258 static void RegisterOptionParam(std::string name, OBFormat* pFormat,
00259 int numberParams=0, Option_type typ=OUTOPTIONS);
00260
00262 static int GetOptionParams(std::string name, Option_type typ);
00264
00266
00267
00268 std::vector<std::string> GetSupportedInputFormat();
00269
00270 std::vector<std::string> GetSupportedOutputFormat();
00272
00274
00275
00276 int Convert(std::istream* is, std::ostream* os);
00277
00279 int Convert();
00280
00283 int FullConvert(std::vector<std::string>& FileList,
00284 std::string& OutputFileName, std::vector<std::string>& OutputFileList);
00286
00288
00289 bool AddChemObject(OBBase* pOb);
00290 OBBase* GetChemObject();
00291 bool IsLast();
00292 bool IsFirstInput();
00293 int GetOutputIndex() const ;
00294 void SetOutputIndex(int indx);
00295 void SetMoreFilesToCome();
00296 void SetOneObjectOnly(bool b=true);
00297 void SetLast(bool b){SetOneObjectOnly(b);}
00298
00299
00300
00301
00302 static OBFormat* GetDefaultFormat(){return pDefaultFormat;};
00303
00305
00308 bool Write(OBBase* pOb, std::ostream* pout=NULL);
00309
00311
00317 std::string WriteString(OBBase* pOb, bool trimWhitespace = false);
00318
00320
00325 bool WriteFile(OBBase* pOb, std::string filePath);
00326
00331 void CloseOutFile();
00332
00334
00338 bool Read(OBBase* pOb, std::istream* pin=NULL);
00339
00341
00345 bool ReadString(OBBase* pOb, std::string input);
00346
00348
00354 bool ReadFile(OBBase* pOb, std::string filePath);
00355
00356 protected:
00358 static std::string BatchFileName(std::string& BaseName, std::string& InFile);
00360 static std::string IncrementedFileName(std::string& BaseName, const int Count);
00362 static bool CheckForUnintendedBatch(const std::string& infile, const std::string& outfile);
00363
00365
00366 protected:
00367 bool SetStartAndEnd();
00368 static FMapType& FormatsMap();
00369 static FMapType& FormatsMIMEMap();
00370 typedef std::map<std::string,int> OPAMapType;
00371 static OPAMapType& OptionParamArray(Option_type typ);
00372 static int LoadFormatFiles();
00373 bool OpenAndSetFormat(bool SetFormat, std::ifstream* is);
00374
00375 std::string InFilename;
00376 std::istream* pInStream;
00377 std::ostream* pOutStream;
00378 static OBFormat* pDefaultFormat;
00379 OBFormat* pInFormat;
00380 OBFormat* pOutFormat;
00381
00382 std::map<std::string,std::string> OptionsArray[3];
00383
00384 int Index;
00385 unsigned int StartNumber;
00386 unsigned int EndNumber;
00387 int Count;
00388 bool m_IsFirstInput;
00389 bool m_IsLast;
00390 bool MoreFilesToCome;
00391 bool OneObjectOnly;
00392 bool ReadyToInput;
00393 bool CheckedForGzip;
00394 bool NeedToFreeInStream;
00395 bool NeedToFreeOutStream;
00396
00397 static int FormatFilesLoaded;
00398 OBBase* pOb1;
00399 std::streampos wInpos;
00400 std::streampos rInpos;
00401 size_t wInlen;
00402 size_t rInlen;
00403
00404 OBConversion* pAuxConv;
00405
00406 std::vector<std::string> SupportedInputFormat;
00407 std::vector<std::string> SupportedOutputFormat;
00408
00409 };
00410
00412 #define NOTREADABLE 0x01
00413 #define READONEONLY 0x02
00414 #define READBINARY 0x04
00415 #define ZEROATOMSOK 0x08
00416 #define NOTWRITABLE 0x10
00417 #define WRITEONEONLY 0x20
00418 #define WRITEBINARY 0x40
00419 #define READXML 0x80
00420 #define DEFAULTFORMAT 0x4000
00421
00422 }
00423 #endif //OB_CONV_H
00424
00427
00428