Coverage Report

Created: 2026-02-26 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openbabel/include/openbabel/obmolecformat.h
Line
Count
Source
1
/**********************************************************************
2
obmolecformat.h - Subclass of OBFormat for conversion of OBMol.
3
4
Copyright (C) 2005 Chris Morley
5
6
This file is part of the Open Babel project.
7
For more information, see <http://openbabel.org/>
8
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation version 2 of the License.
12
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
***********************************************************************/
18
19
#ifndef OB_MOLECULEFORMAT_H
20
#define OB_MOLECULEFORMAT_H
21
22
#include <ciso646>  // detect std::lib
23
#include <unordered_map>
24
25
#include <openbabel/babelconfig.h>
26
#include <openbabel/obconversion.h>
27
#include <typeinfo>
28
#include <cstdlib>
29
30
namespace OpenBabel {
31
32
  class OBMol;
33
  class OBDescriptor;
34
  class OBReaction;
35
36
// This macro is used in DLL builds. If it has not
37
// been set in babelconfig.h, define it as nothing.
38
#ifndef OBCOMMON
39
  #define OBCOMMON
40
#endif
41
42
/** \class OBMoleculeFormat obmolecformat.h <openbabel/obmolecformat.h>
43
    \brief An OBFormat convenience subclass for conversion to/from OBMol data
44
45
    This class is not intended for direct use outside of Open Babel, unless
46
    you're writing a new format converting to or from an OBMol molecule.
47
    (e.g., see http://openbabel.org/wiki/HowTo:Add_A_New_File_Format).
48
49
    An OBFormat which converts to and/or from OBMol can derive from this class
50
    to save duplicating the ReadChemObject() and/or WriteChemObject() methods.
51
    Derive directly from OBFormat if the object converted is not OBMol or
52
    if interaction with the framework is required during the execution
53
    of ReadMolecule() or WriteMolecule(), as for example in CMLFormat
54
**/
55
56
//////////////////////////////////////////////////////////////////////
57
58
class OBCOMMON OBMoleculeFormat : public OBFormat
59
{
60
public:
61
62
  OBMoleculeFormat()
63
218
  {
64
218
    if(!OptionsRegistered)
65
2
    {
66
2
      OptionsRegistered=true;
67
2
      OBConversion::RegisterOptionParam("b",         this, 0, OBConversion::INOPTIONS);
68
2
      OBConversion::RegisterOptionParam("s",         this, 0, OBConversion::INOPTIONS);
69
2
      OBConversion::RegisterOptionParam("title",     this, 1, OBConversion::GENOPTIONS);
70
2
      OBConversion::RegisterOptionParam("addtotitle",this, 1, OBConversion::GENOPTIONS);
71
2
      OBConversion::RegisterOptionParam("property",  this, 2, OBConversion::GENOPTIONS);
72
2
      OBConversion::RegisterOptionParam("C",         this, 0, OBConversion::GENOPTIONS);
73
2
      OBConversion::RegisterOptionParam("j",         this, 0, OBConversion::GENOPTIONS);
74
2
      OBConversion::RegisterOptionParam("join",      this, 0, OBConversion::GENOPTIONS);
75
2
      OBConversion::RegisterOptionParam("separate",  this, 0, OBConversion::GENOPTIONS);
76
77
      //The follow are OBMol options, which should not be in OBConversion.
78
      //But here isn't entirely appropriate either, since one could have
79
      //OBMol formats loaded but which don't derived from this class.
80
      //However, this possibility is remote.
81
2
      OBConversion::RegisterOptionParam("s", nullptr, 1,OBConversion::GENOPTIONS);
82
2
      OBConversion::RegisterOptionParam("v", nullptr, 1,OBConversion::GENOPTIONS);
83
2
      OBConversion::RegisterOptionParam("h", nullptr, 0,OBConversion::GENOPTIONS);
84
2
      OBConversion::RegisterOptionParam("d", nullptr, 0,OBConversion::GENOPTIONS);
85
2
      OBConversion::RegisterOptionParam("b", nullptr, 0,OBConversion::GENOPTIONS);
86
2
      OBConversion::RegisterOptionParam("c", nullptr, 0,OBConversion::GENOPTIONS);
87
2
      OBConversion::RegisterOptionParam("p", nullptr, 1,OBConversion::GENOPTIONS);
88
2
      OBConversion::RegisterOptionParam("t", nullptr, 0,OBConversion::GENOPTIONS);
89
2
      OBConversion::RegisterOptionParam("k", nullptr, 0,OBConversion::GENOPTIONS);
90
2
      OBConversion::RegisterOptionParam("filter", nullptr, 1,OBConversion::GENOPTIONS);
91
2
      OBConversion::RegisterOptionParam("add", nullptr, 1,OBConversion::GENOPTIONS);
92
2
      OBConversion::RegisterOptionParam("delete", nullptr, 1,OBConversion::GENOPTIONS);
93
2
      OBConversion::RegisterOptionParam("append", nullptr, 1,OBConversion::GENOPTIONS);
94
2
    }
95
218
  }
96
97
  //! Static routine,  which can be called from elsewhere
98
  static bool ReadChemObjectImpl(OBConversion* pConv, OBFormat*);
99
  //! Static routine,  which can be called from elsewhere
100
  static bool WriteChemObjectImpl(OBConversion* pConv, OBFormat*);
101
102
  /// The "Convert" interface for reading a new molecule
103
  bool ReadChemObject(OBConversion* pConv) override
104
12
  { return ReadChemObjectImpl(pConv, this);}
105
106
  /// The "Convert" interface for writing a new molecule
107
  bool WriteChemObject(OBConversion* pConv) override
108
0
  { return WriteChemObjectImpl(pConv, this);}
109
110
  ///Applies output options to molecule. Returns false to terminate output.
111
  static bool DoOutputOptions(OBBase* pOb, OBConversion* pConv);
112
113
  /// \name Routines to handle the -C option for combining data from several OBMols
114
  //@{
115
  //! Defer output of a molecule until later, so it can be combined with others
116
  //! \return Success, or false if no molecule was read.
117
  static bool   DeferMolOutput(OBMol* pmol, OBConversion* pConv, OBFormat* pF);
118
  //! Write out all molecules queued with DeferMolOutput
119
  static bool   OutputDeferredMols(OBConversion* pConv);
120
  //! Delete the list of queued molecules from DeferMolOutput
121
  static bool   DeleteDeferredMols();
122
  //! \return the OBMol which combines @p pFirst and @p pSecond (i.e.)
123
  static OBMol* MakeCombinedMolecule(OBMol* pFirst, OBMol* pSecond);
124
  //@}
125
126
  //!When sent an OBReaction object, output all the constituent molecules
127
  static bool OutputMolsFromReaction
128
    (OBReaction* pReact, OBConversion* pConv, OBFormat* pFormat);
129
130
  typedef std::unordered_map<std::string, unsigned> NameIndexType;
131
132
  // documentation in obmolecformat.cpp
133
  static bool   ReadNameIndex(NameIndexType& index, const std::string& datafilename,
134
                  OBFormat* pInFormat);
135
136
  //! \return the type of data converted by this format (here, OBMol)
137
  const std::type_info& GetType() override
138
0
  {
139
0
    return typeid(OBMol*);
140
0
  }
141
142
private:
143
144
  static bool OptionsRegistered;
145
  static std::map<std::string, OBMol*> IMols;
146
  static OBMol* _jmol; //!< Accumulates molecules with the -j option
147
  static std::vector<OBMol> MolArray; //!< Used in --separate option
148
  static bool StoredMolsReady; //!< Used in --separate option
149
  static OBDescriptor* _pDesc;
150
151
};
152
153
}
154
#endif //OB_MOLECULEFORMAT_H
155
156
//! \file obmolecformat.h
157
//! \brief Subclass of OBFormat for conversion of OBMol.