/src/openbabel/include/openbabel/text.h
Line | Count | Source |
1 | | /********************************************************************** |
2 | | text.h - Declaration and Implementation of OBText |
3 | | |
4 | | Copyright (C) 2008 by 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_TEXT_H |
20 | | #define OB_TEXT_H |
21 | | |
22 | | #include <openbabel/babelconfig.h> |
23 | | #include <openbabel/base.h> |
24 | | |
25 | | namespace OpenBabel |
26 | | { |
27 | | //! \class OBText text.h <openbabel/text.h> |
28 | | //! \brief An object containing just text |
29 | | class OBText : public OBBase |
30 | | { |
31 | | private: |
32 | | std::string txt; |
33 | | public: |
34 | | //Constructors |
35 | 14 | OBText(){} |
36 | 0 | OBText(const std::string& text) :txt(text) {} |
37 | | |
38 | | ///\return all the text |
39 | 7 | std::string GetText()const { return txt; } |
40 | | |
41 | | /**\return text from position \param pos up to, but not including, |
42 | | the line containing the next occurrence of "OPENBABEL_INSERT". |
43 | | \param pos is updated to the start of the next line. |
44 | | If "OPENBABEL_INSERT" is not found, and \param ToInsertOnly is false |
45 | | the text up to the end of the file is returned and \param pos is set to 0. |
46 | | If "OPENBABEL_INSERT" is not found, and \param ToInsertOnly is true |
47 | | an empty string is return and \param pos is unchaged. |
48 | | |
49 | | Inserting OpenBabel output into boilerplate text. |
50 | | Suppose you wanted to insert XML output from OB into into a template XML document |
51 | | using the babel interface |
52 | | babel template.text inputfile.xxx outputfile.yyy |
53 | | The template file could contain a line: |
54 | | <!-- OPENBABEL_INSERT here --> |
55 | | and still be well-formed XML. |
56 | | The template file would be read by TextFormat and passed to the output as an OBText object. |
57 | | This could be processed in the output format's WriteChemObject() or WriteMolecule() |
58 | | in the following way (see cmlreactformat.cpp) |
59 | | <code> |
60 | | OBText* ptext = dynamic_cast<OBText*>(pOb); |
61 | | if(ptext) { |
62 | | string::size_type pos = 0; |
63 | | *pConv->GetOutStream() << ptext->GetText(pos); //Output text up to insertion point |
64 | | _text = ptext->GetText(pos); //Save text after insertion point to be output at the end |
65 | | } |
66 | | </code> |
67 | | **/ |
68 | | std::string GetText(std::string::size_type& pos, bool ToInsertOnly=false) const |
69 | 0 | { |
70 | 0 | std::string::size_type oldpos = pos; |
71 | 0 | std::string::size_type newpos = txt.find("OPENBABEL_INSERT", pos); |
72 | 0 | if(newpos== std::string::npos)//not found: return rest of txt |
73 | 0 | { |
74 | 0 | if(ToInsertOnly) |
75 | 0 | return(""); |
76 | 0 | pos = 0; |
77 | 0 | return txt.substr(oldpos); |
78 | 0 | } |
79 | 0 |
|
80 | 0 | newpos = txt.rfind('\n', newpos); //to end of previous line |
81 | 0 | pos = txt.find("\n", newpos+1)+1; //to past end of line, or 0 |
82 | 0 | return txt.substr(oldpos, newpos-oldpos); |
83 | 0 | } |
84 | | |
85 | 14 | void SetText(const std::string& text){ txt = text; } |
86 | | }; |
87 | | |
88 | | }//namespace |
89 | | #endif //OB_TEXT_H |
90 | | |
91 | | //! \file text.h |
92 | | //! \brief Handle generic non-chemical text processing |