Loading...
Searching...
No Matches
obconversion.h
Go to the documentation of this file.
/**********************************************************************
obconversion.h - Handle file conversions. Declaration of OBFormat, OBConversion
Copyright (C) 2004-2009 by Chris Morley
This file is part of the Open Babel project.
For more information, see <http://openbabel.org/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
#ifndef OB_CONV_H
#define OB_CONV_H
#include <openbabel/babelconfig.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cassert>
#include <string>
#include <vector>
#include <map>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <openbabel/oberror.h>
#include <openbabel/format.h>
#include <openbabel/lineend.h>
// These macros are used in DLL builds. If they have not
// been set in babelconfig.h, define them as nothing.
#ifndef OBCONV
#define OBCONV
#endif
#ifndef OBDLL
#define OBDLL
#endif
namespace OpenBabel {
// Needed to preserve deprecated API
typedef OBPlugin::PluginIterator Formatpos;
OBERROR extern OBMessageHandler obErrorLog;
//*************************************************
// Class introduction in obconversion.cpp
class OBCONV OBConversion
{
public:
OBConversion(std::istream* is=nullptr, std::ostream* os=nullptr);
OBConversion(std::string inFilename, std::string outFilename="");
OBConversion(const OBConversion& o);
OBConversion& operator=(const OBConversion& rhs);
virtual ~OBConversion();
static int RegisterFormat(const char* ID, OBFormat* pFormat, const char* MIME = nullptr);
static OBFormat* FindFormat(const char* ID);
static OBFormat* FindFormat(const std::string ID);
static OBFormat* FormatFromExt(const char* filename);
static OBFormat* FormatFromExt(const char* filename, bool& isgzip);
static OBFormat* FormatFromExt(const std::string filename);
static OBFormat* FormatFromExt(const std::string filename, bool& isgzip);
static OBFormat* FormatFromMIME(const char* MIME);
#ifndef SWIG
static bool GetNextFormat(Formatpos& itr, const char*& str,OBFormat*& pFormat);
#endif
static const char* Description(); //generic conversion options
std::istream* GetInStream() const {return pInput;};
std::ostream* GetOutStream() const {return pOutput;};
// if false, then will be treated as gzipped stream only if z/zin is set.
void SetInStream(std::istream* pIn, bool takeOwnership=false);
void SetOutStream(std::ostream* pOut, bool takeOwnership=false);
bool SetInAndOutFormats(const char* inID, const char* outID, bool ingzip=false, bool outgzip=false);
bool SetInAndOutFormats(OBFormat* pIn, OBFormat* pOut, bool ingzip=false, bool outgzip=false);
bool SetInFormat(const char* inID, bool isgzip=false);
bool SetInFormat(OBFormat* pIn, bool isgzip=false);
bool SetOutFormat(const char* outID, bool isgzip=false);
bool SetOutFormat(OBFormat* pOut, bool isgzip=false);
OBFormat* GetInFormat() const{return pInFormat;};
OBFormat* GetOutFormat() const{return pOutFormat;};
bool GetInGzipped() const{return inFormatGzip;};
bool GetOutGzipped() const{return outFormatGzip;};
std::string GetInFilename() const{return InFilename;};
std::string GetOutFilename() const{return OutFilename;};
std::streampos GetInPos()const{return wInpos;};
size_t GetInLen()const{return wInlen;};
const char* GetTitle() const;
OBConversion* GetAuxConv() const {return pAuxConv;};
void SetAuxConv(OBConversion* pConv) {pAuxConv=pConv;};
enum Option_type { INOPTIONS, OUTOPTIONS, GENOPTIONS, ALL };
const char* IsOption(const char* opt,Option_type opttyp=OUTOPTIONS);
const std::map<std::string,std::string>* GetOptions(Option_type opttyp)
{ return &OptionsArray[opttyp];};
void AddOption(const char* opt, Option_type opttyp=OUTOPTIONS, const char* txt=nullptr);
bool RemoveOption(const char* opt, Option_type optype);
void SetOptions(const char* options, Option_type opttyp);
static void RegisterOptionParam(std::string name, OBFormat* pFormat,
int numberParams=0, Option_type typ=OUTOPTIONS);
static int GetOptionParams(std::string name, Option_type typ);
void CopyOptions(OBConversion* pSourceConv, Option_type typ=ALL);
// @brief Set and return the list of supported input format
std::vector<std::string> GetSupportedInputFormat();
// @brief Set and return the list of supported output format
std::vector<std::string> GetSupportedOutputFormat();
int Convert(std::istream* is, std::ostream* os);
int Convert();
int FullConvert(std::vector<std::string>& FileList,
std::string& OutputFileName, std::vector<std::string>& OutputFileList);
int AddChemObject(OBBase* pOb);
OBBase* GetChemObject();
bool IsLast();
bool IsFirstInput();
void SetFirstInput(bool b=true);
int GetOutputIndex() const ;
void SetOutputIndex(int indx);
void SetMoreFilesToCome();
void SetOneObjectOnly(bool b=true);
void SetLast(bool b){SetOneObjectOnly(b);}
bool IsLastFile(){ return !MoreFilesToCome;}
int GetCount()const { return Count; }
static OBFormat* GetDefaultFormat(){return OBFormat::FindType(nullptr);};
bool Write(OBBase* pOb, std::ostream* pout=nullptr);
std::string WriteString(OBBase* pOb, bool trimWhitespace = false);
bool WriteFile(OBBase* pOb, std::string filePath);
void CloseOutFile();
bool Read(OBBase* pOb, std::istream* pin=nullptr);
// OBBase* ReadObject(std::istream* pin=NULL);
bool ReadString(OBBase* pOb, std::string input);
bool ReadFile(OBBase* pOb, std::string filePath);
bool OpenInAndOutFiles(std::string infilepath, std::string outfilepath);
void ReportNumberConverted(int count, OBFormat* pFormat=nullptr);
int NumInputObjects();
protected:
static std::string BatchFileName(std::string& BaseName, std::string& InFile);
static std::string IncrementedFileName(std::string& BaseName, const int Count);
static bool CheckForUnintendedBatch(const std::string& infile, const std::string& outfile);
void ClearInStreams();
protected:
//helper class for saving stream state
struct StreamState
{
std::ios *pStream; //active stream
std::vector<std::ios *> ownedStreams; //streams we own the memory to
StreamState(): pStream(nullptr) {}
~StreamState()
{
assert(ownedStreams.size() == 0); //should be popped
}
void pushInput(OBConversion& conv);
void popInput(OBConversion& conv);
void pushOutput(OBConversion& conv);
void popOutput(OBConversion& conv);
bool isSet() const { return pStream != nullptr; }
};
bool SetStartAndEnd();
// static FMapType& FormatsMap();///<contains ID and pointer to all OBFormat classes
// static FMapType& FormatsMIMEMap();///<contains MIME and pointer to all OBFormat classes
typedef std::map<std::string,int> OPAMapType;
static OPAMapType& OptionParamArray(Option_type typ);
bool OpenAndSetFormat(bool SetFormat, std::ifstream* is, std::stringstream* ss=nullptr);
std::string InFilename, OutFilename; //OutFileName added v2.4.0
typedef FilteringInputStream< LineEndingExtractor > LEInStream;
std::istream *pInput; //input stream, may be filtered
std::vector<std::istream *> ownedInStreams; //streams we own the memory to
std::ostream *pOutput; //output stream, may have filters applied
std::vector<std::ostream *> ownedOutStreams; //streams we own the memory to
static OBFormat* pDefaultFormat;
OBFormat* pInFormat;
OBFormat* pOutFormat;
std::map<std::string,std::string> OptionsArray[3];
int Index;
unsigned int StartNumber;
unsigned int EndNumber;
int Count;
bool m_IsFirstInput;
bool m_IsLast;
bool MoreFilesToCome;
bool OneObjectOnly;
bool ReadyToInput;
bool SkippedMolecules;
//unlike the z and zin options, these are not sticky - setting formats will reset them
bool inFormatGzip;
bool outFormatGzip;
OBBase* pOb1;
std::streampos wInpos;
std::streampos rInpos;
size_t wInlen;
size_t rInlen;
OBConversion* pAuxConv;
std::vector<std::string> SupportedInputFormat;
std::vector<std::string> SupportedOutputFormat;
};
} //namespace OpenBabel
#endif //OB_CONV_H
OpenBabel::FilteringInputStream< LineEndingExtractor >
OpenBabel::OBBase
Base Class.
Definition base.h:240
OpenBabel::OBConversion
Class to convert from one format to another.
Definition obconversion.h:60
OpenBabel::OBConversion::Index
int Index
Definition obconversion.h:394
OpenBabel::OBConversion::SkippedMolecules
bool SkippedMolecules
Definition obconversion.h:403
OpenBabel::OBConversion::SetLast
void SetLast(bool b)
Synonym for SetOneObjectOnly().
Definition obconversion.h:245
OpenBabel::OBConversion::GetOutStream
std::ostream * GetOutStream() const
Definition obconversion.h:108
OpenBabel::OBConversion::rInlen
size_t rInlen
length in the input stream of the object being read
Definition obconversion.h:413
OpenBabel::OBConversion::GetCount
int GetCount() const
Number of objects read and processed Incremented after options are processed, so 0 for first object....
Definition obconversion.h:249
OpenBabel::OBConversion::InFilename
std::string InFilename
Definition obconversion.h:377
OpenBabel::OBConversion::pOb1
OBBase * pOb1
Definition obconversion.h:409
OpenBabel::OBConversion::operator=
OBConversion & operator=(const OBConversion &rhs)
Assignment. Stream ownership is not copied. Source remains responsible for the memory.
Definition obconversion.cpp:270
OpenBabel::OBConversion::SupportedInputFormat
std::vector< std::string > SupportedInputFormat
list of supported input format
Definition obconversion.h:417
OpenBabel::OBConversion::OutFilename
std::string OutFilename
Definition obconversion.h:377
OpenBabel::OBConversion::Description
static const char * Description()
Definition obconversion.cpp:1155
OpenBabel::OBConversion::pInput
std::istream * pInput
Definition obconversion.h:381
OpenBabel::OBConversion::GetInStream
std::istream * GetInStream() const
Definition obconversion.h:107
OpenBabel::OBConversion::pOutput
std::ostream * pOutput
Definition obconversion.h:384
OpenBabel::OBConversion::pDefaultFormat
static OBFormat * pDefaultFormat
Definition obconversion.h:388
OpenBabel::OBConversion::pAuxConv
OBConversion * pAuxConv
Way to extend OBConversion.
Definition obconversion.h:415
OpenBabel::OBConversion::OptionParamArray
static OPAMapType & OptionParamArray(Option_type typ)
Definition obconversion.cpp:1659
OpenBabel::OBConversion::GetInFormat
OBFormat * GetInFormat() const
Definition obconversion.h:126
OpenBabel::OBConversion::OptionsArray
std::map< std::string, std::string > OptionsArray[3]
Definition obconversion.h:392
OpenBabel::OBConversion::FormatFromExt
static OBFormat * FormatFromExt(const char *filename)
Searches registered formats for an ID the same as the file extension.
Definition obconversion.cpp:808
OpenBabel::OBConversion::ClearInStreams
void ClearInStreams()
OpenBabel::OBConversion::pOutFormat
OBFormat * pOutFormat
Definition obconversion.h:390
OpenBabel::OBConversion::SetOneObjectOnly
void SetOneObjectOnly(bool b=true)
Used with multiple input files. Off by default.
Definition obconversion.cpp:769
OpenBabel::OBConversion::SupportedOutputFormat
std::vector< std::string > SupportedOutputFormat
list of supported output format
Definition obconversion.h:418
OpenBabel::OBConversion::SetAuxConv
void SetAuxConv(OBConversion *pConv)
Definition obconversion.h:144
OpenBabel::OBConversion::rInpos
std::streampos rInpos
position in the input stream of the object being read
Definition obconversion.h:411
OpenBabel::OBConversion::m_IsLast
bool m_IsLast
Definition obconversion.h:399
OpenBabel::OBConversion::OneObjectOnly
bool OneObjectOnly
Definition obconversion.h:401
OpenBabel::OBConversion::pInFormat
OBFormat * pInFormat
Definition obconversion.h:389
OpenBabel::OBConversion::GetOutGzipped
bool GetOutGzipped() const
Definition obconversion.h:129
OpenBabel::OBConversion::OpenAndSetFormat
bool OpenAndSetFormat(bool SetFormat, std::ifstream *is, std::stringstream *ss=nullptr)
Definition obconversion.cpp:1541
OpenBabel::OBConversion::outFormatGzip
bool outFormatGzip
Definition obconversion.h:407
OpenBabel::OBConversion::inFormatGzip
bool inFormatGzip
skip molecules using -f and -l
Definition obconversion.h:406
OpenBabel::OBConversion::GetOutFilename
std::string GetOutFilename() const
Definition obconversion.h:131
OpenBabel::OBConversion::GetAuxConv
OBConversion * GetAuxConv() const
Extension method: deleted in ~OBConversion().
Definition obconversion.h:143
OpenBabel::OBConversion::FindFormat
static OBFormat * FindFormat(const char *ID)
Searches registered formats.
Definition obconversion.cpp:748
OpenBabel::OBConversion::ReadyToInput
bool ReadyToInput
Definition obconversion.h:402
OpenBabel::OBConversion::OPAMapType
std::map< std::string, int > OPAMapType
Definition obconversion.h:373
OpenBabel::OBConversion::GetDefaultFormat
static OBFormat * GetDefaultFormat()
The default format is set in a single OBFormat class (generally it is OBMol).
Definition obconversion.h:254
OpenBabel::OBConversion::Option_type
Option_type
Three types of options set on the the command line by -a? , -x? , or -?
Definition obconversion.h:184
OpenBabel::OBConversion::INOPTIONS
@ INOPTIONS
Definition obconversion.h:184
OpenBabel::OBConversion::ALL
@ ALL
Definition obconversion.h:184
OpenBabel::OBConversion::GENOPTIONS
@ GENOPTIONS
Definition obconversion.h:184
OpenBabel::OBConversion::OUTOPTIONS
@ OUTOPTIONS
Definition obconversion.h:184
OpenBabel::OBConversion::Count
int Count
Definition obconversion.h:397
OpenBabel::OBConversion::GetInGzipped
bool GetInGzipped() const
Definition obconversion.h:128
OpenBabel::OBConversion::GetOptions
const std::map< std::string, std::string > * GetOptions(Option_type opttyp)
Access the map with option name as key and any associated text as value.
Definition obconversion.h:190
OpenBabel::OBConversion::MoreFilesToCome
bool MoreFilesToCome
Definition obconversion.h:400
OpenBabel::OBConversion::wInpos
std::streampos wInpos
position in the input stream of the object being written
Definition obconversion.h:410
OpenBabel::OBConversion::IsLastFile
bool IsLastFile()
True if no more files to be read.
Definition obconversion.h:246
OpenBabel::OBConversion::RegisterFormat
static int RegisterFormat(const char *ID, OBFormat *pFormat, const char *MIME=nullptr)
Called once by each format class.
Definition obconversion.cpp:331
OpenBabel::OBConversion::StartNumber
unsigned int StartNumber
Definition obconversion.h:395
OpenBabel::OBConversion::FormatFromMIME
static OBFormat * FormatFromMIME(const char *MIME)
Searches registered formats for a MIME the same as the chemical MIME type passed.
Definition obconversion.cpp:825
OpenBabel::OBConversion::LEInStream
FilteringInputStream< LineEndingExtractor > LEInStream
Definition obconversion.h:379
OpenBabel::OBConversion::GetInFilename
std::string GetInFilename() const
Definition obconversion.h:130
OpenBabel::OBConversion::GetOutFormat
OBFormat * GetOutFormat() const
Definition obconversion.h:127
OpenBabel::OBConversion::OBConversion
OBConversion(std::istream *is=nullptr, std::ostream *os=nullptr)
Definition obconversion.cpp:230
OpenBabel::OBConversion::EndNumber
unsigned int EndNumber
Definition obconversion.h:396
OpenBabel::OBConversion::ownedOutStreams
std::vector< std::ostream * > ownedOutStreams
Definition obconversion.h:385
OpenBabel::OBConversion::m_IsFirstInput
bool m_IsFirstInput
Definition obconversion.h:398
OpenBabel::OBConversion::GetNextFormat
static bool GetNextFormat(Formatpos &itr, const char *&str, OBFormat *&pFormat)
Deprecated!.Repeatedly called to recover available Formats.
Definition obconversion.cpp:1798
OpenBabel::OBConversion::GetInLen
size_t GetInLen() const
Get the length in the input stream of the object being read.
Definition obconversion.h:137
OpenBabel::OBConversion::ownedInStreams
std::vector< std::istream * > ownedInStreams
Definition obconversion.h:382
OpenBabel::OBConversion::GetInPos
std::streampos GetInPos() const
Get the position in the input stream of the object being read.
Definition obconversion.h:134
OpenBabel::OBConversion::wInlen
size_t wInlen
length in the input stream of the object being written
Definition obconversion.h:412
OpenBabel::OBFormat
Base class for file formats.
Definition format.h:45
OpenBabel::OBMessageHandler
Handle error messages, warnings, debugging information and the like.
Definition oberror.h:107
format.h
Declarations for OBFormat.
OpenBabel::OBPlugin::PluginIterator
PluginMapType::const_iterator PluginIterator
Definition plugin.h:60
lineend.h
Translate line endings automatically (UNIX, Classic Mac, DOS).
OpenBabel
Global namespace for all Open Babel code.
Definition alias.h:23
OpenBabel::obErrorLog
OBMessageHandler obErrorLog
Global OBMessageHandler error handler.
Definition oberror.h:187
OpenBabel::Formatpos
OBPlugin::PluginIterator Formatpos
Definition obconversion.h:52
oberror.h
Handle error messages, warnings, notices, etc.
OpenBabel::OBConversion::StreamState::isSet
bool isSet() const
Definition obconversion.h:367
OpenBabel::OBConversion::StreamState::StreamState
StreamState()
Definition obconversion.h:355
OpenBabel::OBConversion::StreamState::pStream
std::ios * pStream
Definition obconversion.h:352
OpenBabel::OBConversion::StreamState::~StreamState
~StreamState()
Definition obconversion.h:356
OpenBabel::OBConversion::StreamState::ownedStreams
std::vector< std::ios * > ownedStreams
Definition obconversion.h:353