InputSource.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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: InputSource.h /main/1 1996/07/29 16:55:17 cde-hp $ */
  24. // Copyright (c) 1994 James Clark
  25. // See the file COPYING for copying permission.
  26. #ifndef InputSource_INCLUDED
  27. #define InputSource_INCLUDED 1
  28. #ifdef __GNUG__
  29. #pragma interface
  30. #endif
  31. #include "types.h"
  32. #include "Link.h"
  33. #include "Ptr.h"
  34. #include "Location.h"
  35. #include "XcharMap.h"
  36. #include <stddef.h>
  37. #ifdef SP_NAMESPACE
  38. namespace SP_NAMESPACE {
  39. #endif
  40. class Messenger;
  41. class NamedCharRef;
  42. class SP_API InputSource : public Link {
  43. public:
  44. enum { eE = -1 }; // end of entity signal
  45. virtual ~InputSource();
  46. Xchar get(Messenger &);
  47. virtual void pushCharRef(Char ch, const NamedCharRef &) = 0;
  48. const Location &currentLocation() const;
  49. const Char *currentTokenStart() const;
  50. size_t currentTokenLength() const;
  51. const Char *currentTokenEnd() const;
  52. Index nextIndex() const;
  53. // Discard all but the last character of the current token.
  54. void discardInitial();
  55. void startToken();
  56. void startTokenNoMulticode();
  57. void endToken(size_t length);
  58. Xchar tokenChar(Messenger &);
  59. void ungetToken();
  60. void setMarkupScanTable(const XcharMap<unsigned char> &);
  61. Boolean scanSuppress() const;
  62. void extendToBufferEnd();
  63. virtual void willNotRewind();
  64. virtual Boolean rewind(Messenger &) = 0;
  65. Boolean accessError() const;
  66. protected:
  67. InputSource(InputSourceOrigin *origin, const Char *start, const Char *end);
  68. void reset(const Char *start, const Char *end);
  69. InputSourceOrigin *inputSourceOrigin();
  70. void noteCharRef(Index replacementIndex, const NamedCharRef &);
  71. const Char *cur();
  72. const Char *start();
  73. const Char *end();
  74. Index startIndex();
  75. void changeBuffer(const Char *newBase, const Char *oldBase);
  76. void advanceEnd(const Char *newEnd);
  77. void moveLeft();
  78. void moveStart(const Char *newStart);
  79. Char nextChar();
  80. void setAccessError();
  81. private:
  82. InputSource(const InputSource &); // undefined
  83. void operator=(const InputSource &); // undefined
  84. virtual Xchar fill(Messenger &) = 0;
  85. void advanceStart(const Char *to);
  86. void advanceStartMulticode(const Char *to);
  87. const Char *cur_;
  88. const Char *start_;
  89. const Char *end_;
  90. Location startLocation_;
  91. Ptr<InputSourceOrigin> origin_;
  92. Boolean accessError_;
  93. Boolean scanSuppress_;
  94. Boolean scanSuppressSingle_;
  95. Index scanSuppressIndex_;
  96. Boolean multicode_;
  97. XcharMap<unsigned char> markupScanTable_;
  98. };
  99. inline
  100. void InputSource::advanceStart(const Char *to)
  101. {
  102. if (multicode_)
  103. advanceStartMulticode(to);
  104. else {
  105. startLocation_ += to - start_;
  106. start_ = to;
  107. }
  108. }
  109. inline
  110. Xchar InputSource::get(Messenger &mgr)
  111. {
  112. advanceStart(cur_);
  113. return cur_ < end_ ? *cur_++ : fill(mgr);
  114. }
  115. inline
  116. void InputSource::startTokenNoMulticode()
  117. {
  118. startLocation_ += cur_ - start_;
  119. start_ = cur_;
  120. }
  121. inline
  122. void InputSource::startToken()
  123. {
  124. advanceStart(cur_);
  125. }
  126. inline
  127. void InputSource::endToken(size_t length)
  128. {
  129. cur_ = start_ + length;
  130. }
  131. inline
  132. Xchar InputSource::tokenChar(Messenger &mgr)
  133. {
  134. return cur_ < end_ ? *cur_++ : fill(mgr);
  135. }
  136. inline
  137. void InputSource::extendToBufferEnd()
  138. {
  139. cur_ = end_;
  140. }
  141. inline
  142. const Char *InputSource::cur()
  143. {
  144. return cur_;
  145. }
  146. inline
  147. const Char *InputSource::start()
  148. {
  149. return start_;
  150. }
  151. inline
  152. const Char *InputSource::end()
  153. {
  154. return end_;
  155. }
  156. inline
  157. void InputSource::changeBuffer(const Char *newBase, const Char *oldBase)
  158. {
  159. cur_ = newBase + (cur_ - oldBase);
  160. start_ = newBase + (start_ - oldBase);
  161. end_ = newBase + (end_ - oldBase);
  162. }
  163. inline
  164. void InputSource::moveStart(const Char *newStart)
  165. {
  166. cur_ = newStart + (cur_ - start_);
  167. end_ = newStart + (end_ - start_);
  168. start_ = newStart;
  169. }
  170. inline
  171. void InputSource::advanceEnd(const Char *newEnd)
  172. {
  173. end_ = newEnd;
  174. }
  175. inline
  176. Char InputSource::nextChar()
  177. {
  178. return *cur_++;
  179. }
  180. inline
  181. Index InputSource::startIndex()
  182. {
  183. return startLocation_.index();
  184. }
  185. inline
  186. void InputSource::moveLeft()
  187. {
  188. start_--;
  189. cur_--;
  190. }
  191. inline
  192. void InputSource::noteCharRef(Index replacementIndex, const NamedCharRef &ref)
  193. {
  194. origin_->noteCharRef(replacementIndex, ref);
  195. }
  196. inline
  197. const Location &InputSource::currentLocation() const
  198. {
  199. return startLocation_;
  200. }
  201. inline
  202. const Char *InputSource::currentTokenStart() const
  203. {
  204. return start_;
  205. }
  206. inline
  207. size_t InputSource::currentTokenLength() const
  208. {
  209. return cur_ - start_;
  210. }
  211. inline
  212. Index InputSource::nextIndex() const
  213. {
  214. return startLocation_.index() + (cur_ - start_);
  215. }
  216. inline
  217. const Char *InputSource::currentTokenEnd() const
  218. {
  219. return cur_;
  220. }
  221. inline
  222. void InputSource::discardInitial()
  223. {
  224. advanceStart(cur_ - 1);
  225. }
  226. inline
  227. void InputSource::ungetToken()
  228. {
  229. cur_ = start_;
  230. }
  231. inline
  232. void InputSource::setMarkupScanTable(const XcharMap<unsigned char> &table)
  233. {
  234. markupScanTable_ = table;
  235. multicode_ = 1;
  236. }
  237. inline
  238. Boolean InputSource::scanSuppress() const
  239. {
  240. return scanSuppress_ && (!scanSuppressSingle_
  241. || startLocation_.index() == scanSuppressIndex_);
  242. }
  243. inline
  244. InputSourceOrigin *InputSource::inputSourceOrigin()
  245. {
  246. return origin_.pointer();
  247. }
  248. inline
  249. void InputSource::setAccessError()
  250. {
  251. accessError_ = 1;
  252. }
  253. inline
  254. Boolean InputSource::accessError() const
  255. {
  256. return accessError_;
  257. }
  258. #ifdef SP_NAMESPACE
  259. }
  260. #endif
  261. #endif /* not InputSource_INCLUDED */