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