Fixed2CodingSystem.C 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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: Fixed2CodingSystem.C /main/1 1996/07/29 16:52:03 cde-hp $ */
  24. // Copyright (c) 1994 James Clark
  25. // See the file COPYING for copying permission.
  26. // This uses a big endian byte order irrespective of host byte order.
  27. // Nothing special is done with FEFF/FFFE.
  28. #include "splib.h"
  29. #ifdef SP_MULTI_BYTE
  30. #include "Fixed2CodingSystem.h"
  31. #include "macros.h"
  32. #if defined(__linux__) || defined(CSRG_BASED) || defined(sun)
  33. #include <iostream>
  34. #else
  35. #include <iostream.h>
  36. #endif
  37. #ifdef SP_NAMESPACE
  38. namespace SP_NAMESPACE {
  39. #endif
  40. class Fixed2Decoder : public Decoder {
  41. public:
  42. Fixed2Decoder();
  43. size_t decode(Char *to, const char *from, size_t fromLen,
  44. const char **rest);
  45. Boolean convertOffset(unsigned long &offset) const;
  46. };
  47. class Fixed2Encoder : public Encoder {
  48. public:
  49. Fixed2Encoder();
  50. ~Fixed2Encoder();
  51. void output(Char *, size_t, streambuf *);
  52. void output(const Char *, size_t, streambuf *);
  53. private:
  54. void allocBuf(size_t);
  55. char *buf_;
  56. size_t bufSize_;
  57. };
  58. Decoder *Fixed2CodingSystem::makeDecoder() const
  59. {
  60. return new Fixed2Decoder;
  61. }
  62. Encoder *Fixed2CodingSystem::makeEncoder() const
  63. {
  64. return new Fixed2Encoder;
  65. }
  66. unsigned Fixed2CodingSystem::fixedBytesPerChar() const
  67. {
  68. return 2;
  69. }
  70. Fixed2Decoder::Fixed2Decoder()
  71. : Decoder(2)
  72. {
  73. }
  74. size_t Fixed2Decoder::decode(Char *to, const char *from, size_t fromLen,
  75. const char **rest)
  76. {
  77. #ifdef BIG_ENDIAN
  78. if (sizeof(Char) == 2 && from == (char *)to) {
  79. *rest = from + (fromLen & ~1);
  80. return fromLen/2;
  81. }
  82. #endif
  83. fromLen &= ~1;
  84. *rest = from + fromLen;
  85. for (size_t n = fromLen; n > 0; n -= 2) {
  86. *to++ = ((unsigned char)from[0] << 8) + (unsigned char)from[1];
  87. from += 2;
  88. }
  89. return fromLen/2;
  90. }
  91. Boolean Fixed2Decoder::convertOffset(unsigned long &n) const
  92. {
  93. n *= 2;
  94. return true;
  95. }
  96. Fixed2Encoder::Fixed2Encoder()
  97. : buf_(0), bufSize_(0)
  98. {
  99. }
  100. Fixed2Encoder::~Fixed2Encoder()
  101. {
  102. delete [] buf_;
  103. }
  104. void Fixed2Encoder::allocBuf(size_t n)
  105. {
  106. if (bufSize_ < n) {
  107. delete [] buf_;
  108. buf_ = new char[bufSize_ = n];
  109. }
  110. }
  111. // FIXME handle errors from streambuf::sputn
  112. void Fixed2Encoder::output(Char *s, size_t n, streambuf *sb)
  113. {
  114. #ifdef BIG_ENDIAN
  115. if (sizeof(Char) == 2) {
  116. sb->sputn((char *)s, n*2);
  117. return;
  118. }
  119. #endif
  120. ASSERT(sizeof(Char) >= 2);
  121. char *p = (char *)s;
  122. for (size_t i = 0; i < n; i++) {
  123. *p++ = (s[i] >> 8) & 0xff;
  124. *p++ = s[i] & 0xff;
  125. }
  126. sb->sputn((char *)s, n*2);
  127. }
  128. void Fixed2Encoder::output(const Char *s, size_t n, streambuf *sb)
  129. {
  130. #ifdef BIG_ENDIAN
  131. if (sizeof(Char) == 2) {
  132. sb->sputn((char *)s, n*2);
  133. return;
  134. }
  135. #endif
  136. allocBuf(n*2);
  137. for (size_t i = 0; i < n; i++) {
  138. buf_[i*2] = (s[i] >> 8) & 0xff;
  139. buf_[i*2 + 1] = s[i] & 0xff;
  140. }
  141. sb->sputn(buf_, n*2);
  142. }
  143. #ifdef SP_NAMESPACE
  144. }
  145. #endif
  146. #else /* not SP_MULTI_BYTE */
  147. #ifndef __GNUG__
  148. static char non_empty_translation_unit; // sigh
  149. #endif
  150. #endif /* not SP_MULTI_BYTE */