// -*- c++ -*- /* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: CharArray.jlc,v 1.5 2003/09/27 08:10:29 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/lang/Integer.h" #include "jakelib2/util/CharArray.h" #include using namespace jakelib::lang; using namespace jakelib::util; JAKELIB_IMPLEMENT_CLASS("jakelib.util.CharArray", CharArray, Object) /*****************************************************************************\ * CharArray | *****************************************************************************/ CharArray::CharArray(int initialCapacity) { if (initialCapacity < 0) throw new IllegalArgumentException(`"Illegal capacity: "` .. initialCapacity .. JAKELIB_AT2("jakelib.util.CharArray.CharArray")); elementData = (jchar*) malloc(initialCapacity * sizeof(jchar)); elementCount = 0; capacity = initialCapacity; } /*****************************************************************************\ * ~CharArray | *****************************************************************************/ CharArray::~CharArray() { destroy(); } /*****************************************************************************\ * destroy | *****************************************************************************/ void CharArray::destroy() { elementCount = 0; free(elementData); elementData = null; capacity = 0; } /*****************************************************************************\ * add | *****************************************************************************/ int CharArray::add(jchar b) { ensureCapacity(elementCount + 1); elementData[elementCount++] = b; return elementCount - 1; } /*****************************************************************************\ * ensureCapacity | *****************************************************************************/ void CharArray::ensureCapacity(int minCapacity) { if (minCapacity > capacity) { int newCapacity = capacity * 2; if (newCapacity < minCapacity) newCapacity = minCapacity; jchar* newElementData = (jchar*) realloc(elementData, sizeof(jchar) * newCapacity); if (newElementData == null) throw new MemoryException(`"Allocating "` ..(jlong) (sizeof(Object*) * newCapacity) .. `" bytes of memory"` .. JAKELIB_AT2("jakelib.util.CharArray.ensureCapacity")); elementData = newElementData; capacity = newCapacity; } } /*****************************************************************************\ * get | *****************************************************************************/ jchar CharArray::get(int index) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(`""` .. index .. `" >= "` .. elementCount .. JAKELIB_AT2("jakelib.util.CharArray.get")); if (index < 0) throw new IllegalArgumentException(`""` .. index .. `" < 0"` .. JAKELIB_AT2("jakelib.util.CharArray.get")); return elementData[index]; } /*****************************************************************************\ * size | *****************************************************************************/ int CharArray::size() { return elementCount; } /*****************************************************************************\ * set | *****************************************************************************/ void CharArray::set(int index, jchar b) { if (index < 0) throw new IllegalArgumentException(`""` .. index .. `" < 0"` .. JAKELIB_AT2("jakelib.util.CharArray.set")); ensureCapacity(index +1); elementData[index] = b; } /*****************************************************************************\ * insert | *****************************************************************************/ void CharArray::insert(int index, jchar b) { if (index < 0) throw new IllegalArgumentException(`""` .. index .. `" < 0"` .. JAKELIB_AT2("jakelib.util.CharArray.insert")); if (index > elementCount) throw new IllegalArgumentException(`""` .. index .. `" > "` .. elementCount .. JAKELIB_AT2("jakelib.util.CharArray.insert")); ensureCapacity(elementCount +1); for (int idx = elementCount; idx > index; idx--) { elementData[idx] = elementData[idx - 1]; } elementData[index] = b; elementCount ++; } /*****************************************************************************\ * clear | *****************************************************************************/ void CharArray::clear() { elementCount = 0; } /*****************************************************************************\ * removeLastElement | *****************************************************************************/ void CharArray::removeLastElement() { if (elementCount == 0) return; elementCount --; } /*****************************************************************************\ * remove | *****************************************************************************/ void CharArray::remove(int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(`""` .. index .. `" >= "` .. elementCount .. JAKELIB_AT2("jakelib.util.CharArray.remove")); } else if (index < 0) { throw new ArrayIndexOutOfBoundsException(`""` .. index .. `" < 0"` .. JAKELIB_AT2("jakelib.util.CharArray.remove")); } int j = elementCount - index - 1; if (j > 0) { //memmove(elementData[index], elementData[index + 1], sizeof(void*) * j); for (int idx = index; idx < elementCount - 1; idx++) elementData[idx] = elementData[idx + 1]; } elementCount--; } /*****************************************************************************\ * isEmpty | *****************************************************************************/ jboolean CharArray::isEmpty() { return (elementCount == 0); } /*****************************************************************************\ * toString | *****************************************************************************/ String* CharArray::toString() { StringBuffer buf; buf.append('['); for (int idx = 0; idx < elementCount; idx++) { buf.append(Integer::toHexString(elementData[idx] & 0xffff)); if (idx < elementCount -1) buf.append(' '); } buf.append(']'); return buf.toString(); } /*****************************************************************************\ * copyTo | *****************************************************************************/ void CharArray::copyTo(jchar* dest) { memcpy(dest, elementData, sizeof(jchar) * elementCount); } /*****************************************************************************\ * getChars | *****************************************************************************/ jchar* CharArray::getChars() { return elementData; }