/src/openbabel/src/formats/copyformat.cpp
Line | Count | Source |
1 | | /********************************************************************** |
2 | | Copyright (C) 2005 by Chris Morley |
3 | | |
4 | | This program is free software; you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation version 2 of the License. |
7 | | |
8 | | This program is distributed in the hope that it will be useful, |
9 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | | GNU General Public License for more details. |
12 | | ***********************************************************************/ |
13 | | #include <openbabel/babelconfig.h> |
14 | | #include <openbabel/obconversion.h> |
15 | | |
16 | | using namespace std; |
17 | | namespace OpenBabel |
18 | | { |
19 | | |
20 | | class CopyFormat : public OBFormat |
21 | | { |
22 | | public: |
23 | | CopyFormat() |
24 | 6 | { |
25 | 6 | OBConversion::RegisterFormat("copy",this); |
26 | 6 | } |
27 | | |
28 | | const char* Description() override // required |
29 | 0 | { |
30 | 0 | return |
31 | 0 | "Copy raw text\n" |
32 | 0 | "A utility format for exactly copying the text of a chemical file format\n" |
33 | 0 | "This format allows you to filter molecules from multimolecule files\n" |
34 | 0 | "without the risk of losing any additional information they contain,\n" |
35 | 0 | "since no format conversion is carried out.\n\n" |
36 | |
|
37 | 0 | ".. warning::\n\n" |
38 | 0 | " Currently not working correctly for files with Windows line endings.\n\n" |
39 | |
|
40 | 0 | "Example:\n\n" |
41 | |
|
42 | 0 | " Extract only structures that include at least one aromatic carbon\n" |
43 | 0 | " (by matching the SMARTS pattern ``[c]``)::\n\n" |
44 | |
|
45 | 0 | " obabel database.sdf -ocopy -O new.sdf -s '[c]' \n\n" |
46 | |
|
47 | 0 | ".. note::\n\n" |
48 | 0 | " XML files may be missing non-object elements\n" |
49 | 0 | " at the start or end and so may no longer be well formed.\n\n" |
50 | 0 | ; |
51 | 0 | } |
52 | | |
53 | | unsigned int Flags() override |
54 | 6 | { |
55 | 6 | return NOTREADABLE; |
56 | 6 | } |
57 | | |
58 | | ///////////////////////////////////////////////////////////////// |
59 | | bool WriteChemObject(OBConversion* pConv) override |
60 | 0 | { |
61 | 0 | pConv->GetChemObject();//needed to increment pConv->Index |
62 | |
|
63 | 0 | istream& ifs = *pConv->GetInStream(); |
64 | 0 | ostream& ofs = *pConv->GetOutStream(); |
65 | |
|
66 | 0 | streampos startpos = pConv->GetInPos(); |
67 | 0 | int len = pConv->GetInLen(); |
68 | 0 | if(len>0) |
69 | 0 | { |
70 | 0 | streampos curpos = ifs.tellg(); |
71 | 0 | if(ifs.eof()) |
72 | 0 | ifs.clear(); |
73 | 0 | ifs.seekg(startpos); |
74 | |
|
75 | 0 | char* buf = new char[len+1]; |
76 | 0 | ifs.read(buf,len); |
77 | 0 | ofs.write(buf,len); |
78 | 0 | delete[] buf; |
79 | |
|
80 | 0 | ifs.seekg(curpos); |
81 | 0 | } |
82 | 0 | else |
83 | 0 | { |
84 | | //When no length recorded, copy the whole input stream |
85 | | //Seem to need to treat stringstreams differently |
86 | 0 | stringstream* pss = dynamic_cast<stringstream*>(&ifs); |
87 | 0 | if(pss) |
88 | 0 | ofs << pss->str() << flush; |
89 | 0 | else |
90 | 0 | ofs << ifs.rdbuf() << flush; |
91 | 0 | } |
92 | 0 | return true; |
93 | 0 | } |
94 | | |
95 | | }; |
96 | | |
97 | | //Make an instance of the format class |
98 | | CopyFormat theCopyFormat; |
99 | | |
100 | | |
101 | | } //namespace OpenBabel |
102 | | |