#ifndef XCLASSES_STRBUF
#define XCLASSES_STRBUF

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: strbuf.cxx,v 1.10 1999/11/22 11:19:18 schmitzj Exp $)
 * 
 * Copyright (C) 1998 Jürgen Schmitz
 *
 * 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 library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 *-----------------------------------------------------------------------
 */

#include <string.h>
#include <stdlib.h>
#include "../include/strbuf.h"

Xcl_strbuf::Xcl_strbuf(void)
{
	buf=NULL;
	used=0;
	size=0;
}
Xcl_strbuf::~Xcl_strbuf()
{
	if (buf) free(buf);
}

char *Xcl_strbuf::operator+(char *t)
{
	operator+=(t);
	return buf;
}
void Xcl_strbuf::operator+=(char *t)
{
	if (!t) return;
	int l=strlen(t);
	if (l+used+1>=size || !buf)
	{
		buf=(char*)realloc(buf,size+l+100);
		if (!buf)
		{
			size=0;
			used=0;
			return;
		}
		size+=l+100;
	}
	buf[used]=0;
	strcpy(buf+used,t);
	used+=l;
}
void Xcl_strbuf::replace(char *t)
{
	used=0;
	if (buf) buf[0]=0;
	operator+=(t);
}

char *Xcl_strbuf::operator()(void)
{
	return buf?buf:(char*)"";
}
void Xcl_strbuf::operator()(int nsize)
{
	if (nsize>=size)
	{
		buf=(char*)realloc(buf,nsize+1);
		size=nsize+1;
		used=0;
		buf[used]=0;
	}
}
void Xcl_strbuf::update(void)
{
	used=strlen(buf);
}
void Xcl_strbuf::clear(void)
{
	if (buf)
	{
		free(buf);
	}
	buf=NULL;
	used=0;
	size=0;
}

void Xcl_strbuf::resize(int r)
{
	if (r<used)
	{
		buf[r]=0;
		used=r;
	}
}

#endif
