CharsetDecl.C 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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: CharsetDecl.C /main/1 1996/07/29 16:47:07 cde-hp $ */
  24. // Copyright (c) 1994 James Clark
  25. // See the file COPYING for copying permission.
  26. #ifdef __GNUG__
  27. #pragma implementation
  28. #endif
  29. #include "splib.h"
  30. #include "CharsetDecl.h"
  31. #include "macros.h"
  32. #include "ISet.h"
  33. #include "constant.h"
  34. #ifdef SP_NAMESPACE
  35. namespace SP_NAMESPACE {
  36. #endif
  37. CharsetDeclRange::CharsetDeclRange()
  38. : descMin_(0),
  39. count_(0),
  40. type_(unused),
  41. baseMin_(0)
  42. {
  43. }
  44. CharsetDeclRange::CharsetDeclRange(WideChar descMin, Number count,
  45. WideChar baseMin)
  46. : descMin_(descMin),
  47. count_(count),
  48. type_(number),
  49. baseMin_(baseMin)
  50. {
  51. }
  52. CharsetDeclRange::CharsetDeclRange(WideChar descMin, Number count)
  53. : descMin_(descMin),
  54. count_(count),
  55. type_(unused),
  56. baseMin_(0)
  57. {
  58. }
  59. CharsetDeclRange::CharsetDeclRange(WideChar descMin, Number count,
  60. const StringC &str)
  61. : descMin_(descMin),
  62. count_(count),
  63. type_(string),
  64. str_(str),
  65. baseMin_(0)
  66. {
  67. }
  68. void CharsetDeclRange::rangeDeclared(WideChar min, Number count,
  69. ISet<WideChar> &declared) const
  70. {
  71. if (count > 0 && min + count > descMin_ && min < descMin_ + count_) {
  72. WideChar commMin = (descMin_ > min) ? descMin_ : min;
  73. WideChar commMax = min + ((min + count < descMin_ + count_
  74. ? count
  75. : descMin_ + count_ - min) - 1);
  76. ASSERT(commMin <= commMax);
  77. declared.addRange(commMin, commMax);
  78. }
  79. }
  80. void CharsetDeclRange::usedSet(ISet<Char> &set) const
  81. {
  82. if (type_ != unused && count_ > 0 && descMin_ <= charMax) {
  83. Char max;
  84. if (charMax - descMin_ < count_ - 1)
  85. max = charMax;
  86. else
  87. max = Char(descMin_ + (count_ - 1));
  88. set.addRange(Char(descMin_), max);
  89. }
  90. }
  91. void CharsetDeclRange::stringToChar(const StringC &str, ISet<WideChar> &to)
  92. const
  93. {
  94. if (type_ == string && str_ == str && count_ > 0)
  95. to.addRange(descMin_, descMin_ + (count_ - 1));
  96. }
  97. void CharsetDeclRange::numberToChar(Number n, ISet<WideChar> &to,
  98. Number &count)
  99. const
  100. {
  101. if (type_ == number && n >= baseMin_ && n - baseMin_ < count_) {
  102. Number thisCount = count_ - (n - baseMin_);
  103. if (to.isEmpty() || thisCount < count)
  104. count = thisCount;
  105. to.add(descMin_ + (n - baseMin_));
  106. }
  107. }
  108. Boolean CharsetDeclRange::getCharInfo(WideChar fromChar,
  109. CharsetDeclRange::Type &type,
  110. Number &n,
  111. StringC &str,
  112. Number &count) const
  113. {
  114. if (fromChar >= descMin_ && fromChar - descMin_ < count_) {
  115. type = type_;
  116. if (type == number)
  117. n = baseMin_ + (fromChar - descMin_);
  118. else if (type == string)
  119. str = str_;
  120. count = count_ - (fromChar - descMin_);
  121. return 1;
  122. }
  123. else
  124. return 0;
  125. }
  126. CharsetDeclSection::CharsetDeclSection()
  127. {
  128. }
  129. void CharsetDeclSection::setPublicId(const PublicId &id)
  130. {
  131. baseset_ = id;
  132. }
  133. void CharsetDeclSection::addRange(const CharsetDeclRange &range)
  134. {
  135. ranges_.push_back(range);
  136. }
  137. void CharsetDeclSection::rangeDeclared(WideChar min, Number count,
  138. ISet<WideChar> &declared) const
  139. {
  140. for (size_t i = 0; i < ranges_.size(); i++)
  141. ranges_[i].rangeDeclared(min, count, declared);
  142. }
  143. void CharsetDeclSection::usedSet(ISet<Char> &set) const
  144. {
  145. for (size_t i = 0; i < ranges_.size(); i++)
  146. ranges_[i].usedSet(set);
  147. }
  148. void CharsetDeclSection::stringToChar(const StringC &str, ISet<WideChar> &to)
  149. const
  150. {
  151. for (size_t i = 0; i < ranges_.size(); i++)
  152. ranges_[i].stringToChar(str, to);
  153. }
  154. void CharsetDeclSection::numberToChar(const PublicId *id, Number n,
  155. ISet<WideChar> &to, Number &count) const
  156. {
  157. PublicId::OwnerType ownerType;
  158. StringC seq1, seq2;
  159. if (id->string() == baseset_.string()
  160. // Assume that 2 ISO character sets are the same if
  161. // their designating sequences are the same.
  162. || (id->getOwnerType(ownerType)
  163. && ownerType == PublicId::ISO
  164. && baseset_.getOwnerType(ownerType)
  165. && ownerType == PublicId::ISO
  166. && id->getDesignatingSequence(seq1)
  167. && baseset_.getDesignatingSequence(seq2)
  168. && seq1 == seq2)) {
  169. for (size_t i = 0; i < ranges_.size(); i++)
  170. ranges_[i].numberToChar(n, to, count);
  171. }
  172. }
  173. Boolean CharsetDeclSection::getCharInfo(WideChar fromChar,
  174. const PublicId *&id,
  175. CharsetDeclRange::Type &type,
  176. Number &n,
  177. StringC &str,
  178. Number &count) const
  179. {
  180. for (size_t i = 0; i < ranges_.size(); i++)
  181. if (ranges_[i].getCharInfo(fromChar, type, n, str, count)) {
  182. id = &baseset_;
  183. return 1;
  184. }
  185. return 0;
  186. }
  187. CharsetDecl::CharsetDecl()
  188. {
  189. }
  190. void CharsetDecl::addSection(const PublicId &id)
  191. {
  192. sections_.resize(sections_.size() + 1);
  193. sections_.back().setPublicId(id);
  194. }
  195. void CharsetDecl::swap(CharsetDecl &to)
  196. {
  197. sections_.swap(to.sections_);
  198. declaredSet_.swap(to.declaredSet_);
  199. }
  200. void CharsetDecl::clear()
  201. {
  202. sections_.clear();
  203. }
  204. void CharsetDecl::addRange(WideChar min, Number count, WideChar baseMin)
  205. {
  206. if (count > 0)
  207. declaredSet_.addRange(min, min + (count - 1));
  208. CharsetDeclRange range(min, count, baseMin);
  209. sections_.back().addRange(range);
  210. }
  211. void CharsetDecl::addRange(WideChar min, Number count)
  212. {
  213. if (count > 0)
  214. declaredSet_.addRange(min, min + (count - 1));
  215. CharsetDeclRange range(min, count);
  216. sections_.back().addRange(range);
  217. }
  218. void CharsetDecl::addRange(WideChar min, Number count, const StringC &str)
  219. {
  220. if (count > 0)
  221. declaredSet_.addRange(min, min + (count - 1));
  222. CharsetDeclRange range(min, count, str);
  223. sections_.back().addRange(range);
  224. }
  225. void CharsetDecl::rangeDeclared(WideChar min, Number count,
  226. ISet<WideChar> &declared) const
  227. {
  228. for (size_t i = 0; i < sections_.size(); i++)
  229. sections_[i].rangeDeclared(min, count, declared);
  230. }
  231. void CharsetDecl::usedSet(ISet<Char> &set) const
  232. {
  233. for (size_t i = 0; i < sections_.size(); i++)
  234. sections_[i].usedSet(set);
  235. }
  236. Boolean CharsetDecl::getCharInfo(WideChar fromChar,
  237. const PublicId *&id,
  238. CharsetDeclRange::Type &type,
  239. Number &n,
  240. StringC &str,
  241. Number &count) const
  242. {
  243. for (size_t i = 0; i < sections_.size(); i++)
  244. if (sections_[i].getCharInfo(fromChar, id, type, n, str, count))
  245. return 1;
  246. return 0;
  247. }
  248. void CharsetDecl::stringToChar(const StringC &str, ISet<WideChar> &to) const
  249. {
  250. for (size_t i = 0; i < sections_.size(); i++)
  251. sections_[i].stringToChar(str, to);
  252. }
  253. void CharsetDecl::numberToChar(const PublicId *id, Number n,
  254. ISet<WideChar> &to, Number &count) const
  255. {
  256. for (size_t i = 0; i < sections_.size(); i++)
  257. sections_[i].numberToChar(id, n, to, count);
  258. }
  259. #ifdef SP_NAMESPACE
  260. }
  261. #endif