File: format.cpp

package info (click to toggle)
openbabel 2.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 36,644 kB
  • ctags: 33,717
  • sloc: cpp: 242,528; ansic: 87,037; sh: 10,280; perl: 5,518; python: 5,156; pascal: 793; makefile: 747; cs: 392; xml: 97; ruby: 54; java: 23
file content (132 lines) | stat: -rw-r--r-- 3,876 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**********************************************************************
obconversion.cpp -  Definitions for OBFormat

Copyright (C) 2004 by Chris Morley
Some portions Copyright (C) 2005-2007 by Geoffrey Hutchison

This file is part of the Open Babel project.
For more information, see <http://openbabel.sourceforge.net/>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
***********************************************************************/
#include <openbabel/babelconfig.h>
#include <openbabel/format.h>
#include <typeinfo>

using namespace std;
namespace OpenBabel
{
#if defined(__CYGWIN__) || defined(__MINGW32__)
  // macro to implement static OBPlugin::PluginMapType& Map()
  PLUGIN_CPP_FILE(OBFormat)
#endif

int OBFormat::RegisterFormat(const char* ID, const char* MIME)
{
  GetMap()[ID] = this;
  if (MIME)
    FormatsMIMEMap()[MIME] = this;
  if(Flags() & DEFAULTFORMAT)
    Default() = this;

  //ensure "formats" is registered as a plugin
  PluginMap()[TypeID()] =this;
  _id=ID;
  return GetMap().size();
}

//////////////////////////////////////////////////////////
const char* OBFormat::TargetClassDescription()
{
  //Provides class of default format unless overridden
  if(OBFormat::FindType(NULL))
    return OBFormat::FindType(NULL)->TargetClassDescription();
  else
    return "";
}

//////////////////////////////////////////////////////////
const type_info& OBFormat::GetType()
{
  //Provides info on class of default format unless overridden
  if(OBFormat::FindType(NULL))
    return OBFormat::FindType(NULL)->GetType();
  else
    return typeid(this); //rubbish return if DefaultFormat not set
}

//////////////////////////////////////////////////////////
OBFormat* OBFormat::FormatFromMIME(const char* MIME)
{
  if(FormatsMIMEMap().find(MIME) == FormatsMIMEMap().end())
    return NULL;
  else
    return static_cast<OBFormat*>(FormatsMIMEMap()[MIME]);
}

//////////////////////////////////////////////////////////
bool OBFormat::Display(std::string& txt, const char* param, const char* ID)
{
  //No output for formats which can't be written or read
  if((Flags() & NOTREADABLE) && (Flags() & NOTWRITABLE))
    return false;

  bool justread=false, justwrite=false;
  //No output if formats is not readable or writable if this was requested
  if(param)
  {
    if((!strncasecmp(param, "in", 2) || !strncasecmp(param, "read",4)))
    {
      justread=true;
      if(Flags() & NOTREADABLE)
        return false;
    }
    if((!strncasecmp(param, "out",3) || !strncasecmp(param, "write",5)))
    {
      justwrite=true;
      if(Flags() & NOTWRITABLE)
        return false;
    }
  }

  //Use the provided ID if possible. If more than one ID has been registed
  //for the format, e.g. "smiles" and "smi", the contents of the member 
  //variable _id, returned by GetID() is the last one.
  if(ID)
    txt = ID;
  else
    txt = GetID();
  txt += " -- ";
  txt += FirstLine(Description());
  if(!justread && (Flags() & NOTWRITABLE))
    txt += " [Read-only]";
  if(!justwrite && (Flags() & NOTREADABLE))
    txt += " [Write-only]";

  if(param && strstr(param, "verbose"))
  {
    const char* nl = strchr(Description(), '\n');
    if(nl)
    {
      txt += ++nl; // add rest of description
      if(strlen(SpecificationURL()))
      {
        txt += "\nSpecification at: ";
        txt += SpecificationURL();
      }
      txt += "\n";
    }
  }
  return true;//means txt has been updated
}
}//namespace

//! \file format.cpp
//! \brief Base class OBFormat for file formats