1 #include "RDKitImpl.h"
2 #include "RDMolImpl.h"
3 #include "RDMolSupplierImpl.h"
4 #include <GraphMol/RDKitBase.h>
5 #include <GraphMol/SmilesParse/SmilesParse.h>
6 #include <GraphMol/FileParsers/FileParsers.h>
7 #include <GraphMol/FileParsers/MolSupplier.h>
8 #include <string>
9 
10 /* Implementation file */
NS_IMPL_ISUPPORTS1(RDKitImpl,IRDKit)11 NS_IMPL_ISUPPORTS1(RDKitImpl, IRDKit)
12 
13 RDKitImpl::RDKitImpl() { /* member initializers and constructor code */ }
14 
~RDKitImpl()15 RDKitImpl::~RDKitImpl() { /* destructor code */ }
16 
17 /* unsigned long strlen (in string arg); */
Strlen(const char * arg,PRUint32 * _retval)18 NS_IMETHODIMP RDKitImpl::Strlen(const char *arg, PRUint32 *_retval) {
19   std::string text(arg);
20   *_retval = text.size();
21   return NS_OK;
22 }
23 
24 /* IRDMolecule MolFromSmiles (in string smiles); */
MolFromSmiles(const char * smiles,IRDMolecule ** _retval)25 NS_IMETHODIMP RDKitImpl::MolFromSmiles(const char *smiles,
26                                        IRDMolecule **_retval) {
27   std::string smi(smiles);
28   RDKit::ROMol *roMol = RDKit::SmilesToMol(smiles);
29   if (!roMol) return NS_ERROR_FAILURE;
30 
31   RDMolecule *mol = new RDMolecule(roMol);
32   if (!mol) return NS_ERROR_OUT_OF_MEMORY;
33   *_retval = static_cast<IRDMolecule *>(mol);
34 
35   // FIX: does this leak?
36   NS_ADDREF(*_retval);
37 
38   return NS_OK;
39 }
40 
41 /* IRDMolecule MolFromMolBlock (in string molBlock); */
MolFromMolBlock(const char * molBlock,IRDMolecule ** _retval)42 NS_IMETHODIMP RDKitImpl::MolFromMolBlock(const char *molBlock,
43                                          IRDMolecule **_retval) {
44   RDKit::ROMol *roMol = RDKit::MolBlockToMol(std::string(molBlock));
45   if (!roMol) return NS_ERROR_FAILURE;
46 
47   RDMolecule *mol = new RDMolecule(roMol);
48   if (!mol) return NS_ERROR_OUT_OF_MEMORY;
49   *_retval = static_cast<IRDMolecule *>(mol);
50 
51   // FIX: does this leak?
52   NS_ADDREF(*_retval);
53 
54   return NS_OK;
55 }
56 
57 /* IRDMolSupplier SupplierFromSDFile (in string fileName); */
SupplierFromSDFile(const char * fileName,IRDMolSupplier ** _retval)58 NS_IMETHODIMP RDKitImpl::SupplierFromSDFile(const char *fileName,
59                                             IRDMolSupplier **_retval) {
60   RDKit::MolSupplier *sdSuppl = new RDKit::SDMolSupplier(std::string(fileName));
61   if (!sdSuppl) return NS_ERROR_FAILURE;
62 
63   RDMolSupplier *suppl = new RDMolSupplier(sdSuppl);
64   if (!suppl) return NS_ERROR_OUT_OF_MEMORY;
65   *_retval = static_cast<IRDMolSupplier *>(suppl);
66 
67   // FIX: does this leak?
68   NS_ADDREF(*_retval);
69 
70   return NS_OK;
71 }
72