~ubuntu-branches/ubuntu/trusty/openjade1.3/trusty

« back to all changes in this revision

Viewing changes to lib/Fixed2CodingSystem.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2002-04-09 00:01:50 UTC
  • Revision ID: james.westby@ubuntu.com-20020409000150-r9rkyalxlhvf9ba3
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

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