1 /**********************************************************************
2 Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
3 Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
4 Some portions Copyright (C) 2004 by Chris Morley
5 
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation version 2 of the License.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 ***********************************************************************/
15 #include <openbabel/babelconfig.h>
16 
17 #include <openbabel/obmolecformat.h>
18 #include <openbabel/mol.h>
19 #include <openbabel/atom.h>
20 #include <openbabel/elements.h>
21 #include <cstdlib>
22 
23 
24 using namespace std;
25 namespace OpenBabel
26 {
27 
28 class CCCFormat : public OBMoleculeFormat
29 {
30 public:
31     //Register this format type ID
CCCFormat()32     CCCFormat()
33     {
34         OBConversion::RegisterFormat("ccc",this);
35     }
36 
Description()37     virtual const char* Description() //required
38     {
39         return
40           "CCC format\n"
41           "No comments yet\n";
42     };
43 
SpecificationURL()44   virtual const char* SpecificationURL()
45   {return "";}; //optional
46 
47     //Flags() can return be any the following combined by | or be omitted if none apply
48     // NOTREADABLE  READONEONLY  NOTWRITABLE  WRITEONEONLY
Flags()49     virtual unsigned int Flags()
50     {
51         return NOTWRITABLE;
52     };
53 
54     ////////////////////////////////////////////////////
55     /// The "API" interface functions
56     virtual bool ReadMolecule(OBBase* pOb, OBConversion* pConv);
57 
58  };
59 
60 //Make an instance of the format class
61 CCCFormat theCCCFormat;
62 
63 /////////////////////////////////////////////////////////////////
ReadMolecule(OBBase * pOb,OBConversion * pConv)64 bool CCCFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
65 {
66 
67     OBMol* pmol = pOb->CastAndClear<OBMol>();
68     if (pmol == nullptr)
69         return false;
70 
71     //Define some references so we can use the old parameter names
72     istream &ifs = *pConv->GetInStream();
73     OBMol &mol = *pmol;
74     mol.SetTitle( pConv->GetTitle()); //default title is the filename
75 
76     char buffer[BUFF_SIZE];
77     ifs.getline(buffer,BUFF_SIZE);
78 
79     if (strlen(buffer) > 5)
80         mol.SetTitle(&buffer[5]);
81     mol.SetEnergy(0.0);
82 
83     int natoms;
84     ifs.getline(buffer,BUFF_SIZE);
85     sscanf(buffer,"%*s%d",&natoms);
86     mol.ReserveAtoms(natoms);
87     mol.BeginModify();
88 
89     int end,order;
90     double x,y,z;
91     OBAtom atom;
92     vector3 v;
93     vector<string> vs;
94     char element[3];
95     element[2] = '\0';
96 
97     for (int i = 1;i <= natoms;i++)
98     {
99         if (!ifs.getline(buffer,BUFF_SIZE))
100             return(false);
101         atom.Clear();
102         element[0] = buffer[0];
103         element[1] = (buffer[1] != ' ') ? buffer[1]:'\0';
104         atom.SetAtomicNum(OBElements::GetAtomicNum(element));
105         sscanf(&buffer[15],"%lf%lf%lf",&x,&y,&z);
106         v.Set(x,y,z);
107         atom.SetVector(v);
108 
109         if (!mol.AddAtom(atom))
110             return(false);
111         tokenize(vs,&buffer[60]);
112         vector<string>::iterator j;
113 
114         for (j = vs.begin();j != vs.end();++j)
115             if (!j->empty())
116             {
117                 //get the bond order
118                 switch((char)(*j)[j->size()-1])
119                 {
120                 case 'S':
121                     order = 1;
122                     break;
123                 case 'D':
124                     order = 2;
125                     break;
126                 case 'T':
127                     order = 3;
128                     break;
129                 default:
130                     order = 1;
131                 }
132                 (*j)[j->size()-1] = ' ';
133                 end = atoi(j->c_str());
134                 if (i>end)
135                     mol.AddBond(i,end,order);
136             }
137     }
138 
139     mol.EndModify();
140     return(true);
141 }
142 
143 } //namespace OpenBabel
144