// This may look like C code, but it is really -*- C++ -*- // ------------------------------------------------------------------ // The Goldware Library // Copyright (C) 1990-1999 Odinn Sorensen // Copyright (C) 1999-2000 Alexander S. Aganichev // ------------------------------------------------------------------ // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 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 // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public // License along with this program; if not, write to the Free // Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, // MA 02111-1307, USA // ------------------------------------------------------------------ // $Id$ // ------------------------------------------------------------------ // String bag class. // ------------------------------------------------------------------ #include #include #include // ------------------------------------------------------------------ const int BLOCKSIZE = 4096; // ------------------------------------------------------------------ GStrBag::GStrBag() { items = 0; bagsize = 0; bag = NULL; blocksize = BLOCKSIZE; currno = 0; } // ------------------------------------------------------------------ GStrBag::~GStrBag() { Reset(); } // ------------------------------------------------------------------ void GStrBag::Reset() { if(bag != NULL) throw_release(bag); } // ------------------------------------------------------------------ int GStrBag::Add(const char* string) { return Add(string, strlen(string)+1); } // ------------------------------------------------------------------ int GStrBag::Add(const void* data, int length) { if(items == 0) bag = (char*)throw_malloc(blocksize); int currsize = bagsize + (items*sizeof(int)); int currsizeblks = (currsize/blocksize) + 1; int newsizeblks = ((currsize+length+sizeof(int))/blocksize) + 1; if(newsizeblks != currsizeblks) bag = (char*)throw_realloc(bag, newsizeblks*blocksize); memmove(bag+bagsize+length, bag+bagsize, items*sizeof(int)); memcpy(bag+bagsize, data, length); ((int*)(bag+bagsize+length))[items] = bagsize; bagsize += length; return items++; } // ------------------------------------------------------------------ void GStrBag::Change(int index, const char* string) { Change(index, string, strlen(string)+1); } // ------------------------------------------------------------------ void GStrBag::Change(int index, const void* data, int length) { int oldpos = Pos(index); int oldlen = bag ? strlen(bag+oldpos)+1 : 0; int lendiff = length - oldlen; int oldsize = bagsize+(items*sizeof(int)); int movesize = oldsize - oldpos - oldlen; int currsizeblks = (oldsize/blocksize) + 1; int newsizeblks = ((oldsize+lendiff)/blocksize) + 1; if(lendiff > 0) { if(newsizeblks != currsizeblks) bag = (char*)throw_realloc(bag, oldsize+lendiff); memmove(bag+oldpos+length, bag+oldpos+oldlen, movesize); } else if(lendiff < 0) { memmove(bag+oldpos+length, bag+oldpos+oldlen, movesize); if(newsizeblks != currsizeblks) bag = (char*)throw_realloc(bag, oldsize+lendiff); } memcpy(bag+oldpos, data, length); bagsize += lendiff; for(int n=index+1; n