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

Core::ByteCharacterEncoder Class Reference

#include <ByteCharacterEncoder.h>

Inheritance diagram for Core::ByteCharacterEncoder:

Inheritance graph
[legend]
Collaboration diagram for Core::ByteCharacterEncoder:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ByteCharacterEncoder (void)
virtual ~ByteCharacterEncoder (void)
virtual StringclassName (void) const
 Answer receiver class name.
virtual void loadFromFile (const char *filename)
virtual void loadFrom (Stream *stream)
virtual void initializeAsciiEncoder (void)
virtual Characterdecode (Integer *value)
virtual Integerencode (Character *character)
virtual long integerValueFrom (String *strNumber)
virtual void parseLine (String *line)

Static Public Member Functions

static ByteCharacterEncoderforASCII (void)

Protected Attributes

ArraydecodeMap
DictionaryencodeMap

Constructor & Destructor Documentation

Core::ByteCharacterEncoder::ByteCharacterEncoder void   ) 
 

Definition at line 41 of file ByteCharacterEncoder.cc.

References decodeMap, and encodeMap.

00042 {
00043     decodeMap = new Array(256);
00044     encodeMap = new Dictionary(320); // 256 * 5/4 -> pregrowed
00045 }

Core::ByteCharacterEncoder::~ByteCharacterEncoder void   )  [virtual]
 

Definition at line 47 of file ByteCharacterEncoder.cc.

References decodeMap, encodeMap, and nil.

00048 {
00049     if (decodeMap != nil) delete decodeMap;
00050     if (encodeMap != nil) delete encodeMap;
00051 }


Member Function Documentation

String * Core::ByteCharacterEncoder::className void   )  const [virtual]
 

Answer receiver class name.

Because there isn't any standard way to obtain class name this method comes to place.

Every class should rewrite this method but many didn't (yet).

Reimplemented from Core::CharacterEncoder.

Definition at line 54 of file ByteCharacterEncoder.cc.

00055 {
00056     return new String("ByteCharacterEncoder");
00057 }

Character * Core::ByteCharacterEncoder::decode Integer value  )  [virtual]
 

Reimplemented from Core::CharacterEncoder.

Definition at line 94 of file ByteCharacterEncoder.cc.

References Core::Array::at(), decodeMap, Core::Object::error(), and nil.

00095 {
00096     Object *ch = decodeMap->at(value->asLong());
00097     if (ch == nil)
00098         error(new String("No character available"),
00099               new String(__PRETTY_FUNCTION__), value);
00100     return dynamic_cast<Character *>(ch);
00101 }

Integer * Core::ByteCharacterEncoder::encode Character character  )  [virtual]
 

Reimplemented from Core::CharacterEncoder.

Definition at line 103 of file ByteCharacterEncoder.cc.

References Core::Dictionary::at(), encodeMap, Core::Object::error(), and nil.

00104 {
00105     Integer *value = dynamic_cast<Integer *>(encodeMap->at(character, nil));
00106     if (value == nil)
00107         error(new String("No character available"),
00108               new String(__PRETTY_FUNCTION__), character);
00109     return value;
00110 }

ByteCharacterEncoder * Core::ByteCharacterEncoder::forASCII void   )  [static]
 

Definition at line 77 of file ByteCharacterEncoder.cc.

References initializeAsciiEncoder().

Referenced by Core::EncodedStreamFactory::initializeAsciiEncoder().

00078 {
00079     ByteCharacterEncoder *ascii = new ByteCharacterEncoder;
00080     ascii->initializeAsciiEncoder();
00081     return ascii;
00082 }

void Core::ByteCharacterEncoder::initializeAsciiEncoder void   )  [virtual]
 

Definition at line 84 of file ByteCharacterEncoder.cc.

References decodeMap, encodeMap, Core::Array::put(), Core::Dictionary::put(), Core::Integer::value(), and Core::Character::value().

Referenced by forASCII().

