Link with C++ linker
[oweals/cde.git] / cde / programs / nsgmls / Text.h
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: Text.h /main/1 1996/07/29 17:06:13 cde-hp $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifndef Text_INCLUDED
28 #define Text_INCLUDED 1
29 #ifdef __GNUG__
30 #pragma interface
31 #endif
32
33 #include "types.h"
34 #include "StringC.h"
35 #include "Vector.h"
36 #include "Location.h"
37 #include "SubstTable.h"
38 #include <stddef.h>
39
40 #ifdef SP_NAMESPACE
41 namespace SP_NAMESPACE {
42 #endif
43
44 class InternalEntity;
45
46 struct SP_API TextItem {
47   TextItem();
48   enum Type {
49     data,
50     cdata,
51     sdata,
52     entityStart,
53     entityEnd,
54     startDelim,
55     endDelim,
56     endDelimA,
57     ignore
58     };
59   Type type;
60   // char that was ignored
61   Char c;
62   // location of this item
63   // data - location of first char
64   // (c/sdata)entityStart - location of first char of entity
65   // (c/sdata)entityEnd - location of entity end in entity
66   // ignore - location of ignored character
67   // startDelim - location of first char of delimiter
68   // endDelim(A) - location of first char of delimiter
69   Location loc;
70   // index of character in chars_ to which this applies
71   size_t index;
72 };
73
74 // This is used to represent literals and attribute values.
75
76 class SP_API Text {
77 public:
78   Text();
79   void clear();
80   void swap(Text &to);
81   void addChar(Char c, const Location &);
82   void addChars(const StringC &, const Location &);
83   void addChars(const Char *, size_t, const Location &);
84   void insertChars(const StringC &, const Location &);
85   void ignoreChar(Char, const Location &);
86   void ignoreLastChar();
87   void addEntityStart(const Location &);
88   void addEntityEnd(const Location &);
89   void addCdata(const InternalEntity *, const ConstPtr<Origin> &);
90   void addSdata(const InternalEntity *, const ConstPtr<Origin> &);
91   void addStartDelim(const Location &loc);
92   void addEndDelim(const Location &loc, Boolean lita);
93   void subst(const SubstTable<Char> &, Char space);
94   void addCharsTokenize(const Char *, size_t, const Location &loc, Char space);
95   void addCharsTokenize(const StringC &, const Location &loc, Char space);
96   void tokenize(Char space, Text &text) const;
97   Location charLocation(size_t i) const;
98   size_t size() const;
99   Char lastChar() const;
100   const StringC &string() const;
101   size_t nDataEntities() const;
102   Boolean fixedEqual(const Text &) const;
103   // Location of first char of start delimiter.
104   Boolean startDelimLocation(Location &) const;
105   // Location of first char of end delimiter
106   Boolean endDelimLocation(Location &) const;
107   // Is delimiter a lit or lita?
108   Boolean delimType(Boolean &lita) const;
109 private:
110   void addSimple(TextItem::Type, const Location &);
111   StringC chars_;
112   Vector<TextItem> items_;
113   friend class TextIter;
114 };
115
116 class SP_API TextIter {
117 public:
118   TextIter(const Text &);
119   void rewind();
120   Boolean next(TextItem::Type &, const Char *&, size_t &,
121                const Location *&);
122   // Alternative interface to next()
123   Boolean valid() const;
124   void advance();
125   TextItem::Type type() const;
126   const Location &location() const;
127   const Char *chars(size_t &length) const;
128 private:
129   const TextItem *ptr_;
130   const Text *text_;
131 };
132
133 inline
134 size_t Text::size() const
135 {
136   return chars_.size();
137 }
138
139 inline
140 Char Text::lastChar() const
141 {
142   return chars_[chars_.size() - 1];
143 }
144
145 inline
146 const StringC &Text::string() const
147 {
148   return chars_;
149 }
150
151 inline
152 void Text::addEntityStart(const Location &loc)
153 {
154   addSimple(TextItem::entityStart, loc);
155 }
156
157 inline
158 void Text::addEntityEnd(const Location &loc)
159 {
160   addSimple(TextItem::entityEnd, loc);
161 }
162
163 inline
164 void Text::addChars(const StringC &s, const Location &loc)
165 {
166   addChars(s.data(), s.size(), loc);
167 }
168
169 inline
170 void Text::addStartDelim(const Location &loc)
171 {
172   addSimple(TextItem::startDelim, loc);
173 }
174
175 inline
176 void Text::addEndDelim(const Location &loc, Boolean lita)
177 {
178   addSimple(lita ? TextItem::endDelimA : TextItem::endDelim,
179             loc);
180 }
181
182 inline
183 void Text::addCharsTokenize(const StringC &str, const Location &loc,
184                             Char space)
185 {
186   addCharsTokenize(str.data(), str.size(), loc, space);
187 }
188
189 inline
190 void TextIter::rewind()
191 {
192   ptr_ = text_->items_.begin();
193 }
194
195 inline
196 void TextIter::advance()
197 {
198   ptr_++;
199 }
200
201 inline
202 Boolean TextIter::valid() const
203 {
204   return ptr_ != (text_->items_.begin() + text_->items_.size());
205 }
206
207 inline
208 const Location &TextIter::location() const
209 {
210   return ptr_->loc;
211 }
212
213 inline
214 TextItem::Type TextIter::type() const
215 {
216   return ptr_->type;
217 }
218
219 #ifdef SP_NAMESPACE
220 }
221 #endif
222
223 #endif /* not Text_INCLUDED */