ucs4.h
Upload User: yingmei828
Upload Date: 2007-01-01
Package Size: 1646k
Code Size: 1k
Development Platform:

Unix_Linux

  1. /*
  2.  * UCS-4
  3.  */
  4. /* Here we accept 0000FFFE/0000FEFF marks as endianness indicators everywhere
  5.    in the stream, not just at the beginning. The default is big-endian. */
  6. /* The state is 0 if big-endian, 1 if little-endian. */
  7. static int
  8. ucs4_mbtowc (conv_t conv, wchar_t *pwc, const unsigned char *s, int n)
  9. {
  10.   state_t state = conv->istate;
  11.   int count = 0;
  12.   for (; n >= 4;) {
  13.     wchar_t wc = (state
  14.                   ? s[0] + (s[1] << 8) + (s[2] << 16) + (s[3] << 24)
  15.                   : (s[0] << 24) + (s[1] << 16) + (s[2] << 8) + s[3]);
  16.     s += 4; n -= 4; count += 4;
  17.     if (wc == 0xfeff) {
  18.     } else if (wc == 0xfffe) {
  19.       state ^= 1;
  20.     } else if (wc <= 0x7fffffff) {
  21.       *pwc = wc;
  22.       conv->istate = state;
  23.       return count;
  24.     } else
  25.       return RET_ILSEQ;
  26.   }
  27.   conv->istate = state;
  28.   return RET_TOOFEW(count);
  29. }
  30. /* But we output UCS-4 in big-endian order, without byte-order mark. */
  31. static int
  32. ucs4_wctomb (conv_t conv, unsigned char *r, wchar_t wc, int n)
  33. {
  34.   if (wc != 0xfffe) {
  35.     if (n >= 4) {
  36.       r[0] = (unsigned char) (wc >> 24);
  37.       r[1] = (unsigned char) (wc >> 16);
  38.       r[2] = (unsigned char) (wc >> 8);
  39.       r[3] = (unsigned char) wc;
  40.       return 4;
  41.     } else
  42.       return RET_TOOSMALL;
  43.   } else
  44.     return RET_ILSEQ;
  45. }