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

ByteString.cc

Go to the documentation of this file.
00001 /*
00002  * ByteString.cc
00003  *
00004  * Smalltalk like class library for C++
00005  * String implementor using c-array of bytes.
00006  *
00007  * Copyright (c) 2004 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/ByteString.h>
00025 #include <stlib/Character.h>
00026 #include <stlib/String.h>
00027 #include <stlib/TwoByteString.h>
00028 
00029 #include <string.h>
00030 
00031 using namespace Core;
00032 
00033 ByteString::ByteString(int size)
00034 {
00035     tally = size;
00036     content = (unsigned char *) GC_malloc(tally * sizeof(unsigned char));
00037     bzero(content, tally);
00038 }
00039 
00040 ByteString::~ByteString(void)
00041 {
00042     GC_free(content);
00043 }
00044 
00045 /* Class-accessing protocol */
00046 String *ByteString::className(void) const
00047 {
00048     return new String("ByteString");
00049 }
00050 
00051 /* Accessing protocol */
00052 Character *ByteString::at(int index) const
00053 {
00054     return Character::value(content[index]);
00055 }
00056 
00057 void ByteString::put(int index, Character *object)
00058 {
00059     content[index] = object->asInteger();
00060 }
00061 
00062 int ByteString::byteSize(void)
00063 {
00064     return 1;
00065 }
00066 
00067 /* Adding protocol */
00068 void ByteString::changeSize(long newSize)
00069 {
00070     unsigned char *oldContent = content;
00071 
00072     content = (unsigned char *) GC_malloc(newSize * sizeof(unsigned char));
00073     bzero(content, newSize);
00074     memcpy(content, oldContent, size() * sizeof(unsigned char));
00075     tally = newSize;
00076 }
00077 
00078 /* Comparing protocol */
00079 bool ByteString::isEqual(StringImpl *string)
00080 {
00081     return string->isEqualToBytes(this->content);
00082 }
00083 
00084 bool ByteString::isEqualToBytes(unsigned char *string)
00085 {
00086     for (int i = 0; i < tally; i++) {
00087         if (this->content[i] != string[i])
00088             return false;
00089     }
00090     return true;
00091 }
00092 
00093 bool ByteString::isEqualToShorts(unsigned short *string)
00094 {
00095     for (int i = 0; i < tally; i++) {
00096         if (this->content[i] != string[i])
00097             return false;
00098     }
00099     return true;
00100 }
00101 
00102 /* Converting protocol */
00103 ByteString *ByteString::asByteString(void)
00104 {
00105     return this;
00106 }
00107 
00108 TwoByteString *ByteString::asTwoByteString(void)
00109 {
00110     TwoByteString *newString = new TwoByteString(size());
00111     for (int i = 0; i < size(); i++) {
00112         newString->putRawValue(i, content[i]);
00113     }
00114     return newString;
00115 }
00116 
00117 /* Testing protocol */
00118 bool ByteString::isByteString(void) const
00119 {
00120     return true;
00121 }

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