00085 {
00086     for (int i = 0; i < 0x7F; i++) {
00087         Character *character = Character::value(i);
00088         encodeMap->put(character, Integer::value(i));
00089         decodeMap->put(i, character);
00090     }
00091 }

long Core::ByteCharacterEncoder::integerValueFrom String strNumber  )  [virtual]
 

Definition at line 113 of file ByteCharacterEncoder.cc.

References Core::String::at(), Core::Character::digitValue(), and Core::String::startsWith().

Referenced by parseLine().

00114 {
00115     unsigned long value = 0, index = 0, radix = 10;
00116     if (strNumber->startsWith("0x")) {
00117         index = 2;
00118         radix = 16;
00119     }
00120 
00121     for (; index < strNumber->size(); index++) {
00122         Character *ch = dynamic_cast<Character *>(strNumber->at(index));
00123         value = value * radix + ch->digitValue();
00124     }
00125     return value;
00126 }

void Core::ByteCharacterEncoder::loadFrom Stream stream  )  [virtual]
 

Definition at line 66 of file ByteCharacterEncoder.cc.

References _ensure, Core::Stream::atEnd(), Core::Stream::close(), Core::Stream::nextLine(), and parseLine().

Referenced by loadFromFile().

00067 {
00068     _ensure(
00069         while (!stream->atEnd()) {
00070             parseLine(dynamic_cast<String *>(stream->nextLine()));
00071         }
00072     ,
00073         stream->close()
00074     );
00075 }

void Core::ByteCharacterEncoder::loadFromFile const char *  filename  )  [virtual]
 

Definition at line 60 of file ByteCharacterEncoder.cc.

References loadFrom(), and Core::IOAccessor::withEncoding().

Referenced by Core::EncodedStreamFactory::createCharacterEncoder().

00061 {
00062     FileAccessor *file = new FileAccessor(filename, O_RDONLY, 0);
00063     loadFrom(file->withEncoding("us-ascii")->readStream());
00064 }

void Core::ByteCharacterEncoder::parseLine String line  )  [virtual]
 

Definition at line 128 of file ByteCharacterEncoder.cc.

References Core::Array::at(), decodeMap, encodeMap, integerValueFrom(), Core::Array::put(), Core::Dictionary::put(), Core::Array::size(), Core::Character::space(), Core::Character::tab(), Core::Integer::value(), and Core::Character::value().

Referenced by loadFrom().

00129 {
00130     long idx;
00131     unsigned long encValue, charValue;
00132     Character *character;
00133 
00134     line->replaceAll(Character::tab(), Character::space());
00135 
00136     /* Remove comments */
00137     idx = line->indexOf('#');
00138     if (idx > 0) line = dynamic_cast<String *>(line->copy(0, idx));
00139     line = line->trimBlanks();
00140     if (idx == 0 || line->isEmpty()) return;
00141 
00142     Array *pair = line->asArrayOfSubstrings();
00143     if (pair->size() >= 2) {  /* At least two items are needed */
00144         encValue = integerValueFrom(dynamic_cast<String *>(pair->at(0)));
00145         charValue = integerValueFrom(dynamic_cast<String *>(pair->at(1)));
00146     }
00147     character = Character::value(charValue);
00148     encodeMap->put(character, Integer::value(encValue));
00149     decodeMap->put(encValue, character);
00150 }


Member Data Documentation

Array* Core::ByteCharacterEncoder::decodeMap [protected]
 

Definition at line 41 of file ByteCharacterEncoder.h.

Referenced by ByteCharacterEncoder(), decode(), initializeAsciiEncoder(), parseLine(), and ~ByteCharacterEncoder().

Dictionary* Core::ByteCharacterEncoder::encodeMap [protected]
 

Definition at line 42 of file ByteCharacterEncoder.h.

Referenced by ByteCharacterEncoder(), encode(), initializeAsciiEncoder(), parseLine(), and ~ByteCharacterEncoder().


The documentation for this class was generated from the following files:
Generated on Mon Nov 27 09:51:09 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2