fixed2codingsystem.cxx

来自「SP是一个基于GNU C++编译器」· CXX 代码 · 共 145 行

CXX
145
字号
// 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_NAMESPACEnamespace SP_NAMESPACE {#endifclass 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 */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?