#ifndef STRINGBUFFER_H
#define STRINGBUFFER_H

/*
 * @(#)StringBuffer.h
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 2002-2004 Jörg P. M. Haeger
 *
 * webCDwriter is free software. See CDWserver.cpp for details.
 */

#include <sys/types.h>

class StringBuffer {
	static off_t no;
	static int existing, existingTop;
	int *buf;
	int len;
	int size, inc;
public:
	StringBuffer(int size = 32, int inc = 16);
	StringBuffer(const StringBuffer &buffer);
	StringBuffer(StringBuffer *buffer);
	StringBuffer(const char *str);
	StringBuffer(String &str, int remove = 0);
	~StringBuffer();
	StringBuffer &operator=(StringBuffer *buffer);
	StringBuffer &operator+(char c) { return append(c); }
	StringBuffer &operator+(int i) { return append(i); }
	StringBuffer &operator+(off_t n) { return append(n); }
	StringBuffer &operator+(const char *str) { return append(str); }
	StringBuffer &append(char c) { return insert(len, c); }
	StringBuffer &append(int i) { return insert(len, i); }
	StringBuffer &append(off_t n) { return insert(len, n); }
	StringBuffer &append(const char *str) { return insert(len, str); }
	StringBuffer &append(const char *str, int offset, int len) {
		return insert(this->len, str, offset, len);
	}
	int capacity() { return size - len; }
	int charAt(int index) const;
	StringBuffer &delete_(int start, int end);
	void ensureCapacity(int minimumCapacity);
	int getExisting() { return existing; }
	int getNo() { return no; }
	int indexOf(const char *str, int fromIndex = 0) const;
	StringBuffer &insert(int offset, char c);
	StringBuffer &insert(int offset, int i);
	StringBuffer &insert(int offset, off_t n);
	StringBuffer &insert(int offset, const char *str);
	StringBuffer &insert(
		int index, const char *str, int offset, int len);
	int lastIndexOf(const char *str) const;
	int length() const { return len; }
	void setCharAt(int index, char ch);
	static void setLog(class Log *aLog);

private:
	int endsWith(const char *suffix) const;
	int equals(const char *str) const;
	int equalsIgnoreCase(const char *str) const;
	void init();
	int startsWith(const char *prefix) const;
	StringBuffer *substring(int beginIndex, int endIndex) const;
	StringBuffer *toUpperCase();
	friend class String;

	class LogProxy {
		private: class Log *logPtr;
		public: LogProxy() { logPtr = NULL; }
		public: void put(int level, String &message);
		public: void setLog(class Log *aLogPtr) { logPtr = aLogPtr; }
	};
	static LogProxy log;
};

#endif
