File: Fixed2CodingSystem.cxx

package info (click to toggle)
jade 1.2.1-43
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 8,808 kB
  • ctags: 16,989
  • sloc: cpp: 120,657; sh: 10,452; ansic: 8,228; perl: 378; makefile: 296; sed: 5
file content (144 lines) | stat: -rw-r--r-- 2,724 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 1994 James Clark
// See the file COPYING for copying permission.

// This uses a big endian byte order irrespective of host byte order.
// Nothing special is done with FEFF/FFFE.

#include "splib.h"

#ifdef SP_MULTI_BYTE

#include "Fixed2CodingSystem.h"
#include "macros.h"

#ifdef SP_NAMESPACE
namespace SP_NAMESPACE {
#endif

class Fixed2Decoder : public Decoder {
public:
  Fixed2Decoder();
  size_t decode(Char *to, const char *from, size_t fromLen,
		const char **rest);
  Boolean convertOffset(unsigned long &offset) const;
};

class Fixed2Encoder : public Encoder {
public:
  Fixed2Encoder();
  ~Fixed2Encoder();
  void output(Char *, size_t, OutputByteStream *);
  void output(const Char *, size_t, OutputByteStream *);
private:
  void allocBuf(size_t);
  char *buf_;
  size_t bufSize_;
};

Decoder *Fixed2CodingSystem::makeDecoder() const
{
  return new Fixed2Decoder;
}

Encoder *Fixed2CodingSystem::makeEncoder() const
{
  return new Fixed2Encoder;
}

unsigned Fixed2CodingSystem::fixedBytesPerChar() const
{
  return 2;
}

Fixed2Decoder::Fixed2Decoder()
: Decoder(2)
{
}

size_t Fixed2Decoder::decode(Char *to, const char *from, size_t fromLen,
			   const char **rest)
{
#ifdef SP_BIG_ENDIAN
  if (sizeof(Char) == 2 && from == (char *)to) {
    *rest = from + (fromLen & ~1);
    return fromLen/2;
  }
#endif
  fromLen &= ~1;
  *rest = from + fromLen;
  for (size_t n = fromLen; n > 0; n -= 2) {
    *to++ = ((unsigned char)from[0] << 8) + (unsigned char)from[1];
    from += 2;
  }
  return fromLen/2;
}

Boolean Fixed2Decoder::convertOffset(unsigned long &n) const
{
  n *= 2;
  return true;
}

Fixed2Encoder::Fixed2Encoder()
: buf_(0), bufSize_(0)
{
}

Fixed2Encoder::~Fixed2Encoder()
{
  delete [] buf_;
}

void Fixed2Encoder::allocBuf(size_t n)
{
  if (bufSize_ < n) {
    delete [] buf_;
    buf_ = new char[bufSize_ = n];
  }
}

void Fixed2Encoder::output(Char *s, size_t n, OutputByteStream *sb)
{
#ifdef SP_BIG_ENDIAN
  if (sizeof(Char) == 2) {
    sb->sputn((char *)s, n*2);
    return;
  }
#endif
  ASSERT(sizeof(Char) >= 2);
  char *p = (char *)s;
  for (size_t i = 0; i < n; i++) {
    Char c = s[i];
    *p++ = (c >> 8) & 0xff;
    *p++ = c & 0xff;
  }
  sb->sputn((char *)s, n*2);
}

void Fixed2Encoder::output(const Char *s, size_t n, OutputByteStream *sb)
{
#ifdef SP_BIG_ENDIAN
  if (sizeof(Char) == 2) {
    sb->sputn((char *)s, n*2);
    return;
  }
#endif
  allocBuf(n*2);
  for (size_t i = 0; i < n; i++) {
    buf_[i*2] = (s[i] >> 8) & 0xff;
    buf_[i*2 + 1] = s[i] & 0xff;
  }
  sb->sputn(buf_, n*2);
}

#ifdef SP_NAMESPACE
}
#endif

#else /* not SP_MULTI_BYTE */

#ifndef __GNUG__
static char non_empty_translation_unit;	// sigh
#endif

#endif /* not SP_MULTI_BYTE */