/*
 * Author:
 *         Kendy Kutzner <kutzner@ira.uka.de>
 *
 * (c) 2005 Research Group Dr. Fuhrmann, University of Karlsruhe, Germany
 * All rights reserved.
 *
 * $Id: buf.h,v 1.13 2005/11/21 10:46:57 kutzner Exp $
 *
 */

#ifndef _BUF_H_
#define _BUF_H_

/** \file 
 * Buffers are used to shuffle data block arround.
 */

#include <string>
using namespace std;

#include "igor.h"

/**
 * Buffers are used to shuffle data blocks arround.
 */

class buf {
	public:

	/** Initialize a buffer.
	 * Buffers start clean. All data is erased. Not to be called directly.
	 */
	buf(){_size = 0; _buf = NULL;};

	/** Free a buffer.
	 * Kills all allocated memory.
	 */
	~buf();
		
	/// Append some data to the buffer.
	void put(const uint8 *, int); 

	/// Append some data to the buffer.
	void put(buf * other); 

	/// Append some data to the buffer.
	void put(string); 

	/// Copy some data from the buffer somewhere else.
	int get(uint8 * ptr, int size); 

	/** Get a pointer to the internal representation.
	 * The buffer itself is not modified.
	 * \return The underlaying pointer.
	 */
	uint8* getraw(){return _buf;}; 

	/** Get a pointer to the internal representation and its size.
	 * The buffer itself is not modified.
	 * \param size A pointer to an int where the size is written to.
	 * \return The underlaying pointer.
	 */
	uint8* getraw(int * size){*size = _size; return getraw();}; 

	/// Return the first line or NULL if there is no newline character
	// (0x0a) in the buf.
	buf * getline(); 

	/// Return the amount of stored data.
	// \return The number of bytes stored.
	int size(){return _size;}; 

	/// undo a get operation.
	void unget(uint8 *, int); 

	/// throw away a buf.
	void clear(); 

	private:
	
	/// The pointer to the allocated memory.
	uint8 * _buf;

	/** The size of the allocated memory.
	 * It is always kept in sync with the number of stored bytes.  This
	 * fact makes this implementation not very efficient, since for every
	 * put() or get() operation a reallocation is neccessary.
	 */
	int _size;
};

#endif

	
