Markup.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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: Markup.h /main/1 1996/07/29 16:56:43 cde-hp $ */
  24. // Copyright (c) 1995 James Clark
  25. // See the file COPYING for copying permission.
  26. #ifndef Markup_INCLUDED
  27. #define Markup_INCLUDED 1
  28. #ifdef __GNUG__
  29. #pragma interface
  30. #endif
  31. #include "StringC.h"
  32. #include "Syntax.h"
  33. #include "Sd.h"
  34. #include "Vector.h"
  35. #include "Text.h"
  36. #include "SdText.h"
  37. #ifdef SP_NAMESPACE
  38. namespace SP_NAMESPACE {
  39. #endif
  40. class EntityOrigin;
  41. struct SP_API MarkupItem {
  42. MarkupItem();
  43. MarkupItem(const MarkupItem &);
  44. ~MarkupItem();
  45. void operator=(const MarkupItem &);
  46. unsigned char type;
  47. unsigned char index;
  48. union {
  49. size_t nChars;
  50. ConstPtr<Origin> *origin; // type == entityStart
  51. Text *text; // type == literal
  52. SdText *sdText; // type == sdLiteral
  53. };
  54. };
  55. class InputSource;
  56. class SP_API Markup {
  57. public:
  58. enum Type {
  59. reservedName,
  60. sdReservedName,
  61. name,
  62. nameToken,
  63. attributeValue,
  64. number,
  65. comment,
  66. s,
  67. shortref,
  68. delimiter,
  69. refEndRe,
  70. entityStart,
  71. entityEnd,
  72. literal,
  73. sdLiteral
  74. };
  75. Markup();
  76. size_t size() const;
  77. void clear();
  78. void resize(size_t);
  79. void addDelim(Syntax::DelimGeneral);
  80. void addReservedName(Syntax::ReservedName, const InputSource *);
  81. void addReservedName(Syntax::ReservedName, const StringC &);
  82. void addSdReservedName(Sd::ReservedName, const InputSource *);
  83. void addSdReservedName(Sd::ReservedName, const Char *, size_t);
  84. void addS(Char);
  85. void addS(const InputSource *);
  86. void addRefEndRe();
  87. void addShortref(const InputSource *);
  88. void addCommentStart();
  89. void addCommentChar(Char);
  90. void addName(const InputSource *);
  91. void addName(const Char *, size_t);
  92. void addNameToken(const InputSource *);
  93. void addNumber(const InputSource *);
  94. void addAttributeValue(const InputSource *);
  95. void addEntityStart(const Ptr<EntityOrigin> &);
  96. void addEntityEnd();
  97. void addLiteral(const Text &);
  98. void addSdLiteral(const SdText &);
  99. void changeToAttributeValue(size_t index);
  100. void swap(Markup &);
  101. private:
  102. StringC chars_;
  103. Vector<MarkupItem> items_;
  104. friend class MarkupIter;
  105. };
  106. class Location;
  107. class SP_API MarkupIter {
  108. public:
  109. MarkupIter(const Markup &);
  110. Markup::Type type() const;
  111. Boolean valid() const;
  112. void advance();
  113. // This updates a Location.
  114. void advance(Location &, const ConstPtr<Syntax> &);
  115. size_t index() const;
  116. const Char *charsPointer() const;
  117. size_t charsLength() const;
  118. const Text &text() const;
  119. const EntityOrigin *entityOrigin() const; // valid for type == entityStart
  120. const SdText &sdText() const;
  121. Syntax::DelimGeneral delimGeneral() const;
  122. Syntax::ReservedName reservedName() const;
  123. Sd::ReservedName sdReservedName() const;
  124. private:
  125. const Char *chars_;
  126. Vector<MarkupItem>::const_iterator items_;
  127. size_t nItems_;
  128. size_t index_;
  129. size_t charIndex_;
  130. };
  131. inline
  132. void Markup::clear()
  133. {
  134. chars_.resize(0);
  135. items_.resize(0);
  136. }
  137. inline
  138. size_t Markup::size() const
  139. {
  140. return items_.size();
  141. }
  142. inline
  143. Boolean MarkupIter::valid() const
  144. {
  145. return index_ < nItems_;
  146. }
  147. inline
  148. size_t MarkupIter::index() const
  149. {
  150. return index_;
  151. }
  152. inline
  153. Markup::Type MarkupIter::type() const
  154. {
  155. return Markup::Type(items_[index_].type);
  156. }
  157. inline
  158. const EntityOrigin *MarkupIter::entityOrigin() const
  159. {
  160. return (*items_[index_].origin)->asEntityOrigin();
  161. }
  162. inline
  163. const Char *MarkupIter::charsPointer() const
  164. {
  165. return chars_ + charIndex_;
  166. }
  167. inline
  168. size_t MarkupIter::charsLength() const
  169. {
  170. return items_[index_].nChars;
  171. }
  172. inline
  173. const Text &MarkupIter::text() const
  174. {
  175. return *items_[index_].text;
  176. }
  177. inline
  178. const SdText &MarkupIter::sdText() const
  179. {
  180. return *items_[index_].sdText;
  181. }
  182. inline
  183. Syntax::DelimGeneral MarkupIter::delimGeneral() const
  184. {
  185. return Syntax::DelimGeneral(items_[index_].index);
  186. }
  187. inline
  188. Syntax::ReservedName MarkupIter::reservedName() const
  189. {
  190. return Syntax::ReservedName(items_[index_].index);
  191. }
  192. inline
  193. Sd::ReservedName MarkupIter::sdReservedName() const
  194. {
  195. return Sd::ReservedName(items_[index_].index);
  196. }
  197. #ifdef SP_NAMESPACE
  198. }
  199. #endif
  200. #endif /* not Markup_INCLUDED */