Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

UCS2StreamEncoder.cc

Go to the documentation of this file.
00001 /*
00002  * UCS2StreamEncoder.h
00003  *
00004  * Smalltalk like class library for C++
00005  * UCS-2 encoding translator.
00006  *
00007  * Copyright (c) 2006 Milan Cermak
00008  */
00009 /*
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 #include "stlib/UCS2StreamEncoder.h"
00025 
00026 #include "stlib/Character.h"
00027 #include "stlib/Integer.h"
00028 #include "stlib/Stream.h"
00029 
00030 namespace Core {
00031 
00032 UCS2StreamEncoder::UCS2StreamEncoder(void)
00033 {
00034     big_endian = false;
00035 }
00036 
00037 /* Class-accessing protocol */
00038 String *UCS2StreamEncoder::className(void) const
00039 {
00040     return new String("UCS2StreamEncoder");
00041 }
00042 
00043 /* Initialize protocol */
00044 void UCS2StreamEncoder::bigEndian(void)
00045 {
00046     big_endian = true;
00047 }
00048 
00049 void UCS2StreamEncoder::littleEndian(void)
00050 {
00051     big_endian = false;
00052 }
00053 
00054 /* Accessing protocol */
00055 Character *UCS2StreamEncoder::decodeFrom(Stream *stream)
00056 {
00057     Integer *firstByte, *secondByte;
00058     unsigned short value;
00059     firstByte = dynamic_cast<Integer *>(stream->next());
00060     secondByte = dynamic_cast<Integer *>(stream->next());
00061 
00062     if (secondByte == nil) return nil; // stream end reached
00063 
00064     if (big_endian)
00065         value = firstByte->asLong() << 8 + secondByte->asLong();
00066     else
00067         value = secondByte->asLong() << 8 + firstByte->asLong();
00068     return Character::value(value);
00069 }
00070 
00071 void UCS2StreamEncoder::encodeTo(Stream *stream, Character *character)
00072 {
00073     if (character->byteSize() > 2)
00074         error(new String("No character available"),
00075               new String(__PRETTY_FUNCTION__), character);
00076 
00077     long value = character->asInteger();
00078     unsigned char first, second;
00079     first = (value >> 8) & 0xff;
00080     second = value & 0xff;
00081     if (big_endian) {
00082         stream->nextPut(Integer::value(first));
00083         stream->nextPut(Integer::value(second));
00084     } else {
00085         stream->nextPut(Integer::value(second));
00086         stream->nextPut(Integer::value(first));
00087     }
00088 }
00089 
00090 }; /* namespace Core */

Generated on Mon Nov 27 09:47:56 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2