Link with C++ linker
[oweals/cde.git] / cde / programs / nsgmls / Location.C
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: Location.C /main/1 1996/07/29 16:56:17 cde-hp $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifdef __GNUG__
28 #pragma implementation
29 #endif
30 #include "splib.h"
31 #include "Location.h"
32
33 #ifdef SP_NAMESPACE
34 namespace SP_NAMESPACE {
35 #endif
36
37 Location::Location()
38 {
39 }
40
41 Location::Location(Origin *origin, Index i)
42 : origin_(origin), index_(i)
43 {
44 }
45
46 Location::Location(ConstPtr<Origin> origin, Index i)
47 : origin_(origin), index_(i)
48 {
49 }
50
51 Origin::~Origin()
52 {
53 }
54
55 const EntityOrigin *Origin::asEntityOrigin() const
56 {
57   return 0;
58 }
59
60 const InputSourceOrigin *Origin::asInputSourceOrigin() const
61 {
62   return 0;
63 }
64
65 Index Origin::refLength() const
66 {
67   return 0;
68 }
69
70 Boolean Origin::origChars(const Char *&) const
71 {
72   return 0;
73 }
74
75 Boolean Origin::inBracketedTextOpenDelim() const
76 {
77   return 0;
78 }
79
80 Boolean Origin::inBracketedTextCloseDelim() const
81 {
82   return 0;
83 }
84
85 Boolean Origin::isNumericCharRef(const Markup *&) const
86 {
87   return 0;
88 }
89
90 Boolean Origin::isNamedCharRef(Index, NamedCharRef &) const
91 {
92   return 0;
93 }
94
95 const EntityDecl *Origin::entityDecl() const
96 {
97   return 0;
98 }
99
100 BracketOrigin::BracketOrigin(const Location &loc, Position pos)
101 : loc_(loc), pos_(pos)
102 {
103 }
104
105 const Location &BracketOrigin::parent() const
106 {
107   return loc_;
108 }
109
110 Boolean BracketOrigin::inBracketedTextOpenDelim() const
111 {
112   return pos_ == open;
113 }
114
115 Boolean BracketOrigin::inBracketedTextCloseDelim() const
116 {
117   return pos_ == close;
118 }
119
120 InputSourceOrigin::InputSourceOrigin()
121 {
122 }
123
124 InputSourceOrigin::InputSourceOrigin(const Location &refLocation)
125 : refLocation_(refLocation)
126 {
127 }
128
129 const InputSourceOrigin *InputSourceOrigin::asInputSourceOrigin() const
130 {
131   return this;
132 }
133
134 Boolean InputSourceOrigin::defLocation(Offset, Location &) const
135 {
136   return 0;
137 }
138
139 const StringC *InputSourceOrigin::entityName() const
140 {
141   return 0;
142 }
143
144 InputSourceOrigin *InputSourceOrigin::copy() const
145 {
146   return new InputSourceOrigin(refLocation_);
147 }
148
149 const Location &InputSourceOrigin::parent() const
150 {
151   return refLocation_;
152 }
153
154 void InputSourceOrigin::setExternalInfo(ExternalInfo *info)
155 {
156   externalInfo_ = info;
157 }
158
159 void InputSourceOrigin::noteCharRef(Index replacementIndex,
160                                const NamedCharRef &ref)
161 {
162   charRefs_.resize(charRefs_.size() + 1);
163   charRefs_.back().replacementIndex = replacementIndex;
164   charRefs_.back().refStartIndex = ref.refStartIndex();
165   charRefs_.back().refEndType = ref.refEndType();
166   charRefs_.back().origNameOffset = charRefOrigNames_.size();
167   charRefOrigNames_ += ref.origName();
168 }
169
170 // Number of character references whose replacement index < ind.
171
172 size_t InputSourceOrigin::nPrecedingCharRefs(Index ind) const
173 {
174   size_t i;
175   // Find i such that
176   // charRefs_[I].replacementIndex >= ind
177   // charRefs_[i - 1].replacementIndex < ind
178   if (charRefs_.size() == 0
179       || ind > charRefs_.back().replacementIndex)
180     // This will be a common case, so optimize it.
181     i = charRefs_.size();
182   else {
183     // Binary search
184     // Invariant:
185     // charRefs_ < i have replacementIndex < ind
186     // charRefs_ >= lim have replacementIndex >= ind
187     i = 0;
188     size_t lim = charRefs_.size();
189     while (i < lim) {
190       size_t mid = i + (lim - i)/2;
191       if (charRefs_[mid].replacementIndex >= ind)
192         lim = mid;
193       else
194         i = mid + 1;
195     }
196   }
197   return i;
198 }
199
200 Offset InputSourceOrigin::startOffset(Index ind) const
201 {
202   size_t n = nPrecedingCharRefs(ind);
203   if (n < charRefs_.size()
204       && ind == charRefs_[n].replacementIndex) {
205     for (;;) {
206       ind = charRefs_[n].refStartIndex;
207       if (n == 0 || charRefs_[n - 1].replacementIndex != ind)
208         break;
209       --n;
210     }
211   }
212   // charRefs[n - 1].replacementIndex < ind
213   return Offset(ind - n);
214 }
215
216 Boolean InputSourceOrigin::isNamedCharRef(Index ind, NamedCharRef &ref) const
217 {
218   size_t n = nPrecedingCharRefs(ind);
219   if (n < charRefs_.size() && ind == charRefs_[n].replacementIndex) {
220     ref.set(charRefs_[n].refStartIndex,
221             charRefs_[n].refEndType,
222             charRefOrigNames_.data() + charRefs_[n].origNameOffset,
223             (n + 1 < charRefs_.size()
224              ? charRefs_[n + 1].origNameOffset
225              : charRefOrigNames_.size())
226             - charRefs_[n].origNameOffset);
227     return 1;
228   }
229   return 0;
230 }
231
232 ReplacementOrigin::ReplacementOrigin(const Location &loc, Char origChar)
233 : loc_(loc), origChar_(origChar)
234 {
235 }
236
237 const Location &ReplacementOrigin::parent() const
238 {
239   return loc_;
240 }
241
242 Boolean ReplacementOrigin::origChars(const Char *&s) const
243 {
244   if (loc_.origin().isNull() || !loc_.origin()->origChars(s))
245     s = &origChar_;
246   return 1;
247 }
248
249 MultiReplacementOrigin::MultiReplacementOrigin(const Location &loc,
250                                                StringC &origChars)
251 : loc_(loc)
252 {
253   origChars.swap(origChars_);
254 }
255
256 const Location &MultiReplacementOrigin::parent() const
257 {
258   return loc_;
259 }
260
261 Boolean MultiReplacementOrigin::origChars(const Char *&s) const
262 {
263   if (loc_.origin().isNull() || !loc_.origin()->origChars(s))
264     s = origChars_.data();
265   return 1;
266 }
267
268 ExternalInfo::~ExternalInfo()
269 {
270 }
271
272 RTTI_DEF0(ExternalInfo)
273
274 NamedCharRef::NamedCharRef()
275 {
276 }
277
278 NamedCharRef::NamedCharRef(Index refStartIndex, RefEndType refEndType,
279                            const StringC &origName)
280 : refStartIndex_(refStartIndex),
281   refEndType_(refEndType),
282   origName_(origName)
283 {
284 }
285
286 void NamedCharRef::set(Index refStartIndex, RefEndType refEndType,
287                        const Char *s, size_t n)
288 {
289   refStartIndex_ = refStartIndex;
290   refEndType_ = refEndType;
291   origName_.assign(s, n);
292 }
293
294 #ifdef SP_NAMESPACE
295 }
296 #endif