Example code using OBConversion in a program

To do a file conversion without manipulating the molecule.

        #include "obconversion.h" //mol.h is not needed
        ...set up an istream is and an ostream os
        OBConversion conv;
        if(conv.SetInAndOutFormats("SMI","MOL"))
        {
                conv.SetOptions("h"); //Optional; (h adds expicit hydrogens)
                conv.Convert(&is,&os);
        }

If necessary, OBConversion can provide a list of the the available formats.
This is not much more complicated that the existing method and is insulated from the chemical methods used to carry out the conversion. The files may describe something other than a molecule, a reaction for instance, but the calling code, apart from the format IDs, would be the same.

A two stage construction is used to allow error handling if the format ID is not recognized. This is necessary now that the formats are dynamic and errors are not caught at compile time.

The file prog2.cpp contains some example code.

To read in a molecule, manipulate it and write it out.

Set up an istream and an ostream, to and from files or elsewhere. (cin and cout are used in the example)

        OBConversion conv;
        if(conv.SetInAndOutFormats("SMI","MOL"))
        {
                OBMol mol;
                if(conv.ReadChemObject(&mol, &cin))
                ...manipulate molecule
                conv->WriteChemObject(&mol,&cout);
        }

ReadChemObject is a templated function so that objects derived from OBBase can also be handled, in addition to OBMol, if the format routines are written appropriately.

The file prog1.cpp contains some example code

To input multiple molecules from within a OBFormat::ReadMolecule() routine

This has been previously discussed as a way for CML to continue a controlled parsing the XML text while outputting molecules it finds on the way.
	//In the handler for <molecule>
	OBMol* pmol = new OBMol;
        ...
	//In the handler for </molecule>
	if(pConv->AddChemObject(pmol->DoTransformations(pConv->GetGeneralOptions())) <0)
		return false;
	// A negative return here means that only one molecule was required, so stop now.
This code has not been tested.