~ubuntu-branches/debian/stretch/openbabel/stretch

« back to all changes in this revision

Viewing changes to src/formats/hinformat.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck
  • Date: 2008-07-22 23:54:58 UTC
  • mfrom: (3.1.10 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080722235458-3o606czluviz4akx
Tags: 2.2.0-2
* Upload to unstable.
* debian/control: Updated descriptions.
* debian/patches/gauss_cube_format.patch: New patch, makes the 
  gaussian cube format available again.
* debian/rules (DEB_DH_MAKESHLIBS_ARGS_libopenbabel3): Removed.
* debian/rules (DEB_CONFIGURE_EXTRA_FLAGS): Likewise.
* debian/libopenbabel3.install: Adjust formats directory to single 
  version hierarchy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
13
GNU General Public License for more details.
14
14
***********************************************************************/
 
15
#include <openbabel/babelconfig.h>
15
16
 
16
 
#include "mol.h"
17
 
#include "obconversion.h"
18
 
#include "obmolecformat.h"
 
17
#include <openbabel/obmolecformat.h>
19
18
 
20
19
using namespace std;
21
20
namespace OpenBabel
22
21
{
23
22
 
24
 
class HINFormat : public OBMoleculeFormat
25
 
{
26
 
public:
 
23
  class HINFormat : public OBMoleculeFormat
 
24
  {
 
25
  public:
27
26
    //Register this format type ID
28
27
    HINFormat()
29
28
    {
30
 
        OBConversion::RegisterFormat("hin",this);
 
29
      OBConversion::RegisterFormat("hin",this, "chemical/x-hin");
31
30
    }
32
31
 
33
32
    virtual const char* Description() //required
34
33
    {
35
 
        return "HyperChem HIN format\n\
 
34
      return "HyperChem HIN format\n\
36
35
              No comments yet\n";
37
36
    };
38
37
 
39
 
  virtual const char* SpecificationURL()
40
 
  { return "";}; //optional
 
38
    virtual const char* SpecificationURL()
 
39
    { return "";}; //optional
 
40
 
 
41
    virtual const char* GetMIMEType() 
 
42
    { return "chemical/x-hin";}; //optional
41
43
 
42
44
    //*** This section identical for most OBMol conversions ***
43
45
    ////////////////////////////////////////////////////
44
46
    /// The "API" interface functions
45
47
    virtual bool ReadMolecule(OBBase* pOb, OBConversion* pConv);
46
48
    virtual bool WriteMolecule(OBBase* pOb, OBConversion* pConv);
47
 
};
48
 
//***
49
 
 
50
 
//Make an instance of the format class
51
 
HINFormat theHINFormat;
52
 
 
53
 
/////////////////////////////////////////////////////////////////
54
 
bool HINFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
55
 
{
56
 
 
57
 
    OBMol* pmol = dynamic_cast<OBMol*>(pOb);
 
49
  };
 
50
  //***
 
51
 
 
52
  //Make an instance of the format class
 
53
  HINFormat theHINFormat;
 
54
 
 
55
  /////////////////////////////////////////////////////////////////
 
56
  bool HINFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
 
57
  {
 
58
 
 
59
    OBMol* pmol = pOb->CastAndClear<OBMol>();
58
60
    if(pmol==NULL)
59
 
        return false;
 
61
      return false;
60
62
 
61
63
    //Define some references so we can use the old parameter names
62
64
    istream &ifs = *pConv->GetInStream();
73
75
    vector<string> vs;
74
76
 
75
77
    ifs.getline(buffer, BUFF_SIZE);
76
 
    while (strstr(buffer,"mol") == NULL)
 
78
    while (ifs.good() && strstr(buffer,"mol") == NULL)
77
79
      {
78
80
        ifs.getline(buffer, BUFF_SIZE);
79
 
        if (ifs.peek() == EOF || !ifs.good())
80
 
          return false;
 
81
        if (ifs.peek() == EOF || !ifs.good())
 
82
          return false;
81
83
      }
82
84
    ifs.getline(buffer, BUFF_SIZE);
 
85
    if (!ifs.good())
 
86
      return false; // ended early
83
87
 
84
88
    mol.BeginModify();
85
 
    while (strstr(buffer,"endmol") == NULL)
86
 
    {
 
89
    while (ifs.good() && strstr(buffer,"endmol") == NULL)
 
90
      {
87
91
        tokenize(vs,buffer); // Don't really know how long it'll be
88
92
        if (vs.size() < 11)
89
 
          {
90
 
            ifs.getline(buffer, BUFF_SIZE);
91
 
            continue;
92
 
          }
 
93
          {
 
94
            ifs.getline(buffer, BUFF_SIZE);
 
95
            continue;
 
96
          }
93
97
 
94
98
        atom = mol.NewAtom();
95
99
        atom->SetAtomicNum(etab.GetAtomicNum(vs[3].c_str()));
 
100
        atom->SetPartialCharge(atof(vs[6].c_str()));
96
101
        x = atof((char*)vs[7].c_str());
97
102
        y = atof((char*)vs[8].c_str());
98
103
        z = atof((char*)vs[9].c_str());
100
105
 
101
106
        max = 11 + 2 * atoi((char *)vs[10].c_str());
102
107
        for (i = 11; i < max; i+=2)
103
 
        {
 
108
          {
104
109
            switch(((char*)vs[i+1].c_str())[0]) // First char in next token
105
 
            {
106
 
            case 's':
 
110
              {
 
111
              case 's':
107
112
                bo = 1;
108
113
                break;
109
 
            case 'd':
 
114
              case 'd':
110
115
                bo = 2;
111
116
                break;
112
 
            case 't':
 
117
              case 't':
113
118
                bo = 3;
114
119
                break;
115
 
            case 'a':
 
120
              case 'a':
116
121
                bo = 5;
117
122
                break;
118
 
            default :
 
123
              default :
119
124
                bo = 1;
120
125
                break;
121
 
            }
 
126
              }
122
127
            mol.AddBond(mol.NumAtoms(), atoi((char *)vs[i].c_str()), bo);
123
 
        }
 
128
          }
124
129
        ifs.getline(buffer, BUFF_SIZE);
125
 
    }
 
130
      }
126
131
 
127
132
    // clean out remaining blank lines
128
133
    while(ifs.peek() != EOF && ifs.good() && 
129
 
          (ifs.peek() == '\n' || ifs.peek() == '\r'))
 
134
          (ifs.peek() == '\n' || ifs.peek() == '\r'))
130
135
      ifs.getline(buffer,BUFF_SIZE);
131
136
 
132
137
    mol.EndModify();
133
138
 
134
139
    mol.SetTitle(title);
 
140
    mol.SetPartialChargesPerceived();
 
141
    
135
142
    return(true);
136
 
}
137
 
 
138
 
////////////////////////////////////////////////////////////////
139
 
 
140
 
bool HINFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
141
 
{
 
143
  }
 
144
 
 
145
  ////////////////////////////////////////////////////////////////
 
146
 
 
147
  bool HINFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
 
148
  {
142
149
    OBMol* pmol = dynamic_cast<OBMol*>(pOb);
143
150
    if(pmol==NULL)
144
 
        return false;
 
151
      return false;
145
152
 
146
153
    //Define some references so we can use the old parameter names
147
154
    ostream &ofs = *pConv->GetOutStream();
152
159
    char buffer[BUFF_SIZE];
153
160
    OBAtom *atom;
154
161
    OBBond *bond;
155
 
    vector<OBEdgeBase*>::iterator j;
 
162
    vector<OBBond*>::iterator j;
156
163
    char bond_char;
157
164
 
158
165
    // make sure to escape titles in double quotes
160
167
    ofs << "mol " << file_num << " \"" << mol.GetTitle() << "\"\n";
161
168
 
162
169
    for(i = 1;i <= mol.NumAtoms(); i++)
163
 
    {
 
170
      {
164
171
        atom = mol.GetAtom(i);
165
172
        snprintf(buffer, BUFF_SIZE, "atom %d - %-3s **  - %8.5f %8.5f  %8.5f  %8.5f %d ",
166
173
                i,
172
179
                atom->GetValence());
173
180
        ofs << buffer;
174
181
        for (bond = atom->BeginBond(j); bond; bond = atom->NextBond(j))
175
 
        {
 
182
          {
176
183
            switch(bond->GetBO())
177
 
            {
178
 
            case 1 :
 
184
              {
 
185
              case 1 :
179
186
                bond_char = 's';
180
187
                break;
181
 
            case 2 :
 
188
              case 2 :
182
189
                bond_char = 'd';
183
190
                break;
184
 
            case 3 :
 
191
              case 3 :
185
192
                bond_char = 't';
186
193
                break;
187
 
            case 5 :
 
194
              case 5 :
188
195
                bond_char = 'a';
189
196
                break;
190
 
            default:
 
197
              default:
191
198
                bond_char = 's';
192
199
                break;
193
 
            }
194
 
            if (bond->IsAromatic())
195
 
              bond_char = 'a';
 
200
              }
 
201
            if (bond->IsAromatic())
 
202
              bond_char = 'a';
196
203
 
197
204
            snprintf(buffer,BUFF_SIZE, "%d %c ", (bond->GetNbrAtom(atom))->GetIdx(), bond_char);
198
205
            ofs << buffer;
199
 
        }
 
206
          }
200
207
        ofs << endl;
201
 
    }
 
208
      }
202
209
    ofs << "endmol " << file_num << endl;
203
210
    return(true);
204
 
}
 
211
  }
205
212
 
206
213
} //namespace OpenBabel